x509_cmp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /* crypto/x509/x509_cmp.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <ctype.h>
  60. #include "internal/cryptlib.h"
  61. #include <openssl/asn1.h>
  62. #include <openssl/objects.h>
  63. #include <openssl/x509.h>
  64. #include <openssl/x509v3.h>
  65. #include "internal/x509_int.h"
  66. int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
  67. {
  68. int i;
  69. const X509_CINF *ai, *bi;
  70. ai = &a->cert_info;
  71. bi = &b->cert_info;
  72. i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber);
  73. if (i)
  74. return (i);
  75. return (X509_NAME_cmp(ai->issuer, bi->issuer));
  76. }
  77. #ifndef OPENSSL_NO_MD5
  78. unsigned long X509_issuer_and_serial_hash(X509 *a)
  79. {
  80. unsigned long ret = 0;
  81. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  82. unsigned char md[16];
  83. char *f;
  84. if (ctx == NULL)
  85. goto err;
  86. f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0);
  87. if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL))
  88. goto err;
  89. if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f)))
  90. goto err;
  91. OPENSSL_free(f);
  92. if (!EVP_DigestUpdate
  93. (ctx, (unsigned char *)a->cert_info.serialNumber.data,
  94. (unsigned long)a->cert_info.serialNumber.length))
  95. goto err;
  96. if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL))
  97. goto err;
  98. ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
  99. ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
  100. ) & 0xffffffffL;
  101. err:
  102. EVP_MD_CTX_free(ctx);
  103. return (ret);
  104. }
  105. #endif
  106. int X509_issuer_name_cmp(const X509 *a, const X509 *b)
  107. {
  108. return (X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer));
  109. }
  110. int X509_subject_name_cmp(const X509 *a, const X509 *b)
  111. {
  112. return (X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject));
  113. }
  114. int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
  115. {
  116. return (X509_NAME_cmp(a->crl.issuer, b->crl.issuer));
  117. }
  118. int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
  119. {
  120. return memcmp(a->sha1_hash, b->sha1_hash, 20);
  121. }
  122. X509_NAME *X509_get_issuer_name(X509 *a)
  123. {
  124. return (a->cert_info.issuer);
  125. }
  126. unsigned long X509_issuer_name_hash(X509 *x)
  127. {
  128. return (X509_NAME_hash(x->cert_info.issuer));
  129. }
  130. #ifndef OPENSSL_NO_MD5
  131. unsigned long X509_issuer_name_hash_old(X509 *x)
  132. {
  133. return (X509_NAME_hash_old(x->cert_info.issuer));
  134. }
  135. #endif
  136. X509_NAME *X509_get_subject_name(X509 *a)
  137. {
  138. return (a->cert_info.subject);
  139. }
  140. ASN1_INTEGER *X509_get_serialNumber(X509 *a)
  141. {
  142. return &a->cert_info.serialNumber;
  143. }
  144. unsigned long X509_subject_name_hash(X509 *x)
  145. {
  146. return (X509_NAME_hash(x->cert_info.subject));
  147. }
  148. #ifndef OPENSSL_NO_MD5
  149. unsigned long X509_subject_name_hash_old(X509 *x)
  150. {
  151. return (X509_NAME_hash_old(x->cert_info.subject));
  152. }
  153. #endif
  154. /*
  155. * Compare two certificates: they must be identical for this to work. NB:
  156. * Although "cmp" operations are generally prototyped to take "const"
  157. * arguments (eg. for use in STACKs), the way X509 handling is - these
  158. * operations may involve ensuring the hashes are up-to-date and ensuring
  159. * certain cert information is cached. So this is the point where the
  160. * "depth-first" constification tree has to halt with an evil cast.
  161. */
  162. int X509_cmp(const X509 *a, const X509 *b)
  163. {
  164. int rv;
  165. /* ensure hash is valid */
  166. X509_check_purpose((X509 *)a, -1, 0);
  167. X509_check_purpose((X509 *)b, -1, 0);
  168. rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
  169. if (rv)
  170. return rv;
  171. /* Check for match against stored encoding too */
  172. if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) {
  173. rv = (int)(a->cert_info.enc.len - b->cert_info.enc.len);
  174. if (rv)
  175. return rv;
  176. return memcmp(a->cert_info.enc.enc, b->cert_info.enc.enc,
  177. a->cert_info.enc.len);
  178. }
  179. return rv;
  180. }
  181. int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
  182. {
  183. int ret;
  184. /* Ensure canonical encoding is present and up to date */
  185. if (!a->canon_enc || a->modified) {
  186. ret = i2d_X509_NAME((X509_NAME *)a, NULL);
  187. if (ret < 0)
  188. return -2;
  189. }
  190. if (!b->canon_enc || b->modified) {
  191. ret = i2d_X509_NAME((X509_NAME *)b, NULL);
  192. if (ret < 0)
  193. return -2;
  194. }
  195. ret = a->canon_enclen - b->canon_enclen;
  196. if (ret)
  197. return ret;
  198. return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
  199. }
  200. unsigned long X509_NAME_hash(X509_NAME *x)
  201. {
  202. unsigned long ret = 0;
  203. unsigned char md[SHA_DIGEST_LENGTH];
  204. /* Make sure X509_NAME structure contains valid cached encoding */
  205. i2d_X509_NAME(x, NULL);
  206. if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
  207. NULL))
  208. return 0;
  209. ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
  210. ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
  211. ) & 0xffffffffL;
  212. return (ret);
  213. }
  214. #ifndef OPENSSL_NO_MD5
  215. /*
  216. * I now DER encode the name and hash it. Since I cache the DER encoding,
  217. * this is reasonably efficient.
  218. */
  219. unsigned long X509_NAME_hash_old(X509_NAME *x)
  220. {
  221. EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
  222. unsigned long ret = 0;
  223. unsigned char md[16];
  224. if (md_ctx == NULL)
  225. return ret;
  226. /* Make sure X509_NAME structure contains valid cached encoding */
  227. i2d_X509_NAME(x, NULL);
  228. EVP_MD_CTX_set_flags(md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  229. if (EVP_DigestInit_ex(md_ctx, EVP_md5(), NULL)
  230. && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length)
  231. && EVP_DigestFinal_ex(md_ctx, md, NULL))
  232. ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
  233. ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
  234. ) & 0xffffffffL;
  235. EVP_MD_CTX_free(md_ctx);
  236. return (ret);
  237. }
  238. #endif
  239. /* Search a stack of X509 for a match */
  240. X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name,
  241. ASN1_INTEGER *serial)
  242. {
  243. int i;
  244. X509 x, *x509 = NULL;
  245. if (!sk)
  246. return NULL;
  247. x.cert_info.serialNumber = *serial;
  248. x.cert_info.issuer = name;
  249. for (i = 0; i < sk_X509_num(sk); i++) {
  250. x509 = sk_X509_value(sk, i);
  251. if (X509_issuer_and_serial_cmp(x509, &x) == 0)
  252. return (x509);
  253. }
  254. return (NULL);
  255. }
  256. X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
  257. {
  258. X509 *x509;
  259. int i;
  260. for (i = 0; i < sk_X509_num(sk); i++) {
  261. x509 = sk_X509_value(sk, i);
  262. if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
  263. return (x509);
  264. }
  265. return (NULL);
  266. }
  267. EVP_PKEY *X509_get0_pubkey(X509 *x)
  268. {
  269. if (x == NULL)
  270. return NULL;
  271. return X509_PUBKEY_get0(x->cert_info.key);
  272. }
  273. EVP_PKEY *X509_get_pubkey(X509 *x)
  274. {
  275. if (x == NULL)
  276. return NULL;
  277. return X509_PUBKEY_get(x->cert_info.key);
  278. }
  279. ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
  280. {
  281. if (!x)
  282. return NULL;
  283. return x->cert_info.key->public_key;
  284. }
  285. int X509_check_private_key(X509 *x, EVP_PKEY *k)
  286. {
  287. EVP_PKEY *xk;
  288. int ret;
  289. xk = X509_get0_pubkey(x);
  290. if (xk)
  291. ret = EVP_PKEY_cmp(xk, k);
  292. else
  293. ret = -2;
  294. switch (ret) {
  295. case 1:
  296. break;
  297. case 0:
  298. X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH);
  299. break;
  300. case -1:
  301. X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
  302. break;
  303. case -2:
  304. X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
  305. }
  306. if (ret > 0)
  307. return 1;
  308. return 0;
  309. }
  310. /*
  311. * Check a suite B algorithm is permitted: pass in a public key and the NID
  312. * of its signature (or 0 if no signature). The pflags is a pointer to a
  313. * flags field which must contain the suite B verification flags.
  314. */
  315. #ifndef OPENSSL_NO_EC
  316. static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags)
  317. {
  318. const EC_GROUP *grp = NULL;
  319. int curve_nid;
  320. if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC)
  321. grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
  322. if (!grp)
  323. return X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
  324. curve_nid = EC_GROUP_get_curve_name(grp);
  325. /* Check curve is consistent with LOS */
  326. if (curve_nid == NID_secp384r1) { /* P-384 */
  327. /*
  328. * Check signature algorithm is consistent with curve.
  329. */
  330. if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384)
  331. return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
  332. if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS))
  333. return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
  334. /* If we encounter P-384 we cannot use P-256 later */
  335. *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY;
  336. } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */
  337. if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256)
  338. return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
  339. if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY))
  340. return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
  341. } else
  342. return X509_V_ERR_SUITE_B_INVALID_CURVE;
  343. return X509_V_OK;
  344. }
  345. int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
  346. unsigned long flags)
  347. {
  348. int rv, i, sign_nid;
  349. EVP_PKEY *pk = NULL;
  350. unsigned long tflags;
  351. if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
  352. return X509_V_OK;
  353. tflags = flags;
  354. /* If no EE certificate passed in must be first in chain */
  355. if (x == NULL) {
  356. x = sk_X509_value(chain, 0);
  357. i = 1;
  358. } else
  359. i = 0;
  360. if (X509_get_version(x) != 2) {
  361. rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
  362. /* Correct error depth */
  363. i = 0;
  364. goto end;
  365. }
  366. pk = X509_get0_pubkey(x);
  367. /* Check EE key only */
  368. rv = check_suite_b(pk, -1, &tflags);
  369. if (rv != X509_V_OK) {
  370. /* Correct error depth */
  371. i = 0;
  372. goto end;
  373. }
  374. for (; i < sk_X509_num(chain); i++) {
  375. sign_nid = X509_get_signature_nid(x);
  376. x = sk_X509_value(chain, i);
  377. if (X509_get_version(x) != 2) {
  378. rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
  379. goto end;
  380. }
  381. pk = X509_get0_pubkey(x);
  382. rv = check_suite_b(pk, sign_nid, &tflags);
  383. if (rv != X509_V_OK)
  384. goto end;
  385. }
  386. /* Final check: root CA signature */
  387. rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
  388. end:
  389. if (rv != X509_V_OK) {
  390. /* Invalid signature or LOS errors are for previous cert */
  391. if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM
  392. || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i)
  393. i--;
  394. /*
  395. * If we have LOS error and flags changed then we are signing P-384
  396. * with P-256. Use more meaninggul error.
  397. */
  398. if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags)
  399. rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
  400. if (perror_depth)
  401. *perror_depth = i;
  402. }
  403. return rv;
  404. }
  405. int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
  406. {
  407. int sign_nid;
  408. if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
  409. return X509_V_OK;
  410. sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm);
  411. return check_suite_b(pk, sign_nid, &flags);
  412. }
  413. #else
  414. int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
  415. unsigned long flags)
  416. {
  417. return 0;
  418. }
  419. int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
  420. {
  421. return 0;
  422. }
  423. #endif
  424. /*
  425. * Not strictly speaking an "up_ref" as a STACK doesn't have a reference
  426. * count but it has the same effect by duping the STACK and upping the ref of
  427. * each X509 structure.
  428. */
  429. STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain)
  430. {
  431. STACK_OF(X509) *ret;
  432. int i;
  433. ret = sk_X509_dup(chain);
  434. for (i = 0; i < sk_X509_num(ret); i++) {
  435. X509 *x = sk_X509_value(ret, i);
  436. X509_up_ref(x);
  437. }
  438. return ret;
  439. }