cert.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <string.h>
  19. #include <grpc/grpc.h>
  20. #include <grpc/grpc_security.h>
  21. #include <grpc/support/log.h>
  22. #include "src/core/lib/iomgr/load_file.h"
  23. #include "test/core/bad_ssl/server_common.h"
  24. /* This server will present an untrusted cert to the connecting client,
  25. * causing the SSL handshake to fail */
  26. int main(int argc, char** argv) {
  27. const char* addr = bad_ssl_addr(argc, argv);
  28. grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
  29. grpc_server_credentials* ssl_creds;
  30. grpc_server* server;
  31. grpc_slice cert_slice, key_slice;
  32. grpc_init();
  33. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  34. "load_file",
  35. grpc_load_file("src/core/tsi/test_creds/badserver.pem", 1, &cert_slice)));
  36. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  37. "load_file",
  38. grpc_load_file("src/core/tsi/test_creds/badserver.key", 1, &key_slice)));
  39. pem_key_cert_pair.private_key =
  40. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
  41. pem_key_cert_pair.cert_chain =
  42. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
  43. ssl_creds = grpc_ssl_server_credentials_create(nullptr, &pem_key_cert_pair, 1,
  44. 0, nullptr);
  45. server = grpc_server_create(nullptr, nullptr);
  46. GPR_ASSERT(grpc_server_add_http2_port(server, addr, ssl_creds));
  47. grpc_server_credentials_release(ssl_creds);
  48. grpc_slice_unref(cert_slice);
  49. grpc_slice_unref(key_slice);
  50. bad_ssl_run(server);
  51. grpc_shutdown();
  52. return 0;
  53. }