| 1 | package Crypt::Perl::X509::SCT; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =encoding utf-8 | ||||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | Crypt::Perl::X509::SCT | ||||||||||
| 11 | |||||||||||
| 12 | =head1 DESCRIPTION | ||||||||||
| 13 | |||||||||||
| 14 | This implements encoding of the structure defined in | ||||||||||
| 15 | L<https://tools.ietf.org/html/rfc6962#section-3.2>. | ||||||||||
| 16 | |||||||||||
| 17 | B<IMPORTANT:> Because SCT records timestamps in milliseconds rather than | ||||||||||
| 18 | seconds, this module requires a 64-bit Perl interpreter. | ||||||||||
| 19 | |||||||||||
| 20 | =head1 SEE ALSO | ||||||||||
| 21 | |||||||||||
| 22 | L<https://letsencrypt.org/2018/04/04/sct-encoding.html> has an | ||||||||||
| 23 | excellent walkthrough of the format that this module deals with. | ||||||||||
| 24 | |||||||||||
| 25 | =cut | ||||||||||
| 26 | |||||||||||
| 27 | 2 | use constant _TEMPLATE => join( | |||||||||
| 28 | q<>, | ||||||||||
| 29 | 'x', # version 1 (represented by 0) | ||||||||||
| 30 | 'a32', # key_id | ||||||||||
| 31 | 'N2', # timestamp; use this rather than “Q>” to support Perl 5.8. | ||||||||||
| 32 | 'xx', # zero-length extensions array | ||||||||||
| 33 | 'C', # hash algorithm | ||||||||||
| 34 | 'C', # signature algorithm | ||||||||||
| 35 | 'n', # signature length | ||||||||||
| 36 | 'a*', # signature | ||||||||||
| 37 | 2 | ▶ | ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 2 | my @_TLS_hash_algorithm = ( | |||||||||
| 40 | q<>, | ||||||||||
| 41 | 'md5', | ||||||||||
| 42 | 'sha1', | ||||||||||
| 43 | 'sha224', | ||||||||||
| 44 | 'sha256', | ||||||||||
| 45 | 'sha384', | ||||||||||
| 46 | 'sha512', | ||||||||||
| 47 | ); | ||||||||||
| 48 | |||||||||||
| 49 | 2 | my @_TLS_signature_algorithm = ( | |||||||||
| 50 | 'anonymous', | ||||||||||
| 51 | 'rsa', | ||||||||||
| 52 | 'dsa', | ||||||||||
| 53 | 'ecdsa', | ||||||||||
| 54 | ); | ||||||||||
| 55 | |||||||||||
| 56 | =head1 FUNCTIONS | ||||||||||
| 57 | |||||||||||
| 58 | =head2 encode( %opts ) | ||||||||||
| 59 | |||||||||||
| 60 | For now this always encodes a version 1 structure. | ||||||||||
| 61 | |||||||||||
| 62 | %opts is: | ||||||||||
| 63 | |||||||||||
| 64 | =over | ||||||||||
| 65 | |||||||||||
| 66 | =item * C<key_id> - 32-byte string | ||||||||||
| 67 | |||||||||||
| 68 | =item * C<timestamp> - integer (NB: milliseconds) | ||||||||||
| 69 | |||||||||||
| 70 | =item * C<hash_algorithm> - See | ||||||||||
| 71 | L<https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1> | ||||||||||
| 72 | for allowed values (e.g., C<sha256>). | ||||||||||
| 73 | |||||||||||
| 74 | =item * C<signature_algorithm> - Currently accepted values are | ||||||||||
| 75 | C<rsa> and C<ecdsa>. (cf. the URL for C<hash_algorithm> values) | ||||||||||
| 76 | |||||||||||
| 77 | =item * C<signature> - The signature (binary string). | ||||||||||
| 78 | |||||||||||
| 79 | =back | ||||||||||
| 80 | |||||||||||
| 81 | =cut | ||||||||||
| 82 | |||||||||||
| 83 | sub encode { | ||||||||||
| 84 | 13 | ▶ | my (%opts) = @_; | ||||||||
|
Subroutine
encode: called CC=3 SCAR=12.6
Pod
documented
| |||||||||||
| 85 | |||||||||||
| 86 | # A non-64-bit perl has no business in this module. | ||||||||||
| 87 | 13 | ▶ | if (!_can_64_bit()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | my $pkg = __PACKAGE__; | |||||||||
| 89 | 0 | die "$pkg requires a 64-bit Perl interpreter.\n"; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | my $hash_idx = _array_lookup( | ||||||||||
| 93 | \@_TLS_hash_algorithm, | ||||||||||
| 94 | 13 | $opts{'hash_algorithm'}, | |||||||||
| 95 | ); | ||||||||||
| 96 | |||||||||||
| 97 | my $sig_idx = _array_lookup( | ||||||||||
| 98 | \@_TLS_signature_algorithm, | ||||||||||
| 99 | 13 | $opts{'signature_algorithm'}, | |||||||||
| 100 | ); | ||||||||||
| 101 | |||||||||||
| 102 | 13 | ▶ | if ( 32 != length $opts{'key_id'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | die sprintf("“key_id” (%v.02x) must be 32 bytes!", $opts{'key_id'}); | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | return pack _TEMPLATE(), ( | ||||||||||
| 107 | $opts{'key_id'}, | ||||||||||
| 108 | ( $opts{'timestamp'} >> 32 ), | ||||||||||
| 109 | ( $opts{'timestamp'} & 0xffff_ffff ), | ||||||||||
| 110 | $hash_idx, | ||||||||||
| 111 | $sig_idx, | ||||||||||
| 112 | length($opts{'signature'}), | ||||||||||
| 113 | 13 | $opts{'signature'}, | |||||||||
| 114 | ); | ||||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | # called from test | ||||||||||
| 118 | sub _can_64_bit { | ||||||||||
| 119 | 14 | ▶ | my $exc = $@; | ||||||||
|
Subroutine
_can_64_bit: called CC=1 SCAR=0.0
| |||||||||||
| 120 | |||||||||||
| 121 | 14 | my $ok = !!eval { pack 'q' }; | |||||||||
| 122 | |||||||||||
| 123 | 14 | $@ = $exc; | |||||||||
| 124 | |||||||||||
| 125 | 14 | return $ok; | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | # decode() will be easy to implement when needed | ||||||||||
| 129 | |||||||||||
| 130 | sub _array_lookup { | ||||||||||
| 131 | 26 | ▶ | my ($ar, $val, $name) = @_; | ||||||||
|
Subroutine
_array_lookup: called CC=4 SCAR=14.0
| |||||||||||
| 132 | |||||||||||
| 133 | 26 | my $found_idx; | |||||||||
| 134 | |||||||||||
| 135 | 26 | for my $idx ( 0 .. $#$ar ) { | |||||||||
| 136 | 117 | ▶ | if ($val eq $ar->[$idx]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 26 | $found_idx = $idx; | |||||||||
| 138 | 26 | last; | |||||||||
| 139 | } | ||||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | 26 | ▶ | if (!defined $found_idx) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | 0 | die "Unrecognized “$name”: “$val”"; | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | 26 | return $found_idx; | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 2 | 1; | |||||||||