Convert-PEM

 view release on metacpan or  search on metacpan

t/func.pl  view on Meta::CPAN


			# re-read the written file
			lives_ok { $obj2 = $pem->read( Filename => $t->{tx}, Password => "test", Cipher => $t->{name} ) } "can re-read '$t->{tx}' file using module $m";
			diag("error occurred reading file $t->{tx}: ".$pem->errstr()) if $pem->errstr();
			ok defined $obj2, "read $t->{name} object from file '$t->{tx}' using module $m";
			lives_ok { $dec2 = $pem->encode( Content => $obj2) } "Encode $t->{name} object from file $t->{tx} without encryption";
			ok defined $dec2, "Write and re-read $t->{name} object encripted using cipher module $m";
			lives_ok { $der2 = $pem->to_der( Content => $obj2 ) } "DER Encode $t->{name} object from file $t->{tx}";
			$h2 = lc md5_hex($der2);
			ok $h2 eq $t->{hash}, "hash '$h2' of key from file $t->{tx} matches expected value of '$t->{hash}'";

			# compare the original with the written/re-read
			ok defined $dec1 && defined $dec2 && $dec1 eq $dec2, "Read original $t->{name} file '$t->{rx}' and match contents to written file '$t->{tx}'";

			# openssl tests
			if ($ossl) {
 				SKIP: {
 					skip("Author Tests only for openssl test", 1) if !$ENV{AUTHOR_TESTING};
 					skip("No support for IDEA-CBC", 1) if (`openssl enc -ciphers` !~ /idea-cbc/m) && ($t->{tx} =~ /idea/);
  					skip("No support for DES", 1) 
  						if (`openssl enc -ciphers` !~ /des-cbc/m) || (($t->{tx} =~ /des.wr.pem/) && ($ossl_ver =~ /OpenSSL 3/));
					ok !system("openssl rsa -in $t->{tx} -passin pass:test -noout 2> /dev/null"), "use openssl to read file '$t->{tx}' encrypted with $t->{name} using module $m";
 				}
			}
			unlink $t->{tx};
		}
	}
}

sub get_rsa
{
	my $rsa_asn = <<ASN1;

   Version ::= INTEGER  --{  v1(0), v2(1), v3(2)  }

   OtherPrimeInfos ::= SEQUENCE --SIZE(1..MAX) OF OtherPrimeInfo

   OtherPrimeInfo ::= SEQUENCE {
      prime             INTEGER,  -- ri
      exponent          INTEGER,  -- di
      coefficient       INTEGER   -- ti
   }

   RSAPrivateKey ::= SEQUENCE {
          version           INTEGER,
          modulus           INTEGER,  -- n
          publicExponent    INTEGER,  -- e
          privateExponent   INTEGER,  -- d
          prime1            INTEGER,  -- p
          prime2            INTEGER,  -- q
          exponent1         INTEGER,  -- d mod (p-1)
          exponent2         INTEGER,  -- d mod (q-1)
          coefficient       INTEGER,  -- (inverse of q) mod p
          otherPrimeInfos   EXPLICIT OtherPrimeInfos OPTIONAL }

ASN1

	return Convert::PEM->new(
		Name 	=> "RSA PRIVATE KEY",
		ASN  	=> $rsa_asn,
		Macro	=>	"RSAPrivateKey",
		@_,
    );
}


1;



( run in 1.507 second using v1.01-cache-2.11-cpan-437f7b0c052 )