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 "private-lib-core.h"
16
17 #include "ssl_cert.h"
18 #include "ssl_pkey.h"
19 #include "ssl_x509.h"
20 #include "ssl_dbg.h"
21 #include "ssl_port.h"
22
23 /**
24 * @brief create a certification object according to input certification
25 */
__ssl_cert_new(CERT * ic,void * rngctx)26 CERT *__ssl_cert_new(CERT *ic, void *rngctx)
27 {
28 CERT *cert;
29
30 X509 *ix;
31 EVP_PKEY *ipk;
32
33 cert = ssl_mem_zalloc(sizeof(CERT));
34 if (!cert) {
35 SSL_DEBUG(SSL_CERT_ERROR_LEVEL, "no enough memory > (cert)");
36 goto no_mem;
37 }
38
39 if (ic) {
40 ipk = ic->pkey;
41 ix = ic->x509;
42 } else {
43 ipk = NULL;
44 ix = NULL;
45 }
46
47 cert->pkey = __EVP_PKEY_new(ipk, rngctx);
48 if (!cert->pkey) {
49 SSL_DEBUG(SSL_CERT_ERROR_LEVEL, "__EVP_PKEY_new() return NULL");
50 goto pkey_err;
51 }
52
53 cert->x509 = __X509_new(ix);
54 if (!cert->x509) {
55 SSL_DEBUG(SSL_CERT_ERROR_LEVEL, "__X509_new() return NULL");
56 goto x509_err;
57 }
58
59 return cert;
60
61 x509_err:
62 EVP_PKEY_free(cert->pkey);
63 pkey_err:
64 ssl_mem_free(cert);
65 no_mem:
66 return NULL;
67 }
68
69 /**
70 * @brief create a certification object include private key object
71 */
ssl_cert_new(void * rngctx)72 CERT *ssl_cert_new(void *rngctx)
73 {
74 return __ssl_cert_new(NULL, rngctx);
75 }
76
77 /**
78 * @brief free a certification object
79 */
ssl_cert_free(CERT * cert)80 void ssl_cert_free(CERT *cert)
81 {
82 SSL_ASSERT3(cert);
83
84 X509_free(cert->x509);
85
86 EVP_PKEY_free(cert->pkey);
87
88 ssl_mem_free(cert);
89 }
90