examples/make-crls.pl

filter: Statementstmt 100% 45 / 45 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 50 / 50 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« examples/make-cert-chain.pl ↑ Summary
1 #!perl -w
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 make-crls.pl - Example code to make RFC3280-compliant CRLv2s with
9 L<Crypt::OpenSSL::CA>
10  
11 =cut
12  
13 1 ▶use Crypt::OpenSSL::CA;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 DESCRIPTION
16  
17 The test private keys and certificates are assumed to be generated
18 already. See C<make-cert-chain.pl> in the same directory to see how
19 to do that.
20  
21 =cut
22  
23 1 my $ca_certificate = Crypt::OpenSSL::CA::X509->parse
24 (ca_certificate_as_text());
25 1 my $ca_privatekey = Crypt::OpenSSL::CA::PrivateKey->parse
26 (ca_privkey_as_text());
27  
28 =head2 Issuer coordinates
29  
30 The issuer DN and key identifiers are taken directly from the CA
31 certificate.
32  
33 =cut
34  
35 1 our $issuer_dn = $ca_certificate->get_subject_DN;
36 1 our $keyid = $ca_certificate->get_subject_keyid;
37  
38 =head2 CRL Number
39  
40 Long ASN1 integers are supported.
41  
42 =cut
43  
44 1 our $crlnumber = "0x" . ("deadbeef" x 5);
45  
46 =head1 REGULAR CRL
47  
48 First things first.
49  
50 =head2 Global CRL settings
51  
52 CRL dates are supported using the dual ASN.1 date format in
53 conformance with RFC3280 sections 5.1.2.4 and 5.1.2.5.
54  
55 RFC3280 section 5.1.2.1 now makes v2 for CRLs mandatory; not
56 coincidentally, this is the default in L<Crypt::OpenSSL::CA>. The
57 C<authorityKeyIdentifier> and C<crlNumber> extensions are also
58 mandatory. C<authorityKeyIdentifier> MUST NOT be critical as per
59 section 4.2.1.1, while C<crlNumber> MUST be as per 5.1.2.1.
60  
61 =cut
62  
63 {
64 1 my $crl = new Crypt::OpenSSL::CA::X509_CRL;
65 1 $crl->set_issuer_DN($issuer_dn);
66 1 $crl->set_lastUpdate("20070101000000Z");
67 1 $crl->set_nextUpdate("20570101000000Z");
68  
69 1 $crl->set_extension("authorityKeyIdentifier", { keyid => $keyid });
70 1 $crl->set_extension("crlNumber", $crlnumber, -critical => 1);
71  
72 =pod
73  
74 Just for fun, we add a C<freshestCRL> extension as per RFC3280 section
75 5.2.6; the corresponding delta CRL is issued below, see L</DELTA CRL>.
76  
77 =cut
78  
79 1 $crl->set_extension("freshestCRL",
80 "URI:http://www.example.com/deltacrl.crl",
81 -critical => 0);
82  
83 =head2 Revoked Certificates List
84  
85 In order of appearance: a CRLv1-like unadorned entry, an entry with
86 C<unspecified> revocation reason, an entry for a certificate that was
87 put on hold (that is removed by the delta-CRL, see below), and an
88 entry for a certificate whose key was compromised (with a
89 compromiseTime set). Notice that the CRL entries are in no particular
90 order.
91  
92 =cut
93  
94 1 $crl->add_entry("0x10", "20070212100000Z");
95 1 $crl->add_entry("0x11", "20070212100100Z", -reason => "unspecified");
96 1 $crl->add_entry("0x42", "20070212090100Z",
97 -hold_instruction => "holdInstructionPickupToken");
98 1 $crl->add_entry("0x12", "20070212100200Z", -reason => "keyCompromise",
99 -compromise_time => "20070210000000Z");
100  
101 =head2 All done
102  
103 Now we just have to sign the CRL.
104  
105 =cut
106  
107 1 my $crlpem = $crl->sign($ca_privatekey, "sha1");
108 1 print $crlpem;
109 }
110  
111 =head1 DELTA CRL
112  
113 Because we can.
114  
115 =cut
116  
117 {
118 1 my $deltacrl = new Crypt::OpenSSL::CA::X509_CRL;
119 1 $deltacrl->set_issuer_DN($issuer_dn);
120 1 $deltacrl->set_lastUpdate("20070212150000Z");
121 1 $deltacrl->set_nextUpdate("20570101000000Z");
122 1 $deltacrl->set_extension("authorityKeyIdentifier", { keyid => $keyid });
123  
124 =pod
125  
126 (Just make sure to update the CRL number as per RFC3280, section 5.2.3)
127  
128 =cut
129 1 my $deltacrlnumber = $crlnumber; $deltacrlnumber =~ s/beef$/bef0/;
130 1 $deltacrl->set_extension("crlNumber", $deltacrlnumber, -critical => 1);
131 1 $deltacrl->set_extension("deltaCRLIndicator", $crlnumber,
132 -critical => 1 # as per RFC3280 section 5.2.4
133 );
134  
135 =head2 Revoked Certificates List
136  
137 We add a revoked certificate to the CRL, and remove the hold
138 instruction from certificate 0x42.
139  
140 =cut
141  
142 1 $deltacrl->add_entry("0x42", "20070212150900Z", -reason => "removeFromCRL");
143 1 $deltacrl->add_entry("0xdeadbeefdeaff00f", "20070212151000Z");
144  
145 =head2 All done
146  
147 Now we just have to sign the CRL.
148  
149 =cut
150  
151 1 my $deltacrlpem = $deltacrl->sign($ca_privatekey, "sha1");
152 1 print $deltacrlpem;
153 }
154  
155 1 exit 0;
156  
157 =head1 TEST DATA
158  
159 =cut
160  
161 1 ▶sub ca_certificate_as_text { <<'PEM' }
Subroutine ca_certificate_as_text: called CC=1 SCAR=0.0
162 -----BEGIN CERTIFICATE-----
163 MIICsDCCAhmgAwIBAgIJANdqtXzdPS/1MA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
164 BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
165 aWRnaXRzIFB0eSBMdGQwHhcNMDcwMTMxMTcwNjU5WhcNMzcwMTMxMTcwNjU5WjBF
166 MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50
167 ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
168 gQDfGYmlOYbpGkBD/agTUnixLcdh6H1XM13w17RbzaoA7byZD6L+Dn8MZd69PuXc
169 ZAQEUG4Oe6QyAcafsvDb7SHjyJHLoPTOsAZ0ex/0zIJVpw+XyppA8fZx6bnuHKUa
170 bqfj83OLk/ACfQSBX7bcL7Y8hwYcZJcqyjMzt9BT7oCldwIDAQABo4GnMIGkMB0G
171 A1UdDgQWBBTu+qGX79xcvFE8pG5zx2FcqAuV5TB1BgNVHSMEbjBsgBTu+qGX79xc
172 vFE8pG5zx2FcqAuV5aFJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUt
173 U3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJANdqtXzd
174 PS/1MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEACQ+4e3MSlcqkhzgZ
175 rTXpsO/WpBT7aaM7AaecY54hB9uF9PmGC1q3axwZ2b/+Gh5ehQPyAwKevyjNz1y4
176 yP4YeUHO6FIHd0RyGEnM3cqcoqg8TewXlUwOkHphCrZ5eFbxxEarVz1wwkZqd5z0
177 3IInE3EJ7D8rxfbC1c1fdeh8akI=
178 -----END CERTIFICATE-----
179 PEM
180  
181 1 ▶sub ca_privkey_as_text { <<'PEM' }
Subroutine ca_privkey_as_text: called CC=1 SCAR=0.0
182 -----BEGIN RSA PRIVATE KEY-----
183 MIICXgIBAAKBgQDfGYmlOYbpGkBD/agTUnixLcdh6H1XM13w17RbzaoA7byZD6L+
184 Dn8MZd69PuXcZAQEUG4Oe6QyAcafsvDb7SHjyJHLoPTOsAZ0ex/0zIJVpw+XyppA
185 8fZx6bnuHKUabqfj83OLk/ACfQSBX7bcL7Y8hwYcZJcqyjMzt9BT7oCldwIDAQAB
186 AoGBANWLUi8uUy4IDH+H6jskc5XUJcZXjLHM3xxKu74rq4/b/uvbBb58DavGTl+C
187 Nu6vZRDkE5QVUOL0xDPUSauY3RerFnMPdTZZ43WAKYbrrNqA0/xEpEAWv4CxXAMI
188 f3Bf2ypBdFzE268HiaQxv//61ZtIjb7NDu8j6gcRLLVjU0RhAkEA9X0TZScqGLFR
189 84GsSltkoSzx2Q+6d81yjzC27CtQgwEUCFhvFG69jAUzJASogac0hmkGa1lVzylO
190 5D2wgL24PwJBAOinC/ey4XE3isah86Kpgfj8yVj5vtLEodBkUmhNOIrgiHt6+QE+
191 5YwreJikzRB2Bs9idglg+f/0nqlLdKLWBMkCQQDZNxvjRD0+biAKa/IMNUQcTU2N
192 +BnRictVIhCpdkYeNOUJ4V4gYUB81dkDhM+pMU8Lo4CXmguQa4ev81nrAHQ3AkBh
193 ffbW4p/0OKkv2Zfl9xBfDVc2sNlVK07/q7qYuJtUHwkybXLBIeFBXsoXdR/1oO/z
194 obgC8B9zMcf2+4ax4et5AkEAsNLkUpS5EmsdlyuUnHxg5jU30o8XSUznmzR7OX/H
195 hP36rGgrE4mclD0LgazRRMjmWFzT6/RtiQb5OnfFxXaDTQ==
196 -----END RSA PRIVATE KEY-----
197 PEM
198  
« examples/make-cert-chain.pl ↑ Summary