25-test_x509.t 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2023 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 => 43;
  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. # simple way of directly producing a CA-signed cert with private/pubkey input
  71. my $ca = srctop_file(@certs, "ca-cert.pem"); # issuer cert
  72. my $caout = "ca-issued.out";
  73. ok(run(app(["openssl", "x509", "-new", "-force_pubkey", $key, "-subj", "/CN=EE",
  74. "-extfile", $extfile, "-CA", $ca, "-CAkey", $pkey, "-out", $caout]))
  75. && run(app(["openssl", "verify", "-no_check_time",
  76. "-trusted", $ca, "-partial_chain", $caout])));
  77. subtest 'x509 -- x.509 v1 certificate' => sub {
  78. tconversion( -type => 'x509', -prefix => 'x509v1',
  79. -in => srctop_file("test", "testx509.pem") );
  80. };
  81. subtest 'x509 -- first x.509 v3 certificate' => sub {
  82. tconversion( -type => 'x509', -prefix => 'x509v3-1',
  83. -in => srctop_file("test", "v3-cert1.pem") );
  84. };
  85. subtest 'x509 -- second x.509 v3 certificate' => sub {
  86. tconversion( -type => 'x509', -prefix => 'x509v3-2',
  87. -in => srctop_file("test", "v3-cert2.pem") );
  88. };
  89. subtest 'x509 -- pathlen' => sub {
  90. ok(run(test(["v3ext", srctop_file(@certs, "pathlen.pem")])));
  91. };
  92. cert_contains(srctop_file(@certs, "fake-gp.pem"),
  93. "2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678",
  94. 1, 'x500 -- subjectAltName');
  95. cert_contains(srctop_file(@certs, "ext-noAssertion.pem"),
  96. "No Assertion",
  97. 1, 'X.509 Not Assertion Extension');
  98. cert_contains(srctop_file(@certs, "ext-groupAC.pem"),
  99. "Group Attribute Certificate",
  100. 1, 'X.509 Group Attribute Certificate Extension');
  101. cert_contains(srctop_file(@certs, "ext-sOAIdentifier.pem"),
  102. "Source of Authority",
  103. 1, 'X.509 Source of Authority Extension');
  104. cert_contains(srctop_file(@certs, "ext-noRevAvail.pem"),
  105. "No Revocation Available",
  106. 1, 'X.509 No Revocation Available');
  107. cert_contains(srctop_file(@certs, "ext-singleUse.pem"),
  108. "Single Use",
  109. 1, 'X509v3 Single Use');
  110. cert_contains(srctop_file(@certs, "ext-indirectIssuer.pem"),
  111. "Indirect Issuer",
  112. 1, 'X.509 Indirect Issuer');
  113. sub test_errors { # actually tests diagnostics of OSSL_STORE
  114. my ($expected, $cert, @opts) = @_;
  115. my $infile = srctop_file(@certs, $cert);
  116. my @args = qw(openssl x509 -in);
  117. push(@args, $infile, @opts);
  118. my $tmpfile = 'out.txt';
  119. my $res = grep(/-text/, @opts) ? run(app([@args], stdout => $tmpfile))
  120. : !run(app([@args], stderr => $tmpfile));
  121. my $found = 0;
  122. open(my $in, '<', $tmpfile) or die "Could not open file $tmpfile";
  123. while(<$in>) {
  124. print; # this may help debugging
  125. $res &&= !m/asn1 encoding/; # output must not include ASN.1 parse errors
  126. $found = 1 if m/$expected/; # output must include $expected
  127. }
  128. close $in;
  129. # $tmpfile is kept to help with investigation in case of failure
  130. return $res && $found;
  131. }
  132. # 3 tests for non-existence of spurious OSSL_STORE ASN.1 parse error output.
  133. # This requires provoking a failure exit of the app after reading input files.
  134. ok(test_errors("Bad output format", "root-cert.pem", '-outform', 'http'),
  135. "load root-cert errors");
  136. ok(test_errors("RC2-40-CBC", "v3-certs-RC2.p12", '-passin', 'pass:v3-certs'),
  137. "load v3-certs-RC2 no asn1 errors"); # error msg should mention "RC2-40-CBC"
  138. SKIP: {
  139. skip "sm2 not disabled", 1 if !disabled("sm2");
  140. ok(test_errors("Unable to load Public Key", "sm2.pem", '-text'),
  141. "error loading unsupported sm2 cert");
  142. }
  143. # 3 tests for -dateopts formats
  144. ok(run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "rfc_822",
  145. "-in", srctop_file("test/certs", "ca-cert.pem")])),
  146. "Run with rfc_8222 -dateopt format");
  147. ok(run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "iso_8601",
  148. "-in", srctop_file("test/certs", "ca-cert.pem")])),
  149. "Run with iso_8601 -dateopt format");
  150. ok(!run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "invalid_format",
  151. "-in", srctop_file("test/certs", "ca-cert.pem")])),
  152. "Run with invalid -dateopt format");
  153. # extracts issuer from a -text formatted-output
  154. sub get_issuer {
  155. my $f = shift(@_);
  156. my $issuer = "";
  157. open my $fh, $f or die;
  158. while (my $line = <$fh>) {
  159. if ($line =~ /Issuer:/) {
  160. $issuer = $line;
  161. }
  162. }
  163. close $fh;
  164. return $issuer;
  165. }
  166. # Tests for signing certs (broken in 1.1.1o)
  167. my $a_key = "a-key.pem";
  168. my $a_cert = "a-cert.pem";
  169. my $a2_cert = "a2-cert.pem";
  170. my $ca_key = "ca-key.pem";
  171. my $ca_cert = "ca-cert.pem";
  172. my $cnf = srctop_file('apps', 'openssl.cnf');
  173. # Create cert A
  174. ok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:2048",
  175. "-config", $cnf,
  176. "-keyout", $a_key, "-out", $a_cert, "-days", "365",
  177. "-nodes", "-subj", "/CN=test.example.com"])));
  178. # Create cert CA - note key size
  179. ok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:4096",
  180. "-config", $cnf,
  181. "-keyout", $ca_key, "-out", $ca_cert, "-days", "3650",
  182. "-nodes", "-subj", "/CN=ca.example.com"])));
  183. # Sign cert A with CA (errors on 1.1.1o)
  184. ok(run(app(["openssl", "x509", "-in", $a_cert, "-CA", $ca_cert,
  185. "-CAkey", $ca_key, "-set_serial", "1234567890",
  186. "-preserve_dates", "-sha256", "-text", "-out", $a2_cert])));
  187. # verify issuer is CA
  188. ok (get_issuer($a2_cert) =~ /CN=ca.example.com/);
  189. # Tests for issue #16080 (fixed in 1.1.1o)
  190. my $b_key = "b-key.pem";
  191. my $b_csr = "b-cert.csr";
  192. my $b_cert = "b-cert.pem";
  193. # Create the CSR
  194. ok(run(app(["openssl", "req", "-new", "-newkey", "rsa:4096",
  195. "-keyout", $b_key, "-out", $b_csr, "-nodes",
  196. "-config", $cnf,
  197. "-subj", "/CN=b.example.com"])));
  198. # Sign it - position of "-text" matters!
  199. ok(run(app(["openssl", "x509", "-req", "-text", "-CAcreateserial",
  200. "-CA", $ca_cert, "-CAkey", $ca_key,
  201. "-in", $b_csr, "-out", $b_cert])));
  202. # Verify issuer is CA
  203. ok(get_issuer($b_cert) =~ /CN=ca.example.com/);
  204. # although no explicit extensions given:
  205. has_version($b_cert, 3);
  206. has_SKID($b_cert, 1);
  207. has_AKID($b_cert, 1);
  208. # Tests for https://github.com/openssl/openssl/issues/10442 (fixed in 1.1.1a)
  209. # (incorrect default `-CAcreateserial` if `-CA` path has a dot in it)
  210. my $folder_with_dot = "test_x509.folder";
  211. ok(mkdir $folder_with_dot);
  212. my $ca_cert_dot_in_dir = File::Spec->catfile($folder_with_dot, "ca-cert.pem");
  213. ok(copy($ca_cert,$ca_cert_dot_in_dir));
  214. my $ca_serial_dot_in_dir = File::Spec->catfile($folder_with_dot, "ca-cert.srl");
  215. ok(run(app(["openssl", "x509", "-req", "-text", "-CAcreateserial",
  216. "-CA", $ca_cert_dot_in_dir, "-CAkey", $ca_key,
  217. "-in", $b_csr])));
  218. ok(-e $ca_serial_dot_in_dir);
  219. SKIP: {
  220. skip "EC is not supported by this OpenSSL build", 1
  221. if disabled("ec");
  222. ok(run(test(["x509_test"])), "running x509_test");
  223. }