04-test_pem_reading.t 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #! /usr/bin/env perl
  2. # Copyright 2017-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. #
  9. # ======================================================================
  10. use strict;
  11. use warnings;
  12. use File::Compare qw/compare_text/;
  13. use File::Basename;
  14. use OpenSSL::Test qw/:DEFAULT srctop_file data_file/;
  15. use OpenSSL::Test::Utils;
  16. setup("test_pem_reading");
  17. my $testsrc = srctop_file("test", "recipes", basename($0));
  18. my $cmd = "openssl";
  19. # map input PEM file to 1 if it should be accepted; 0 when should be rejected
  20. my %cert_expected = (
  21. "cert-1023line.pem" => 1,
  22. "cert-1024line.pem" => 1,
  23. "cert-1025line.pem" => 1,
  24. "cert-254-chars-at-the-end.pem" => 1,
  25. "cert-254-chars-in-the-middle.pem" => 1,
  26. "cert-255line.pem" => 1,
  27. "cert-256line.pem" => 1,
  28. "cert-257line.pem" => 1,
  29. "cert-blankline.pem" => 0,
  30. "cert-bom.pem" => 1,
  31. "cert-comment.pem" => 0,
  32. "cert-earlypad.pem" => 0,
  33. "cert-extrapad.pem" => 0,
  34. "cert-infixwhitespace.pem" => 1,
  35. "cert-junk.pem" => 0,
  36. "cert-leadingwhitespace.pem" => 1,
  37. "cert-longline.pem" => 1,
  38. "cert-misalignedpad.pem" => 0,
  39. "cert-onecolumn.pem" => 1,
  40. "cert-oneline.pem" => 1,
  41. "cert-oneline-multiple-of-254.pem" => 1,
  42. "cert-shortandlongline.pem" => 1,
  43. "cert-shortline.pem" => 1,
  44. "cert-threecolumn.pem" => 1,
  45. "cert-trailingwhitespace.pem" => 1,
  46. "cert.pem" => 1
  47. );
  48. my %dsa_expected = (
  49. "dsa-1023line.pem" => 0,
  50. "dsa-1024line.pem" => 0,
  51. "dsa-1025line.pem" => 0,
  52. "dsa-255line.pem" => 0,
  53. "dsa-256line.pem" => 0,
  54. "dsa-257line.pem" => 0,
  55. "dsa-blankline.pem" => 0,
  56. "dsa-comment.pem" => 0,
  57. "dsa-corruptedheader.pem" => 0,
  58. "dsa-corruptiv.pem" => 0,
  59. "dsa-earlypad.pem" => 0,
  60. "dsa-extrapad.pem" => 0,
  61. "dsa-infixwhitespace.pem" => 0,
  62. "dsa-junk.pem" => 0,
  63. "dsa-leadingwhitespace.pem" => 0,
  64. "dsa-longline.pem" => 0,
  65. "dsa-misalignedpad.pem" => 0,
  66. "dsa-onecolumn.pem" => 0,
  67. "dsa-oneline.pem" => 0,
  68. "dsa-onelineheader.pem" => 0,
  69. "dsa-shortandlongline.pem" => 0,
  70. "dsa-shortline.pem" => 0,
  71. "dsa-threecolumn.pem" => 0,
  72. "dsa-trailingwhitespace.pem" => 1,
  73. "dsa.pem" => 1
  74. );
  75. plan tests => scalar keys(%cert_expected) + scalar keys(%dsa_expected) + 4;
  76. foreach my $input (keys %cert_expected) {
  77. my @common = ($cmd, "x509", "-text", "-noout", "-inform", "PEM", "-in");
  78. my @data = run(app([@common, data_file($input)], stderr => undef), capture => 1);
  79. my @match = grep /The Great State of Long-Winded Certificate Field Names Whereby to Increase the Output Size/, @data;
  80. is((scalar @match > 0 ? 1 : 0), $cert_expected{$input});
  81. }
  82. SKIP: {
  83. skip "DSA support disabled, skipping...", (scalar keys %dsa_expected) unless !disabled("dsa");
  84. foreach my $input (keys %dsa_expected) {
  85. my @common = ($cmd, "pkey", "-inform", "PEM", "-passin", "file:" . data_file("wellknown"), "-noout", "-text", "-in");
  86. my @data;
  87. {
  88. local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
  89. @data = run(app([@common, data_file($input)], stderr => undef), capture => 1);
  90. }
  91. my @match = grep /68:42:02:16:63:54:16:eb:06:5c:ab:06:72:3b:78:/, @data;
  92. is((scalar @match > 0 ? 1 : 0), $dsa_expected{$input});
  93. }
  94. }
  95. my @common = ($cmd, "pkey", "-inform", "PEM", "-noout", "-text", "-in");
  96. my @data = run(app([@common, data_file("beermug.pem")], stderr => undef), capture => 1);
  97. my @match = grep /00:a0:3a:21:14:5d:cd:b6:d5:a0:3e:49:23:c1:3a:/, @data;
  98. ok(scalar @match > 0 ? 1 : 0);
  99. my $certkeycert = srctop_file("test", "certs", "cert-key-cert.pem");
  100. @data = run(app([@common, $certkeycert], stderr => "outerr.txt"), capture => 1);
  101. open DATA, "outerr.txt";
  102. @match = grep /:error:/, <DATA>;
  103. close DATA;
  104. ok(scalar @match > 0 ? 0 : 1);
  105. @match = grep /70:40:4c:20:6a:16:ba:38:b5:c9:b1:4c:b6:b8:db:/, @data;
  106. ok(scalar @match > 0 ? 1 : 0);
  107. ok(run(test(["pemtest", $certkeycert])), "running pemtest");