25-test_x509.t 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 File::Spec;
  11. use OpenSSL::Test::Utils;
  12. use OpenSSL::Test qw/:DEFAULT srctop_file/;
  13. setup("test_x509");
  14. plan tests => 51;
  15. # Prevent MSys2 filename munging for arguments that look like file paths but
  16. # aren't
  17. $ENV{MSYS2_ARG_CONV_EXCL} = "/CN=";
  18. require_ok(srctop_file("test", "recipes", "tconversion.pl"));
  19. my @certs = qw(test certs);
  20. my $pem = srctop_file(@certs, "cyrillic.pem");
  21. my $out_msb = "out-cyrillic.msb";
  22. my $out_utf8 = "out-cyrillic.utf8";
  23. my $der = "cyrillic.der";
  24. my $der2 = "cyrillic.der";
  25. my $msb = srctop_file(@certs, "cyrillic.msb");
  26. my $utf = srctop_file(@certs, "cyrillic.utf8");
  27. ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_msb,
  28. "-nameopt", "esc_msb"])));
  29. is(cmp_text($out_msb, $msb),
  30. 0, 'Comparing esc_msb output with cyrillic.msb');
  31. ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_utf8,
  32. "-nameopt", "utf8"])));
  33. is(cmp_text($out_utf8, $utf),
  34. 0, 'Comparing utf8 output with cyrillic.utf8');
  35. SKIP: {
  36. skip "DES disabled", 1 if disabled("des");
  37. skip "Platform doesn't support command line UTF-8", 1 if $^O =~ /^(VMS|msys)$/;
  38. my $p12 = srctop_file("test", "shibboleth.pfx");
  39. my $p12pass = "σύνθημα γνώρισμα";
  40. my $out_pem = "out.pem";
  41. ok(run(app(["openssl", "x509", "-text", "-in", $p12, "-out", $out_pem,
  42. "-passin", "pass:$p12pass"])));
  43. # not unlinking $out_pem
  44. }
  45. ok(!run(app(["openssl", "x509", "-in", $pem, "-inform", "DER",
  46. "-out", $der, "-outform", "DER"])),
  47. "Checking failure of mismatching -inform DER");
  48. ok(run(app(["openssl", "x509", "-in", $pem, "-inform", "PEM",
  49. "-out", $der, "-outform", "DER"])),
  50. "Conversion to DER");
  51. ok(!run(app(["openssl", "x509", "-in", $der, "-inform", "PEM",
  52. "-out", $der2, "-outform", "DER"])),
  53. "Checking failure of mismatching -inform PEM");
  54. # producing and checking self-issued (but not self-signed) cert
  55. my $subj = "/CN=CA"; # using same DN as in issuer of ee-cert.pem
  56. my $extfile = srctop_file("test", "v3_ca_exts.cnf");
  57. my $pkey = srctop_file(@certs, "ca-key.pem"); # issuer private key
  58. my $pubkey = "ca-pubkey.pem"; # the corresponding issuer public key
  59. # use any (different) key for signing our self-issued cert:
  60. my $key = srctop_file(@certs, "serverkey.pem");
  61. my $selfout = "self-issued.out";
  62. my $testcert = srctop_file(@certs, "ee-cert.pem");
  63. ok(run(app(["openssl", "pkey", "-in", $pkey, "-pubout", "-out", $pubkey]))
  64. && run(app(["openssl", "x509", "-new", "-force_pubkey", $pubkey, "-subj", $subj,
  65. "-extfile", $extfile, "-key", $key, "-out", $selfout]))
  66. && run(app(["openssl", "verify", "-no_check_time",
  67. "-trusted", $selfout, "-partial_chain", $testcert])));
  68. # not unlinking $pubkey
  69. # not unlinking $selfout
  70. # test -set_issuer option
  71. my $ca_issu = srctop_file(@certs, "ca-cert.pem"); # issuer cert
  72. my $caout_issu = "ca-issu.out";
  73. ok(run(app(["openssl", "x509", "-new", "-force_pubkey", $key, "-subj", "/CN=EE",
  74. "-set_issuer", "/CN=TEST-CA", "-extfile", $extfile, "-CA", $ca_issu,
  75. "-CAkey", $pkey, "-text", "-out", $caout_issu])));
  76. ok(get_issuer($caout_issu) =~ /CN=TEST-CA/);
  77. # not unlinking $caout
  78. # simple way of directly producing a CA-signed cert with private/pubkey input
  79. my $ca = srctop_file(@certs, "ca-cert.pem"); # issuer cert
  80. my $caout = "ca-issued.out";
  81. ok(run(app(["openssl", "x509", "-new", "-force_pubkey", $key, "-subj", "/CN=EE",
  82. "-extfile", $extfile, "-CA", $ca, "-CAkey", $pkey, "-out", $caout]))
  83. && run(app(["openssl", "verify", "-no_check_time",
  84. "-trusted", $ca, "-partial_chain", $caout])));
  85. subtest 'x509 -- x.509 v1 certificate' => sub {
  86. tconversion( -type => 'x509', -prefix => 'x509v1',
  87. -in => srctop_file("test", "testx509.pem") );
  88. };
  89. subtest 'x509 -- first x.509 v3 certificate' => sub {
  90. tconversion( -type => 'x509', -prefix => 'x509v3-1',
  91. -in => srctop_file("test", "v3-cert1.pem") );
  92. };
  93. subtest 'x509 -- second x.509 v3 certificate' => sub {
  94. tconversion( -type => 'x509', -prefix => 'x509v3-2',
  95. -in => srctop_file("test", "v3-cert2.pem") );
  96. };
  97. subtest 'x509 -- pathlen' => sub {
  98. ok(run(test(["v3ext", srctop_file(@certs, "pathlen.pem")])));
  99. };
  100. cert_contains(srctop_file(@certs, "fake-gp.pem"),
  101. "2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678",
  102. 1, 'x500 -- subjectAltName');
  103. cert_contains(srctop_file(@certs, "ext-noAssertion.pem"),
  104. "No Assertion",
  105. 1, 'X.509 Not Assertion Extension');
  106. cert_contains(srctop_file(@certs, "ext-groupAC.pem"),
  107. "Group Attribute Certificate",
  108. 1, 'X.509 Group Attribute Certificate Extension');
  109. cert_contains(srctop_file(@certs, "ext-sOAIdentifier.pem"),
  110. "Source of Authority",
  111. 1, 'X.509 Source of Authority Extension');
  112. cert_contains(srctop_file(@certs, "ext-noRevAvail.pem"),
  113. "No Revocation Available",
  114. 1, 'X.509 No Revocation Available');
  115. cert_contains(srctop_file(@certs, "ext-singleUse.pem"),
  116. "Single Use",
  117. 1, 'X509v3 Single Use');
  118. cert_contains(srctop_file(@certs, "ext-indirectIssuer.pem"),
  119. "Indirect Issuer",
  120. 1, 'X.509 Indirect Issuer');
  121. sub test_errors { # actually tests diagnostics of OSSL_STORE
  122. my ($expected, $cert, @opts) = @_;
  123. my $infile = srctop_file(@certs, $cert);
  124. my @args = qw(openssl x509 -in);
  125. push(@args, $infile, @opts);
  126. my $tmpfile = 'out.txt';
  127. my $res = grep(/-text/, @opts) ? run(app([@args], stdout => $tmpfile))
  128. : !run(app([@args], stderr => $tmpfile));
  129. my $found = 0;
  130. open(my $in, '<', $tmpfile) or die "Could not open file $tmpfile";
  131. while(<$in>) {
  132. print; # this may help debugging
  133. $res &&= !m/asn1 encoding/; # output must not include ASN.1 parse errors
  134. $found = 1 if m/$expected/; # output must include $expected
  135. }
  136. close $in;
  137. # $tmpfile is kept to help with investigation in case of failure
  138. return $res && $found;
  139. }
  140. # 3 tests for non-existence of spurious OSSL_STORE ASN.1 parse error output.
  141. # This requires provoking a failure exit of the app after reading input files.
  142. ok(test_errors("Bad output format", "root-cert.pem", '-outform', 'http'),
  143. "load root-cert errors");
  144. ok(test_errors("RC2-40-CBC", "v3-certs-RC2.p12", '-passin', 'pass:v3-certs'),
  145. "load v3-certs-RC2 no asn1 errors"); # error msg should mention "RC2-40-CBC"
  146. SKIP: {
  147. skip "sm2 not disabled", 1 if !disabled("sm2");
  148. ok(test_errors("Unable to load Public Key", "sm2.pem", '-text'),
  149. "error loading unsupported sm2 cert");
  150. }
  151. # 3 tests for -dateopts formats
  152. ok(run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "rfc_822",
  153. "-in", srctop_file("test/certs", "ca-cert.pem")])),
  154. "Run with rfc_8222 -dateopt format");
  155. ok(run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "iso_8601",
  156. "-in", srctop_file("test/certs", "ca-cert.pem")])),
  157. "Run with iso_8601 -dateopt format");
  158. ok(!run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "invalid_format",
  159. "-in", srctop_file("test/certs", "ca-cert.pem")])),
  160. "Run with invalid -dateopt format");
  161. # Tests for signing certs (broken in 1.1.1o)
  162. my $a_key = "a-key.pem";
  163. my $a_cert = "a-cert.pem";
  164. my $a2_cert = "a2-cert.pem";
  165. my $ca_key = "ca-key.pem";
  166. my $ca_cert = "ca-cert.pem";
  167. my $cnf = srctop_file('apps', 'openssl.cnf');
  168. # Create cert A
  169. ok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:2048",
  170. "-config", $cnf,
  171. "-keyout", $a_key, "-out", $a_cert, "-days", "365",
  172. "-nodes", "-subj", "/CN=test.example.com"])));
  173. # Create cert CA - note key size
  174. ok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:4096",
  175. "-config", $cnf,
  176. "-keyout", $ca_key, "-out", $ca_cert, "-days", "3650",
  177. "-nodes", "-subj", "/CN=ca.example.com"])));
  178. # Sign cert A with CA (errors on 1.1.1o)
  179. ok(run(app(["openssl", "x509", "-in", $a_cert, "-CA", $ca_cert,
  180. "-CAkey", $ca_key, "-set_serial", "1234567890",
  181. "-preserve_dates", "-sha256", "-text", "-out", $a2_cert])));
  182. # verify issuer is CA
  183. ok(get_issuer($a2_cert) =~ /CN=ca.example.com/);
  184. my $in_csr = srctop_file('test', 'certs', 'x509-check.csr');
  185. my $in_key = srctop_file('test', 'certs', 'x509-check-key.pem');
  186. my $invextfile = srctop_file('test', 'invalid-x509.cnf');
  187. # Test that invalid extensions settings fail
  188. ok(!run(app(["openssl", "x509", "-req", "-in", $in_csr, "-signkey", $in_key,
  189. "-out", "/dev/null", "-days", "3650" , "-extensions", "ext",
  190. "-extfile", $invextfile])));
  191. # Tests for issue #16080 (fixed in 1.1.1o)
  192. my $b_key = "b-key.pem";
  193. my $b_csr = "b-cert.csr";
  194. my $b_cert = "b-cert.pem";
  195. # Create the CSR
  196. ok(run(app(["openssl", "req", "-new", "-newkey", "rsa:4096",
  197. "-keyout", $b_key, "-out", $b_csr, "-nodes",
  198. "-config", $cnf,
  199. "-subj", "/CN=b.example.com"])));
  200. # Sign it - position of "-text" matters!
  201. ok(run(app(["openssl", "x509", "-req", "-text", "-CAcreateserial",
  202. "-CA", $ca_cert, "-CAkey", $ca_key,
  203. "-in", $b_csr, "-out", $b_cert])));
  204. # Verify issuer is CA
  205. ok(get_issuer($b_cert) =~ /CN=ca.example.com/);
  206. # although no explicit extensions given:
  207. has_version($b_cert, 3);
  208. has_SKID($b_cert, 1);
  209. has_AKID($b_cert, 1);
  210. # Tests for https://github.com/openssl/openssl/issues/10442 (fixed in 1.1.1a)
  211. # (incorrect default `-CAcreateserial` if `-CA` path has a dot in it)
  212. my $folder_with_dot = "test_x509.folder";
  213. ok(mkdir $folder_with_dot);
  214. my $ca_cert_dot_in_dir = File::Spec->catfile($folder_with_dot, "ca-cert.pem");
  215. ok(copy($ca_cert,$ca_cert_dot_in_dir));
  216. my $ca_serial_dot_in_dir = File::Spec->catfile($folder_with_dot, "ca-cert.srl");
  217. ok(run(app(["openssl", "x509", "-req", "-text", "-CAcreateserial",
  218. "-CA", $ca_cert_dot_in_dir, "-CAkey", $ca_key,
  219. "-in", $b_csr])));
  220. ok(-e $ca_serial_dot_in_dir);
  221. # Tests for explict start and end dates of certificates
  222. my %today = (strftime("%Y-%m-%d", gmtime) => 1);
  223. my $enddate;
  224. ok(run(app(["openssl", "x509", "-req", "-text",
  225. "-key", $b_key,
  226. "-not_before", "20231031000000Z",
  227. "-not_after", "today",
  228. "-in", $b_csr, "-out", $b_cert]))
  229. && get_not_before($b_cert) =~ /Oct 31 00:00:00 2023 GMT/
  230. && ++$today{strftime("%Y-%m-%d", gmtime)}
  231. && (grep { defined $today{$_} } get_not_after_date($b_cert)));
  232. # explicit start and end dates
  233. ok(run(app(["openssl", "x509", "-req", "-text",
  234. "-key", $b_key,
  235. "-not_before", "20231031000000Z",
  236. "-not_after", "20231231000000Z",
  237. "-days", "99",
  238. "-in", $b_csr, "-out", $b_cert]))
  239. && get_not_before($b_cert) =~ /Oct 31 00:00:00 2023 GMT/
  240. && get_not_after($b_cert) =~ /Dec 31 00:00:00 2023 GMT/);
  241. # start date today and days
  242. %today = (strftime("%Y-%m-%d", gmtime) => 1);
  243. $enddate = strftime("%Y-%m-%d", gmtime(time + 99 * 24 * 60 * 60));
  244. ok(run(app(["openssl", "x509", "-req", "-text",
  245. "-key", $b_key,
  246. "-not_before", "today",
  247. "-days", "99",
  248. "-in", $b_csr, "-out", $b_cert]))
  249. && ++$today{strftime("%Y-%m-%d", gmtime)}
  250. && (grep { defined $today{$_} } get_not_before_date($b_cert))
  251. && get_not_after_date($b_cert) eq $enddate);
  252. # end date before start date
  253. ok(!run(app(["openssl", "x509", "-req", "-text",
  254. "-key", $b_key,
  255. "-not_before", "today",
  256. "-not_after", "20231031000000Z",
  257. "-in", $b_csr, "-out", $b_cert])));
  258. # default days option
  259. %today = (strftime("%Y-%m-%d", gmtime) => 1);
  260. $enddate = strftime("%Y-%m-%d", gmtime(time + 30 * 24 * 60 * 60));
  261. ok(run(app(["openssl", "x509", "-req", "-text",
  262. "-key", $b_key,
  263. "-in", $b_csr, "-out", $b_cert]))
  264. && ++$today{strftime("%Y-%m-%d", gmtime)}
  265. && (grep { defined $today{$_} } get_not_before_date($b_cert))
  266. && get_not_after_date($b_cert) eq $enddate);
  267. SKIP: {
  268. skip "EC is not supported by this OpenSSL build", 1
  269. if disabled("ec");
  270. ok(run(test(["x509_test"])), "running x509_test");
  271. }