2
0

x509_cmp.c 12 KB

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