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