ssl_x509.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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)
  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. return 1;
  168. }
  169. /**
  170. * @brief set the SSL certification
  171. */
  172. int SSL_use_certificate(SSL *ssl, X509 *x)
  173. {
  174. SSL_ASSERT1(ssl);
  175. SSL_ASSERT1(x);
  176. if (ssl->cert->x509 == x)
  177. return 1;
  178. X509_free(ssl->cert->x509);
  179. ssl->cert->x509 = x;
  180. return 1;
  181. }
  182. /**
  183. * @brief get the SSL certification point
  184. */
  185. X509 *SSL_get_certificate(const SSL *ssl)
  186. {
  187. SSL_ASSERT2(ssl);
  188. return ssl->cert->x509;
  189. }
  190. /**
  191. * @brief load certification into the SSL context
  192. */
  193. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
  194. const unsigned char *d)
  195. {
  196. int ret;
  197. X509 *x;
  198. x = d2i_X509(NULL, d, len);
  199. if (!x) {
  200. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_X509() return NULL");
  201. goto failed1;
  202. }
  203. ret = SSL_CTX_use_certificate(ctx, x);
  204. if (!ret) {
  205. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_CTX_use_certificate() return %d", ret);
  206. goto failed2;
  207. }
  208. return 1;
  209. failed2:
  210. X509_free(x);
  211. failed1:
  212. return 0;
  213. }
  214. /**
  215. * @brief load certification into the SSL
  216. */
  217. int SSL_use_certificate_ASN1(SSL *ssl, int len,
  218. const unsigned char *d)
  219. {
  220. int ret;
  221. X509 *x;
  222. x = d2i_X509(NULL, d, len);
  223. if (!x) {
  224. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_X509() return NULL");
  225. goto failed1;
  226. }
  227. ret = SSL_use_certificate(ssl, x);
  228. if (!ret) {
  229. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_use_certificate() return %d", ret);
  230. goto failed2;
  231. }
  232. return 1;
  233. failed2:
  234. X509_free(x);
  235. failed1:
  236. return 0;
  237. }
  238. /**
  239. * @brief load the certification file into SSL context
  240. */
  241. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
  242. {
  243. return 0;
  244. }
  245. /**
  246. * @brief load the certification file into SSL
  247. */
  248. int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
  249. {
  250. return 0;
  251. }
  252. /**
  253. * @brief get peer certification
  254. */
  255. X509 *SSL_get_peer_certificate(const SSL *ssl)
  256. {
  257. SSL_ASSERT2(ssl);
  258. return ssl->session->peer;
  259. }
  260. /**
  261. * @brief set SSL context client CA certification
  262. */
  263. int X509_STORE_add_cert(X509_STORE *store, X509 *x) {
  264. x->ref_counter++;
  265. SSL_CTX *ctx = (SSL_CTX *)store;
  266. SSL_ASSERT1(ctx);
  267. SSL_ASSERT1(x);
  268. if (ctx->client_CA == x) {
  269. return 1;
  270. }
  271. if (ctx->client_CA!=NULL) {
  272. X509_free(ctx->client_CA);
  273. }
  274. ctx->client_CA = x;
  275. return 1;
  276. }
  277. /**
  278. * @brief create a BIO object
  279. */
  280. BIO *BIO_new(void *method) {
  281. BIO *b = (BIO *)malloc(sizeof(BIO));
  282. return b;
  283. }
  284. /**
  285. * @brief load data into BIO.
  286. *
  287. * Normally BIO_write should append data but doesn't happen here, and
  288. * 'data' cannot be freed after the function is called, it should remain valid
  289. * until BIO object is in use.
  290. */
  291. int BIO_write(BIO *b, const void * data, int dlen) {
  292. b->data = data;
  293. b->dlen = dlen;
  294. return 1;
  295. }
  296. /**
  297. * @brief load a character certification context into system context.
  298. *
  299. * If '*cert' is pointed to the certification, then load certification
  300. * into it, or create a new X509 certification object.
  301. */
  302. X509 * PEM_read_bio_X509(BIO *bp, X509 **cert, void *cb, void *u) {
  303. int m = 0;
  304. int ret;
  305. X509 *x;
  306. SSL_ASSERT2(bp->data);
  307. SSL_ASSERT2(bp->dlen);
  308. if (cert && *cert) {
  309. x = *cert;
  310. } else {
  311. x = X509_new();
  312. if (!x) {
  313. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_new() return NULL");
  314. goto failed;
  315. }
  316. m = 1;
  317. }
  318. ret = X509_METHOD_CALL(load, x, bp->data, bp->dlen);
  319. if (ret) {
  320. SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_METHOD_CALL(load) return %d", ret);
  321. goto failed;
  322. }
  323. return x;
  324. failed:
  325. if (m) {
  326. X509_free(x);
  327. }
  328. return NULL;
  329. }
  330. /**
  331. * @brief get the memory BIO method function
  332. */
  333. void *BIO_s_mem(void) {
  334. return NULL;
  335. }
  336. /**
  337. * @brief get the SSL context object X509 certification storage
  338. */
  339. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  340. return (X509_STORE *)ctx;
  341. }
  342. /**
  343. * @brief free a BIO object
  344. */
  345. void BIO_free(BIO *b) {
  346. free(b);
  347. }