« blib/lib/Crypt/Perl/X509/Extension/basicConstraints.pm
↑ Summary
lib/Crypt/Perl/ECDSA/EC/Point.pm »
| 1 | package Crypt::Perl::X509::Name; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =encoding utf-8 | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Crypt::Perl::X509::Name - Representation of Distinguished Name | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | #This encodes each key/value into separate | ||
| 15 | #RelativeDistinguishedName structures, as OpenSSL does by default. | ||
| 16 | #Unless you know otherwise, this is probably what you want. | ||
| 17 | #(See ENCODING below for more details.) | ||
| 18 | my $name = Crypt::Perl::X509::Name->new( | ||
| 19 | streetAddress => '...', #keys are short OID names | ||
| 20 | localityName => '...', | ||
| 21 | #... | ||
| 22 | ); | ||
| 23 | |||
| 24 | my $der = $name->encode(); | ||
| 25 | |||
| 26 | =head1 DISCUSSION | ||
| 27 | |||
| 28 | This is useful to represent the Subject and Issuer parts of an | ||
| 29 | X.509 (i.e., SSL/TLS) certificate as well as the name portion of | ||
| 30 | a PCKS #10 Certificate Signing Request (CSR). | ||
| 31 | |||
| 32 | =head1 ENCODING | ||
| 33 | |||
| 34 | L<RFC 5280 §4.1.2.4|https://tools.ietf.org/html/rfc5280#section-4.1.2.4> | ||
| 35 | defines the C<Name> type as an ordered C<SEQUENCE> of unordered C<SET>s | ||
| 36 | —C<RelativeDistinguishedName> objects, or “RDN”s—of key/value pairs. | ||
| 37 | OpenSSL defaults to having each RDN contain only one key/value | ||
| 38 | pair. (L<You can also have it create “multi-value RDNs”.|http://openssl.6102.n7.nabble.com/Multi-value-RDNs-and-openssl-cnf-format-td7925.html>) I’m unclear as to why this is, | ||
| 39 | but I suspect it has to do with ease of matching up C<Name> values; since | ||
| 40 | the RDNs are unordered, to compare one multi-value RDN against another takes | ||
| 41 | more work than to compare two ordered lists of single-value RDNs, which can be | ||
| 42 | done with a simple text equality check. | ||
| 43 | (cf. L<RFC 5280 §7.1|http://tools.ietf.org/html/rfc5280#section-7.1>) | ||
| 44 | |||
| 45 | If you need a multi-value RDN, it can be gotten by grouping key/value pairs | ||
| 46 | in an array reference, thus: | ||
| 47 | |||
| 48 | my $name = Crypt::Perl::X509::Name->new( | ||
| 49 | |||
| 50 | #a multi-value RDN | ||
| 51 | [ streetAddress => '...', localityName => '...' ], | ||
| 52 | |||
| 53 | #regular key/value pair becomes its own single-value RDN | ||
| 54 | stateOrProvinceName => '...', | ||
| 55 | ); | ||
| 56 | |||
| 57 | =head1 ABOUT C<commonName> | ||
| 58 | |||
| 59 | Note that C<commonName> is | ||
| 60 | deprecated (cf. L<RFC 6125 §2.3|https://tools.ietf.org/html/rfc6125#section-2.3>, | ||
| 61 | L<CA Browser Forum Baseline Requirements §7.1.4.2.2|https://cabforum.org/wp-content/uploads/CA-Browser-Forum-BR-1.4.1.pdf>) | ||
| 62 | for use in X.509 certificates, but many CAs still require it as of | ||
| 63 | December 2016. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | use parent qw( Crypt::Perl::ASN1::Encodee ); | ||
| 68 | |||
| 69 | use Crypt::Perl::ASN1 (); | ||
| 70 | use Crypt::Perl::X509::RelativeDistinguishedName (); | ||
| 71 | |||
| 72 | use constant ASN1 => Crypt::Perl::X509::RelativeDistinguishedName::ASN1() . <<END; | ||
| 73 | RDNSequence ::= SEQUENCE OF ANY -- RelativeDistinguishedName | ||
| 74 | |||
| 75 | Name ::= CHOICE { | ||
| 76 | rdnSequence RDNSequence | ||
| 77 | } | ||
| 78 | END | ||
| 79 | |||
| 80 | #XXX TODO de-duplicate | ||
| 81 | *get_OID = \&Crypt::Perl::X509::RelativeDistinguishedName::get_OID; | ||
| 82 | *encode_string = \&Crypt::Perl::X509::RelativeDistinguishedName::encode_string; | ||
| 83 | |||
| 84 | sub new { | ||
| 85 | my ($class, @inputs) = @_; | ||
| 86 | |||
| 87 | my @seq; | ||
| 88 | |||
| 89 | while (@inputs) { | ||
| 90 | if (my $ref = ref $inputs[0]) { | ||
| 91 | my $input = shift @inputs; | ||
| 92 | die "Invalid RDN ref: $ref" if $ref ne 'ARRAY'; | ||
| 93 | push @seq, Crypt::Perl::X509::RelativeDistinguishedName->new( @$input )->encode(); | ||
| 94 | } | ||
| 95 | |||
| 96 | #Legacy-ish … | ||
| 97 | else { | ||
| 98 | my ($k, $v) = splice( @inputs, 0, 2 ); | ||
| 99 | my $rdn = Crypt::Perl::X509::RelativeDistinguishedName->new( $k, $v )->encode(); | ||
| 100 | push @seq, $rdn; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | return bless \@seq, $class; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub _encode_params { | ||
| 108 | return { rdnSequence => [ @{ $_[0] } ] }; | ||
| 109 | } | ||
| 110 | |||
| 111 | 1; |