ssl_x509.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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_x509.h"
  14. #include "ssl_methods.h"
  15. #include "ssl_dbg.h"
  16. #include "ssl_port.h"
  17. /**
  18. * @brief show X509 certification information
  19. */
  20. int __X509_show_info(X509 *x)
  21. {
  22. return X509_METHOD_CALL(show_info, x);
  23. }
  24. /**
  25. * @brief create a X509 certification object according to input X509 certification
  26. */
  27. X509* __X509_new(X509 *ix)
  28. {
  29. int ret;
  30. X509 *x;
  31. x = ssl_mem_zalloc(sizeof(X509));
  32. if (!x) {
  33. SSL_DEBUG(SSL_X509_ERROR_LEVEL, "no enough memory > (x)");
  34. goto no_mem;
  35. }
  36. if (ix)
  37. x->method = ix->method;
  38. else
  39. x->method = X509_method();
  40. ret = X509_METHOD_CALL(new, x, ix);
  41. if (ret) {
  42. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_METHOD_CALL(new) return %d", ret);
  43. goto failed;
  44. }
  45. return x;
  46. failed:
  47. ssl_mem_free(x);
  48. no_mem:
  49. return NULL;
  50. }
  51. /**
  52. * @brief create a X509 certification object
  53. */
  54. X509* X509_new(void)
  55. {
  56. return __X509_new(NULL);
  57. }
  58. /**
  59. * @brief free a X509 certification object
  60. */
  61. void X509_free(X509 *x)
  62. {
  63. SSL_ASSERT3(x);
  64. X509_METHOD_CALL(free, x);
  65. ssl_mem_free(x);
  66. };
  67. /**
  68. * @brief load a character certification context into system context. If '*cert' is pointed to the
  69. * certification, then load certification into it. Or create a new X509 certification object
  70. */
  71. X509* d2i_X509(X509 **cert, const unsigned char *buffer, long len)
  72. {
  73. int m = 0;
  74. int ret;
  75. X509 *x;
  76. SSL_ASSERT2(buffer);
  77. SSL_ASSERT2(len);
  78. if (cert && *cert) {
  79. x = *cert;
  80. } else {
  81. x = X509_new();
  82. if (!x) {
  83. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_new() return NULL");
  84. goto failed1;
  85. }
  86. m = 1;
  87. }
  88. ret = X509_METHOD_CALL(load, x, buffer, len);
  89. if (ret) {
  90. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_METHOD_CALL(load) return %d", ret);
  91. goto failed2;
  92. }
  93. return x;
  94. failed2:
  95. if (m)
  96. X509_free(x);
  97. failed1:
  98. return NULL;
  99. }
  100. /**
  101. * @brief set SSL context client CA certification
  102. */
  103. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
  104. {
  105. SSL_ASSERT1(ctx);
  106. SSL_ASSERT1(x);
  107. if (ctx->client_CA == x)
  108. return 1;
  109. X509_free(ctx->client_CA);
  110. ctx->client_CA = x;
  111. return 1;
  112. }
  113. /**
  114. * @brief add CA client certification into the SSL
  115. */
  116. int SSL_add_client_CA(SSL *ssl, X509 *x)
  117. {
  118. SSL_ASSERT1(ssl);
  119. SSL_ASSERT1(x);
  120. if (ssl->client_CA == x)
  121. return 1;
  122. X509_free(ssl->client_CA);
  123. ssl->client_CA = x;
  124. return 1;
  125. }
  126. /**
  127. * @brief set the SSL context certification
  128. */
  129. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
  130. {
  131. SSL_ASSERT1(ctx);
  132. SSL_ASSERT1(x);
  133. if (ctx->cert->x509 == x)
  134. return 1;
  135. X509_free(ctx->cert->x509);
  136. ctx->cert->x509 = x;
  137. return 1;
  138. }
  139. /**
  140. * @brief set the SSL certification
  141. */
  142. int SSL_use_certificate(SSL *ssl, X509 *x)
  143. {
  144. SSL_ASSERT1(ssl);
  145. SSL_ASSERT1(x);
  146. if (ssl->cert->x509 == x)
  147. return 1;
  148. X509_free(ssl->cert->x509);
  149. ssl->cert->x509 = x;
  150. return 1;
  151. }
  152. /**
  153. * @brief get the SSL certification point
  154. */
  155. X509 *SSL_get_certificate(const SSL *ssl)
  156. {
  157. SSL_ASSERT2(ssl);
  158. return ssl->cert->x509;
  159. }
  160. /**
  161. * @brief load certification into the SSL context
  162. */
  163. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
  164. const unsigned char *d)
  165. {
  166. int ret;
  167. X509 *x;
  168. x = d2i_X509(NULL, d, len);
  169. if (!x) {
  170. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_X509() return NULL");
  171. goto failed1;
  172. }
  173. ret = SSL_CTX_use_certificate(ctx, x);
  174. if (!ret) {
  175. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_CTX_use_certificate() return %d", ret);
  176. goto failed2;
  177. }
  178. return 1;
  179. failed2:
  180. X509_free(x);
  181. failed1:
  182. return 0;
  183. }
  184. /**
  185. * @brief load certification into the SSL
  186. */
  187. int SSL_use_certificate_ASN1(SSL *ssl, int len,
  188. const unsigned char *d)
  189. {
  190. int ret;
  191. X509 *x;
  192. x = d2i_X509(NULL, d, len);
  193. if (!x) {
  194. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_X509() return NULL");
  195. goto failed1;
  196. }
  197. ret = SSL_use_certificate(ssl, x);
  198. if (!ret) {
  199. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_use_certificate() return %d", ret);
  200. goto failed2;
  201. }
  202. return 1;
  203. failed2:
  204. X509_free(x);
  205. failed1:
  206. return 0;
  207. }
  208. /**
  209. * @brief load the certification file into SSL context
  210. */
  211. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
  212. {
  213. return 0;
  214. }
  215. /**
  216. * @brief load the certification file into SSL
  217. */
  218. int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
  219. {
  220. return 0;
  221. }
  222. /**
  223. * @brief get peer certification
  224. */
  225. X509 *SSL_get_peer_certificate(const SSL *ssl)
  226. {
  227. SSL_ASSERT2(ssl);
  228. return ssl->session->peer;
  229. }