x509_cmp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1.h>
  12. #include <openssl/objects.h>
  13. #include <openssl/x509.h>
  14. #include <openssl/x509v3.h>
  15. #include "crypto/x509.h"
  16. int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
  17. {
  18. int i;
  19. const X509_CINF *ai, *bi;
  20. ai = &a->cert_info;
  21. bi = &b->cert_info;
  22. i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber);
  23. if (i)
  24. return i;
  25. return X509_NAME_cmp(ai->issuer, bi->issuer);
  26. }
  27. #ifndef OPENSSL_NO_MD5
  28. unsigned long X509_issuer_and_serial_hash(X509 *a)
  29. {
  30. unsigned long ret = 0;
  31. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  32. unsigned char md[16];
  33. char *f;
  34. if (ctx == NULL)
  35. goto err;
  36. f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0);
  37. if (f == NULL)
  38. goto err;
  39. if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL))
  40. goto err;
  41. if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f)))
  42. goto err;
  43. OPENSSL_free(f);
  44. if (!EVP_DigestUpdate
  45. (ctx, (unsigned char *)a->cert_info.serialNumber.data,
  46. (unsigned long)a->cert_info.serialNumber.length))
  47. goto err;
  48. if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL))
  49. goto err;
  50. ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
  51. ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
  52. ) & 0xffffffffL;
  53. err:
  54. EVP_MD_CTX_free(ctx);
  55. return ret;
  56. }
  57. #endif
  58. int X509_issuer_name_cmp(const X509 *a, const X509 *b)
  59. {
  60. return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer);
  61. }
  62. int X509_subject_name_cmp(const X509 *a, const X509 *b)
  63. {
  64. return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject);
  65. }
  66. int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
  67. {
  68. return X509_NAME_cmp(a->crl.issuer, b->crl.issuer);
  69. }
  70. int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
  71. {
  72. return memcmp(a->sha1_hash, b->sha1_hash, 20);
  73. }
  74. X509_NAME *X509_get_issuer_name(const X509 *a)
  75. {
  76. return a->cert_info.issuer;
  77. }
  78. unsigned long X509_issuer_name_hash(X509 *x)
  79. {
  80. return X509_NAME_hash(x->cert_info.issuer);
  81. }
  82. #ifndef OPENSSL_NO_MD5
  83. unsigned long X509_issuer_name_hash_old(X509 *x)
  84. {
  85. return X509_NAME_hash_old(x->cert_info.issuer);
  86. }
  87. #endif
  88. X509_NAME *X509_get_subject_name(const X509 *a)
  89. {
  90. return a->cert_info.subject;
  91. }
  92. ASN1_INTEGER *X509_get_serialNumber(X509 *a)
  93. {
  94. return &a->cert_info.serialNumber;
  95. }
  96. const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a)
  97. {
  98. return &a->cert_info.serialNumber;
  99. }
  100. unsigned long X509_subject_name_hash(X509 *x)
  101. {
  102. return X509_NAME_hash(x->cert_info.subject);
  103. }
  104. #ifndef OPENSSL_NO_MD5
  105. unsigned long X509_subject_name_hash_old(X509 *x)
  106. {
  107. return X509_NAME_hash_old(x->cert_info.subject);
  108. }
  109. #endif
  110. /*
  111. * Compare two certificates: they must be identical for this to work. NB:
  112. * Although "cmp" operations are generally prototyped to take "const"
  113. * arguments (eg. for use in STACKs), the way X509 handling is - these
  114. * operations may involve ensuring the hashes are up-to-date and ensuring
  115. * certain cert information is cached. So this is the point where the
  116. * "depth-first" constification tree has to halt with an evil cast.
  117. */
  118. int X509_cmp(const X509 *a, const X509 *b)
  119. {
  120. int rv = 0;
  121. if (a == b) /* for efficiency */
  122. return 0;
  123. /* try to make sure hash is valid */
  124. (void)X509_check_purpose((X509 *)a, -1, 0);
  125. (void)X509_check_purpose((X509 *)b, -1, 0);
  126. if ((a->ex_flags & EXFLAG_NO_FINGERPRINT) == 0
  127. && (b->ex_flags & EXFLAG_NO_FINGERPRINT) == 0)
  128. rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
  129. if (rv != 0)
  130. return rv;
  131. /* Check for match against stored encoding too */
  132. if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) {
  133. if (a->cert_info.enc.len < b->cert_info.enc.len)
  134. return -1;
  135. if (a->cert_info.enc.len > b->cert_info.enc.len)
  136. return 1;
  137. return memcmp(a->cert_info.enc.enc, b->cert_info.enc.enc,
  138. a->cert_info.enc.len);
  139. }
  140. return rv;
  141. }
  142. int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
  143. {
  144. int ret;
  145. /* Ensure canonical encoding is present and up to date */
  146. if (!a->canon_enc || a->modified) {
  147. ret = i2d_X509_NAME((X509_NAME *)a, NULL);
  148. if (ret < 0)
  149. return -2;
  150. }
  151. if (!b->canon_enc || b->modified) {
  152. ret = i2d_X509_NAME((X509_NAME *)b, NULL);
  153. if (ret < 0)
  154. return -2;
  155. }
  156. ret = a->canon_enclen - b->canon_enclen;
  157. if (ret != 0 || a->canon_enclen == 0)
  158. return ret;
  159. return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
  160. }
  161. unsigned long X509_NAME_hash(X509_NAME *x)
  162. {
  163. unsigned long ret = 0;
  164. unsigned char md[SHA_DIGEST_LENGTH];
  165. /* Make sure X509_NAME structure contains valid cached encoding */
  166. i2d_X509_NAME(x, NULL);
  167. if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
  168. NULL))
  169. return 0;
  170. ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
  171. ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
  172. ) & 0xffffffffL;
  173. return ret;
  174. }
  175. #ifndef OPENSSL_NO_MD5
  176. /*
  177. * I now DER encode the name and hash it. Since I cache the DER encoding,
  178. * this is reasonably efficient.
  179. */
  180. unsigned long X509_NAME_hash_old(X509_NAME *x)
  181. {
  182. EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
  183. unsigned long ret = 0;
  184. unsigned char md[16];
  185. if (md_ctx == NULL)
  186. return ret;
  187. /* Make sure X509_NAME structure contains valid cached encoding */
  188. i2d_X509_NAME(x, NULL);
  189. EVP_MD_CTX_set_flags(md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  190. if (EVP_DigestInit_ex(md_ctx, EVP_md5(), NULL)
  191. && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length)
  192. && EVP_DigestFinal_ex(md_ctx, md, NULL))
  193. ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
  194. ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
  195. ) & 0xffffffffL;
  196. EVP_MD_CTX_free(md_ctx);
  197. return ret;
  198. }
  199. #endif
  200. /* Search a stack of X509 for a match */
  201. X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name,
  202. ASN1_INTEGER *serial)
  203. {
  204. int i;
  205. X509 x, *x509 = NULL;
  206. if (!sk)
  207. return NULL;
  208. x.cert_info.serialNumber = *serial;
  209. x.cert_info.issuer = name;
  210. for (i = 0; i < sk_X509_num(sk); i++) {
  211. x509 = sk_X509_value(sk, i);
  212. if (X509_issuer_and_serial_cmp(x509, &x) == 0)
  213. return x509;
  214. }
  215. return NULL;
  216. }
  217. X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
  218. {
  219. X509 *x509;
  220. int i;
  221. for (i = 0; i < sk_X509_num(sk); i++) {
  222. x509 = sk_X509_value(sk, i);
  223. if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
  224. return x509;
  225. }
  226. return NULL;
  227. }
  228. EVP_PKEY *X509_get0_pubkey(const X509 *x)
  229. {
  230. if (x == NULL)
  231. return NULL;
  232. return X509_PUBKEY_get0(x->cert_info.key);
  233. }
  234. EVP_PKEY *X509_get_pubkey(X509 *x)
  235. {
  236. if (x == NULL)
  237. return NULL;
  238. return X509_PUBKEY_get(x->cert_info.key);
  239. }
  240. int X509_check_private_key(const X509 *x, const EVP_PKEY *k)
  241. {
  242. const EVP_PKEY *xk;
  243. int ret;
  244. xk = X509_get0_pubkey(x);
  245. if (xk)
  246. ret = EVP_PKEY_cmp(xk, k);
  247. else
  248. ret = -2;
  249. switch (ret) {
  250. case 1:
  251. break;
  252. case 0:
  253. X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH);
  254. break;
  255. case -1:
  256. X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
  257. break;
  258. case -2:
  259. X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
  260. }
  261. if (ret > 0)
  262. return 1;
  263. return 0;
  264. }
  265. /*
  266. * Check a suite B algorithm is permitted: pass in a public key and the NID
  267. * of its signature (or 0 if no signature). The pflags is a pointer to a
  268. * flags field which must contain the suite B verification flags.
  269. */
  270. #ifndef OPENSSL_NO_EC
  271. static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags)
  272. {
  273. const EC_GROUP *grp = NULL;
  274. int curve_nid;
  275. if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC)
  276. grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
  277. if (!grp)
  278. return X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
  279. curve_nid = EC_GROUP_get_curve_name(grp);
  280. /* Check curve is consistent with LOS */
  281. if (curve_nid == NID_secp384r1) { /* P-384 */
  282. /*
  283. * Check signature algorithm is consistent with curve.
  284. */
  285. if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384)
  286. return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
  287. if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS))
  288. return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
  289. /* If we encounter P-384 we cannot use P-256 later */
  290. *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY;
  291. } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */
  292. if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256)
  293. return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
  294. if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY))
  295. return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
  296. } else
  297. return X509_V_ERR_SUITE_B_INVALID_CURVE;
  298. return X509_V_OK;
  299. }
  300. int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
  301. unsigned long flags)
  302. {
  303. int rv, i, sign_nid;
  304. EVP_PKEY *pk;
  305. unsigned long tflags = flags;
  306. if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
  307. return X509_V_OK;
  308. /* If no EE certificate passed in must be first in chain */
  309. if (x == NULL) {
  310. x = sk_X509_value(chain, 0);
  311. i = 1;
  312. } else
  313. i = 0;
  314. pk = X509_get0_pubkey(x);
  315. /*
  316. * With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build
  317. * a chain all, just report trust success or failure, but must also report
  318. * Suite-B errors if applicable. This is indicated via a NULL chain
  319. * pointer. All we need to do is check the leaf key algorithm.
  320. */
  321. if (chain == NULL)
  322. return check_suite_b(pk, -1, &tflags);
  323. if (X509_get_version(x) != 2) {
  324. rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
  325. /* Correct error depth */
  326. i = 0;
  327. goto end;
  328. }
  329. /* Check EE key only */
  330. rv = check_suite_b(pk, -1, &tflags);
  331. if (rv != X509_V_OK) {
  332. /* Correct error depth */
  333. i = 0;
  334. goto end;
  335. }
  336. for (; i < sk_X509_num(chain); i++) {
  337. sign_nid = X509_get_signature_nid(x);
  338. x = sk_X509_value(chain, i);
  339. if (X509_get_version(x) != 2) {
  340. rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
  341. goto end;
  342. }
  343. pk = X509_get0_pubkey(x);
  344. rv = check_suite_b(pk, sign_nid, &tflags);
  345. if (rv != X509_V_OK)
  346. goto end;
  347. }
  348. /* Final check: root CA signature */
  349. rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
  350. end:
  351. if (rv != X509_V_OK) {
  352. /* Invalid signature or LOS errors are for previous cert */
  353. if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM
  354. || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i)
  355. i--;
  356. /*
  357. * If we have LOS error and flags changed then we are signing P-384
  358. * with P-256. Use more meaningful error.
  359. */
  360. if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags)
  361. rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
  362. if (perror_depth)
  363. *perror_depth = i;
  364. }
  365. return rv;
  366. }
  367. int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
  368. {
  369. int sign_nid;
  370. if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
  371. return X509_V_OK;
  372. sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm);
  373. return check_suite_b(pk, sign_nid, &flags);
  374. }
  375. #else
  376. int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
  377. unsigned long flags)
  378. {
  379. return 0;
  380. }
  381. int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
  382. {
  383. return 0;
  384. }
  385. #endif
  386. /*
  387. * Not strictly speaking an "up_ref" as a STACK doesn't have a reference
  388. * count but it has the same effect by duping the STACK and upping the ref of
  389. * each X509 structure.
  390. */
  391. STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain)
  392. {
  393. STACK_OF(X509) *ret;
  394. int i;
  395. ret = sk_X509_dup(chain);
  396. if (ret == NULL)
  397. return NULL;
  398. for (i = 0; i < sk_X509_num(ret); i++) {
  399. X509 *x = sk_X509_value(ret, i);
  400. if (!X509_up_ref(x))
  401. goto err;
  402. }
  403. return ret;
  404. err:
  405. while (i-- > 0)
  406. X509_free (sk_X509_value(ret, i));
  407. sk_X509_free(ret);
  408. return NULL;
  409. }