x509.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. * Copyright (c) 2007-2017, 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. static int x509_v3_subject_alt_name(const uint8_t *cert, int offset,
  43. X509_CTX *x509_ctx);
  44. static int x509_v3_basic_constraints(const uint8_t *cert, int offset,
  45. X509_CTX *x509_ctx);
  46. static int x509_v3_key_usage(const uint8_t *cert, int offset,
  47. X509_CTX *x509_ctx);
  48. /**
  49. * Retrieve the signature from a certificate.
  50. */
  51. static const uint8_t *get_signature(const uint8_t *asn1_sig, int *len)
  52. {
  53. int offset = 0;
  54. const uint8_t *ptr = NULL;
  55. if (asn1_next_obj(asn1_sig, &offset, ASN1_SEQUENCE) < 0 ||
  56. asn1_skip_obj(asn1_sig, &offset, ASN1_SEQUENCE))
  57. goto end_get_sig;
  58. if (asn1_sig[offset++] != ASN1_OCTET_STRING)
  59. goto end_get_sig;
  60. *len = get_asn1_length(asn1_sig, &offset);
  61. ptr = &asn1_sig[offset]; /* all ok */
  62. end_get_sig:
  63. return ptr;
  64. }
  65. #endif
  66. /**
  67. * Construct a new x509 object.
  68. * @return 0 if ok. < 0 if there was a problem.
  69. */
  70. int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx)
  71. {
  72. int begin_tbs, end_tbs;
  73. int ret = X509_NOT_OK, offset = 0, cert_size = 0;
  74. int version = 0;
  75. X509_CTX *x509_ctx;
  76. #ifdef CONFIG_SSL_CERT_VERIFICATION /* only care if doing verification */
  77. BI_CTX *bi_ctx;
  78. #endif
  79. *ctx = (X509_CTX *)calloc(1, sizeof(X509_CTX));
  80. x509_ctx = *ctx;
  81. /* get the certificate size */
  82. asn1_skip_obj(cert, &cert_size, ASN1_SEQUENCE);
  83. if (asn1_next_obj(cert, &offset, ASN1_SEQUENCE) < 0)
  84. goto end_cert;
  85. begin_tbs = offset; /* start of the tbs */
  86. end_tbs = begin_tbs; /* work out the end of the tbs */
  87. asn1_skip_obj(cert, &end_tbs, ASN1_SEQUENCE);
  88. if (asn1_next_obj(cert, &offset, ASN1_SEQUENCE) < 0)
  89. goto end_cert;
  90. /* optional version */
  91. if (cert[offset] == ASN1_EXPLICIT_TAG &&
  92. asn1_version(cert, &offset, &version) == X509_NOT_OK)
  93. goto end_cert;
  94. if (asn1_skip_obj(cert, &offset, ASN1_INTEGER) || /* serial number */
  95. asn1_next_obj(cert, &offset, ASN1_SEQUENCE) < 0)
  96. goto end_cert;
  97. /* make sure the signature is ok */
  98. if (asn1_signature_type(cert, &offset, x509_ctx))
  99. {
  100. ret = X509_VFY_ERROR_UNSUPPORTED_DIGEST;
  101. goto end_cert;
  102. }
  103. if (asn1_name(cert, &offset, x509_ctx->ca_cert_dn) ||
  104. asn1_validity(cert, &offset, x509_ctx) ||
  105. asn1_name(cert, &offset, x509_ctx->cert_dn) ||
  106. asn1_public_key(cert, &offset, x509_ctx))
  107. {
  108. goto end_cert;
  109. }
  110. #ifdef CONFIG_SSL_CERT_VERIFICATION /* only care if doing verification */
  111. bi_ctx = x509_ctx->rsa_ctx->bi_ctx;
  112. /* use the appropriate signature algorithm */
  113. switch (x509_ctx->sig_type)
  114. {
  115. case SIG_TYPE_MD5:
  116. {
  117. MD5_CTX md5_ctx;
  118. uint8_t md5_dgst[MD5_SIZE];
  119. MD5_Init(&md5_ctx);
  120. MD5_Update(&md5_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
  121. MD5_Final(md5_dgst, &md5_ctx);
  122. x509_ctx->digest = bi_import(bi_ctx, md5_dgst, MD5_SIZE);
  123. }
  124. break;
  125. case SIG_TYPE_SHA1:
  126. {
  127. SHA1_CTX sha_ctx;
  128. uint8_t sha_dgst[SHA1_SIZE];
  129. SHA1_Init(&sha_ctx);
  130. SHA1_Update(&sha_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
  131. SHA1_Final(sha_dgst, &sha_ctx);
  132. x509_ctx->digest = bi_import(bi_ctx, sha_dgst, SHA1_SIZE);
  133. }
  134. break;
  135. case SIG_TYPE_SHA256:
  136. {
  137. SHA256_CTX sha256_ctx;
  138. uint8_t sha256_dgst[SHA256_SIZE];
  139. SHA256_Init(&sha256_ctx);
  140. SHA256_Update(&sha256_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
  141. SHA256_Final(sha256_dgst, &sha256_ctx);
  142. x509_ctx->digest = bi_import(bi_ctx, sha256_dgst, SHA256_SIZE);
  143. }
  144. break;
  145. #ifndef WITHOUTH_SHA512
  146. case SIG_TYPE_SHA384:
  147. {
  148. SHA384_CTX sha384_ctx;
  149. uint8_t sha384_dgst[SHA384_SIZE];
  150. SHA384_Init(&sha384_ctx);
  151. SHA384_Update(&sha384_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
  152. SHA384_Final(sha384_dgst, &sha384_ctx);
  153. x509_ctx->digest = bi_import(bi_ctx, sha384_dgst, SHA384_SIZE);
  154. }
  155. break;
  156. case SIG_TYPE_SHA512:
  157. {
  158. SHA512_CTX sha512_ctx;
  159. uint8_t sha512_dgst[SHA512_SIZE];
  160. SHA512_Init(&sha512_ctx);
  161. SHA512_Update(&sha512_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
  162. SHA512_Final(sha512_dgst, &sha512_ctx);
  163. x509_ctx->digest = bi_import(bi_ctx, sha512_dgst, SHA512_SIZE);
  164. }
  165. break;
  166. #endif
  167. }
  168. if (version == 2 && asn1_next_obj(cert, &offset, ASN1_V3_DATA) > 0)
  169. {
  170. x509_v3_subject_alt_name(cert, offset, x509_ctx);
  171. x509_v3_basic_constraints(cert, offset, x509_ctx);
  172. x509_v3_key_usage(cert, offset, x509_ctx);
  173. }
  174. offset = end_tbs; /* skip the rest of v3 data */
  175. if (asn1_skip_obj(cert, &offset, ASN1_SEQUENCE) ||
  176. asn1_signature(cert, &offset, x509_ctx))
  177. goto end_cert;
  178. #endif
  179. ret = X509_OK;
  180. end_cert:
  181. if (len)
  182. {
  183. *len = cert_size;
  184. }
  185. if (ret)
  186. {
  187. #ifdef CONFIG_SSL_FULL_MODE
  188. printf("Error: Invalid X509 ASN.1 file (%s)\n",
  189. x509_display_error(ret));
  190. #endif
  191. x509_free(x509_ctx);
  192. *ctx = NULL;
  193. }
  194. return ret;
  195. }
  196. #ifdef CONFIG_SSL_CERT_VERIFICATION /* only care if doing verification */
  197. static int x509_v3_subject_alt_name(const uint8_t *cert, int offset,
  198. X509_CTX *x509_ctx)
  199. {
  200. if ((offset = asn1_is_subject_alt_name(cert, offset)) > 0)
  201. {
  202. x509_ctx->subject_alt_name_present = true;
  203. x509_ctx->subject_alt_name_is_critical =
  204. asn1_is_critical_ext(cert, &offset);
  205. if (asn1_next_obj(cert, &offset, ASN1_OCTET_STRING) > 0)
  206. {
  207. int altlen;
  208. if ((altlen = asn1_next_obj(cert, &offset, ASN1_SEQUENCE)) > 0)
  209. {
  210. int endalt = offset + altlen;
  211. int totalnames = 0;
  212. while (offset < endalt)
  213. {
  214. int type = cert[offset++];
  215. int dnslen = get_asn1_length(cert, &offset);
  216. if (type == ASN1_CONTEXT_DNSNAME)
  217. {
  218. x509_ctx->subject_alt_dnsnames = (char**)
  219. realloc(x509_ctx->subject_alt_dnsnames,
  220. (totalnames + 2) * sizeof(char*));
  221. x509_ctx->subject_alt_dnsnames[totalnames] =
  222. (char*)malloc(dnslen + 1);
  223. x509_ctx->subject_alt_dnsnames[totalnames+1] = NULL;
  224. memcpy(x509_ctx->subject_alt_dnsnames[totalnames],
  225. cert + offset, dnslen);
  226. x509_ctx->subject_alt_dnsnames[totalnames][dnslen] = 0;
  227. totalnames++;
  228. }
  229. offset += dnslen;
  230. }
  231. }
  232. }
  233. }
  234. return X509_OK;
  235. }
  236. /**
  237. * Basic constraints - see https://tools.ietf.org/html/rfc5280#page-39
  238. */
  239. static int x509_v3_basic_constraints(const uint8_t *cert, int offset,
  240. X509_CTX *x509_ctx)
  241. {
  242. int ret = X509_OK;
  243. int lenSeq, l= 0;
  244. if ((offset = asn1_is_basic_constraints(cert, offset)) == 0)
  245. goto end_contraints;
  246. x509_ctx->basic_constraint_present = true;
  247. x509_ctx->basic_constraint_is_critical =
  248. asn1_is_critical_ext(cert, &offset);
  249. /* Assign Defaults in case not specified
  250. basic_constraint_cA will already by zero by virtue of the calloc */
  251. x509_ctx->basic_constraint_cA = 0;
  252. /* basic_constraint_pathLenConstraint is unlimited by default.
  253. 10000 is just a large number (limits.h is not already included) */
  254. x509_ctx->basic_constraint_pathLenConstraint = 10000;
  255. if ((asn1_next_obj(cert, &offset, ASN1_OCTET_STRING) < 0) ||
  256. ((lenSeq = asn1_next_obj(cert, &offset, ASN1_SEQUENCE)) < 0))
  257. {
  258. ret = X509_NOT_OK;
  259. }
  260. /* If the Sequence Length is greater than zero,
  261. continue with the basic_constraint_cA */
  262. if ((lenSeq>0)&&(asn1_get_bool(cert, &offset,
  263. &x509_ctx->basic_constraint_cA) < 0))
  264. {
  265. ret = X509_NOT_OK;
  266. }
  267. /* If the Sequence Length is greater than 3, it has more content than
  268. the basic_constraint_cA bool, so grab the pathLenConstraint */
  269. if ((lenSeq>3) && (asn1_get_int(cert, &offset,
  270. &x509_ctx->basic_constraint_pathLenConstraint) < 0))
  271. {
  272. ret = X509_NOT_OK;
  273. }
  274. end_contraints:
  275. return ret;
  276. }
  277. /*
  278. * Key usage - see https://tools.ietf.org/html/rfc5280#section-4.2.1.3
  279. */
  280. static int x509_v3_key_usage(const uint8_t *cert, int offset,
  281. X509_CTX *x509_ctx)
  282. {
  283. int ret = X509_OK;
  284. if ((offset = asn1_is_key_usage(cert, offset)) == 0)
  285. goto end_key_usage;
  286. x509_ctx->key_usage_present = true;
  287. x509_ctx->key_usage_is_critical = asn1_is_critical_ext(cert, &offset);
  288. if (asn1_next_obj(cert, &offset, ASN1_OCTET_STRING) < 0 ||
  289. asn1_get_bit_string_as_int(cert, &offset, &x509_ctx->key_usage))
  290. {
  291. ret = X509_NOT_OK;
  292. }
  293. end_key_usage:
  294. return ret;
  295. }
  296. #endif
  297. /**
  298. * Free an X.509 object's resources.
  299. */
  300. void x509_free(X509_CTX *x509_ctx)
  301. {
  302. X509_CTX *next;
  303. int i;
  304. if (x509_ctx == NULL) /* if already null, then don't bother */
  305. return;
  306. for (i = 0; i < X509_NUM_DN_TYPES; i++)
  307. {
  308. free(x509_ctx->ca_cert_dn[i]);
  309. free(x509_ctx->cert_dn[i]);
  310. }
  311. free(x509_ctx->signature);
  312. #ifdef CONFIG_SSL_CERT_VERIFICATION
  313. if (x509_ctx->digest)
  314. {
  315. bi_free(x509_ctx->rsa_ctx->bi_ctx, x509_ctx->digest);
  316. }
  317. if (x509_ctx->subject_alt_dnsnames)
  318. {
  319. for (i = 0; x509_ctx->subject_alt_dnsnames[i]; ++i)
  320. free(x509_ctx->subject_alt_dnsnames[i]);
  321. free(x509_ctx->subject_alt_dnsnames);
  322. }
  323. #endif
  324. RSA_free(x509_ctx->rsa_ctx);
  325. next = x509_ctx->next;
  326. free(x509_ctx);
  327. x509_free(next); /* clear the chain */
  328. }
  329. #ifdef CONFIG_SSL_CERT_VERIFICATION
  330. /**
  331. * Take a signature and decrypt it.
  332. */
  333. static bigint *sig_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
  334. bigint *modulus, bigint *pub_exp)
  335. {
  336. int i, size;
  337. bigint *decrypted_bi, *dat_bi;
  338. bigint *bir = NULL;
  339. uint8_t *block = (uint8_t *)alloca(sig_len);
  340. /* decrypt */
  341. dat_bi = bi_import(ctx, sig, sig_len);
  342. ctx->mod_offset = BIGINT_M_OFFSET;
  343. /* convert to a normal block */
  344. decrypted_bi = bi_mod_power2(ctx, dat_bi, modulus, pub_exp);
  345. bi_export(ctx, decrypted_bi, block, sig_len);
  346. ctx->mod_offset = BIGINT_M_OFFSET;
  347. i = 10; /* start at the first possible non-padded byte */
  348. while (block[i++] && i < sig_len);
  349. size = sig_len - i;
  350. /* get only the bit we want */
  351. if (size > 0)
  352. {
  353. int len;
  354. const uint8_t *sig_ptr = get_signature(&block[i], &len);
  355. if (sig_ptr)
  356. {
  357. bir = bi_import(ctx, sig_ptr, len);
  358. }
  359. }
  360. /* save a few bytes of memory */
  361. bi_clear_cache(ctx);
  362. return bir;
  363. }
  364. /**
  365. * Do some basic checks on the certificate chain.
  366. *
  367. * Certificate verification consists of a number of checks:
  368. * - The date of the certificate is after the start date.
  369. * - The date of the certificate is before the finish date.
  370. * - A root certificate exists in the certificate store.
  371. * - That the certificate(s) are not self-signed.
  372. * - The certificate chain is valid.
  373. * - The signature of the certificate is valid.
  374. * - Basic constraints
  375. */
  376. int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert,
  377. int *pathLenConstraint)
  378. {
  379. int ret = X509_OK, i = 0;
  380. bigint *cert_sig;
  381. X509_CTX *next_cert = NULL;
  382. BI_CTX *ctx = NULL;
  383. bigint *mod = NULL, *expn = NULL;
  384. int match_ca_cert = 0;
  385. struct timeval tv;
  386. uint8_t is_self_signed = 0;
  387. if (cert == NULL)
  388. {
  389. ret = X509_VFY_ERROR_NO_TRUSTED_CERT;
  390. goto end_verify;
  391. }
  392. /* a self-signed certificate that is not in the CA store - use this
  393. to check the signature */
  394. if (asn1_compare_dn(cert->ca_cert_dn, cert->cert_dn) == 0)
  395. {
  396. is_self_signed = 1;
  397. ctx = cert->rsa_ctx->bi_ctx;
  398. mod = cert->rsa_ctx->m;
  399. expn = cert->rsa_ctx->e;
  400. }
  401. gettimeofday(&tv, NULL);
  402. /* check the not before date */
  403. if (tv.tv_sec < cert->not_before)
  404. {
  405. ret = X509_VFY_ERROR_NOT_YET_VALID;
  406. goto end_verify;
  407. }
  408. /* check the not after date */
  409. if (tv.tv_sec > cert->not_after)
  410. {
  411. ret = X509_VFY_ERROR_EXPIRED;
  412. goto end_verify;
  413. }
  414. if (cert->basic_constraint_present)
  415. {
  416. /* If the cA boolean is not asserted,
  417. then the keyCertSign bit in the key usage extension MUST NOT be
  418. asserted. */
  419. if (!cert->basic_constraint_cA &&
  420. IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_CERT_SIGN))
  421. {
  422. ret = X509_VFY_ERROR_BASIC_CONSTRAINT;
  423. goto end_verify;
  424. }
  425. /* The pathLenConstraint field is meaningful only if the cA boolean is
  426. asserted and the key usage extension, if present, asserts the
  427. keyCertSign bit. In this case, it gives the maximum number of
  428. non-self-issued intermediate certificates that may follow this
  429. certificate in a valid certification path. */
  430. if (cert->basic_constraint_cA &&
  431. (!cert->key_usage_present ||
  432. IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_CERT_SIGN)) &&
  433. (cert->basic_constraint_pathLenConstraint+1) < *pathLenConstraint)
  434. {
  435. ret = X509_VFY_ERROR_BASIC_CONSTRAINT;
  436. goto end_verify;
  437. }
  438. }
  439. next_cert = cert->next;
  440. /* last cert in the chain - look for a trusted cert */
  441. if (next_cert == NULL)
  442. {
  443. if (ca_cert_ctx != NULL)
  444. {
  445. /* go through the CA store */
  446. while (i < CONFIG_X509_MAX_CA_CERTS && ca_cert_ctx->cert[i])
  447. {
  448. /* the extension is present but the cA boolean is not
  449. asserted, then the certified public key MUST NOT be used
  450. to verify certificate signatures. */
  451. if (cert->basic_constraint_present &&
  452. !ca_cert_ctx->cert[i]->basic_constraint_cA)
  453. continue;
  454. if (asn1_compare_dn(cert->ca_cert_dn,
  455. ca_cert_ctx->cert[i]->cert_dn) == 0)
  456. {
  457. /* use this CA certificate for signature verification */
  458. match_ca_cert = true;
  459. ctx = ca_cert_ctx->cert[i]->rsa_ctx->bi_ctx;
  460. mod = ca_cert_ctx->cert[i]->rsa_ctx->m;
  461. expn = ca_cert_ctx->cert[i]->rsa_ctx->e;
  462. break;
  463. }
  464. i++;
  465. }
  466. }
  467. /* couldn't find a trusted cert (& let self-signed errors
  468. be returned) */
  469. if (!match_ca_cert && !is_self_signed)
  470. {
  471. ret = X509_VFY_ERROR_NO_TRUSTED_CERT;
  472. goto end_verify;
  473. }
  474. }
  475. else if (asn1_compare_dn(cert->ca_cert_dn, next_cert->cert_dn) != 0)
  476. {
  477. /* check the chain */
  478. ret = X509_VFY_ERROR_INVALID_CHAIN;
  479. goto end_verify;
  480. }
  481. else /* use the next certificate in the chain for signature verify */
  482. {
  483. ctx = next_cert->rsa_ctx->bi_ctx;
  484. mod = next_cert->rsa_ctx->m;
  485. expn = next_cert->rsa_ctx->e;
  486. }
  487. /* cert is self signed */
  488. if (!match_ca_cert && is_self_signed)
  489. {
  490. ret = X509_VFY_ERROR_SELF_SIGNED;
  491. goto end_verify;
  492. }
  493. /* check the signature */
  494. cert_sig = sig_verify(ctx, cert->signature, cert->sig_len,
  495. bi_clone(ctx, mod), bi_clone(ctx, expn));
  496. if (cert_sig && cert->digest)
  497. {
  498. if (bi_compare(cert_sig, cert->digest) != 0)
  499. ret = X509_VFY_ERROR_BAD_SIGNATURE;
  500. bi_free(ctx, cert_sig);
  501. }
  502. else
  503. {
  504. ret = X509_VFY_ERROR_BAD_SIGNATURE;
  505. }
  506. if (ret)
  507. goto end_verify;
  508. /* go down the certificate chain using recursion. */
  509. if (next_cert != NULL)
  510. {
  511. (*pathLenConstraint)++; /* don't include last certificate */
  512. ret = x509_verify(ca_cert_ctx, next_cert, pathLenConstraint);
  513. }
  514. end_verify:
  515. return ret;
  516. }
  517. #endif
  518. #if defined (CONFIG_SSL_FULL_MODE)
  519. /**
  520. * Used for diagnostics.
  521. */
  522. static const char *not_part_of_cert = "<Not Part Of Certificate>";
  523. void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx)
  524. {
  525. if (cert == NULL)
  526. return;
  527. printf("=== CERTIFICATE ISSUED TO ===\n");
  528. printf("Common Name (CN):\t\t");
  529. printf("%s\n", cert->cert_dn[X509_COMMON_NAME] ?
  530. cert->cert_dn[X509_COMMON_NAME] : not_part_of_cert);
  531. printf("Organization (O):\t\t");
  532. printf("%s\n", cert->cert_dn[X509_ORGANIZATION] ?
  533. cert->cert_dn[X509_ORGANIZATION] : not_part_of_cert);
  534. if (cert->cert_dn[X509_ORGANIZATIONAL_UNIT])
  535. {
  536. printf("Organizational Unit (OU):\t");
  537. printf("%s\n", cert->cert_dn[X509_ORGANIZATIONAL_UNIT]);
  538. }
  539. if (cert->cert_dn[X509_LOCATION])
  540. {
  541. printf("Location (L):\t\t\t");
  542. printf("%s\n", cert->cert_dn[X509_LOCATION]);
  543. }
  544. if (cert->cert_dn[X509_COUNTRY])
  545. {
  546. printf("Country (C):\t\t\t");
  547. printf("%s\n", cert->cert_dn[X509_COUNTRY]);
  548. }
  549. if (cert->cert_dn[X509_STATE])
  550. {
  551. printf("State (ST):\t\t\t");
  552. printf("%s\n", cert->cert_dn[X509_STATE]);
  553. }
  554. if (cert->basic_constraint_present)
  555. {
  556. printf("Basic Constraints:\t\t%sCA:%s, pathlen:%d\n",
  557. cert->basic_constraint_is_critical ?
  558. "critical, " : "",
  559. cert->basic_constraint_cA? "TRUE" : "FALSE",
  560. cert->basic_constraint_pathLenConstraint);
  561. }
  562. if (cert->key_usage_present)
  563. {
  564. printf("Key Usage:\t\t\t%s", cert->key_usage_is_critical ?
  565. "critical, " : "");
  566. bool has_started = false;
  567. if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_DIGITAL_SIGNATURE))
  568. {
  569. printf("Digital Signature");
  570. has_started = true;
  571. }
  572. if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_NON_REPUDIATION))
  573. {
  574. if (has_started)
  575. printf(", ");
  576. printf("Non Repudiation");
  577. has_started = true;
  578. }
  579. if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_ENCIPHERMENT))
  580. {
  581. if (has_started)
  582. printf(", ");
  583. printf("Key Encipherment");
  584. has_started = true;
  585. }
  586. if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_DATA_ENCIPHERMENT))
  587. {
  588. if (has_started)
  589. printf(", ");
  590. printf("Data Encipherment");
  591. has_started = true;
  592. }
  593. if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_AGREEMENT))
  594. {
  595. if (has_started)
  596. printf(", ");
  597. printf("Key Agreement");
  598. has_started = true;
  599. }
  600. if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_CERT_SIGN))
  601. {
  602. if (has_started)
  603. printf(", ");
  604. printf("Key Cert Sign");
  605. has_started = true;
  606. }
  607. if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_CRL_SIGN))
  608. {
  609. if (has_started)
  610. printf(", ");
  611. printf("CRL Sign");
  612. has_started = true;
  613. }
  614. if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_ENCIPHER_ONLY))
  615. {
  616. if (has_started)
  617. printf(", ");
  618. printf("Encipher Only");
  619. has_started = true;
  620. }
  621. if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_DECIPHER_ONLY))
  622. {
  623. if (has_started)
  624. printf(", ");
  625. printf("Decipher Only");
  626. has_started = true;
  627. }
  628. printf("\n");
  629. }
  630. if (cert->subject_alt_name_present)
  631. {
  632. printf("Subject Alt Name:\t\t%s", cert->subject_alt_name_is_critical
  633. ? "critical, " : "");
  634. if (cert->subject_alt_dnsnames)
  635. {
  636. int i = 0;
  637. while (cert->subject_alt_dnsnames[i])
  638. printf("%s ", cert->subject_alt_dnsnames[i++]);
  639. }
  640. printf("\n");
  641. }
  642. printf("=== CERTIFICATE ISSUED BY ===\n");
  643. printf("Common Name (CN):\t\t");
  644. printf("%s\n", cert->ca_cert_dn[X509_COMMON_NAME] ?
  645. cert->ca_cert_dn[X509_COMMON_NAME] : not_part_of_cert);
  646. printf("Organization (O):\t\t");
  647. printf("%s\n", cert->ca_cert_dn[X509_ORGANIZATION] ?
  648. cert->ca_cert_dn[X509_ORGANIZATION] : not_part_of_cert);
  649. if (cert->ca_cert_dn[X509_ORGANIZATIONAL_UNIT])
  650. {
  651. printf("Organizational Unit (OU):\t");
  652. printf("%s\n", cert->ca_cert_dn[X509_ORGANIZATIONAL_UNIT]);
  653. }
  654. if (cert->ca_cert_dn[X509_LOCATION])
  655. {
  656. printf("Location (L):\t\t\t");
  657. printf("%s\n", cert->ca_cert_dn[X509_LOCATION]);
  658. }
  659. if (cert->ca_cert_dn[X509_COUNTRY])
  660. {
  661. printf("Country (C):\t\t\t");
  662. printf("%s\n", cert->ca_cert_dn[X509_COUNTRY]);
  663. }
  664. if (cert->ca_cert_dn[X509_STATE])
  665. {
  666. printf("State (ST):\t\t\t");
  667. printf("%s\n", cert->ca_cert_dn[X509_STATE]);
  668. }
  669. printf("Not Before:\t\t\t%s", ctime(&cert->not_before));
  670. printf("Not After:\t\t\t%s", ctime(&cert->not_after));
  671. printf("RSA bitsize:\t\t\t%d\n", cert->rsa_ctx->num_octets*8);
  672. printf("Sig Type:\t\t\t");
  673. switch (cert->sig_type)
  674. {
  675. case SIG_TYPE_MD5:
  676. printf("MD5\n");
  677. break;
  678. case SIG_TYPE_SHA1:
  679. printf("SHA1\n");
  680. break;
  681. case SIG_TYPE_SHA256:
  682. printf("SHA256\n");
  683. break;
  684. case SIG_TYPE_SHA384:
  685. printf("SHA384\n");
  686. break;
  687. case SIG_TYPE_SHA512:
  688. printf("SHA512\n");
  689. break;
  690. default:
  691. printf("Unrecognized: %d\n", cert->sig_type);
  692. break;
  693. }
  694. if (ca_cert_ctx)
  695. {
  696. int pathLenConstraint = 0;
  697. printf("Verify:\t\t\t\t%s\n",
  698. x509_display_error(x509_verify(ca_cert_ctx, cert,
  699. &pathLenConstraint)));
  700. }
  701. #if 0
  702. print_blob("Signature", cert->signature, cert->sig_len);
  703. bi_print("Modulus", cert->rsa_ctx->m);
  704. bi_print("Pub Exp", cert->rsa_ctx->e);
  705. #endif
  706. if (ca_cert_ctx)
  707. {
  708. x509_print(cert->next, ca_cert_ctx);
  709. }
  710. TTY_FLUSH();
  711. }
  712. const char * x509_display_error(int error)
  713. {
  714. switch (error)
  715. {
  716. case X509_OK:
  717. return "Certificate verify successful";
  718. case X509_NOT_OK:
  719. return "X509 not ok";
  720. case X509_VFY_ERROR_NO_TRUSTED_CERT:
  721. return "No trusted cert is available";
  722. case X509_VFY_ERROR_BAD_SIGNATURE:
  723. return "Bad signature";
  724. case X509_VFY_ERROR_NOT_YET_VALID:
  725. return "Cert is not yet valid";
  726. case X509_VFY_ERROR_EXPIRED:
  727. return "Cert has expired";
  728. case X509_VFY_ERROR_SELF_SIGNED:
  729. return "Cert is self-signed";
  730. case X509_VFY_ERROR_INVALID_CHAIN:
  731. return "Chain is invalid (check order of certs)";
  732. case X509_VFY_ERROR_UNSUPPORTED_DIGEST:
  733. return "Unsupported digest";
  734. case X509_INVALID_PRIV_KEY:
  735. return "Invalid private key";
  736. case X509_VFY_ERROR_BASIC_CONSTRAINT:
  737. return "Basic constraint invalid";
  738. default:
  739. return "Unknown";
  740. }
  741. }
  742. #endif /* CONFIG_SSL_FULL_MODE */