x509.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Copyright (c) 2007-2015, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @file x509.c
  32. *
  33. * Certificate processing.
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <time.h>
  39. #include "os_port.h"
  40. #include "crypto_misc.h"
  41. #ifdef CONFIG_SSL_CERT_VERIFICATION
  42. /**
  43. * Retrieve the signature from a certificate.
  44. */
  45. static const uint8_t *get_signature(const uint8_t *asn1_sig, int *len)
  46. {
  47. int offset = 0;
  48. const uint8_t *ptr = NULL;
  49. if (asn1_next_obj(asn1_sig, &offset, ASN1_SEQUENCE) < 0 ||
  50. asn1_skip_obj(asn1_sig, &offset, ASN1_SEQUENCE))
  51. goto end_get_sig;
  52. if (asn1_sig[offset++] != ASN1_OCTET_STRING)
  53. goto end_get_sig;
  54. *len = get_asn1_length(asn1_sig, &offset);
  55. ptr = &asn1_sig[offset]; /* all ok */
  56. end_get_sig:
  57. return ptr;
  58. }
  59. #endif
  60. /**
  61. * Construct a new x509 object.
  62. * @return 0 if ok. < 0 if there was a problem.
  63. */
  64. int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx)
  65. {
  66. int begin_tbs, end_tbs;
  67. int ret = X509_NOT_OK, offset = 0, cert_size = 0;
  68. X509_CTX *x509_ctx;
  69. BI_CTX *bi_ctx;
  70. *ctx = (X509_CTX *)calloc(1, sizeof(X509_CTX));
  71. x509_ctx = *ctx;
  72. /* get the certificate size */
  73. asn1_skip_obj(cert, &cert_size, ASN1_SEQUENCE);
  74. if (asn1_next_obj(cert, &offset, ASN1_SEQUENCE) < 0)
  75. goto end_cert;
  76. begin_tbs = offset; /* start of the tbs */
  77. end_tbs = begin_tbs; /* work out the end of the tbs */
  78. asn1_skip_obj(cert, &end_tbs, ASN1_SEQUENCE);
  79. if (asn1_next_obj(cert, &offset, ASN1_SEQUENCE) < 0)
  80. goto end_cert;
  81. if (cert[offset] == ASN1_EXPLICIT_TAG) /* optional version */
  82. {
  83. if (asn1_version(cert, &offset, x509_ctx))
  84. goto end_cert;
  85. }
  86. if (asn1_skip_obj(cert, &offset, ASN1_INTEGER) || /* serial number */
  87. asn1_next_obj(cert, &offset, ASN1_SEQUENCE) < 0)
  88. goto end_cert;
  89. /* make sure the signature is ok */
  90. if (asn1_signature_type(cert, &offset, x509_ctx))
  91. {
  92. ret = X509_VFY_ERROR_UNSUPPORTED_DIGEST;
  93. goto end_cert;
  94. }
  95. if (asn1_name(cert, &offset, x509_ctx->ca_cert_dn) ||
  96. asn1_validity(cert, &offset, x509_ctx) ||
  97. asn1_name(cert, &offset, x509_ctx->cert_dn) ||
  98. asn1_public_key(cert, &offset, x509_ctx))
  99. {
  100. goto end_cert;
  101. }
  102. bi_ctx = x509_ctx->rsa_ctx->bi_ctx;
  103. #ifdef CONFIG_SSL_CERT_VERIFICATION /* only care if doing verification */
  104. /* use the appropriate signature algorithm */
  105. switch (x509_ctx->sig_type)
  106. {
  107. case SIG_TYPE_MD5:
  108. {
  109. MD5_CTX md5_ctx;
  110. uint8_t md5_dgst[MD5_SIZE];
  111. MD5_Init(&md5_ctx);
  112. MD5_Update(&md5_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
  113. MD5_Final(md5_dgst, &md5_ctx);
  114. x509_ctx->digest = bi_import(bi_ctx, md5_dgst, MD5_SIZE);
  115. }
  116. break;
  117. case SIG_TYPE_SHA1:
  118. {
  119. SHA1_CTX sha_ctx;
  120. uint8_t sha_dgst[SHA1_SIZE];
  121. SHA1_Init(&sha_ctx);
  122. SHA1_Update(&sha_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
  123. SHA1_Final(sha_dgst, &sha_ctx);
  124. x509_ctx->digest = bi_import(bi_ctx, sha_dgst, SHA1_SIZE);
  125. }
  126. break;
  127. case SIG_TYPE_SHA256:
  128. {
  129. SHA256_CTX sha256_ctx;
  130. uint8_t sha256_dgst[SHA256_SIZE];
  131. SHA256_Init(&sha256_ctx);
  132. SHA256_Update(&sha256_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
  133. SHA256_Final(sha256_dgst, &sha256_ctx);
  134. x509_ctx->digest = bi_import(bi_ctx, sha256_dgst, SHA256_SIZE);
  135. }
  136. break;
  137. #ifndef WITHOUTH_SHA512
  138. case SIG_TYPE_SHA384:
  139. {
  140. SHA384_CTX sha384_ctx;
  141. uint8_t sha384_dgst[SHA384_SIZE];
  142. SHA384_Init(&sha384_ctx);
  143. SHA384_Update(&sha384_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
  144. SHA384_Final(sha384_dgst, &sha384_ctx);
  145. x509_ctx->digest = bi_import(bi_ctx, sha384_dgst, SHA384_SIZE);
  146. }
  147. break;
  148. case SIG_TYPE_SHA512:
  149. {
  150. SHA512_CTX sha512_ctx;
  151. uint8_t sha512_dgst[SHA512_SIZE];
  152. SHA512_Init(&sha512_ctx);
  153. SHA512_Update(&sha512_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
  154. SHA512_Final(sha512_dgst, &sha512_ctx);
  155. x509_ctx->digest = bi_import(bi_ctx, sha512_dgst, SHA512_SIZE);
  156. }
  157. break;
  158. #endif
  159. }
  160. if (cert[offset] == ASN1_V3_DATA)
  161. {
  162. int suboffset;
  163. ++offset;
  164. get_asn1_length(cert, &offset);
  165. if ((suboffset = asn1_find_subjectaltname(cert, offset)) > 0)
  166. {
  167. if (asn1_next_obj(cert, &suboffset, ASN1_OCTET_STRING) > 0)
  168. {
  169. int altlen;
  170. if ((altlen = asn1_next_obj(cert,
  171. &suboffset, ASN1_SEQUENCE)) > 0)
  172. {
  173. int endalt = suboffset + altlen;
  174. int totalnames = 0;
  175. while (suboffset < endalt)
  176. {
  177. int type = cert[suboffset++];
  178. int dnslen = get_asn1_length(cert, &suboffset);
  179. if (type == ASN1_CONTEXT_DNSNAME)
  180. {
  181. x509_ctx->subject_alt_dnsnames = (char**)
  182. realloc(x509_ctx->subject_alt_dnsnames,
  183. (totalnames + 2) * sizeof(char*));
  184. x509_ctx->subject_alt_dnsnames[totalnames] =
  185. (char*)malloc(dnslen + 1);
  186. x509_ctx->subject_alt_dnsnames[totalnames+1] = NULL;
  187. memcpy(x509_ctx->subject_alt_dnsnames[totalnames],
  188. cert + suboffset, dnslen);
  189. x509_ctx->subject_alt_dnsnames[
  190. totalnames][dnslen] = 0;
  191. ++totalnames;
  192. }
  193. suboffset += dnslen;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. offset = end_tbs; /* skip the rest of v3 data */
  200. if (asn1_skip_obj(cert, &offset, ASN1_SEQUENCE) ||
  201. asn1_signature(cert, &offset, x509_ctx))
  202. goto end_cert;
  203. #endif
  204. ret = X509_OK;
  205. end_cert:
  206. if (len)
  207. {
  208. *len = cert_size;
  209. }
  210. if (ret)
  211. {
  212. #ifdef CONFIG_SSL_FULL_MODE
  213. printf("Error: Invalid X509 ASN.1 file (%s)\n",
  214. x509_display_error(ret));
  215. #endif
  216. x509_free(x509_ctx);
  217. *ctx = NULL;
  218. }
  219. return ret;
  220. }
  221. /**
  222. * Free an X.509 object's resources.
  223. */
  224. void x509_free(X509_CTX *x509_ctx)
  225. {
  226. X509_CTX *next;
  227. int i;
  228. if (x509_ctx == NULL) /* if already null, then don't bother */
  229. return;
  230. for (i = 0; i < X509_NUM_DN_TYPES; i++)
  231. {
  232. free(x509_ctx->ca_cert_dn[i]);
  233. free(x509_ctx->cert_dn[i]);
  234. }
  235. free(x509_ctx->signature);
  236. #ifdef CONFIG_SSL_CERT_VERIFICATION
  237. if (x509_ctx->digest)
  238. {
  239. bi_free(x509_ctx->rsa_ctx->bi_ctx, x509_ctx->digest);
  240. }
  241. if (x509_ctx->subject_alt_dnsnames)
  242. {
  243. for (i = 0; x509_ctx->subject_alt_dnsnames[i]; ++i)
  244. free(x509_ctx->subject_alt_dnsnames[i]);
  245. free(x509_ctx->subject_alt_dnsnames);
  246. }
  247. #endif
  248. RSA_free(x509_ctx->rsa_ctx);
  249. next = x509_ctx->next;
  250. free(x509_ctx);
  251. x509_free(next); /* clear the chain */
  252. }
  253. #ifdef CONFIG_SSL_CERT_VERIFICATION
  254. /**
  255. * Take a signature and decrypt it.
  256. */
  257. static bigint *sig_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
  258. bigint *modulus, bigint *pub_exp)
  259. {
  260. int i, size;
  261. bigint *decrypted_bi, *dat_bi;
  262. bigint *bir = NULL;
  263. uint8_t *block = (uint8_t *)alloca(sig_len);
  264. /* decrypt */
  265. dat_bi = bi_import(ctx, sig, sig_len);
  266. ctx->mod_offset = BIGINT_M_OFFSET;
  267. /* convert to a normal block */
  268. decrypted_bi = bi_mod_power2(ctx, dat_bi, modulus, pub_exp);
  269. bi_export(ctx, decrypted_bi, block, sig_len);
  270. ctx->mod_offset = BIGINT_M_OFFSET;
  271. i = 10; /* start at the first possible non-padded byte */
  272. while (block[i++] && i < sig_len);
  273. size = sig_len - i;
  274. /* get only the bit we want */
  275. if (size > 0)
  276. {
  277. int len;
  278. const uint8_t *sig_ptr = get_signature(&block[i], &len);
  279. if (sig_ptr)
  280. {
  281. bir = bi_import(ctx, sig_ptr, len);
  282. }
  283. }
  284. /* save a few bytes of memory */
  285. bi_clear_cache(ctx);
  286. return bir;
  287. }
  288. /**
  289. * Do some basic checks on the certificate chain.
  290. *
  291. * Certificate verification consists of a number of checks:
  292. * - The date of the certificate is after the start date.
  293. * - The date of the certificate is before the finish date.
  294. * - A root certificate exists in the certificate store.
  295. * - That the certificate(s) are not self-signed.
  296. * - The certificate chain is valid.
  297. * - The signature of the certificate is valid.
  298. */
  299. int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert)
  300. {
  301. int ret = X509_OK, i = 0;
  302. bigint *cert_sig;
  303. X509_CTX *next_cert = NULL;
  304. BI_CTX *ctx = NULL;
  305. bigint *mod = NULL, *expn = NULL;
  306. int match_ca_cert = 0;
  307. struct timeval tv;
  308. uint8_t is_self_signed = 0;
  309. if (cert == NULL)
  310. {
  311. ret = X509_VFY_ERROR_NO_TRUSTED_CERT;
  312. goto end_verify;
  313. }
  314. /* a self-signed certificate that is not in the CA store - use this
  315. to check the signature */
  316. if (asn1_compare_dn(cert->ca_cert_dn, cert->cert_dn) == 0)
  317. {
  318. is_self_signed = 1;
  319. ctx = cert->rsa_ctx->bi_ctx;
  320. mod = cert->rsa_ctx->m;
  321. expn = cert->rsa_ctx->e;
  322. }
  323. gettimeofday(&tv, NULL);
  324. /* check the not before date */
  325. if (tv.tv_sec < cert->not_before)
  326. {
  327. ret = X509_VFY_ERROR_NOT_YET_VALID;
  328. goto end_verify;
  329. }
  330. /* check the not after date */
  331. if (tv.tv_sec > cert->not_after)
  332. {
  333. ret = X509_VFY_ERROR_EXPIRED;
  334. goto end_verify;
  335. }
  336. next_cert = cert->next;
  337. /* last cert in the chain - look for a trusted cert */
  338. if (next_cert == NULL)
  339. {
  340. if (ca_cert_ctx != NULL)
  341. {
  342. /* go thu the CA store */
  343. while (i < CONFIG_X509_MAX_CA_CERTS && ca_cert_ctx->cert[i])
  344. {
  345. if (asn1_compare_dn(cert->ca_cert_dn,
  346. ca_cert_ctx->cert[i]->cert_dn) == 0)
  347. {
  348. /* use this CA certificate for signature verification */
  349. match_ca_cert = 1;
  350. ctx = ca_cert_ctx->cert[i]->rsa_ctx->bi_ctx;
  351. mod = ca_cert_ctx->cert[i]->rsa_ctx->m;
  352. expn = ca_cert_ctx->cert[i]->rsa_ctx->e;
  353. break;
  354. }
  355. i++;
  356. }
  357. }
  358. /* couldn't find a trusted cert (& let self-signed errors
  359. be returned) */
  360. if (!match_ca_cert && !is_self_signed)
  361. {
  362. ret = X509_VFY_ERROR_NO_TRUSTED_CERT;
  363. goto end_verify;
  364. }
  365. }
  366. else if (asn1_compare_dn(cert->ca_cert_dn, next_cert->cert_dn) != 0)
  367. {
  368. /* check the chain */
  369. ret = X509_VFY_ERROR_INVALID_CHAIN;
  370. goto end_verify;
  371. }
  372. else /* use the next certificate in the chain for signature verify */
  373. {
  374. ctx = next_cert->rsa_ctx->bi_ctx;
  375. mod = next_cert->rsa_ctx->m;
  376. expn = next_cert->rsa_ctx->e;
  377. }
  378. /* cert is self signed */
  379. if (!match_ca_cert && is_self_signed)
  380. {
  381. ret = X509_VFY_ERROR_SELF_SIGNED;
  382. goto end_verify;
  383. }
  384. /* check the signature */
  385. cert_sig = sig_verify(ctx, cert->signature, cert->sig_len,
  386. bi_clone(ctx, mod), bi_clone(ctx, expn));
  387. if (cert_sig && cert->digest)
  388. {
  389. if (bi_compare(cert_sig, cert->digest) != 0)
  390. ret = X509_VFY_ERROR_BAD_SIGNATURE;
  391. bi_free(ctx, cert_sig);
  392. }
  393. else
  394. {
  395. ret = X509_VFY_ERROR_BAD_SIGNATURE;
  396. }
  397. if (ret)
  398. goto end_verify;
  399. /* go down the certificate chain using recursion. */
  400. if (next_cert != NULL)
  401. {
  402. ret = x509_verify(ca_cert_ctx, next_cert);
  403. }
  404. end_verify:
  405. return ret;
  406. }
  407. #endif
  408. #if defined (CONFIG_SSL_FULL_MODE)
  409. /**
  410. * Used for diagnostics.
  411. */
  412. static const char *not_part_of_cert = "<Not Part Of Certificate>";
  413. void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx)
  414. {
  415. if (cert == NULL)
  416. return;
  417. printf("=== CERTIFICATE ISSUED TO ===\n");
  418. printf("Common Name (CN):\t\t");
  419. printf("%s\n", cert->cert_dn[X509_COMMON_NAME] ?
  420. cert->cert_dn[X509_COMMON_NAME] : not_part_of_cert);
  421. printf("Organization (O):\t\t");
  422. printf("%s\n", cert->cert_dn[X509_ORGANIZATION] ?
  423. cert->cert_dn[X509_ORGANIZATION] : not_part_of_cert);
  424. printf("Organizational Unit (OU):\t");
  425. printf("%s\n", cert->cert_dn[X509_ORGANIZATIONAL_UNIT] ?
  426. cert->cert_dn[X509_ORGANIZATIONAL_UNIT] : not_part_of_cert);
  427. printf("=== CERTIFICATE ISSUED BY ===\n");
  428. printf("Common Name (CN):\t\t");
  429. printf("%s\n", cert->ca_cert_dn[X509_COMMON_NAME] ?
  430. cert->ca_cert_dn[X509_COMMON_NAME] : not_part_of_cert);
  431. printf("Organization (O):\t\t");
  432. printf("%s\n", cert->ca_cert_dn[X509_ORGANIZATION] ?
  433. cert->ca_cert_dn[X509_ORGANIZATION] : not_part_of_cert);
  434. printf("Organizational Unit (OU):\t");
  435. printf("%s\n", cert->ca_cert_dn[X509_ORGANIZATIONAL_UNIT] ?
  436. cert->ca_cert_dn[X509_ORGANIZATIONAL_UNIT] : not_part_of_cert);
  437. printf("Not Before:\t\t\t%s", ctime(&cert->not_before));
  438. printf("Not After:\t\t\t%s", ctime(&cert->not_after));
  439. printf("RSA bitsize:\t\t\t%d\n", cert->rsa_ctx->num_octets*8);
  440. printf("Sig Type:\t\t\t");
  441. switch (cert->sig_type)
  442. {
  443. case SIG_TYPE_MD2:
  444. printf("MD2\n");
  445. break;
  446. case SIG_TYPE_MD5:
  447. printf("MD5\n");
  448. break;
  449. case SIG_TYPE_SHA1:
  450. printf("SHA1\n");
  451. break;
  452. case SIG_TYPE_SHA256:
  453. printf("SHA256\n");
  454. break;
  455. case SIG_TYPE_SHA384:
  456. printf("SHA384\n");
  457. break;
  458. case SIG_TYPE_SHA512:
  459. printf("SHA512\n");
  460. break;
  461. default:
  462. printf("Unrecognized: %d\n", cert->sig_type);
  463. break;
  464. }
  465. if (ca_cert_ctx)
  466. {
  467. printf("Verify:\t\t\t\t%s\n",
  468. x509_display_error(x509_verify(ca_cert_ctx, cert)));
  469. }
  470. #if 0
  471. print_blob("Signature", cert->signature, cert->sig_len);
  472. bi_print("Modulus", cert->rsa_ctx->m);
  473. bi_print("Pub Exp", cert->rsa_ctx->e);
  474. #endif
  475. if (ca_cert_ctx)
  476. {
  477. x509_print(cert->next, ca_cert_ctx);
  478. }
  479. TTY_FLUSH();
  480. }
  481. const char * x509_display_error(int error)
  482. {
  483. switch (error)
  484. {
  485. case X509_OK:
  486. return "Certificate verify successful";
  487. case X509_NOT_OK:
  488. return "X509 not ok";
  489. case X509_VFY_ERROR_NO_TRUSTED_CERT:
  490. return "No trusted cert is available";
  491. case X509_VFY_ERROR_BAD_SIGNATURE:
  492. return "Bad signature";
  493. case X509_VFY_ERROR_NOT_YET_VALID:
  494. return "Cert is not yet valid";
  495. case X509_VFY_ERROR_EXPIRED:
  496. return "Cert has expired";
  497. case X509_VFY_ERROR_SELF_SIGNED:
  498. return "Cert is self-signed";
  499. case X509_VFY_ERROR_INVALID_CHAIN:
  500. return "Chain is invalid (check order of certs)";
  501. case X509_VFY_ERROR_UNSUPPORTED_DIGEST:
  502. return "Unsupported digest";
  503. case X509_INVALID_PRIV_KEY:
  504. return "Invalid private key";
  505. default:
  506. return "Unknown";
  507. }
  508. }
  509. #endif /* CONFIG_SSL_FULL_MODE */