2
0

x509_cmp.c 13 KB

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