xref: /src/crypto/openssl/test/recipes/04-test_encoder_decoder.t (revision e7be843b4a162e68651d3911f0357ed464915629)
19dd13e84SCy Schubert#! /usr/bin/env perl
229536654SEnji Cooper# Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
39dd13e84SCy Schubert#
49dd13e84SCy Schubert# Licensed under the Apache License 2.0 (the "License").  You may not use
59dd13e84SCy Schubert# this file except in compliance with the License.  You can obtain a copy
69dd13e84SCy Schubert# in the file LICENSE in the source distribution or at
79dd13e84SCy Schubert# https://www.openssl.org/source/license.html
89dd13e84SCy Schubert
99dd13e84SCy Schubertuse strict;
109dd13e84SCy Schubertuse warnings;
119dd13e84SCy Schubert
129dd13e84SCy Schubertuse OpenSSL::Test qw/:DEFAULT srctop_dir srctop_file bldtop_dir bldtop_file/;
139dd13e84SCy Schubertuse OpenSSL::Test::Utils;
149dd13e84SCy Schubert
159dd13e84SCy SchubertBEGIN {
169dd13e84SCy Schubert    setup("test_encoder_decoder");
179dd13e84SCy Schubert}
189dd13e84SCy Schubert
199dd13e84SCy Schubertuse lib srctop_dir('Configurations');
209dd13e84SCy Schubertuse lib bldtop_dir('.');
219dd13e84SCy Schubertuse platform;
229dd13e84SCy Schubert
239dd13e84SCy Schubertmy $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
249dd13e84SCy Schubert
259dd13e84SCy Schubertmy $rsa_key = srctop_file("test", "certs", "ee-key.pem");
269dd13e84SCy Schubertmy $pss_key = srctop_file("test", "certs", "ca-pss-key.pem");
279dd13e84SCy Schubert
2829536654SEnji Cooperplan tests => ($no_fips ? 0 : 5) + 2;     # FIPS install test + test
299dd13e84SCy Schubert
309dd13e84SCy Schubertmy $conf = srctop_file("test", "default.cnf");
311c342803SEnji Cooper
321c342803SEnji Cooper# Check if the specified pattern occurs in the given file
331c342803SEnji Cooper# Returns 1 if the pattern is found and 0 if not
341c342803SEnji Coopersub find_line_file {
351c342803SEnji Cooper    my ($key, $file) = @_;
361c342803SEnji Cooper
371c342803SEnji Cooper    open(my $in, $file) or return -1;
381c342803SEnji Cooper    while (my $line = <$in>) {
391c342803SEnji Cooper        if ($line =~ /$key/) {
401c342803SEnji Cooper            close($in);
411c342803SEnji Cooper            return 1;
421c342803SEnji Cooper        }
431c342803SEnji Cooper    }
441c342803SEnji Cooper    close($in);
451c342803SEnji Cooper    return 0;
461c342803SEnji Cooper}
471c342803SEnji Cooper
489dd13e84SCy Schubertok(run(test(["endecode_test", "-rsa", $rsa_key,
499dd13e84SCy Schubert                              "-pss", $pss_key,
509dd13e84SCy Schubert                              "-config", $conf,
519dd13e84SCy Schubert                              "-provider", "default"])));
529dd13e84SCy Schubert
539dd13e84SCy Schubert# Run with non-default library context
549dd13e84SCy Schubertok(run(test(["endecode_test", "-rsa", $rsa_key,
559dd13e84SCy Schubert                              "-pss", $pss_key,
569dd13e84SCy Schubert                              "-context",
579dd13e84SCy Schubert                              "-config", $conf,
589dd13e84SCy Schubert                              "-provider", "default"])));
599dd13e84SCy Schubert
609dd13e84SCy Schubertunless ($no_fips) {
619dd13e84SCy Schubert    # Run with fips library context
629dd13e84SCy Schubert    my $conf = srctop_file("test", "fips-and-base.cnf");
639dd13e84SCy Schubert    ok(run(test(["endecode_test", "-rsa", $rsa_key,
649dd13e84SCy Schubert                                  "-pss", $pss_key,
659dd13e84SCy Schubert                                  "-config", $conf,
669dd13e84SCy Schubert                                  "-provider", "fips"])));
671c342803SEnji CooperSKIP: {
681c342803SEnji Cooper    skip "EC disabled", 2 if disabled("ec");
691c342803SEnji Cooper    ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'EC',
701c342803SEnji Cooper                 '-pkeyopt', 'group:P-256', '-text',
711c342803SEnji Cooper                 '-config', $conf, '-provider', 'fips', '-out', 'ec.txt' ])),
721c342803SEnji Cooper       'Print a FIPS provider EC private key');
731c342803SEnji Cooper    ok(find_line_file('NIST CURVE: P-256', 'ec.txt') == 1,
741c342803SEnji Cooper       'Printing an FIPS provider EC private key');
759dd13e84SCy Schubert}
7629536654SEnji Cooper    my $no_des = disabled("des");
7729536654SEnji CooperSKIP: {
7829536654SEnji Cooper    skip "MD5 disabled", 2 if disabled("md5");
7929536654SEnji Cooper    ok(run(app([ 'openssl', 'genrsa', '-aes128', '-out', 'epki.pem',
8029536654SEnji Cooper                 '-traditional', '-passout', 'pass:pass' ])),
8129536654SEnji Cooper       "rsa encrypted using a non fips algorithm MD5 in pbe");
8229536654SEnji Cooper
8329536654SEnji Cooper    my $conf2 = srctop_file("test", "default-and-fips.cnf");
8429536654SEnji Cooper    ok(run(test(['decoder_propq_test', '-config', $conf2,
8529536654SEnji Cooper                 '-provider', 'fips', 'epki.pem'])));
8629536654SEnji Cooper}
871c342803SEnji Cooper}
88