lib/Crypt/Perl/X509/Name.pm

filter: Statementstmt 100% 34 / 34 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 95.9% 47 / 49 CC 4 SCAR 7.4
CC
4
Cov
98%
new
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.1
« lib/Crypt/Perl/X509/Extension/authorityKeyIdentifier.pm ↑ Summary lib/Crypt/Perl/PKCS10/Attributes.pm »
1 package Crypt::Perl::X509::Name;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 4 ▶use parent qw( Crypt::Perl::ASN1::Encodee );
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 4 ▶use Crypt::Perl::ASN1 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
70 4 ▶use Crypt::Perl::X509::RelativeDistinguishedName ();
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 4 ▶use constant ASN1 => Crypt::Perl::X509::RelativeDistinguishedName::ASN1() . <<END;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 RDNSequence ::= SEQUENCE OF ANY -- RelativeDistinguishedName
74  
75 Name ::= CHOICE {
76 rdnSequence RDNSequence
77 }
78 END
79  
80 #XXX TODO de-duplicate
81 4 *get_OID = \&Crypt::Perl::X509::RelativeDistinguishedName::get_OID;
82 4 *encode_string = \&Crypt::Perl::X509::RelativeDistinguishedName::encode_string;
83  
84 sub new {
85 51 ▶ my ($class, @inputs) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
86  
87 51 my @seq;
88  
89 51 ▶ while (@inputs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@inputs) 72 51 123
90 72 ▶ if (my $ref = ref $inputs[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $inputs[0]) { } 12 60 72
91 12 my $input = shift @inputs;
92 12 ▶ die "Invalid RDN ref: $ref" if $ref ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref ne "ARRAY" 0 12 12
93 12 push @seq, Crypt::Perl::X509::RelativeDistinguishedName->new( @$input )->encode();
94 }
95  
96 #Legacy-ish …
97 else {
98 60 my ($k, $v) = splice( @inputs, 0, 2 );
99 60 my $rdn = Crypt::Perl::X509::RelativeDistinguishedName->new( $k, $v )->encode();
100 60 push @seq, $rdn;
101 }
102 }
103  
104 51 return bless \@seq, $class;
105 }
106  
107 sub _encode_params {
108 51 ▶ return { rdnSequence => [ @{ $_[0] } ] };
Subroutine _encode_params: called CC=1 SCAR=0.0
109 }
110  
111 4 1;
« lib/Crypt/Perl/X509/Extension/authorityKeyIdentifier.pm ↑ Summary lib/Crypt/Perl/PKCS10/Attributes.pm »