x509_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright 2022-2025 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. #define OPENSSL_SUPPRESS_DEPRECATED /* EVP_PKEY_get1/set1_RSA */
  10. #include <openssl/x509.h>
  11. #include <openssl/asn1.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/rsa.h>
  14. #include <openssl/pem.h>
  15. #include "crypto/x509.h" /* x509_st definition */
  16. #include "testutil.h"
  17. static EVP_PKEY *pubkey = NULL;
  18. static EVP_PKEY *privkey = NULL;
  19. static EVP_MD *signmd = NULL;
  20. /* EC key pair used for signing */
  21. static const unsigned char privkeydata[] = {
  22. 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x7d, 0x2b, 0xfe, 0x5c, 0xcb, 0xcb, 0x27, 0xd6, 0x28,
  23. 0xfe, 0x98, 0x34, 0x84, 0x4a, 0x13, 0x6f, 0x70, 0xc4, 0x1a, 0x0b, 0xfc, 0xde, 0xb0, 0xb2, 0x32,
  24. 0xb1, 0xdd, 0x4f, 0x0e, 0xbc, 0xdf, 0x89, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  25. 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xbf, 0x82, 0xd9, 0xc9, 0x4b, 0x19, 0x43,
  26. 0x45, 0x6b, 0xd4, 0x50, 0x64, 0x9b, 0xd5, 0x8d, 0x5a, 0xd9, 0xdc, 0xc9, 0x24, 0x23, 0x7a, 0x3b,
  27. 0x48, 0x23, 0xe2, 0x2a, 0x24, 0xf2, 0x9c, 0x6f, 0x87, 0xd0, 0xc4, 0x0f, 0xcc, 0x7e, 0x7c, 0x8d,
  28. 0xfc, 0x08, 0x46, 0x37, 0x85, 0x4f, 0x5b, 0x3a, 0x0b, 0x97, 0xd7, 0x57, 0x2a, 0x5a, 0x6b, 0x7a,
  29. 0x0b, 0xe4, 0xe8, 0x9c, 0x4a, 0xbb, 0xbf, 0x09, 0x4d
  30. };
  31. static const unsigned char pubkeydata[] = {
  32. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
  33. 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xbf, 0x82, 0xd9, 0xc9, 0x4b,
  34. 0x19, 0x43, 0x45, 0x6b, 0xd4, 0x50, 0x64, 0x9b, 0xd5, 0x8d, 0x5a, 0xd9, 0xdc, 0xc9, 0x24, 0x23,
  35. 0x7a, 0x3b, 0x48, 0x23, 0xe2, 0x2a, 0x24, 0xf2, 0x9c, 0x6f, 0x87, 0xd0, 0xc4, 0x0f, 0xcc, 0x7e,
  36. 0x7c, 0x8d, 0xfc, 0x08, 0x46, 0x37, 0x85, 0x4f, 0x5b, 0x3a, 0x0b, 0x97, 0xd7, 0x57, 0x2a, 0x5a,
  37. 0x6b, 0x7a, 0x0b, 0xe4, 0xe8, 0x9c, 0x4a, 0xbb, 0xbf, 0x09, 0x4d
  38. };
  39. /* Self signed cert using ECDSA-SHA256 with the keypair listed above */
  40. static const unsigned char certdata[] = {
  41. 0x30, 0x82, 0x01, 0x86, 0x30, 0x82, 0x01, 0x2d, 0x02, 0x14, 0x75, 0xd6, 0x04, 0xd2, 0x80, 0x61,
  42. 0xd3, 0x32, 0xbc, 0xae, 0x38, 0x58, 0xfe, 0x12, 0x42, 0x81, 0x7a, 0xdd, 0x0b, 0x99, 0x30, 0x0a,
  43. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09,
  44. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  45. 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21,
  46. 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
  47. 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74,
  48. 0x64, 0x30, 0x20, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x30, 0x31, 0x32, 0x30, 0x37, 0x32, 0x37, 0x35,
  49. 0x35, 0x5a, 0x18, 0x0f, 0x32, 0x30, 0x35, 0x30, 0x30, 0x32, 0x32, 0x37, 0x30, 0x37, 0x32, 0x37,
  50. 0x35, 0x35, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  51. 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d,
  52. 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a,
  53. 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69,
  54. 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07,
  55. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01,
  56. 0x07, 0x03, 0x42, 0x00, 0x04, 0xbf, 0x82, 0xd9, 0xc9, 0x4b, 0x19, 0x43, 0x45, 0x6b, 0xd4, 0x50,
  57. 0x64, 0x9b, 0xd5, 0x8d, 0x5a, 0xd9, 0xdc, 0xc9, 0x24, 0x23, 0x7a, 0x3b, 0x48, 0x23, 0xe2, 0x2a,
  58. 0x24, 0xf2, 0x9c, 0x6f, 0x87, 0xd0, 0xc4, 0x0f, 0xcc, 0x7e, 0x7c, 0x8d, 0xfc, 0x08, 0x46, 0x37,
  59. 0x85, 0x4f, 0x5b, 0x3a, 0x0b, 0x97, 0xd7, 0x57, 0x2a, 0x5a, 0x6b, 0x7a, 0x0b, 0xe4, 0xe8, 0x9c,
  60. 0x4a, 0xbb, 0xbf, 0x09, 0x4d, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03,
  61. 0x02, 0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20, 0x5f, 0x45, 0x7f, 0xa4, 0x6a, 0x03, 0xfd, 0xe7,
  62. 0xf3, 0x42, 0x43, 0x38, 0x5b, 0x81, 0x08, 0x1a, 0x47, 0x8e, 0x59, 0x3a, 0x28, 0x5b, 0x97, 0x67,
  63. 0x47, 0x66, 0x2a, 0x16, 0xf5, 0xce, 0xf5, 0x92, 0x02, 0x20, 0x22, 0x0e, 0xab, 0x35, 0xdf, 0x49,
  64. 0xb1, 0x86, 0xa3, 0x3b, 0x26, 0xda, 0x7e, 0x8b, 0x44, 0x45, 0xc6, 0x46, 0x14, 0x04, 0x22, 0x2b,
  65. 0xe5, 0x2a, 0x62, 0x84, 0xc5, 0x94, 0xa0, 0x1b, 0xaa, 0xa9
  66. };
  67. /* Some simple CRL data */
  68. static const unsigned char crldata[] = {
  69. 0x30, 0x81, 0x8B, 0x30, 0x31, 0x02, 0x01, 0x01, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE,
  70. 0x3D, 0x04, 0x03, 0x02, 0x05, 0x00, 0x30, 0x0F, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04,
  71. 0x03, 0x0C, 0x04, 0x54, 0x65, 0x73, 0x74, 0x17, 0x0D, 0x32, 0x32, 0x31, 0x30, 0x31, 0x32, 0x30,
  72. 0x35, 0x33, 0x34, 0x30, 0x31, 0x5A, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04,
  73. 0x03, 0x02, 0x05, 0x00, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, 0x20, 0x75, 0xAC, 0xA9, 0xB5, 0xFE,
  74. 0x63, 0x09, 0x8B, 0x57, 0x4F, 0xBB, 0xC6, 0x0C, 0xA9, 0x9A, 0x7C, 0x55, 0x89, 0xF9, 0x9C, 0x48,
  75. 0xE9, 0xF3, 0xED, 0xE5, 0xC2, 0x88, 0xCE, 0xEC, 0xB1, 0x51, 0xF1, 0x02, 0x21, 0x00, 0x8B, 0x93,
  76. 0xC5, 0xA6, 0x28, 0x48, 0x5A, 0x4E, 0x10, 0x52, 0x82, 0x12, 0x2F, 0xC4, 0x62, 0x2D, 0x3F, 0x5A,
  77. 0x62, 0x7F, 0x9D, 0x1B, 0x12, 0xC5, 0x36, 0x25, 0x73, 0x03, 0xF4, 0xDE, 0x62, 0x24
  78. };
  79. /*
  80. * Test for Regression discussed in PR #19388
  81. * In order for this simple test to fail, it requires the digest used for
  82. * signing to be different from the alg within the loaded cert.
  83. */
  84. static int test_x509_tbs_cache(void)
  85. {
  86. int ret;
  87. X509 *x = NULL;
  88. const unsigned char *p = certdata;
  89. ret = TEST_ptr(x = d2i_X509(NULL, &p, sizeof(certdata)))
  90. && TEST_int_gt(X509_sign(x, privkey, signmd), 0)
  91. && TEST_int_eq(X509_verify(x, pubkey), 1);
  92. X509_free(x);
  93. return ret;
  94. }
  95. /*
  96. * Test for Regression discussed in PR #19388
  97. * In order for this simple test to fail, it requires the digest used for
  98. * signing to be different from the alg within the loaded cert.
  99. */
  100. static int test_x509_crl_tbs_cache(void)
  101. {
  102. int ret;
  103. X509_CRL *crl = NULL;
  104. const unsigned char *p = crldata;
  105. ret = TEST_ptr(crl = d2i_X509_CRL(NULL, &p, sizeof(crldata)))
  106. && TEST_int_gt(X509_CRL_sign(crl, privkey, signmd), 0)
  107. && TEST_int_eq(X509_CRL_verify(crl, pubkey), 1);
  108. X509_CRL_free(crl);
  109. return ret;
  110. }
  111. static int test_asn1_item_verify(void)
  112. {
  113. int ret = 0;
  114. BIO *bio = NULL;
  115. X509 *x509 = NULL;
  116. const char *certfile;
  117. const ASN1_BIT_STRING *sig = NULL;
  118. const X509_ALGOR *alg = NULL;
  119. EVP_PKEY *pkey;
  120. #ifndef OPENSSL_NO_DEPRECATED_3_0
  121. RSA *rsa = NULL;
  122. #endif
  123. if (!TEST_ptr(certfile = test_get_argument(0))
  124. || !TEST_ptr(bio = BIO_new_file(certfile, "r"))
  125. || !TEST_ptr(x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))
  126. || !TEST_ptr(pkey = X509_get0_pubkey(x509)))
  127. goto err;
  128. #ifndef OPENSSL_NO_DEPRECATED_3_0
  129. /* Issue #24575 requires legacy key but the test is useful anyway */
  130. if (!TEST_ptr(rsa = EVP_PKEY_get1_RSA(pkey)))
  131. goto err;
  132. if (!TEST_int_gt(EVP_PKEY_set1_RSA(pkey, rsa), 0))
  133. goto err;
  134. #endif
  135. X509_get0_signature(&sig, &alg, x509);
  136. if (!TEST_int_gt(ASN1_item_verify(ASN1_ITEM_rptr(X509_CINF),
  137. (X509_ALGOR *)alg, (ASN1_BIT_STRING *)sig,
  138. &x509->cert_info, pkey), 0))
  139. goto err;
  140. ERR_set_mark();
  141. if (!TEST_int_lt(ASN1_item_verify(ASN1_ITEM_rptr(X509_CINF),
  142. (X509_ALGOR *)alg, (ASN1_BIT_STRING *)sig,
  143. NULL, pkey), 0)) {
  144. ERR_clear_last_mark();
  145. goto err;
  146. }
  147. ERR_pop_to_mark();
  148. ret = 1;
  149. err:
  150. #ifndef OPENSSL_NO_DEPRECATED_3_0
  151. RSA_free(rsa);
  152. #endif
  153. X509_free(x509);
  154. BIO_free(bio);
  155. return ret;
  156. }
  157. static int test_x509_delete_last_extension(void)
  158. {
  159. int ret = 0;
  160. X509 *x509 = NULL;
  161. X509_EXTENSION *ext = NULL;
  162. ASN1_OBJECT *obj = NULL;
  163. if (!TEST_ptr((x509 = X509_new()))
  164. /* Initially, there are no extensions and thus no extension list. */
  165. || !TEST_ptr_null(X509_get0_extensions(x509))
  166. /* Add an extension. */
  167. || !TEST_ptr((ext = X509_EXTENSION_new()))
  168. || !TEST_ptr((obj = OBJ_nid2obj(NID_subject_key_identifier)))
  169. || !TEST_int_eq(X509_EXTENSION_set_object(ext, obj), 1)
  170. || !TEST_int_eq(X509_add_ext(x509, ext, -1), 1)
  171. /* There should now be an extension list. */
  172. || !TEST_ptr(X509_get0_extensions(x509))
  173. || !TEST_int_eq(sk_X509_EXTENSION_num(X509_get0_extensions(x509)), 1))
  174. goto err;
  175. /* Delete the extension. */
  176. X509_EXTENSION_free(X509_delete_ext(x509, 0));
  177. /* The extension list should be NULL again. */
  178. if (!TEST_ptr_null(X509_get0_extensions(x509)))
  179. goto err;
  180. ret = 1;
  181. err:
  182. X509_free(x509);
  183. X509_EXTENSION_free(ext);
  184. return ret;
  185. }
  186. static int test_x509_crl_delete_last_extension(void)
  187. {
  188. int ret = 0;
  189. X509_CRL *crl = NULL;
  190. X509_EXTENSION *ext = NULL;
  191. ASN1_OBJECT *obj = NULL;
  192. if (!TEST_ptr((crl = X509_CRL_new()))
  193. /* Initially, there are no extensions and thus no extension list. */
  194. || !TEST_ptr_null(X509_CRL_get0_extensions(crl))
  195. /* Add an extension. */
  196. || !TEST_ptr((ext = X509_EXTENSION_new()))
  197. || !TEST_ptr((obj = OBJ_nid2obj(NID_subject_key_identifier)))
  198. || !TEST_int_eq(X509_EXTENSION_set_object(ext, obj), 1)
  199. || !TEST_int_eq(X509_CRL_add_ext(crl, ext, -1), 1)
  200. /* There should now be an extension list. */
  201. || !TEST_ptr(X509_CRL_get0_extensions(crl))
  202. || !TEST_int_eq(sk_X509_EXTENSION_num(X509_CRL_get0_extensions(crl)),
  203. 1))
  204. goto err;
  205. /* Delete the extension. */
  206. X509_EXTENSION_free(X509_CRL_delete_ext(crl, 0));
  207. /* The extension list should be NULL again. */
  208. if (!TEST_ptr_null(X509_CRL_get0_extensions(crl)))
  209. goto err;
  210. ret = 1;
  211. err:
  212. X509_CRL_free(crl);
  213. X509_EXTENSION_free(ext);
  214. return ret;
  215. }
  216. static int test_x509_revoked_delete_last_extension(void)
  217. {
  218. int ret = 0;
  219. X509_REVOKED *rev = NULL;
  220. X509_EXTENSION *ext = NULL;
  221. ASN1_OBJECT *obj = NULL;
  222. if (!TEST_ptr((rev = X509_REVOKED_new()))
  223. /* Initially, there are no extensions and thus no extension list. */
  224. || !TEST_ptr_null(X509_REVOKED_get0_extensions(rev))
  225. /* Add an extension. */
  226. || !TEST_ptr((ext = X509_EXTENSION_new()))
  227. || !TEST_ptr((obj = OBJ_nid2obj(NID_subject_key_identifier)))
  228. || !TEST_int_eq(X509_EXTENSION_set_object(ext, obj), 1)
  229. || !TEST_int_eq(X509_REVOKED_add_ext(rev, ext, -1), 1)
  230. /* There should now be an extension list. */
  231. || !TEST_ptr(X509_REVOKED_get0_extensions(rev))
  232. || !TEST_int_eq(sk_X509_EXTENSION_num(X509_REVOKED_get0_extensions(rev)), 1))
  233. goto err;
  234. /* Delete the extension. */
  235. X509_EXTENSION_free(X509_REVOKED_delete_ext(rev, 0));
  236. /* The extension list should be NULL again. */
  237. if (!TEST_ptr_null(X509_REVOKED_get0_extensions(rev)))
  238. goto err;
  239. ret = 1;
  240. err:
  241. X509_REVOKED_free(rev);
  242. X509_EXTENSION_free(ext);
  243. return ret;
  244. }
  245. OPT_TEST_DECLARE_USAGE("<pss-self-signed-cert.pem>\n")
  246. int setup_tests(void)
  247. {
  248. const unsigned char *p;
  249. int cnt;
  250. cnt = test_get_argument_count();
  251. if (cnt != 1) {
  252. TEST_error("Must specify a certificate file self-signed with RSA-PSS.\n");
  253. return 0;
  254. }
  255. p = pubkeydata;
  256. pubkey = d2i_PUBKEY(NULL, &p, sizeof(pubkeydata));
  257. p = privkeydata;
  258. privkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(privkeydata));
  259. if (pubkey == NULL || privkey == NULL) {
  260. BIO_printf(bio_err, "Failed to create keys\n");
  261. return 0;
  262. }
  263. /* Note this digest is different from the certificate digest */
  264. signmd = EVP_MD_fetch(NULL, "SHA384", NULL);
  265. if (signmd == NULL) {
  266. BIO_printf(bio_err, "Failed to fetch digest\n");
  267. return 0;
  268. }
  269. ADD_TEST(test_x509_tbs_cache);
  270. ADD_TEST(test_x509_crl_tbs_cache);
  271. ADD_TEST(test_asn1_item_verify);
  272. ADD_TEST(test_x509_delete_last_extension);
  273. ADD_TEST(test_x509_crl_delete_last_extension);
  274. ADD_TEST(test_x509_revoked_delete_last_extension);
  275. return 1;
  276. }
  277. void cleanup_tests(void)
  278. {
  279. EVP_MD_free(signmd);
  280. EVP_PKEY_free(pubkey);
  281. EVP_PKEY_free(privkey);
  282. }