ssl_cert.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "ssl_cert.h"
  14. #include "ssl_pkey.h"
  15. #include "ssl_x509.h"
  16. #include "ssl_dbg.h"
  17. #include "ssl_port.h"
  18. /**
  19. * @brief create a certification object according to input certification
  20. */
  21. CERT *__ssl_cert_new(CERT *ic)
  22. {
  23. CERT *cert;
  24. X509 *ix;
  25. EVP_PKEY *ipk;
  26. cert = ssl_mem_zalloc(sizeof(CERT));
  27. if (!cert)
  28. SSL_RET(failed1, "ssl_mem_zalloc\n");
  29. if (ic) {
  30. ipk = ic->pkey;
  31. ix = ic->x509;
  32. } else {
  33. ipk = NULL;
  34. ix = NULL;
  35. }
  36. cert->pkey = __EVP_PKEY_new(ipk);
  37. if (!cert->pkey)
  38. SSL_RET(failed2, "__EVP_PKEY_new\n");
  39. cert->x509 = __X509_new(ix);
  40. if (!cert->x509)
  41. SSL_RET(failed3, "__X509_new\n");
  42. return cert;
  43. failed3:
  44. EVP_PKEY_free(cert->pkey);
  45. failed2:
  46. ssl_mem_free(cert);
  47. failed1:
  48. return NULL;
  49. }
  50. /**
  51. * @brief create a certification object include private key object
  52. */
  53. CERT *ssl_cert_new(void)
  54. {
  55. return __ssl_cert_new(NULL);
  56. }
  57. /**
  58. * @brief free a certification object
  59. */
  60. void ssl_cert_free(CERT *cert)
  61. {
  62. X509_free(cert->x509);
  63. EVP_PKEY_free(cert->pkey);
  64. ssl_mem_free(cert);
  65. }