ssl_pkey.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /**
  18. * @brief create a private key object according to input private key
  19. */
  20. EVP_PKEY* __EVP_PKEY_new(EVP_PKEY *ipk)
  21. {
  22. int ret;
  23. EVP_PKEY *pkey;
  24. pkey = ssl_mem_zalloc(sizeof(EVP_PKEY));
  25. if (!pkey) {
  26. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "no enough memory > (pkey)");
  27. goto no_mem;
  28. }
  29. if (ipk) {
  30. pkey->method = ipk->method;
  31. } else {
  32. pkey->method = EVP_PKEY_method();
  33. }
  34. ret = EVP_PKEY_METHOD_CALL(new, pkey, ipk);
  35. if (ret) {
  36. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_METHOD_CALL(new) return %d", ret);
  37. goto failed;
  38. }
  39. return pkey;
  40. failed:
  41. ssl_mem_free(pkey);
  42. no_mem:
  43. return NULL;
  44. }
  45. /**
  46. * @brief create a private key object
  47. */
  48. EVP_PKEY* EVP_PKEY_new(void)
  49. {
  50. return __EVP_PKEY_new(NULL);
  51. }
  52. /**
  53. * @brief free a private key object
  54. */
  55. void EVP_PKEY_free(EVP_PKEY *pkey)
  56. {
  57. SSL_ASSERT3(pkey);
  58. EVP_PKEY_METHOD_CALL(free, pkey);
  59. ssl_mem_free(pkey);
  60. }
  61. /**
  62. * @brief load a character key context into system context. If '*a' is pointed to the
  63. * private key, then load key into it. Or create a new private key object
  64. */
  65. EVP_PKEY *d2i_PrivateKey(int type,
  66. EVP_PKEY **a,
  67. const unsigned char **pp,
  68. long length)
  69. {
  70. int m = 0;
  71. int ret;
  72. EVP_PKEY *pkey;
  73. SSL_ASSERT2(pp);
  74. SSL_ASSERT2(*pp);
  75. SSL_ASSERT2(length);
  76. if (a && *a) {
  77. pkey = *a;
  78. } else {
  79. pkey = EVP_PKEY_new();;
  80. if (!pkey) {
  81. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_new() return NULL");
  82. goto failed1;
  83. }
  84. m = 1;
  85. }
  86. ret = EVP_PKEY_METHOD_CALL(load, pkey, *pp, length);
  87. if (ret) {
  88. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_METHOD_CALL(load) return %d", ret);
  89. goto failed2;
  90. }
  91. if (a)
  92. *a = pkey;
  93. return pkey;
  94. failed2:
  95. if (m)
  96. EVP_PKEY_free(pkey);
  97. failed1:
  98. return NULL;
  99. }
  100. /**
  101. * @brief set the SSL context private key
  102. */
  103. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
  104. {
  105. SSL_ASSERT1(ctx);
  106. SSL_ASSERT1(pkey);
  107. if (ctx->cert->pkey == pkey)
  108. return 1;
  109. if (ctx->cert->pkey)
  110. EVP_PKEY_free(ctx->cert->pkey);
  111. ctx->cert->pkey = pkey;
  112. return 1;
  113. }
  114. /**
  115. * @brief set the SSL private key
  116. */
  117. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
  118. {
  119. SSL_ASSERT1(ssl);
  120. SSL_ASSERT1(pkey);
  121. if (ssl->cert->pkey == pkey)
  122. return 1;
  123. if (ssl->cert->pkey)
  124. EVP_PKEY_free(ssl->cert->pkey);
  125. ssl->cert->pkey = pkey;
  126. return 1;
  127. }
  128. /**
  129. * @brief load private key into the SSL context
  130. */
  131. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
  132. const unsigned char *d, long len)
  133. {
  134. int ret;
  135. EVP_PKEY *pk;
  136. pk = d2i_PrivateKey(0, NULL, &d, len);
  137. if (!pk) {
  138. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_PrivateKey() return NULL");
  139. goto failed1;
  140. }
  141. ret = SSL_CTX_use_PrivateKey(ctx, pk);
  142. if (!ret) {
  143. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_CTX_use_PrivateKey() return %d", ret);
  144. goto failed2;
  145. }
  146. return 1;
  147. failed2:
  148. EVP_PKEY_free(pk);
  149. failed1:
  150. return 0;
  151. }
  152. /**
  153. * @brief load private key into the SSL
  154. */
  155. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl,
  156. const unsigned char *d, long len)
  157. {
  158. int ret;
  159. EVP_PKEY *pk;
  160. pk = d2i_PrivateKey(0, NULL, &d, len);
  161. if (!pk) {
  162. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_PrivateKey() return NULL");
  163. goto failed1;
  164. }
  165. ret = SSL_use_PrivateKey(ssl, pk);
  166. if (!ret) {
  167. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_use_PrivateKey() return %d", ret);
  168. goto failed2;
  169. }
  170. return 1;
  171. failed2:
  172. EVP_PKEY_free(pk);
  173. failed1:
  174. return 0;
  175. }
  176. /**
  177. * @brief load the private key file into SSL context
  178. */
  179. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  180. {
  181. return 0;
  182. }
  183. /**
  184. * @brief load the private key file into SSL
  185. */
  186. int SSL_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  187. {
  188. return 0;
  189. }
  190. /**
  191. * @brief load the RSA ASN1 private key into SSL context
  192. */
  193. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
  194. {
  195. return SSL_CTX_use_PrivateKey_ASN1(0, ctx, d, len);
  196. }