ssl_x509.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 "ssl.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. x->ref_counter = 1;
  38. if (ix && ix->method)
  39. x->method = ix->method;
  40. else
  41. x->method = X509_method();
  42. ret = X509_METHOD_CALL(new, x, ix);
  43. if (ret) {
  44. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_METHOD_CALL(new) return %d", ret);
  45. goto failed;
  46. }
  47. return x;
  48. failed:
  49. ssl_mem_free(x);
  50. no_mem:
  51. return NULL;
  52. }
  53. /**
  54. * @brief create a X509 certification object
  55. */
  56. X509* X509_new(void)
  57. {
  58. return __X509_new(NULL);
  59. }
  60. /**
  61. * @brief free a X509 certification object
  62. */
  63. void X509_free(X509 *x)
  64. {
  65. SSL_ASSERT3(x);
  66. if (--x->ref_counter > 0) {
  67. return;
  68. }
  69. X509_METHOD_CALL(free, x);
  70. ssl_mem_free(x);
  71. };
  72. /**
  73. * @brief load a character certification context into system context. If '*cert' is pointed to the
  74. * certification, then load certification into it. Or create a new X509 certification object
  75. */
  76. X509* d2i_X509(X509 **cert, const unsigned char *buffer, long len)
  77. {
  78. int m = 0;
  79. int ret;
  80. X509 *x;
  81. SSL_ASSERT2(buffer);
  82. SSL_ASSERT2(len);
  83. if (cert && *cert) {
  84. x = *cert;
  85. } else {
  86. x = X509_new();
  87. if (!x) {
  88. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_new() return NULL");
  89. goto failed1;
  90. }
  91. m = 1;
  92. }
  93. ret = X509_METHOD_CALL(load, x, buffer, len);
  94. if (ret) {
  95. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_METHOD_CALL(load) return %d", ret);
  96. goto failed2;
  97. }
  98. return x;
  99. failed2:
  100. if (m)
  101. X509_free(x);
  102. failed1:
  103. return NULL;
  104. }
  105. /**
  106. * @brief return SSL X509 verify parameters
  107. */
  108. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
  109. {
  110. return &ssl->param;
  111. }
  112. /**
  113. * @brief set X509 host verification flags
  114. */
  115. int X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
  116. unsigned long flags)
  117. {
  118. /* flags not supported yet */
  119. return 0;
  120. }
  121. /**
  122. * @brief clear X509 host verification flags
  123. */
  124. int X509_VERIFY_PARAM_clear_hostflags(X509_VERIFY_PARAM *param,
  125. unsigned long flags)
  126. {
  127. /* flags not supported yet */
  128. return 0;
  129. }
  130. /**
  131. * @brief set SSL context client CA certification
  132. */
  133. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
  134. {
  135. SSL_ASSERT1(ctx);
  136. SSL_ASSERT1(x);
  137. if (ctx->client_CA == x)
  138. return 1;
  139. X509_free(ctx->client_CA);
  140. ctx->client_CA = x;
  141. return 1;
  142. }
  143. /**
  144. * @brief add CA client certification into the SSL
  145. */
  146. int SSL_add_client_CA(SSL *ssl, X509 *x)
  147. {
  148. SSL_ASSERT1(ssl);
  149. SSL_ASSERT1(x);
  150. if (ssl->client_CA == x)
  151. return 1;
  152. X509_free(ssl->client_CA);
  153. ssl->client_CA = x;
  154. return 1;
  155. }
  156. /**
  157. * @brief set the SSL context certification
  158. */
  159. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
  160. {
  161. SSL_ASSERT1(ctx);
  162. SSL_ASSERT1(x);
  163. if (ctx->cert->x509 == x)
  164. return 1;
  165. X509_free(ctx->cert->x509);
  166. ctx->cert->x509 = x;
  167. x->ref_counter++;
  168. return 1;
  169. }
  170. /**
  171. * @brief set the SSL certification
  172. */
  173. int SSL_use_certificate(SSL *ssl, X509 *x)
  174. {
  175. SSL_ASSERT1(ssl);
  176. SSL_ASSERT1(x);
  177. if (ssl->cert->x509 == x)
  178. return 1;
  179. X509_free(ssl->cert->x509);
  180. ssl->cert->x509 = x;
  181. return 1;
  182. }
  183. long SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x)
  184. {
  185. return SSL_CTX_use_certificate(ctx, x);
  186. }
  187. /**
  188. * @brief get the SSL certification point
  189. */
  190. X509 *SSL_get_certificate(const SSL *ssl)
  191. {
  192. SSL_ASSERT2(ssl);
  193. return ssl->cert->x509;
  194. }
  195. /**
  196. * @brief load certification into the SSL context
  197. */
  198. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
  199. const unsigned char *d)
  200. {
  201. int ret;
  202. X509 *x;
  203. x = d2i_X509(NULL, d, len);
  204. if (!x) {
  205. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_X509() return NULL");
  206. goto failed1;
  207. }
  208. ret = SSL_CTX_use_certificate(ctx, x); // This uses the "x" so increments ref_count
  209. if (!ret) {
  210. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_CTX_use_certificate() return %d", ret);
  211. goto failed2;
  212. }
  213. X509_free(x); // decrements ref_count, so in case of happy flow doesn't free the "x"
  214. return 1;
  215. failed2:
  216. X509_free(x);
  217. failed1:
  218. return 0;
  219. }
  220. /**
  221. * @brief load certification into the SSL
  222. */
  223. int SSL_use_certificate_ASN1(SSL *ssl, int len,
  224. const unsigned char *d)
  225. {
  226. int ret;
  227. X509 *x;
  228. x = d2i_X509(NULL, d, len);
  229. if (!x) {
  230. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_X509() return NULL");
  231. goto failed1;
  232. }
  233. ret = SSL_use_certificate(ssl, x);
  234. if (!ret) {
  235. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_use_certificate() return %d", ret);
  236. goto failed2;
  237. }
  238. return 1;
  239. failed2:
  240. X509_free(x);
  241. failed1:
  242. return 0;
  243. }
  244. /**
  245. * @brief load the certification file into SSL context
  246. */
  247. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
  248. {
  249. return 0;
  250. }
  251. /**
  252. * @brief load the certification file into SSL
  253. */
  254. int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
  255. {
  256. return 0;
  257. }
  258. /**
  259. * @brief get peer certification
  260. */
  261. X509 *SSL_get_peer_certificate(const SSL *ssl)
  262. {
  263. SSL_ASSERT2(ssl);
  264. return ssl->session->peer;
  265. }
  266. /**
  267. * @brief set SSL context client CA certification
  268. */
  269. int X509_STORE_add_cert(X509_STORE *store, X509 *x) {
  270. x->ref_counter++;
  271. SSL_CTX *ctx = (SSL_CTX *)store;
  272. SSL_ASSERT1(ctx);
  273. SSL_ASSERT1(x);
  274. if (ctx->client_CA == x) {
  275. return 1;
  276. }
  277. if (ctx->client_CA!=NULL) {
  278. X509_free(ctx->client_CA);
  279. }
  280. ctx->client_CA = x;
  281. return 1;
  282. }
  283. /**
  284. * @brief load a character certification context into system context.
  285. *
  286. * If '*cert' is pointed to the certification, then load certification
  287. * into it, or create a new X509 certification object.
  288. */
  289. X509 * PEM_read_bio_X509(BIO *bp, X509 **cert, pem_password_cb cb, void *u) {
  290. int m = 0;
  291. int ret;
  292. X509 *x;
  293. SSL_ASSERT2(BIO_method_type(bp) & BIO_TYPE_MEM);
  294. if (bp->data == NULL || bp->dlen == 0) {
  295. return NULL;
  296. }
  297. if (cert && *cert) {
  298. x = *cert;
  299. } else {
  300. x = X509_new();
  301. if (!x) {
  302. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_new() return NULL");
  303. goto failed;
  304. }
  305. m = 1;
  306. }
  307. ret = X509_METHOD_CALL(load, x, bp->data, bp->dlen);
  308. if (ret) {
  309. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_METHOD_CALL(load) return %d", ret);
  310. goto failed;
  311. }
  312. // If buffer successfully created a X509 from the bio, mark the buffer as consumed
  313. bp->data = NULL;
  314. bp->dlen = 0;
  315. return x;
  316. failed:
  317. if (m) {
  318. X509_free(x);
  319. }
  320. return NULL;
  321. }
  322. X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **cert, pem_password_cb *cb, void *u)
  323. {
  324. return PEM_read_bio_X509(bp, cert, cb, u);
  325. }
  326. /**
  327. * @brief get the SSL context object X509 certification storage
  328. */
  329. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  330. return (X509_STORE *)ctx;
  331. }