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