| 1 | package Convert::SSH2::Format::PKCS8; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $VERSION = '0.01'; | |||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | extends 'Convert::SSH2::Format::Base'; | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Carp qw(confess); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Convert::ASN1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Convert::SSH2::Format::PKCS8 - Format SSH key data as PKCS8 | ||||||||||
| 15 | |||||||||||
| 16 | =head1 PURPOSE | ||||||||||
| 17 | |||||||||||
| 18 | This module formats SSH2 RSA public keys as PKCS8 strings. | ||||||||||
| 19 | |||||||||||
| 20 | These look like | ||||||||||
| 21 | |||||||||||
| 22 | -----BEGIN PUBLIC KEY----- | ||||||||||
| 23 | ... Base64 data ... | ||||||||||
| 24 | -----END PUBLIC KEY----- | ||||||||||
| 25 | |||||||||||
| 26 | Generally, you shouldn't instantiate this class on its own. It will be called by L<Convert::SSH2> | ||||||||||
| 27 | when needed. | ||||||||||
| 28 | |||||||||||
| 29 | =head1 ATTRIBUTES | ||||||||||
| 30 | |||||||||||
| 31 | =over | ||||||||||
| 32 | |||||||||||
| 33 | =item asn | ||||||||||
| 34 | |||||||||||
| 35 | Holds an ASN converter. Defaults to L<Convert::ASN1>. | ||||||||||
| 36 | |||||||||||
| 37 | =back | ||||||||||
| 38 | |||||||||||
| 39 | =cut | ||||||||||
| 40 | |||||||||||
| 41 | has 'asn' => ( | ||||||||||
| 42 | is => 'ro', | ||||||||||
| 43 | 2 | ▶ | default => sub { Convert::ASN1->new(); }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 1 | ); | |||||||||
| 45 | |||||||||||
| 46 | =over | ||||||||||
| 47 | |||||||||||
| 48 | =item asn_template | ||||||||||
| 49 | |||||||||||
| 50 | The ASN encoding template. | ||||||||||
| 51 | |||||||||||
| 52 | Defaults to: | ||||||||||
| 53 | |||||||||||
| 54 | SEQUENCE { | ||||||||||
| 55 | SEQUENCE { | ||||||||||
| 56 | OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1), | ||||||||||
| 57 | NULL | ||||||||||
| 58 | } | ||||||||||
| 59 | BIT STRING { | ||||||||||
| 60 | RSAPublicKey ::= SEQUENCE { | ||||||||||
| 61 | modulus INTEGER, -- n | ||||||||||
| 62 | publicExponent INTEGER -- e | ||||||||||
| 63 | } | ||||||||||
| 64 | } | ||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | =back | ||||||||||
| 68 | |||||||||||
| 69 | =cut | ||||||||||
| 70 | |||||||||||
| 71 | has 'asn_template' => ( | ||||||||||
| 72 | is => 'ro', | ||||||||||
| 73 | 2 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 74 | 2 | return <<_EOT; | |||||||||
| 75 | key SEQUENCE { | ||||||||||
| 76 | algo SEQUENCE { | ||||||||||
| 77 | id OBJECT IDENTIFIER, | ||||||||||
| 78 | n NULL | ||||||||||
| 79 | } | ||||||||||
| 80 | bs BIT STRING | ||||||||||
| 81 | } | ||||||||||
| 82 | _EOT | ||||||||||
| 83 | }, | ||||||||||
| 84 | 1 | ); | |||||||||
| 85 | |||||||||||
| 86 | =head1 METHOD | ||||||||||
| 87 | |||||||||||
| 88 | =over | ||||||||||
| 89 | |||||||||||
| 90 | =item generate() | ||||||||||
| 91 | |||||||||||
| 92 | Returns a PKCS8 formatted string, given C<n> and C<e>. | ||||||||||
| 93 | |||||||||||
| 94 | =back | ||||||||||
| 95 | |||||||||||
| 96 | =cut | ||||||||||
| 97 | |||||||||||
| 98 | 2 | ▶ | sub generate { | ||||||||
|
Subroutine
generate: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||
| 99 | 2 | my $self = shift; | |||||||||
| 100 | |||||||||||
| 101 | 2 | ▶ | $self->asn->prepare(q| | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | rsa SEQUENCE { | ||||||||||
| 103 | mod INTEGER, | ||||||||||
| 104 | exp INTEGER | ||||||||||
| 105 | } | ||||||||||
| 106 | |) or die; | ||||||||||
| 107 | |||||||||||
| 108 | 2 | ▶ | my $rsa_asn = $self->asn->encode( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | rsa => { | ||||||||||
| 110 | mod => $self->n, | ||||||||||
| 111 | exp => $self->e, | ||||||||||
| 112 | } | ||||||||||
| 113 | ) or die; | ||||||||||
| 114 | |||||||||||
| 115 | 2 | ▶ | $self->asn->prepare($self->asn_template) or die; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | |||||||||||
| 117 | 2 | ▶ | my $pdu = $self->asn->encode( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | key => { | ||||||||||
| 119 | algo => { | ||||||||||
| 120 | id => '1.2.840.113549.1.1.1', | ||||||||||
| 121 | n => '', | ||||||||||
| 122 | }, | ||||||||||
| 123 | bs => $rsa_asn, | ||||||||||
| 124 | } | ||||||||||
| 125 | ) or die; | ||||||||||
| 126 | |||||||||||
| 127 | 2 | my $b64 = encode_base64($pdu, ""); | |||||||||
| 128 | |||||||||||
| 129 | 2 | my $out = "-----BEGIN PUBLIC KEY-----\n"; | |||||||||
| 130 | 2 | $out .= $self->format_lines($b64); | |||||||||
| 131 | 2 | $out .= "-----END PUBLIC KEY-----\n"; | |||||||||
| 132 | |||||||||||
| 133 | 2 | return $out; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | =head1 SEE ALSO | ||||||||||
| 137 | |||||||||||
| 138 | L<Convert::SSH2> | ||||||||||
| 139 | |||||||||||
| 140 | =cut | ||||||||||
| 141 | |||||||||||
| 142 | 1 | 1; | |||||||||