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 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "ssl_cert.h" 16 #include "ssl_pkey.h" 17 #include "ssl_x509.h" 18 #include "ssl_dbg.h" 19 #include "ssl_port.h" 20 21 /** 22 * @brief create a certification object according to input certification 23 */ __ssl_cert_new(CERT * ic)24CERT *__ssl_cert_new(CERT *ic) 25 { 26 CERT *cert; 27 28 X509 *ix; 29 EVP_PKEY *ipk; 30 31 cert = ssl_mem_zalloc(sizeof(CERT)); 32 if (!cert) { 33 SSL_DEBUG(SSL_CERT_ERROR_LEVEL, "no enough memory > (cert)"); 34 goto no_mem; 35 } 36 37 if (ic) { 38 ipk = ic->pkey; 39 ix = ic->x509; 40 } else { 41 ipk = NULL; 42 ix = NULL; 43 } 44 45 cert->pkey = __EVP_PKEY_new(ipk); 46 if (!cert->pkey) { 47 SSL_DEBUG(SSL_CERT_ERROR_LEVEL, "__EVP_PKEY_new() return NULL"); 48 goto pkey_err; 49 } 50 51 cert->x509 = __X509_new(ix); 52 if (!cert->x509) { 53 SSL_DEBUG(SSL_CERT_ERROR_LEVEL, "__X509_new() return NULL"); 54 goto x509_err; 55 } 56 57 return cert; 58 59 x509_err: 60 EVP_PKEY_free(cert->pkey); 61 pkey_err: 62 ssl_mem_free(cert); 63 no_mem: 64 return NULL; 65 } 66 67 /** 68 * @brief create a certification object include private key object 69 */ ssl_cert_new(void)70CERT *ssl_cert_new(void) 71 { 72 return __ssl_cert_new(NULL); 73 } 74 75 /** 76 * @brief free a certification object 77 */ ssl_cert_free(CERT * cert)78void ssl_cert_free(CERT *cert) 79 { 80 SSL_ASSERT3(cert); 81 82 X509_free(cert->x509); 83 84 EVP_PKEY_free(cert->pkey); 85 86 ssl_mem_free(cert); 87 } 88