ssl_x509.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #include <assert.h>
  18. /**
  19. * @brief show X509 certification information
  20. */
  21. int __X509_show_info(X509 *x)
  22. {
  23. return X509_METHOD_CALL(show_info, x);
  24. }
  25. /**
  26. * @brief create a X509 certification object according to input X509 certification
  27. */
  28. X509* __X509_new(X509 *ix)
  29. {
  30. int ret;
  31. X509 *x;
  32. x = ssl_mem_zalloc(sizeof(X509));
  33. if (!x) {
  34. SSL_DEBUG(SSL_X509_ERROR_LEVEL, "no enough memory > (x)");
  35. goto no_mem;
  36. }
  37. if (ix)
  38. x->method = ix->method;
  39. else
  40. x->method = X509_method();
  41. ret = X509_METHOD_CALL(new, x, ix);
  42. if (ret) {
  43. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_METHOD_CALL(new) return %d", ret);
  44. goto failed;
  45. }
  46. return x;
  47. failed:
  48. ssl_mem_free(x);
  49. no_mem:
  50. return NULL;
  51. }
  52. /**
  53. * @brief create a X509 certification object
  54. */
  55. X509* X509_new(void)
  56. {
  57. return __X509_new(NULL);
  58. }
  59. /**
  60. * @brief free a X509 certification object
  61. */
  62. void X509_free(X509 *x)
  63. {
  64. SSL_ASSERT3(x);
  65. X509_METHOD_CALL(free, x);
  66. ssl_mem_free(x);
  67. };
  68. /**
  69. * @brief load a character certification context into system context. If '*cert' is pointed to the
  70. * certification, then load certification into it. Or create a new X509 certification object
  71. */
  72. X509* d2i_X509(X509 **cert, const unsigned char *buffer, long len)
  73. {
  74. int m = 0;
  75. int ret;
  76. X509 *x;
  77. SSL_ASSERT2(buffer);
  78. SSL_ASSERT2(len);
  79. if (cert && *cert) {
  80. x = *cert;
  81. } else {
  82. x = X509_new();
  83. if (!x) {
  84. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_new() return NULL");
  85. goto failed1;
  86. }
  87. m = 1;
  88. }
  89. ret = X509_METHOD_CALL(load, x, buffer, len);
  90. if (ret) {
  91. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_METHOD_CALL(load) return %d", ret);
  92. goto failed2;
  93. }
  94. return x;
  95. failed2:
  96. if (m)
  97. X509_free(x);
  98. failed1:
  99. return NULL;
  100. }
  101. /**
  102. * @brief return SSL X509 verify parameters
  103. */
  104. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
  105. {
  106. return &ssl->param;
  107. }
  108. /**
  109. * @brief set X509 host verification flags
  110. */
  111. int X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
  112. unsigned long flags)
  113. {
  114. /* flags not supported yet */
  115. return 0;
  116. }
  117. /**
  118. * @brief clear X509 host verification flags
  119. */
  120. int X509_VERIFY_PARAM_clear_hostflags(X509_VERIFY_PARAM *param,
  121. unsigned long flags)
  122. {
  123. /* flags not supported yet */
  124. return 0;
  125. }
  126. /**
  127. * @brief set SSL context client CA certification
  128. */
  129. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
  130. {
  131. SSL_ASSERT1(ctx);
  132. SSL_ASSERT1(x);
  133. assert(ctx);
  134. if (ctx->client_CA == x)
  135. return 1;
  136. X509_free(ctx->client_CA);
  137. ctx->client_CA = x;
  138. return 1;
  139. }
  140. /**
  141. * @brief add CA client certification into the SSL
  142. */
  143. int SSL_CTX_add_client_CA_ASN1(SSL_CTX *ctx, int len,
  144. const unsigned char *d)
  145. {
  146. X509 *x;
  147. x = d2i_X509(NULL, d, len);
  148. if (!x) {
  149. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_X509() return NULL");
  150. return 0;
  151. }
  152. SSL_ASSERT1(ctx);
  153. X509_free(ctx->client_CA);
  154. ctx->client_CA = x;
  155. return 1;
  156. }
  157. /**
  158. * @brief add CA client certification into the SSL
  159. */
  160. int SSL_add_client_CA(SSL *ssl, X509 *x)
  161. {
  162. SSL_ASSERT1(ssl);
  163. SSL_ASSERT1(x);
  164. if (ssl->client_CA == x)
  165. return 1;
  166. X509_free(ssl->client_CA);
  167. ssl->client_CA = x;
  168. return 1;
  169. }
  170. /**
  171. * @brief set the SSL context certification
  172. */
  173. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
  174. {
  175. SSL_ASSERT1(ctx);
  176. SSL_ASSERT1(x);
  177. if (ctx->cert->x509 == x)
  178. return 1;
  179. X509_free(ctx->cert->x509);
  180. ctx->cert->x509 = x;
  181. return 1;
  182. }
  183. /**
  184. * @brief set the SSL certification
  185. */
  186. int SSL_use_certificate(SSL *ssl, X509 *x)
  187. {
  188. SSL_ASSERT1(ssl);
  189. SSL_ASSERT1(x);
  190. if (ssl->cert->x509 == x)
  191. return 1;
  192. X509_free(ssl->cert->x509);
  193. ssl->cert->x509 = x;
  194. return 1;
  195. }
  196. /**
  197. * @brief get the SSL certification point
  198. */
  199. X509 *SSL_get_certificate(const SSL *ssl)
  200. {
  201. SSL_ASSERT2(ssl);
  202. return ssl->cert->x509;
  203. }
  204. /**
  205. * @brief load certification into the SSL context
  206. */
  207. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
  208. const unsigned char *d)
  209. {
  210. int ret;
  211. X509 *x;
  212. x = d2i_X509(NULL, d, len);
  213. if (!x) {
  214. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_X509() return NULL");
  215. goto failed1;
  216. }
  217. ret = SSL_CTX_use_certificate(ctx, x);
  218. if (!ret) {
  219. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_CTX_use_certificate() return %d", ret);
  220. goto failed2;
  221. }
  222. return 1;
  223. failed2:
  224. X509_free(x);
  225. failed1:
  226. return 0;
  227. }
  228. /**
  229. * @brief load certification into the SSL
  230. */
  231. int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
  232. {
  233. int ret;
  234. X509 *x;
  235. x = d2i_X509(NULL, d, len);
  236. if (!x) {
  237. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_X509() return NULL");
  238. goto failed1;
  239. }
  240. ret = SSL_use_certificate(ssl, x);
  241. if (!ret) {
  242. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_use_certificate() return %d", ret);
  243. goto failed2;
  244. }
  245. return 1;
  246. failed2:
  247. X509_free(x);
  248. failed1:
  249. return 0;
  250. }
  251. /**
  252. * @brief load the certification file into SSL context
  253. */
  254. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
  255. {
  256. return 0;
  257. }
  258. /**
  259. * @brief load the certification file into SSL
  260. */
  261. int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
  262. {
  263. return 0;
  264. }
  265. /**
  266. * @brief get peer certification
  267. */
  268. X509 *SSL_get_peer_certificate(const SSL *ssl)
  269. {
  270. SSL_ASSERT2(ssl);
  271. return ssl->session->peer;
  272. }
  273. int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
  274. {
  275. return X509_V_ERR_UNSPECIFIED;
  276. }
  277. int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
  278. {
  279. return 0;
  280. }
  281. const char *X509_verify_cert_error_string(long n)
  282. {
  283. return "unknown";
  284. }