25-test_x509.t 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (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 qw/:DEFAULT srctop_file/;
  12. setup("test_x509");
  13. plan tests => 16;
  14. # Prevent MSys2 filename munging for arguments that look like file paths but
  15. # aren't
  16. $ENV{MSYS2_ARG_CONV_EXCL} = "/CN=";
  17. require_ok(srctop_file('test','recipes','tconversion.pl'));
  18. my $pem = srctop_file("test/certs", "cyrillic.pem");
  19. my $out = "cyrillic.out";
  20. my $msb = srctop_file("test/certs", "cyrillic.msb");
  21. my $utf = srctop_file("test/certs", "cyrillic.utf8");
  22. ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out,
  23. "-nameopt", "esc_msb"])));
  24. is(cmp_text($out, srctop_file("test/certs", "cyrillic.msb")),
  25. 0, 'Comparing esc_msb output');
  26. ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out,
  27. "-nameopt", "utf8"])));
  28. is(cmp_text($out, srctop_file("test/certs", "cyrillic.utf8")),
  29. 0, 'Comparing utf8 output');
  30. unlink $out;
  31. subtest 'x509 -- x.509 v1 certificate' => sub {
  32. tconversion("x509", srctop_file("test","testx509.pem"));
  33. };
  34. subtest 'x509 -- first x.509 v3 certificate' => sub {
  35. tconversion("x509", srctop_file("test","v3-cert1.pem"));
  36. };
  37. subtest 'x509 -- second x.509 v3 certificate' => sub {
  38. tconversion("x509", srctop_file("test","v3-cert2.pem"));
  39. };
  40. subtest 'x509 -- pathlen' => sub {
  41. ok(run(test(["v3ext", srctop_file("test/certs", "pathlen.pem")])));
  42. };
  43. # extracts issuer from a -text formatted-output
  44. sub get_issuer {
  45. my $f = shift(@_);
  46. my $issuer = "";
  47. open my $fh, $f or die;
  48. while (my $line = <$fh>) {
  49. if ($line =~ /Issuer:/) {
  50. $issuer = $line;
  51. }
  52. }
  53. close $fh;
  54. return $issuer;
  55. }
  56. # Tests for signing certs (broken in 1.1.1o)
  57. my $a_key = "a-key.pem";
  58. my $a_cert = "a-cert.pem";
  59. my $a2_cert = "a2-cert.pem";
  60. my $ca_key = "ca-key.pem";
  61. my $ca_cert = "ca-cert.pem";
  62. my $cnf = srctop_file('apps', 'openssl.cnf');
  63. # Create cert A
  64. ok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:2048",
  65. "-config", $cnf,
  66. "-keyout", $a_key, "-out", $a_cert, "-days", "365",
  67. "-nodes", "-subj", "/CN=test.example.com"])));
  68. # Create cert CA - note key size
  69. ok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:4096",
  70. "-config", $cnf,
  71. "-keyout", $ca_key, "-out", $ca_cert, "-days", "3650",
  72. "-nodes", "-subj", "/CN=ca.example.com"])));
  73. # Sign cert A with CA (errors on 1.1.1o)
  74. ok(run(app(["openssl", "x509", "-in", $a_cert, "-CA", $ca_cert,
  75. "-CAkey", $ca_key, "-set_serial", "1234567890",
  76. "-preserve_dates", "-sha256", "-text", "-out", $a2_cert])));
  77. # verify issuer is CA
  78. ok (get_issuer($a2_cert) =~ /CN = ca.example.com/);
  79. # Tests for issue #16080 (fixed in 1.1.1o)
  80. my $b_key = "b-key.pem";
  81. my $b_csr = "b-cert.csr";
  82. my $b_cert = "b-cert.pem";
  83. # Create the CSR
  84. ok(run(app(["openssl", "req", "-new", "-newkey", "rsa:4096",
  85. "-keyout", $b_key, "-out", $b_csr, "-nodes",
  86. "-config", $cnf,
  87. "-subj", "/CN=b.example.com"])));
  88. # Sign it - position of "-text" matters!
  89. ok(run(app(["openssl", "x509", "-req", "-text", "-CAcreateserial",
  90. "-CA", $ca_cert, "-CAkey", $ca_key,
  91. "-in", $b_csr, "-out", $b_cert])));
  92. # Verify issuer is CA
  93. ok(get_issuer($b_cert) =~ /CN = ca.example.com/);