25-test_req.t 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2024 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. use strict;
  9. use warnings;
  10. use OpenSSL::Test::Utils;
  11. use OpenSSL::Test qw/:DEFAULT srctop_file/;
  12. setup("test_req");
  13. plan tests => 109;
  14. require_ok(srctop_file('test', 'recipes', 'tconversion.pl'));
  15. my @certs = qw(test certs);
  16. # What type of key to generate?
  17. my @req_new;
  18. if (disabled("rsa")) {
  19. @req_new = ("-newkey", "dsa:".srctop_file("apps", "dsa512.pem"));
  20. } else {
  21. @req_new = ("-new");
  22. note("There should be a 2 sequences of .'s and some +'s.");
  23. note("There should not be more that at most 80 per line");
  24. }
  25. # Prevent MSys2 filename munging for arguments that look like file paths but
  26. # aren't
  27. $ENV{MSYS2_ARG_CONV_EXCL} = "/CN=";
  28. # Check for duplicate -addext parameters, and one "working" case.
  29. my @addext_args = ( "openssl", "req", "-new", "-out", "testreq.pem",
  30. "-key", srctop_file(@certs, "ee-key.pem"),
  31. "-config", srctop_file("test", "test.cnf"), @req_new );
  32. my $val = "subjectAltName=DNS:example.com";
  33. my $val1 = "subjectAltName=otherName:1.2.3.4;UTF8:test,email:info\@example.com";
  34. my $val2 = " " . $val;
  35. my $val3 = $val;
  36. $val3 =~ s/=/ =/;
  37. ok( run(app([@addext_args, "-addext", $val])));
  38. ok( run(app([@addext_args, "-addext", $val1])));
  39. $val1 =~ s/UTF8/XXXX/; # execute the error handling in do_othername
  40. ok(!run(app([@addext_args, "-addext", $val1])));
  41. ok(!run(app([@addext_args, "-addext", $val, "-addext", $val])));
  42. ok(!run(app([@addext_args, "-addext", $val, "-addext", $val2])));
  43. ok(!run(app([@addext_args, "-addext", $val, "-addext", $val3])));
  44. ok(!run(app([@addext_args, "-addext", $val2, "-addext", $val3])));
  45. ok(run(app([@addext_args, "-addext", "SXNetID=1:one, 2:two, 3:three"])));
  46. ok(run(app([@addext_args, "-addext", "subjectAltName=dirName:dirname_sec"])));
  47. # If a CSR is provided with neither of -key or -CA/-CAkey, this should fail.
  48. ok(!run(app(["openssl", "req", "-x509",
  49. "-in", srctop_file(@certs, "x509-check.csr"),
  50. "-out", "testreq.pem"])));
  51. subtest "generating alt certificate requests with RSA" => sub {
  52. plan tests => 3;
  53. SKIP: {
  54. skip "RSA is not supported by this OpenSSL build", 2
  55. if disabled("rsa");
  56. ok(run(app(["openssl", "req",
  57. "-config", srctop_file("test", "test.cnf"),
  58. "-section", "altreq",
  59. "-new", "-out", "testreq-rsa.pem", "-utf8",
  60. "-key", srctop_file("test", "testrsa.pem")])),
  61. "Generating request");
  62. ok(run(app(["openssl", "req",
  63. "-config", srctop_file("test", "test.cnf"),
  64. "-verify", "-in", "testreq-rsa.pem", "-noout"])),
  65. "Verifying signature on request");
  66. ok(run(app(["openssl", "req",
  67. "-config", srctop_file("test", "test.cnf"),
  68. "-section", "altreq",
  69. "-verify", "-in", "testreq-rsa.pem", "-noout"])),
  70. "Verifying signature on request");
  71. }
  72. };
  73. subtest "generating certificate requests with RSA" => sub {
  74. plan tests => 8;
  75. SKIP: {
  76. skip "RSA is not supported by this OpenSSL build", 2
  77. if disabled("rsa");
  78. ok(!run(app(["openssl", "req",
  79. "-config", srctop_file("test", "test.cnf"),
  80. "-new", "-out", "testreq-rsa.pem", "-utf8",
  81. "-key", srctop_file("test", "testrsa.pem"),
  82. "-keyform", "DER"])),
  83. "Checking that mismatching keyform fails");
  84. ok(run(app(["openssl", "req",
  85. "-config", srctop_file("test", "test.cnf"),
  86. "-new", "-out", "testreq-rsa.pem", "-utf8",
  87. "-key", srctop_file("test", "testrsa.pem"),
  88. "-keyform", "PEM"])),
  89. "Generating request");
  90. ok(run(app(["openssl", "req",
  91. "-config", srctop_file("test", "test.cnf"),
  92. "-verify", "-in", "testreq-rsa.pem", "-noout"])),
  93. "Verifying signature on request");
  94. ok(run(app(["openssl", "req",
  95. "-config", srctop_file("test", "test.cnf"),
  96. "-modulus", "-in", "testreq-rsa.pem", "-noout"])),
  97. "Printing a modulus of the request key");
  98. ok(run(app(["openssl", "req",
  99. "-config", srctop_file("test", "test.cnf"),
  100. "-new", "-out", "testreq_withattrs_pem.pem", "-utf8",
  101. "-key", srctop_file("test", "testrsa_withattrs.pem")])),
  102. "Generating request from a key with extra attributes - PEM");
  103. ok(run(app(["openssl", "req",
  104. "-config", srctop_file("test", "test.cnf"),
  105. "-verify", "-in", "testreq_withattrs_pem.pem", "-noout"])),
  106. "Verifying signature on request from a key with extra attributes - PEM");
  107. ok(run(app(["openssl", "req",
  108. "-config", srctop_file("test", "test.cnf"),
  109. "-new", "-out", "testreq_withattrs_der.pem", "-utf8",
  110. "-key", srctop_file("test", "testrsa_withattrs.der"),
  111. "-keyform", "DER"])),
  112. "Generating request from a key with extra attributes - PEM");
  113. ok(run(app(["openssl", "req",
  114. "-config", srctop_file("test", "test.cnf"),
  115. "-verify", "-in", "testreq_withattrs_der.pem", "-noout"])),
  116. "Verifying signature on request from a key with extra attributes - PEM");
  117. }
  118. };
  119. subtest "generating certificate requests with RSA-PSS" => sub {
  120. plan tests => 12;
  121. SKIP: {
  122. skip "RSA is not supported by this OpenSSL build", 2
  123. if disabled("rsa");
  124. ok(run(app(["openssl", "req",
  125. "-config", srctop_file("test", "test.cnf"),
  126. "-new", "-out", "testreq-rsapss.pem", "-utf8",
  127. "-key", srctop_file("test", "testrsapss.pem")])),
  128. "Generating request");
  129. ok(run(app(["openssl", "req",
  130. "-config", srctop_file("test", "test.cnf"),
  131. "-verify", "-in", "testreq-rsapss.pem", "-noout"])),
  132. "Verifying signature on request");
  133. ok(run(app(["openssl", "req",
  134. "-config", srctop_file("test", "test.cnf"),
  135. "-new", "-out", "testreq-rsapss2.pem", "-utf8",
  136. "-sigopt", "rsa_padding_mode:pss",
  137. "-sigopt", "rsa_pss_saltlen:-1",
  138. "-key", srctop_file("test", "testrsapss.pem")])),
  139. "Generating request");
  140. ok(run(app(["openssl", "req",
  141. "-config", srctop_file("test", "test.cnf"),
  142. "-verify", "-in", "testreq-rsapss2.pem", "-noout"])),
  143. "Verifying signature on request");
  144. ok(run(app(["openssl", "req",
  145. "-config", srctop_file("test", "test.cnf"),
  146. "-new", "-out", "testreq-rsapssmand.pem", "-utf8",
  147. "-sigopt", "rsa_padding_mode:pss",
  148. "-key", srctop_file("test", "testrsapssmandatory.pem")])),
  149. "Generating request");
  150. ok(run(app(["openssl", "req",
  151. "-config", srctop_file("test", "test.cnf"),
  152. "-verify", "-in", "testreq-rsapssmand.pem", "-noout"])),
  153. "Verifying signature on request");
  154. ok(run(app(["openssl", "req",
  155. "-config", srctop_file("test", "test.cnf"),
  156. "-new", "-out", "testreq-rsapssmand2.pem", "-utf8",
  157. "-sigopt", "rsa_pss_saltlen:100",
  158. "-key", srctop_file("test", "testrsapssmandatory.pem")])),
  159. "Generating request");
  160. ok(run(app(["openssl", "req",
  161. "-config", srctop_file("test", "test.cnf"),
  162. "-verify", "-in", "testreq-rsapssmand2.pem", "-noout"])),
  163. "Verifying signature on request");
  164. ok(!run(app(["openssl", "req",
  165. "-config", srctop_file("test", "test.cnf"),
  166. "-new", "-out", "testreq-rsapss3.pem", "-utf8",
  167. "-sigopt", "rsa_padding_mode:pkcs1",
  168. "-key", srctop_file("test", "testrsapss.pem")])),
  169. "Generating request with expected failure");
  170. ok(!run(app(["openssl", "req",
  171. "-config", srctop_file("test", "test.cnf"),
  172. "-new", "-out", "testreq-rsapss3.pem", "-utf8",
  173. "-sigopt", "rsa_pss_saltlen:-5",
  174. "-key", srctop_file("test", "testrsapss.pem")])),
  175. "Generating request with expected failure");
  176. ok(!run(app(["openssl", "req",
  177. "-config", srctop_file("test", "test.cnf"),
  178. "-new", "-out", "testreq-rsapssmand3.pem", "-utf8",
  179. "-sigopt", "rsa_pss_saltlen:10",
  180. "-key", srctop_file("test", "testrsapssmandatory.pem")])),
  181. "Generating request with expected failure");
  182. ok(!run(app(["openssl", "req",
  183. "-config", srctop_file("test", "test.cnf"),
  184. "-new", "-out", "testreq-rsapssmand3.pem", "-utf8",
  185. "-sha256",
  186. "-key", srctop_file("test", "testrsapssmandatory.pem")])),
  187. "Generating request with expected failure");
  188. }
  189. };
  190. subtest "generating certificate requests with DSA" => sub {
  191. plan tests => 2;
  192. SKIP: {
  193. skip "DSA is not supported by this OpenSSL build", 2
  194. if disabled("dsa");
  195. ok(run(app(["openssl", "req",
  196. "-config", srctop_file("test", "test.cnf"),
  197. "-new", "-out", "testreq-dsa.pem", "-utf8",
  198. "-key", srctop_file("test", "testdsa.pem")])),
  199. "Generating request");
  200. ok(run(app(["openssl", "req",
  201. "-config", srctop_file("test", "test.cnf"),
  202. "-verify", "-in", "testreq-dsa.pem", "-noout"])),
  203. "Verifying signature on request");
  204. }
  205. };
  206. subtest "generating certificate requests with ECDSA" => sub {
  207. plan tests => 2;
  208. SKIP: {
  209. skip "ECDSA is not supported by this OpenSSL build", 2
  210. if disabled("ec");
  211. ok(run(app(["openssl", "req",
  212. "-config", srctop_file("test", "test.cnf"),
  213. "-new", "-out", "testreq-ec.pem", "-utf8",
  214. "-key", srctop_file("test", "testec-p256.pem")])),
  215. "Generating request");
  216. ok(run(app(["openssl", "req",
  217. "-config", srctop_file("test", "test.cnf"),
  218. "-verify", "-in", "testreq-ec.pem", "-noout"])),
  219. "Verifying signature on request");
  220. }
  221. };
  222. subtest "generating certificate requests with Ed25519" => sub {
  223. plan tests => 2;
  224. SKIP: {
  225. skip "Ed25519 is not supported by this OpenSSL build", 2
  226. if disabled("ecx");
  227. ok(run(app(["openssl", "req",
  228. "-config", srctop_file("test", "test.cnf"),
  229. "-new", "-out", "testreq-ed25519.pem", "-utf8",
  230. "-key", srctop_file("test", "tested25519.pem")])),
  231. "Generating request");
  232. ok(run(app(["openssl", "req",
  233. "-config", srctop_file("test", "test.cnf"),
  234. "-verify", "-in", "testreq-ed25519.pem", "-noout"])),
  235. "Verifying signature on request");
  236. }
  237. };
  238. subtest "generating certificate requests with Ed448" => sub {
  239. plan tests => 2;
  240. SKIP: {
  241. skip "Ed448 is not supported by this OpenSSL build", 2
  242. if disabled("ecx");
  243. ok(run(app(["openssl", "req",
  244. "-config", srctop_file("test", "test.cnf"),
  245. "-new", "-out", "testreq-ed448.pem", "-utf8",
  246. "-key", srctop_file("test", "tested448.pem")])),
  247. "Generating request");
  248. ok(run(app(["openssl", "req",
  249. "-config", srctop_file("test", "test.cnf"),
  250. "-verify", "-in", "testreq-ed448.pem", "-noout"])),
  251. "Verifying signature on request");
  252. }
  253. };
  254. subtest "generating certificate requests" => sub {
  255. plan tests => 2;
  256. ok(run(app(["openssl", "req", "-config", srctop_file("test", "test.cnf"),
  257. "-key", srctop_file(@certs, "ee-key.pem"),
  258. @req_new, "-out", "testreq.pem"])),
  259. "Generating request");
  260. ok(run(app(["openssl", "req", "-config", srctop_file("test", "test.cnf"),
  261. "-verify", "-in", "testreq.pem", "-noout"])),
  262. "Verifying signature on request");
  263. };
  264. subtest "generating SM2 certificate requests" => sub {
  265. plan tests => 4;
  266. SKIP: {
  267. skip "SM2 is not supported by this OpenSSL build", 4
  268. if disabled("sm2");
  269. ok(run(app(["openssl", "req",
  270. "-config", srctop_file("test", "test.cnf"),
  271. "-new", "-key", srctop_file(@certs, "sm2.key"),
  272. "-sigopt", "distid:1234567812345678",
  273. "-out", "testreq-sm2.pem", "-sm3"])),
  274. "Generating SM2 certificate request");
  275. ok(run(app(["openssl", "req",
  276. "-config", srctop_file("test", "test.cnf"),
  277. "-verify", "-in", "testreq-sm2.pem", "-noout",
  278. "-vfyopt", "distid:1234567812345678", "-sm3"])),
  279. "Verifying signature on SM2 certificate request");
  280. ok(run(app(["openssl", "req",
  281. "-config", srctop_file("test", "test.cnf"),
  282. "-new", "-key", srctop_file(@certs, "sm2.key"),
  283. "-sigopt", "hexdistid:DEADBEEF",
  284. "-out", "testreq-sm2.pem", "-sm3"])),
  285. "Generating SM2 certificate request with hex id");
  286. ok(run(app(["openssl", "req",
  287. "-config", srctop_file("test", "test.cnf"),
  288. "-verify", "-in", "testreq-sm2.pem", "-noout",
  289. "-vfyopt", "hexdistid:DEADBEEF", "-sm3"])),
  290. "Verifying signature on SM2 certificate request");
  291. }
  292. };
  293. my @openssl_args = ("req", "-config", srctop_file("apps", "openssl.cnf"));
  294. run_conversion('req conversions',
  295. "testreq.pem");
  296. run_conversion('req conversions -- testreq2',
  297. srctop_file("test", "testreq2.pem"));
  298. sub run_conversion {
  299. my $title = shift;
  300. my $reqfile = shift;
  301. subtest $title => sub {
  302. run(app(["openssl", @openssl_args,
  303. "-in", $reqfile, "-inform", "p",
  304. "-noout", "-text"],
  305. stderr => "req-check.err", stdout => undef));
  306. open DATA, "req-check.err";
  307. SKIP: {
  308. plan skip_all => "skipping req conversion test for $reqfile"
  309. if grep /Unknown Public Key/, map { s/\R//; } <DATA>;
  310. tconversion( -type => 'req', -in => $reqfile,
  311. -args => [ @openssl_args ] );
  312. }
  313. close DATA;
  314. unlink "req-check.err";
  315. done_testing();
  316. };
  317. }
  318. # Test both generation and verification of certs w.r.t. RFC 5280 requirements
  319. my $ca_cert; # will be set below
  320. sub generate_cert {
  321. my $cert = shift @_;
  322. my $ss = $cert =~ m/self-signed/;
  323. my $is_ca = $cert =~ m/CA/;
  324. my $cn = $is_ca ? "CA" : "EE";
  325. my $ca_key = srctop_file(@certs, "ca-key.pem");
  326. my $key = $is_ca ? $ca_key : srctop_file(@certs, "ee-key.pem");
  327. my @cmd = ("openssl", "req", "-config", "", "-x509",
  328. "-subj", "/CN=$cn", @_, "-out", $cert);
  329. push(@cmd, ("-key", $key)) if $ss;
  330. push(@cmd, ("-CA", $ca_cert, "-CAkey", $ca_key)) unless $ss;
  331. ok(run(app([@cmd])), "generate $cert");
  332. }
  333. sub has_keyUsage {
  334. my $cert = shift @_;
  335. my $expect = shift @_;
  336. cert_contains($cert, "Key Usage", $expect);
  337. }
  338. sub strict_verify {
  339. my $cert = shift @_;
  340. my $expect = shift @_;
  341. my $trusted = shift @_;
  342. $trusted = $cert unless $trusted;
  343. ok(run(app(["openssl", "verify", "-x509_strict", "-trusted", $trusted,
  344. "-partial_chain", $cert])) == $expect,
  345. "strict verify allow $cert");
  346. }
  347. my @v3_ca = ("-addext", "basicConstraints = critical,CA:true",
  348. "-addext", "keyUsage = keyCertSign");
  349. my $SKID_AKID = "subjectKeyIdentifier,authorityKeyIdentifier";
  350. # # SKID
  351. my $cert = "self-signed_default_SKID_no_explicit_exts.pem";
  352. generate_cert($cert);
  353. has_version($cert, 3);
  354. has_SKID($cert, 1); # SKID added, though no explicit extensions given
  355. has_AKID($cert, 0);
  356. my $cert = "self-signed_v3_CA_hash_SKID.pem";
  357. generate_cert($cert, @v3_ca, "-addext", "subjectKeyIdentifier = hash");
  358. has_SKID($cert, 1); # explicit hash SKID
  359. $cert = "self-signed_v3_CA_no_SKID.pem";
  360. generate_cert($cert, @v3_ca, "-addext", "subjectKeyIdentifier = none");
  361. cert_ext_has_n_different_lines($cert, 0, $SKID_AKID); # no SKID and no AKID
  362. #TODO strict_verify($cert, 0);
  363. $cert = "self-signed_v3_CA_given_SKID.pem";
  364. generate_cert($cert, @v3_ca, "-addext", "subjectKeyIdentifier = 45");
  365. cert_contains($cert, "Subject Key Identifier: 45 ", 1); # given SKID
  366. strict_verify($cert, 1);
  367. # AKID of self-signed certs
  368. $cert = "self-signed_v1_CA_no_KIDs.pem";
  369. generate_cert($cert, "-x509v1");
  370. has_version($cert, 1);
  371. cert_ext_has_n_different_lines($cert, 0, $SKID_AKID); # no SKID and no AKID
  372. #TODO strict_verify($cert, 1); # self-signed v1 root cert should be accepted as CA
  373. $ca_cert = "self-signed_v3_CA_default_SKID.pem"; # will also be used below
  374. generate_cert($ca_cert, @v3_ca);
  375. has_SKID($ca_cert, 1); # default SKID
  376. has_AKID($ca_cert, 0); # no default AKID
  377. strict_verify($ca_cert, 1);
  378. $cert = "self-signed_v3_CA_no_AKID.pem";
  379. generate_cert($cert, @v3_ca, "-addext", "authorityKeyIdentifier = none");
  380. has_AKID($cert, 0); # forced no AKID
  381. $cert = "self-signed_v3_CA_explicit_AKID.pem";
  382. generate_cert($cert, @v3_ca, "-addext", "authorityKeyIdentifier = keyid");
  383. has_AKID($cert, 0); # for self-signed cert, AKID suppressed and not forced
  384. $cert = "self-signed_v3_CA_forced_AKID.pem";
  385. generate_cert($cert, @v3_ca, "-addext", "authorityKeyIdentifier = keyid:always");
  386. cert_ext_has_n_different_lines($cert, 3, $SKID_AKID); # forced AKID, AKID == SKID
  387. strict_verify($cert, 1);
  388. $cert = "self-signed_v3_CA_issuer_AKID.pem";
  389. generate_cert($cert, @v3_ca, "-addext", "authorityKeyIdentifier = issuer");
  390. has_AKID($cert, 0); # suppressed AKID since not forced
  391. $cert = "self-signed_v3_CA_forced_issuer_AKID.pem";
  392. generate_cert($cert, @v3_ca, "-addext", "authorityKeyIdentifier = issuer:always");
  393. cert_contains($cert, "Authority Key Identifier: DirName:/CN=CA serial:", 1); # forced issuer AKID
  394. $cert = "self-signed_v3_CA_nonforced_keyid_issuer_AKID.pem";
  395. generate_cert($cert, @v3_ca, "-addext", "authorityKeyIdentifier = keyid, issuer");
  396. has_AKID($cert, 0); # AKID not present because not forced and cert self-signed
  397. $cert = "self-signed_v3_CA_keyid_forced_issuer_AKID.pem";
  398. generate_cert($cert, @v3_ca, "-addext", "authorityKeyIdentifier = keyid, issuer:always");
  399. cert_contains($cert, "Authority Key Identifier: DirName:/CN=CA serial:", 1); # issuer AKID forced, with keyid not forced
  400. $cert = "self-signed_v3_CA_forced_keyid_issuer_AKID.pem";
  401. generate_cert($cert, @v3_ca, "-addext", "authorityKeyIdentifier = keyid:always, issuer");
  402. has_AKID($cert, 1); # AKID with keyid forced
  403. cert_contains($cert, "Authority Key Identifier: DirName:/CN=CA serial:", 0); # no issuer AKID
  404. $cert = "self-signed_v3_CA_forced_keyid_forced_issuer_AKID.pem";
  405. generate_cert($cert, @v3_ca, "-addext", "authorityKeyIdentifier = keyid:always, issuer:always");
  406. cert_contains($cert, "Authority Key Identifier: keyid(:[0-9A-Fa-f]{2})+ DirName:/CN=CA serial:", 1); # AKID with keyid and issuer forced
  407. $cert = "self-signed_v3_EE_wrong_keyUsage.pem";
  408. generate_cert($cert, "-addext", "keyUsage = keyCertSign");
  409. #TODO strict_verify($cert, 1); # should be accepted because RFC 5280 does not apply
  410. # AKID of self-issued but not self-signed certs
  411. $cert = "self-issued_x509_v3_CA_default_KIDs.pem";
  412. ok(run(app([("openssl", "x509", "-copy_extensions", "copy",
  413. "-req", "-in", srctop_file(@certs, "ext-check.csr"),
  414. "-key", srctop_file(@certs, "ca-key.pem"),
  415. "-force_pubkey", srctop_file("test", "testrsapub.pem"),
  416. "-out", $cert)])), "generate using x509: $cert");
  417. cert_contains($cert, "Issuer: CN=test .*? Subject: CN=test", 1);
  418. cert_ext_has_n_different_lines($cert, 4, $SKID_AKID); # SKID != AKID
  419. strict_verify($cert, 1);
  420. $cert = "self-issued_v3_CA_default_KIDs.pem";
  421. generate_cert($cert, "-addext", "keyUsage = dataEncipherment",
  422. "-in", srctop_file(@certs, "x509-check.csr"));
  423. cert_contains($cert, "Issuer: CN=CA .*? Subject: CN=CA", 1);
  424. cert_ext_has_n_different_lines($cert, 4, $SKID_AKID); # SKID != AKID
  425. strict_verify($cert, 1);
  426. $cert = "self-issued_v3_CA_no_AKID.pem";
  427. generate_cert($cert, "-addext", "authorityKeyIdentifier = none",
  428. "-in", srctop_file(@certs, "x509-check.csr"));
  429. has_version($cert, 3);
  430. has_SKID($cert, 1); # SKID added, though no explicit extensions given
  431. has_AKID($cert, 0);
  432. strict_verify($cert, 1);
  433. $cert = "self-issued_v3_CA_explicit_AKID.pem";
  434. generate_cert($cert, "-addext", "authorityKeyIdentifier = keyid",
  435. "-in", srctop_file(@certs, "x509-check.csr"));
  436. cert_ext_has_n_different_lines($cert, 4, $SKID_AKID); # SKID != AKID
  437. strict_verify($cert, 1);
  438. $cert = "self-issued_v3_CA_forced_AKID.pem";
  439. generate_cert($cert, "-addext", "authorityKeyIdentifier = keyid:always",
  440. "-in", srctop_file(@certs, "x509-check.csr"));
  441. cert_ext_has_n_different_lines($cert, 4, $SKID_AKID); # SKID != AKID
  442. $cert = "self-issued_v3_CA_issuer_AKID.pem";
  443. generate_cert($cert, @v3_ca, "-addext", "authorityKeyIdentifier = issuer",
  444. "-in", srctop_file(@certs, "x509-check.csr"));
  445. cert_contains($cert, "Authority Key Identifier: DirName:/CN=CA serial:", 1); # just issuer AKID
  446. $cert = "self-issued_v3_CA_forced_issuer_AKID.pem";
  447. generate_cert($cert, @v3_ca, "-addext", "authorityKeyIdentifier = issuer:always",
  448. "-in", srctop_file(@certs, "x509-check.csr"));
  449. cert_contains($cert, "Authority Key Identifier: DirName:/CN=CA serial:", 1); # just issuer AKID
  450. $cert = "self-issued_v3_CA_keyid_issuer_AKID.pem";
  451. generate_cert($cert, "-addext", "authorityKeyIdentifier = keyid, issuer",
  452. "-in", srctop_file(@certs, "x509-check.csr"));
  453. cert_ext_has_n_different_lines($cert, 4, $SKID_AKID); # SKID != AKID, not forced
  454. $cert = "self-issued_v3_CA_keyid_forced_issuer_AKID.pem";
  455. generate_cert($cert, "-addext", "authorityKeyIdentifier = keyid, issuer:always",
  456. "-in", srctop_file(@certs, "x509-check.csr"));
  457. cert_ext_has_n_different_lines($cert, 6, $SKID_AKID); # SKID != AKID, with forced issuer
  458. $cert = "self-issued_v3_CA_forced_keyid_and_issuer_AKID.pem";
  459. generate_cert($cert, "-addext", "authorityKeyIdentifier = keyid:always, issuer:always",
  460. "-in", srctop_file(@certs, "x509-check.csr"));
  461. cert_ext_has_n_different_lines($cert, 6, $SKID_AKID); # SKID != AKID, both forced
  462. # AKID of not self-issued certs
  463. $cert = "regular_v3_EE_default_KIDs_no_other_exts.pem";
  464. generate_cert($cert, "-key", srctop_file(@certs, "ee-key.pem"));
  465. has_version($cert, 3);
  466. cert_ext_has_n_different_lines($cert, 4, $SKID_AKID); # SKID != AKID
  467. $cert = "regular_v3_EE_default_KIDs.pem";
  468. generate_cert($cert, "-addext", "keyUsage = dataEncipherment",
  469. "-key", srctop_file(@certs, "ee-key.pem"));
  470. cert_ext_has_n_different_lines($cert, 4, $SKID_AKID); # SKID != AKID
  471. strict_verify($cert, 1, $ca_cert);
  472. $cert = "regular_v3_EE_copied_exts_default_KIDs.pem";
  473. generate_cert($cert, "-copy_extensions", "copy",
  474. "-in", srctop_file(@certs, "ext-check.csr"));
  475. cert_ext_has_n_different_lines($cert, 4, $SKID_AKID); # SKID != AKID
  476. strict_verify($cert, 1);
  477. $cert = "v3_EE_no_AKID.pem";
  478. generate_cert($cert, "-addext", "authorityKeyIdentifier = none",
  479. "-key", srctop_file(@certs, "ee-key.pem"));
  480. has_SKID($cert, 1);
  481. has_AKID($cert, 0);
  482. strict_verify($cert, 0, $ca_cert);
  483. # Key Usage
  484. $cert = "self-signed_CA_no_keyUsage.pem";
  485. generate_cert($cert, "-in", srctop_file(@certs, "ext-check.csr"));
  486. has_keyUsage($cert, 0);
  487. $cert = "self-signed_CA_with_keyUsages.pem";
  488. generate_cert($cert, "-in", srctop_file(@certs, "ext-check.csr"),
  489. "-copy_extensions", "copy");
  490. has_keyUsage($cert, 1);
  491. # Generate cert using req with '-modulus'
  492. ok(run(app(["openssl", "req", "-x509", "-new", "-days", "365",
  493. "-key", srctop_file("test", "testrsa.pem"),
  494. "-config", srctop_file('test', 'test.cnf'),
  495. "-out", "testreq-cert.pem",
  496. "-modulus"])), "cert req creation - with -modulus");
  497. # Verify cert
  498. ok(run(app(["openssl", "x509", "-in", "testreq-cert.pem",
  499. "-noout", "-text"])), "cert verification");
  500. # Generate cert with explicit start and end dates
  501. my %today = (strftime("%Y-%m-%d", gmtime) => 1);
  502. my $cert = "self-signed_explicit_date.pem";
  503. ok(run(app(["openssl", "req", "-x509", "-new", "-text",
  504. "-config", srctop_file('test', 'test.cnf'),
  505. "-key", srctop_file("test", "testrsa.pem"),
  506. "-not_before", "today",
  507. "-not_after", "today",
  508. "-out", $cert]))
  509. && ++$today{strftime("%Y-%m-%d", gmtime)}
  510. && (grep { defined $today{$_} } get_not_before_date($cert))
  511. && (grep { defined $today{$_} } get_not_after_date($cert)), "explicit start and end dates");