25-test_x509.t 5.0 KB

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