1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "ssl_methods.h"
21 * @brief create a private key object according to input private key
23 EVP_PKEY* __EVP_PKEY_new(EVP_PKEY *ipk)
28 pkey = ssl_mem_zalloc(sizeof(EVP_PKEY));
30 SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "no enough memory > (pkey)");
35 pkey->method = ipk->method;
37 pkey->method = EVP_PKEY_method();
40 ret = EVP_PKEY_METHOD_CALL(new, pkey, ipk);
42 SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_METHOD_CALL(new) return %d", ret);
55 * @brief create a private key object
57 EVP_PKEY* EVP_PKEY_new(void)
59 return __EVP_PKEY_new(NULL);
63 * @brief free a private key object
65 void EVP_PKEY_free(EVP_PKEY *pkey)
69 EVP_PKEY_METHOD_CALL(free, pkey);
75 * @brief load a character key context into system context. If '*a' is pointed to the
76 * private key, then load key into it. Or create a new private key object
78 EVP_PKEY *d2i_PrivateKey(int type,
80 const unsigned char **pp,
94 pkey = EVP_PKEY_new();;
96 SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_new() return NULL");
103 ret = EVP_PKEY_METHOD_CALL(load, pkey, *pp, length);
105 SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_METHOD_CALL(load) return %d", ret);
122 * @brief set the SSL context private key
124 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
129 if (ctx->cert->pkey == pkey)
133 EVP_PKEY_free(ctx->cert->pkey);
135 ctx->cert->pkey = pkey;
141 * @brief set the SSL private key
143 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
148 if (ssl->cert->pkey == pkey)
152 EVP_PKEY_free(ssl->cert->pkey);
154 ssl->cert->pkey = pkey;
160 * @brief load private key into the SSL context
162 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
163 const unsigned char *d, long len)
168 pk = d2i_PrivateKey(0, NULL, &d, len);
170 SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_PrivateKey() return NULL");
174 ret = SSL_CTX_use_PrivateKey(ctx, pk);
176 SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_CTX_use_PrivateKey() return %d", ret);
189 * @brief load private key into the SSL
191 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl,
192 const unsigned char *d, long len)
197 pk = d2i_PrivateKey(0, NULL, &d, len);
199 SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_PrivateKey() return NULL");
203 ret = SSL_use_PrivateKey(ssl, pk);
205 SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_use_PrivateKey() return %d", ret);
218 * @brief load the private key file into SSL context
220 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
226 * @brief load the private key file into SSL
228 int SSL_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
234 * @brief load the RSA ASN1 private key into SSL context
236 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
238 return SSL_CTX_use_PrivateKey_ASN1(0, ctx, d, len);