x509_cmp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Copyright 1995-2016 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 <ctype.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/asn1.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/x509v3.h>
  16. #include "internal/x509_int.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)
  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_CTX *md_ctx = EVP_MD_CTX_new();
  178. unsigned long ret = 0;
  179. unsigned char md[16];
  180. if (md_ctx == NULL)
  181. return ret;
  182. /* Make sure X509_NAME structure contains valid cached encoding */
  183. i2d_X509_NAME(x, NULL);
  184. EVP_MD_CTX_set_flags(md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  185. if (EVP_DigestInit_ex(md_ctx, EVP_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. EVP_MD_CTX_free(md_ctx);
  192. return (ret);
  193. }
  194. #endif
  195. /* Search a stack of X509 for a match */
  196. X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name,
  197. ASN1_INTEGER *serial)
  198. {
  199. int i;
  200. X509 x, *x509 = NULL;
  201. if (!sk)
  202. return NULL;
  203. x.cert_info.serialNumber = *serial;
  204. x.cert_info.issuer = name;
  205. for (i = 0; i < sk_X509_num(sk); i++) {
  206. x509 = sk_X509_value(sk, i);
  207. if (X509_issuer_and_serial_cmp(x509, &x) == 0)
  208. return (x509);
  209. }
  210. return (NULL);
  211. }
  212. X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
  213. {
  214. X509 *x509;
  215. int i;
  216. for (i = 0; i < sk_X509_num(sk); i++) {
  217. x509 = sk_X509_value(sk, i);
  218. if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
  219. return (x509);
  220. }
  221. return (NULL);
  222. }
  223. EVP_PKEY *X509_get0_pubkey(const X509 *x)
  224. {
  225. if (x == NULL)
  226. return NULL;
  227. return X509_PUBKEY_get0(x->cert_info.key);
  228. }
  229. EVP_PKEY *X509_get_pubkey(X509 *x)
  230. {
  231. if (x == NULL)
  232. return NULL;
  233. return X509_PUBKEY_get(x->cert_info.key);
  234. }
  235. int X509_check_private_key(const X509 *x, const EVP_PKEY *k)
  236. {
  237. const EVP_PKEY *xk;
  238. int ret;
  239. xk = X509_get0_pubkey(x);
  240. if (xk)
  241. ret = EVP_PKEY_cmp(xk, k);
  242. else
  243. ret = -2;
  244. switch (ret) {
  245. case 1:
  246. break;
  247. case 0:
  248. X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH);
  249. break;
  250. case -1:
  251. X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
  252. break;
  253. case -2:
  254. X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
  255. }
  256. if (ret > 0)
  257. return 1;
  258. return 0;
  259. }
  260. /*
  261. * Check a suite B algorithm is permitted: pass in a public key and the NID
  262. * of its signature (or 0 if no signature). The pflags is a pointer to a
  263. * flags field which must contain the suite B verification flags.
  264. */
  265. #ifndef OPENSSL_NO_EC
  266. static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags)
  267. {
  268. const EC_GROUP *grp = NULL;
  269. int curve_nid;
  270. if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC)
  271. grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
  272. if (!grp)
  273. return X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
  274. curve_nid = EC_GROUP_get_curve_name(grp);
  275. /* Check curve is consistent with LOS */
  276. if (curve_nid == NID_secp384r1) { /* P-384 */
  277. /*
  278. * Check signature algorithm is consistent with curve.
  279. */
  280. if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384)
  281. return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
  282. if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS))
  283. return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
  284. /* If we encounter P-384 we cannot use P-256 later */
  285. *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY;
  286. } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */
  287. if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256)
  288. return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
  289. if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY))
  290. return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
  291. } else
  292. return X509_V_ERR_SUITE_B_INVALID_CURVE;
  293. return X509_V_OK;
  294. }
  295. int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
  296. unsigned long flags)
  297. {
  298. int rv, i, sign_nid;
  299. EVP_PKEY *pk;
  300. unsigned long tflags = flags;
  301. if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
  302. return X509_V_OK;
  303. /* If no EE certificate passed in must be first in chain */
  304. if (x == NULL) {
  305. x = sk_X509_value(chain, 0);
  306. i = 1;
  307. } else
  308. i = 0;
  309. pk = X509_get0_pubkey(x);
  310. /*
  311. * With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build
  312. * a chain all, just report trust success or failure, but must also report
  313. * Suite-B errors if applicable. This is indicated via a NULL chain
  314. * pointer. All we need to do is check the leaf key algorithm.
  315. */
  316. if (chain == NULL)
  317. return check_suite_b(pk, -1, &tflags);
  318. if (X509_get_version(x) != 2) {
  319. rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
  320. /* Correct error depth */
  321. i = 0;
  322. goto end;
  323. }
  324. /* Check EE key only */
  325. rv = check_suite_b(pk, -1, &tflags);
  326. if (rv != X509_V_OK) {
  327. /* Correct error depth */
  328. i = 0;
  329. goto end;
  330. }
  331. for (; i < sk_X509_num(chain); i++) {
  332. sign_nid = X509_get_signature_nid(x);
  333. x = sk_X509_value(chain, i);
  334. if (X509_get_version(x) != 2) {
  335. rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
  336. goto end;
  337. }
  338. pk = X509_get0_pubkey(x);
  339. rv = check_suite_b(pk, sign_nid, &tflags);
  340. if (rv != X509_V_OK)
  341. goto end;
  342. }
  343. /* Final check: root CA signature */
  344. rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
  345. end:
  346. if (rv != X509_V_OK) {
  347. /* Invalid signature or LOS errors are for previous cert */
  348. if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM
  349. || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i)
  350. i--;
  351. /*
  352. * If we have LOS error and flags changed then we are signing P-384
  353. * with P-256. Use more meaningful error.
  354. */
  355. if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags)
  356. rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
  357. if (perror_depth)
  358. *perror_depth = i;
  359. }
  360. return rv;
  361. }
  362. int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
  363. {
  364. int sign_nid;
  365. if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
  366. return X509_V_OK;
  367. sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm);
  368. return check_suite_b(pk, sign_nid, &flags);
  369. }
  370. #else
  371. int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
  372. unsigned long flags)
  373. {
  374. return 0;
  375. }
  376. int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
  377. {
  378. return 0;
  379. }
  380. #endif
  381. /*
  382. * Not strictly speaking an "up_ref" as a STACK doesn't have a reference
  383. * count but it has the same effect by duping the STACK and upping the ref of
  384. * each X509 structure.
  385. */
  386. STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain)
  387. {
  388. STACK_OF(X509) *ret;
  389. int i;
  390. ret = sk_X509_dup(chain);
  391. for (i = 0; i < sk_X509_num(ret); i++) {
  392. X509 *x = sk_X509_value(ret, i);
  393. X509_up_ref(x);
  394. }
  395. return ret;
  396. }