25-test_x509.t 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2021 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 => 29;
  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 $signkey = 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,
  65. "-subj", $subj, "-extfile", $extfile,
  66. "-signkey", $signkey, "-out", $selfout]))
  67. && run(app(["openssl", "verify", "-no_check_time",
  68. "-trusted", $selfout, "-partial_chain", $testcert])));
  69. # not unlinking $pubkey
  70. # not unlinking $selfout
  71. subtest 'x509 -- x.509 v1 certificate' => sub {
  72. tconversion( -type => 'x509', -prefix => 'x509v1',
  73. -in => srctop_file("test", "testx509.pem") );
  74. };
  75. subtest 'x509 -- first x.509 v3 certificate' => sub {
  76. tconversion( -type => 'x509', -prefix => 'x509v3-1',
  77. -in => srctop_file("test", "v3-cert1.pem") );
  78. };
  79. subtest 'x509 -- second x.509 v3 certificate' => sub {
  80. tconversion( -type => 'x509', -prefix => 'x509v3-2',
  81. -in => srctop_file("test", "v3-cert2.pem") );
  82. };
  83. subtest 'x509 -- pathlen' => sub {
  84. ok(run(test(["v3ext", srctop_file(@certs, "pathlen.pem")])));
  85. };
  86. cert_contains(srctop_file(@certs, "fake-gp.pem"),
  87. "2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678",
  88. 1, 'x500 -- subjectAltName');
  89. sub test_errors { # actually tests diagnostics of OSSL_STORE
  90. my ($expected, $cert, @opts) = @_;
  91. my $infile = srctop_file(@certs, $cert);
  92. my @args = qw(openssl x509 -in);
  93. push(@args, $infile, @opts);
  94. my $tmpfile = 'out.txt';
  95. my $res = grep(/-text/, @opts) ? run(app([@args], stdout => $tmpfile))
  96. : !run(app([@args], stderr => $tmpfile));
  97. my $found = 0;
  98. open(my $in, '<', $tmpfile) or die "Could not open file $tmpfile";
  99. while(<$in>) {
  100. print; # this may help debugging
  101. $res &&= !m/asn1 encoding/; # output must not include ASN.1 parse errors
  102. $found = 1 if m/$expected/; # output must include $expected
  103. }
  104. close $in;
  105. # $tmpfile is kept to help with investigation in case of failure
  106. return $res && $found;
  107. }
  108. # 3 tests for non-existence of spurious OSSL_STORE ASN.1 parse error output.
  109. # This requires provoking a failure exit of the app after reading input files.
  110. ok(test_errors("Bad output format", "root-cert.pem", '-outform', 'http'),
  111. "load root-cert errors");
  112. ok(test_errors("RC2-40-CBC", "v3-certs-RC2.p12", '-passin', 'pass:v3-certs'),
  113. "load v3-certs-RC2 no asn1 errors"); # error msg should mention "RC2-40-CBC"
  114. SKIP: {
  115. skip "sm2 not disabled", 1 if !disabled("sm2");
  116. ok(test_errors("Unable to load Public Key", "sm2.pem", '-text'),
  117. "error loading unsupported sm2 cert");
  118. }
  119. # 3 tests for -dateopts formats
  120. ok(run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "rfc_822",
  121. "-in", srctop_file("test/certs", "ca-cert.pem")])),
  122. "Run with rfc_8222 -dateopt format");
  123. ok(run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "iso_8601",
  124. "-in", srctop_file("test/certs", "ca-cert.pem")])),
  125. "Run with iso_8601 -dateopt format");
  126. ok(!run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "invalid_format",
  127. "-in", srctop_file("test/certs", "ca-cert.pem")])),
  128. "Run with invalid -dateopt format");
  129. # extracts issuer from a -text formatted-output
  130. sub get_issuer {
  131. my $f = shift(@_);
  132. my $issuer = "";
  133. open my $fh, $f or die;
  134. while (my $line = <$fh>) {
  135. if ($line =~ /Issuer:/) {
  136. $issuer = $line;
  137. }
  138. }
  139. close $fh;
  140. return $issuer;
  141. }
  142. # Tests for signing certs (broken in 1.1.1o)
  143. my $a_key = "a-key.pem";
  144. my $a_cert = "a-cert.pem";
  145. my $a2_cert = "a2-cert.pem";
  146. my $ca_key = "ca-key.pem";
  147. my $ca_cert = "ca-cert.pem";
  148. my $cnf = srctop_file('apps', 'openssl.cnf');
  149. # Create cert A
  150. ok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:2048",
  151. "-config", $cnf,
  152. "-keyout", $a_key, "-out", $a_cert, "-days", "365",
  153. "-nodes", "-subj", "/CN=test.example.com"])));
  154. # Create cert CA - note key size
  155. ok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:4096",
  156. "-config", $cnf,
  157. "-keyout", $ca_key, "-out", $ca_cert, "-days", "3650",
  158. "-nodes", "-subj", "/CN=ca.example.com"])));
  159. # Sign cert A with CA (errors on 1.1.1o)
  160. ok(run(app(["openssl", "x509", "-in", $a_cert, "-CA", $ca_cert,
  161. "-CAkey", $ca_key, "-set_serial", "1234567890",
  162. "-preserve_dates", "-sha256", "-text", "-out", $a2_cert])));
  163. # verify issuer is CA
  164. ok (get_issuer($a2_cert) =~ /CN=ca.example.com/);
  165. # Tests for issue #16080 (fixed in 1.1.1o)
  166. my $b_key = "b-key.pem";
  167. my $b_csr = "b-cert.csr";
  168. my $b_cert = "b-cert.pem";
  169. # Create the CSR
  170. ok(run(app(["openssl", "req", "-new", "-newkey", "rsa:4096",
  171. "-keyout", $b_key, "-out", $b_csr, "-nodes",
  172. "-config", $cnf,
  173. "-subj", "/CN=b.example.com"])));
  174. # Sign it - position of "-text" matters!
  175. ok(run(app(["openssl", "x509", "-req", "-text", "-CAcreateserial",
  176. "-CA", $ca_cert, "-CAkey", $ca_key,
  177. "-in", $b_csr, "-out", $b_cert])));
  178. # Verify issuer is CA
  179. ok(get_issuer($b_cert) =~ /CN=ca.example.com/);
  180. SKIP: {
  181. skip "EC is not supported by this OpenSSL build", 1
  182. if disabled("ec");
  183. ok(run(test(["x509_test"])), "running x509_test");
  184. }