1#! /usr/bin/env perl
2# Copyright 2017-2020 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#
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-comment.pem" => 0,
38    "cert-earlypad.pem" => 0,
39    "cert-extrapad.pem" => 0,
40    "cert-infixwhitespace.pem" => 1,
41    "cert-junk.pem" => 0,
42    "cert-leadingwhitespace.pem" => 1,
43    "cert-longline.pem" => 1,
44    "cert-misalignedpad.pem" => 0,
45    "cert-onecolumn.pem" => 1,
46    "cert-oneline.pem" => 1,
47    "cert-oneline-multiple-of-254.pem" => 1,
48    "cert-shortandlongline.pem" => 1,
49    "cert-shortline.pem" => 1,
50    "cert-threecolumn.pem" => 1,
51    "cert-trailingwhitespace.pem" => 1,
52    "cert.pem" => 1
53);
54my %dsa_expected = (
55    "dsa-1023line.pem" => 0,
56    "dsa-1024line.pem" => 0,
57    "dsa-1025line.pem" => 0,
58    "dsa-255line.pem" => 0,
59    "dsa-256line.pem" => 0,
60    "dsa-257line.pem" => 0,
61    "dsa-blankline.pem" => 0,
62    "dsa-comment.pem" => 0,
63    "dsa-corruptedheader.pem" => 0,
64    "dsa-corruptiv.pem" => 0,
65    "dsa-earlypad.pem" => 0,
66    "dsa-extrapad.pem" => 0,
67    "dsa-infixwhitespace.pem" => 0,
68    "dsa-junk.pem" => 0,
69    "dsa-leadingwhitespace.pem" => 0,
70    "dsa-longline.pem" => 0,
71    "dsa-misalignedpad.pem" => 0,
72    "dsa-onecolumn.pem" => 0,
73    "dsa-oneline.pem" => 0,
74    "dsa-onelineheader.pem" => 0,
75    "dsa-shortandlongline.pem" => 0,
76    "dsa-shortline.pem" => 0,
77    "dsa-threecolumn.pem" => 0,
78    "dsa-trailingwhitespace.pem" => 1,
79    "dsa.pem" => 1
80);
81
82plan tests =>  scalar keys(%cert_expected) + scalar keys(%dsa_expected) + 2;
83
84foreach my $input (keys %cert_expected) {
85    my @common = ($cmd, "x509", "-text", "-noout", "-inform", "PEM", "-in");
86    my @data = run(app([@common, data_file($input)], stderr => undef), capture => 1);
87    my @match = grep /The Great State of Long-Winded Certificate Field Names Whereby to Increase the Output Size/, @data;
88    is((scalar @match > 0 ? 1 : 0), $cert_expected{$input});
89}
90SKIP: {
91    skip "DSA support disabled, skipping...", (scalar keys %dsa_expected) unless !disabled("dsa");
92    foreach my $input (keys %dsa_expected) {
93        my @common = ($cmd, "pkey", "-inform", "PEM", "-passin", "file:" . data_file("wellknown"), "-noout", "-text", "-in");
94        my @data;
95        {
96            local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
97            @data = run(app([@common, data_file($input)], stderr => undef), capture => 1);
98        }
99        my @match = grep /68:42:02:16:63:54:16:eb:06:5c:ab:06:72:3b:78:/, @data;
100        is((scalar @match > 0 ? 1 : 0), $dsa_expected{$input});
101    }
102}
103SKIP: {
104    skip "RSA support disabled, skipping...", 1 unless !disabled("rsa");
105    my @common = ($cmd, "pkey", "-inform", "PEM", "-noout", "-text", "-in");
106    my @data = run(app([@common, data_file("beermug.pem")], stderr => undef), capture => 1);
107    my @match = grep /00:a0:3a:21:14:5d:cd:b6:d5:a0:3e:49:23:c1:3a:/, @data;
108    ok(scalar @match > 0 ? 1 : 0);
109}
110
111ok(run(test(["pemtest"])), "running pemtest");
112