| 1 | package Net::DNS::SEC::RSA; | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: RSA.pm 1677 2018-05-22 11:59:10Z willem $ | ||
| 5 | # | ||
| 6 | our $VERSION = (qw$LastChangedRevision: 1677 $)[1]; | ||
| 7 | |||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Net::DNS::SEC::RSA - DNSSEC RSA digital signature algorithm | ||
| 12 | |||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | require Net::DNS::SEC::RSA; | ||
| 17 | |||
| 18 | $signature = Net::DNS::SEC::RSA->sign( $sigdata, $private ); | ||
| 19 | |||
| 20 | $validated = Net::DNS::SEC::RSA->verify( $sigdata, $keyrr, $sigbin ); | ||
| 21 | |||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Implementation of RSA digital signature | ||
| 26 | generation and verification procedures. | ||
| 27 | |||
| 28 | =head2 sign | ||
| 29 | |||
| 30 | $signature = Net::DNS::SEC::RSA->sign( $sigdata, $private ); | ||
| 31 | |||
| 32 | Generates the wire-format signature from the sigdata octet string | ||
| 33 | and the appropriate private key object. | ||
| 34 | |||
| 35 | =head2 verify | ||
| 36 | |||
| 37 | $validated = Net::DNS::SEC::RSA->verify( $sigdata, $keyrr, $sigbin ); | ||
| 38 | |||
| 39 | Verifies the signature over the sigdata octet string using the specified | ||
| 40 | public key resource record. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | use strict; | ||
| 45 | use integer; | ||
| 46 | use warnings; | ||
| 47 | use MIME::Base64; | ||
| 48 | |||
| 49 | my %parameters = ( | ||
| 50 | 1 => [sub { Net::DNS::SEC::libcrypto::EVP_md5() }], | ||
| 51 | 5 => [sub { Net::DNS::SEC::libcrypto::EVP_sha1() }], | ||
| 52 | 7 => [sub { Net::DNS::SEC::libcrypto::EVP_sha1() }], | ||
| 53 | 8 => [sub { Net::DNS::SEC::libcrypto::EVP_sha256() }], | ||
| 54 | 10 => [sub { Net::DNS::SEC::libcrypto::EVP_sha512() }], | ||
| 55 | ); | ||
| 56 | |||
| 57 | |||
| 58 | sub sign { | ||
| 59 | my ( $class, $sigdata, $private ) = @_; | ||
| 60 | |||
| 61 | my $algorithm = $private->algorithm; | ||
| 62 | my ($evpmd) = @{$parameters{$algorithm} || []}; | ||
| 63 | die 'private key not RSA' unless $evpmd; | ||
| 64 | |||
| 65 | my ( $n, $e, $d, $p, $q ) = map decode_base64( $private->$_ ), | ||
| 66 | qw(Modulus PublicExponent PrivateExponent Prime1 Prime2); | ||
| 67 | |||
| 68 | my $rsa = Net::DNS::SEC::libcrypto::RSA_new(); | ||
| 69 | Net::DNS::SEC::libcrypto::RSA_set0_factors( $rsa, $p, $q ); | ||
| 70 | Net::DNS::SEC::libcrypto::RSA_set0_key( $rsa, $n, $e, $d ); | ||
| 71 | |||
| 72 | my $evpkey = Net::DNS::SEC::libcrypto::EVP_PKEY_new(); | ||
| 73 | Net::DNS::SEC::libcrypto::EVP_PKEY_assign_RSA( $evpkey, $rsa ); | ||
| 74 | |||
| 75 | Net::DNS::SEC::libcrypto::EVP_sign( $sigdata, $evpkey, &$evpmd ); | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | sub verify { | ||
| 80 | my ( $class, $sigdata, $keyrr, $sigbin ) = @_; | ||
| 81 | |||
| 82 | my $algorithm = $keyrr->algorithm; | ||
| 83 | my ($evpmd) = @{$parameters{$algorithm} || []}; | ||
| 84 | die 'public key not RSA' unless $evpmd; | ||
| 85 | |||
| 86 | return unless $sigbin; | ||
| 87 | |||
| 88 | my $keybin = $keyrr->keybin; # public key | ||
| 89 | my ( $short, $long ) = unpack( 'Cn', $keybin ); # RFC3110, section 2 | ||
| 90 | my $keyfmt = $short ? "x a$short a*" : "x3 a$long a*"; | ||
| 91 | my ( $exponent, $modulus ) = unpack( $keyfmt, $keybin ); | ||
| 92 | |||
| 93 | my $rsa = Net::DNS::SEC::libcrypto::RSA_new(); | ||
| 94 | Net::DNS::SEC::libcrypto::RSA_set0_key( $rsa, $modulus, $exponent, '' ); | ||
| 95 | |||
| 96 | my $evpkey = Net::DNS::SEC::libcrypto::EVP_PKEY_new(); | ||
| 97 | Net::DNS::SEC::libcrypto::EVP_PKEY_assign_RSA( $evpkey, $rsa ); | ||
| 98 | |||
| 99 | Net::DNS::SEC::libcrypto::EVP_verify( $sigdata, $sigbin, $evpkey, &$evpmd ); | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | __END__ |