ssl_cert.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_DEBUG(SSL_CERT_ERROR_LEVEL, "no enough memory > (cert)");
  29. goto no_mem;
  30. }
  31. if (ic) {
  32. ipk = ic->pkey;
  33. ix = ic->x509;
  34. } else {
  35. ipk = NULL;
  36. ix = NULL;
  37. }
  38. cert->pkey = __EVP_PKEY_new(ipk);
  39. if (!cert->pkey) {
  40. SSL_DEBUG(SSL_CERT_ERROR_LEVEL, "__EVP_PKEY_new() return NULL");
  41. goto pkey_err;
  42. }
  43. cert->x509 = __X509_new(ix);
  44. if (!cert->x509) {
  45. SSL_DEBUG(SSL_CERT_ERROR_LEVEL, "__X509_new() return NULL");
  46. goto x509_err;
  47. }
  48. return cert;
  49. x509_err:
  50. EVP_PKEY_free(cert->pkey);
  51. pkey_err:
  52. ssl_mem_free(cert);
  53. no_mem:
  54. return NULL;
  55. }
  56. /**
  57. * @brief create a certification object include private key object
  58. */
  59. CERT *ssl_cert_new(void)
  60. {
  61. return __ssl_cert_new(NULL);
  62. }
  63. /**
  64. * @brief free a certification object
  65. */
  66. void ssl_cert_free(CERT *cert)
  67. {
  68. SSL_ASSERT3(cert);
  69. X509_free(cert->x509);
  70. EVP_PKEY_free(cert->pkey);
  71. ssl_mem_free(cert);
  72. }