ssl_pkey.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_pkey.h"
  14. #include "ssl_methods.h"
  15. #include "ssl_dbg.h"
  16. #include "ssl_port.h"
  17. #include "openssl/bio.h"
  18. /**
  19. * @brief create a private key object according to input private key
  20. */
  21. EVP_PKEY* __EVP_PKEY_new(EVP_PKEY *ipk)
  22. {
  23. int ret;
  24. EVP_PKEY *pkey;
  25. pkey = ssl_mem_zalloc(sizeof(EVP_PKEY));
  26. if (!pkey) {
  27. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "no enough memory > (pkey)");
  28. goto no_mem;
  29. }
  30. pkey->ref_counter = 1;
  31. if (ipk) {
  32. pkey->method = ipk->method;
  33. } else {
  34. pkey->method = EVP_PKEY_method();
  35. }
  36. ret = EVP_PKEY_METHOD_CALL(new, pkey, ipk);
  37. if (ret) {
  38. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_METHOD_CALL(new) return %d", ret);
  39. goto failed;
  40. }
  41. return pkey;
  42. failed:
  43. ssl_mem_free(pkey);
  44. no_mem:
  45. return NULL;
  46. }
  47. /**
  48. * @brief create a private key object
  49. */
  50. EVP_PKEY* EVP_PKEY_new(void)
  51. {
  52. return __EVP_PKEY_new(NULL);
  53. }
  54. /**
  55. * @brief free a private key object
  56. */
  57. void EVP_PKEY_free(EVP_PKEY *pkey)
  58. {
  59. SSL_ASSERT3(pkey);
  60. if (--pkey->ref_counter > 0) {
  61. return;
  62. }
  63. EVP_PKEY_METHOD_CALL(free, pkey);
  64. ssl_mem_free(pkey);
  65. }
  66. /**
  67. * @brief load a character key context into system context. If '*a' is pointed to the
  68. * private key, then load key into it. Or create a new private key object
  69. */
  70. EVP_PKEY *d2i_PrivateKey(int type,
  71. EVP_PKEY **a,
  72. const unsigned char **pp,
  73. long length)
  74. {
  75. int m = 0;
  76. int ret;
  77. EVP_PKEY *pkey;
  78. SSL_ASSERT2(pp);
  79. SSL_ASSERT2(*pp);
  80. SSL_ASSERT2(length);
  81. if (a && *a) {
  82. pkey = *a;
  83. } else {
  84. pkey = EVP_PKEY_new();;
  85. if (!pkey) {
  86. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_new() return NULL");
  87. goto failed1;
  88. }
  89. m = 1;
  90. }
  91. ret = EVP_PKEY_METHOD_CALL(load, pkey, *pp, length);
  92. if (ret) {
  93. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_METHOD_CALL(load) return %d", ret);
  94. goto failed2;
  95. }
  96. if (a)
  97. *a = pkey;
  98. return pkey;
  99. failed2:
  100. if (m)
  101. EVP_PKEY_free(pkey);
  102. failed1:
  103. return NULL;
  104. }
  105. EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a)
  106. {
  107. return d2i_PrivateKey(0, a, (const unsigned char **)&bp->data, bp->dlen);
  108. }
  109. RSA *d2i_RSAPrivateKey_bio(BIO *bp,RSA **a)
  110. {
  111. return d2i_PrivateKey_bio(bp, (EVP_PKEY**)a);
  112. }
  113. RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x, pem_password_cb *cb, void *u)
  114. {
  115. return PEM_read_bio_PrivateKey(bp, (EVP_PKEY**)x, cb, u);
  116. }
  117. EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **pk, pem_password_cb *cb, void *u)
  118. {
  119. int m = 0;
  120. int ret;
  121. EVP_PKEY *x;
  122. SSL_ASSERT2(BIO_method_type(bp) & BIO_TYPE_MEM);
  123. if (bp->data == NULL || bp->dlen == 0) {
  124. return NULL;
  125. }
  126. if (pk && *pk) {
  127. x = *pk;
  128. } else {
  129. x = EVP_PKEY_new();
  130. if (!x) {
  131. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_new() return NULL");
  132. goto failed;
  133. }
  134. m = 1;
  135. }
  136. ret = EVP_PKEY_METHOD_CALL(load, x, bp->data, bp->dlen);
  137. if (ret) {
  138. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_METHOD_CALL(load) return %d", ret);
  139. goto failed;
  140. }
  141. // If buffer successfully created a EVP_PKEY from the bio, mark the buffer as consumed
  142. bp->data = NULL;
  143. bp->dlen = 0;
  144. return x;
  145. failed:
  146. if (m) {
  147. EVP_PKEY_free(x);
  148. }
  149. return NULL;}
  150. /**
  151. * @brief set the SSL context private key
  152. */
  153. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
  154. {
  155. SSL_ASSERT1(ctx);
  156. SSL_ASSERT1(pkey);
  157. if (ctx->cert->pkey == pkey)
  158. return 1;
  159. if (ctx->cert->pkey)
  160. EVP_PKEY_free(ctx->cert->pkey);
  161. pkey->ref_counter++;
  162. ctx->cert->pkey = pkey;
  163. return 1;
  164. }
  165. /**
  166. * @brief set the SSL private key
  167. */
  168. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
  169. {
  170. SSL_ASSERT1(ssl);
  171. SSL_ASSERT1(pkey);
  172. if (ssl->cert->pkey == pkey)
  173. return 1;
  174. if (ssl->cert->pkey)
  175. EVP_PKEY_free(ssl->cert->pkey);
  176. ssl->cert->pkey = pkey;
  177. return 1;
  178. }
  179. /**
  180. * @brief load private key into the SSL context
  181. */
  182. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
  183. const unsigned char *d, long len)
  184. {
  185. int ret;
  186. EVP_PKEY *pk;
  187. pk = d2i_PrivateKey(0, NULL, &d, len);
  188. if (!pk) {
  189. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_PrivateKey() return NULL");
  190. goto failed1;
  191. }
  192. ret = SSL_CTX_use_PrivateKey(ctx, pk);
  193. if (!ret) {
  194. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_CTX_use_PrivateKey() return %d", ret);
  195. goto failed2;
  196. }
  197. return 1;
  198. failed2:
  199. EVP_PKEY_free(pk);
  200. failed1:
  201. return 0;
  202. }
  203. /**
  204. * @brief load private key into the SSL
  205. */
  206. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl,
  207. const unsigned char *d, long len)
  208. {
  209. int ret;
  210. EVP_PKEY *pk;
  211. pk = d2i_PrivateKey(0, NULL, &d, len);
  212. if (!pk) {
  213. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_PrivateKey() return NULL");
  214. goto failed1;
  215. }
  216. ret = SSL_use_PrivateKey(ssl, pk);
  217. if (!ret) {
  218. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_use_PrivateKey() return %d", ret);
  219. goto failed2;
  220. }
  221. return 1;
  222. failed2:
  223. EVP_PKEY_free(pk);
  224. failed1:
  225. return 0;
  226. }
  227. #define ESP_OPENSSL_FILES_IS_SUPPORTED 0
  228. /**
  229. * @brief load the private key file into SSL context
  230. */
  231. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  232. {
  233. // Using file name as private key is discouraged
  234. SSL_ASSERT1(ESP_OPENSSL_FILES_IS_SUPPORTED);
  235. return -1;
  236. }
  237. /**
  238. * @brief load the private key file into SSL
  239. */
  240. int SSL_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  241. {
  242. // Using file name as private key is discouraged
  243. SSL_ASSERT1(ESP_OPENSSL_FILES_IS_SUPPORTED);
  244. return -1;
  245. }
  246. /**
  247. * @brief load the RSA ASN1 private key into SSL context
  248. */
  249. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
  250. {
  251. return SSL_CTX_use_PrivateKey_ASN1(0, ctx, d, len);
  252. }
  253. void RSA_free (RSA *r)
  254. {
  255. EVP_PKEY_free(r);
  256. }