25-test_x509.t 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 => 11;
  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 "EC disabled", 1 if disabled("ec");
  31. # producing and checking self-issued (but not self-signed) cert
  32. my @path = qw(test certs);
  33. my $subj = "/CN=CA"; # using same DN as in issuer of ee-cert.pem
  34. my $extfile = srctop_file("test", "v3_ca_exts.cnf");
  35. my $pkey = srctop_file(@path, "ca-key.pem"); # issuer private key
  36. my $pubkey = "ca-pubkey.pem"; # the corresponding issuer public key
  37. # use any (different) key for signing our self-issued cert:
  38. my $signkey = srctop_file(@path, "ee-ecdsa-key.pem");
  39. my $selfout = "self-issued.out";
  40. my $testcert = srctop_file(@path, "ee-cert.pem");
  41. ok(run(app(["openssl", "pkey", "-in", $pkey, "-pubout", "-out", $pubkey]))
  42. &&
  43. run(app(["openssl", "x509", "-new", "-force_pubkey", $pubkey,
  44. "-subj", $subj, "-extfile", $extfile,
  45. "-signkey", $signkey, "-out", $selfout]))
  46. &&
  47. run(app(["openssl", "verify", "-no_check_time",
  48. "-trusted", $selfout, "-partial_chain", $testcert])));
  49. unlink $pubkey;
  50. unlink $selfout;
  51. }
  52. subtest 'x509 -- x.509 v1 certificate' => sub {
  53. tconversion("x509", srctop_file("test","testx509.pem"));
  54. };
  55. subtest 'x509 -- first x.509 v3 certificate' => sub {
  56. tconversion("x509", srctop_file("test","v3-cert1.pem"));
  57. };
  58. subtest 'x509 -- second x.509 v3 certificate' => sub {
  59. tconversion("x509", srctop_file("test","v3-cert2.pem"));
  60. };
  61. subtest 'x509 -- pathlen' => sub {
  62. ok(run(test(["v3ext", srctop_file("test/certs", "pathlen.pem")])));
  63. };
  64. subtest 'x500 -- subjectAltName' => sub {
  65. my $fp = srctop_file("test/certs", "fake-gp.pem");
  66. my $out = "ext.out";
  67. ok(run(app(["openssl", "x509", "-text", "-in", $fp, "-out", $out])));
  68. ok(has_doctor_id($out));
  69. unlink $out;
  70. };
  71. sub has_doctor_id {
  72. $_ = shift @_;
  73. open(DATA,$_) or return 0;
  74. $_= join('',<DATA>);
  75. close(DATA);
  76. return m/2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678/;
  77. }