25-test_x509.t 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 => 18;
  15. require_ok(srctop_file("test", "recipes", "tconversion.pl"));
  16. my @certs = qw(test certs);
  17. my $pem = srctop_file(@certs, "cyrillic.pem");
  18. my $out_msb = "out-cyrillic.msb";
  19. my $out_utf8 = "out-cyrillic.utf8";
  20. my $der = "cyrillic.der";
  21. my $der2 = "cyrillic.der";
  22. my $msb = srctop_file(@certs, "cyrillic.msb");
  23. my $utf = srctop_file(@certs, "cyrillic.utf8");
  24. ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_msb,
  25. "-nameopt", "esc_msb"])));
  26. is(cmp_text($out_msb, $msb),
  27. 0, 'Comparing esc_msb output with cyrillic.msb');
  28. ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_utf8,
  29. "-nameopt", "utf8"])));
  30. is(cmp_text($out_utf8, $utf),
  31. 0, 'Comparing utf8 output with cyrillic.utf8');
  32. SKIP: {
  33. skip "DES disabled", 1 if disabled("des");
  34. my $p12 = srctop_file("test", "shibboleth.pfx");
  35. my $p12pass = "σύνθημα γνώρισμα";
  36. my $out_pem = "out.pem";
  37. ok(run(app(["openssl", "x509", "-text", "-in", $p12, "-out", $out_pem,
  38. "-passin", "pass:$p12pass"])));
  39. # not unlinking $out_pem
  40. }
  41. ok(!run(app(["openssl", "x509", "-in", $pem, "-inform", "DER",
  42. "-out", $der, "-outform", "DER"])),
  43. "Checking failure of mismatching -inform DER");
  44. ok(run(app(["openssl", "x509", "-in", $pem, "-inform", "PEM",
  45. "-out", $der, "-outform", "DER"])),
  46. "Conversion to DER");
  47. ok(!run(app(["openssl", "x509", "-in", $der, "-inform", "PEM",
  48. "-out", $der2, "-outform", "DER"])),
  49. "Checking failure of mismatching -inform PEM");
  50. # producing and checking self-issued (but not self-signed) cert
  51. my $subj = "/CN=CA"; # using same DN as in issuer of ee-cert.pem
  52. my $extfile = srctop_file("test", "v3_ca_exts.cnf");
  53. my $pkey = srctop_file(@certs, "ca-key.pem"); # issuer private key
  54. my $pubkey = "ca-pubkey.pem"; # the corresponding issuer public key
  55. # use any (different) key for signing our self-issued cert:
  56. my $signkey = srctop_file(@certs, "serverkey.pem");
  57. my $selfout = "self-issued.out";
  58. my $testcert = srctop_file(@certs, "ee-cert.pem");
  59. ok(run(app(["openssl", "pkey", "-in", $pkey, "-pubout", "-out", $pubkey]))
  60. && run(app(["openssl", "x509", "-new", "-force_pubkey", $pubkey,
  61. "-subj", $subj, "-extfile", $extfile,
  62. "-signkey", $signkey, "-out", $selfout]))
  63. && run(app(["openssl", "verify", "-no_check_time",
  64. "-trusted", $selfout, "-partial_chain", $testcert])));
  65. # not unlinking $pubkey
  66. # not unlinking $selfout
  67. subtest 'x509 -- x.509 v1 certificate' => sub {
  68. tconversion( -type => 'x509', -prefix => 'x509v1',
  69. -in => srctop_file("test", "testx509.pem") );
  70. };
  71. subtest 'x509 -- first x.509 v3 certificate' => sub {
  72. tconversion( -type => 'x509', -prefix => 'x509v3-1',
  73. -in => srctop_file("test", "v3-cert1.pem") );
  74. };
  75. subtest 'x509 -- second x.509 v3 certificate' => sub {
  76. tconversion( -type => 'x509', -prefix => 'x509v3-2',
  77. -in => srctop_file("test", "v3-cert2.pem") );
  78. };
  79. subtest 'x509 -- pathlen' => sub {
  80. ok(run(test(["v3ext", srctop_file(@certs, "pathlen.pem")])));
  81. };
  82. cert_contains(srctop_file(@certs, "fake-gp.pem"),
  83. "2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678",
  84. 1, 'x500 -- subjectAltName');
  85. sub test_errors { # actually tests diagnostics of OSSL_STORE
  86. my ($expected, $cert, @opts) = @_;
  87. my $infile = srctop_file(@certs, $cert);
  88. my @args = qw(openssl x509 -in);
  89. push(@args, $infile, @opts);
  90. my $tmpfile = 'out.txt';
  91. my $res = grep(/-text/, @opts) ? run(app([@args], stdout => $tmpfile))
  92. : !run(app([@args], stderr => $tmpfile));
  93. my $found = 0;
  94. open(my $in, '<', $tmpfile) or die "Could not open file $tmpfile";
  95. while(<$in>) {
  96. print; # this may help debugging
  97. $res &&= !m/asn1 encoding/; # output must not include ASN.1 parse errors
  98. $found = 1 if m/$expected/; # output must include $expected
  99. }
  100. close $in;
  101. # $tmpfile is kept to help with investigation in case of failure
  102. return $res && $found;
  103. }
  104. # 3 tests for non-existence of spurious OSSL_STORE ASN.1 parse error output.
  105. # This requires provoking a failure exit of the app after reading input files.
  106. ok(test_errors("Bad output format", "root-cert.pem", '-outform', 'http'),
  107. "load root-cert errors");
  108. ok(test_errors("RC2-40-CBC", "v3-certs-RC2.p12", '-passin', 'pass:v3-certs'),
  109. "load v3-certs-RC2 no asn1 errors"); # error msg should mention "RC2-40-CBC"
  110. SKIP: {
  111. skip "sm2 not disabled", 1 if !disabled("sm2");
  112. ok(test_errors("unknown group|unsupported algorithm", "sm2.pem", '-text'),
  113. "error loading unsupported sm2 cert");
  114. }