| 1 | #!perl -w | ||
| 2 | # -*- coding: utf-8; -*- | ||
| 3 | |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use utf8; # This source code contains UTF-8 characters. |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | make-cert-chain.pl - Make an RSA X509 certificate chain using | ||
| 11 | L<Crypt::OpenSSL::CA> | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This example code walks the reader through using L<Crypt::OpenSSL::CA> | ||
| 16 | to create X509 certificates. We demonstrate creating a self-signed | ||
| 17 | Certification Authority (CA) certificate, and a user certificate | ||
| 18 | signed by that CA. Both are printed to standard output as text | ||
| 19 | strings in L<Crypt::OpenSSL::CA::AlphabetSoup/PEM> format. | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | 1 | ▶ | use Crypt::OpenSSL::CA; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | |||
| 25 | =head1 CA SELF-SIGNATURE | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | { | ||
| 30 | |||
| 31 | =head2 Private Key | ||
| 32 | |||
| 33 | Crypt::OpenSSL::CA doesn't create private keys by itself, but the | ||
| 34 | openssl command-line tool can whip them up presto. For additional | ||
| 35 | style points, let's say the CA's key is to be password-protected. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | 1 | our $ca_privkey_as_text = `openssl genrsa -passout pass:secret 1024`; | |
| 40 | |||
| 41 | 1 | my $ca_privkey = Crypt::OpenSSL::CA::PrivateKey-> | |
| 42 | parse($ca_privkey_as_text, -password => "secret"); | ||
| 43 | |||
| 44 | =head2 Certificate | ||
| 45 | |||
| 46 | The CA certificate is filled out field after field, starting with the | ||
| 47 | RSA public key. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | 1 | my $ca_pubkey = $ca_privkey->get_public_key; | |
| 52 | 1 | my $ca_cert = Crypt::OpenSSL::CA::X509->new($ca_pubkey); | |
| 53 | 1 | my $ca_serial = "0x1"; | |
| 54 | 1 | $ca_cert->set_serial($ca_serial); | |
| 55 | 1 | $ca_cert->set_notBefore("20080204101500Z"); | |
| 56 | 1 | $ca_cert->set_notAfter("22080204101500Z"); | |
| 57 | |||
| 58 | =pod | ||
| 59 | |||
| 60 | L<Crypt::OpenSSL::CA::AlphabetSoup/DN>s can be provided literally; | ||
| 61 | simply be careful to the DN order. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | 1 | my $ca_dn = Crypt::OpenSSL::CA::X509_NAME->new | |
| 66 | (C => "au", OU => "Yoyodyne Inc", CN => "test CA"); | ||
| 67 | |||
| 68 | 1 | $ca_cert->set_issuer_DN($ca_dn); | |
| 69 | 1 | $ca_cert->set_subject_DN($ca_dn); | |
| 70 | |||
| 71 | =pod | ||
| 72 | |||
| 73 | Note that we need to set some extensions for that CA certificate in | ||
| 74 | order for it to be standards-compliant; see | ||
| 75 | L<Crypt::OpenSSL::CA::Resources> if you are new to that whole shebang. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | 1 | $ca_cert->set_extension("basicConstraints", "CA:TRUE", -critical => 1); | |
| 80 | |||
| 81 | =pod | ||
| 82 | |||
| 83 | Okay, we don't really need key identifiers for standards compliance; | ||
| 84 | they're just here because it's fun to copy them over to the client | ||
| 85 | certificate later on. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | 1 | my $ca_keyid = $ca_pubkey->get_openssl_keyid; | |
| 90 | 1 | $ca_cert->set_extension("subjectKeyIdentifier", $ca_keyid); | |
| 91 | 1 | $ca_cert->set_extension("authorityKeyIdentifier" => | |
| 92 | { | ||
| 93 | keyid => $ca_keyid, | ||
| 94 | issuer => $ca_dn, | ||
| 95 | serial => $ca_serial | ||
| 96 | }); | ||
| 97 | |||
| 98 | =pod | ||
| 99 | |||
| 100 | Sign it, and the certificate is ready! | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | 1 | our $ca_cert_as_text = $ca_cert->sign($ca_privkey, "sha1"); | |
| 105 | 1 | print $ca_cert_as_text; | |
| 106 | } | ||
| 107 | |||
| 108 | =head1 USER CERTIFICATE SIGNING REQUEST | ||
| 109 | |||
| 110 | We are going to play by the rules, and generate the user's certificate from | ||
| 111 | public data only; here, using a PKCS#10 Certificate Signing Request | ||
| 112 | (L<Crypt::OpenSSL::CA::AlphabetSoup/CSR>; | ||
| 113 | L<Crypt::OpenSSL::CA::AlphabetSoup/SPKAC> is also supported). Again | ||
| 114 | L<Crypt::OpenSSL::CA> cannot fabricate PKCS#10's directly, but you could create | ||
| 115 | one with something like | ||
| 116 | |||
| 117 | openssl req -nodes -batch -newkey rsa:1024 -keyout userkey.pem -out user.p10 | ||
| 118 | |||
| 119 | Standards-savvy readers know that PKCS#10 requests can contain other | ||
| 120 | fields than the public key. However, L<Crypt::OpenSSL::CA> ignores | ||
| 121 | them entirely. | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | { | ||
| 126 | 1 | my $user_csr_as_text = <<"PKCS10"; | |
| 127 | -----BEGIN CERTIFICATE REQUEST----- | ||
| 128 | MIIBhDCB7gIBADBFMQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEh | ||
| 129 | MB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEB | ||
| 130 | AQUAA4GNADCBiQKBgQCwoel30tZE9ItO0wfQWx3jGFpMLo41iFhFrqlweTJ7iacM | ||
| 131 | bq58tmpDjEONxhqLkNzm05nb2pylskWzKwLQ9NXvchkzK31HKyp89thiVL7ILClV | ||
| 132 | YRYMz4QLeB75W+xl6q2pcClQ3NrN7CrR9czvmVFOXNWKWxyQXYi2Ad0qVvNF+wID | ||
| 133 | AQABoAAwDQYJKoZIhvcNAQEFBQADgYEAFL1txli+LGSS4V1sVSRdMh054QVk9TKY | ||
| 134 | 50HTKYR44aCax3fDcnp4H7jR5QEHX0TeHCC5cr8cDDWLEYmCb0UBXr70czrap3n2 | ||
| 135 | Du3EgKJUHSURsNbkSHSBKupLrw9Ygmipl4vvHRAX59Bqbz4LGEhALnx0eiwK1TtQ | ||
| 136 | mk7h7g7cYc8= | ||
| 137 | -----END CERTIFICATE REQUEST----- | ||
| 138 | PKCS10 | ||
| 139 | |||
| 140 | 1 | my $user_pubkey = Crypt::OpenSSL::CA::PublicKey-> | |
| 141 | validate_PKCS10($user_csr_as_text); | ||
| 142 | |||
| 143 | =head1 USER CERTIFICATE CREATION | ||
| 144 | |||
| 145 | In a real PKI this would probably not take place in the same process | ||
| 146 | as the L</CA SELF-SIGNATURE> step. So let's pretend that we are | ||
| 147 | reloading from serialized state (ie PEM strings) instead of re-using | ||
| 148 | the objects created above. (To keep ourselves honest, we used curly | ||
| 149 | braces in the Perl script to segregate both signing operations into | ||
| 150 | different lexical scopes.) | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | 1 | my $ca_cert = Crypt::OpenSSL::CA::X509-> | |
| 155 | parse(our $ca_cert_as_text); | ||
| 156 | 1 | my $ca_privkey = Crypt::OpenSSL::CA::PrivateKey-> | |
| 157 | parse(our $ca_privkey_as_text, -password => "secret"); | ||
| 158 | |||
| 159 | =pod | ||
| 160 | |||
| 161 | In order for the user certificate and the CA certificate to chain | ||
| 162 | properly, certain fields in the latter must match those in the former. | ||
| 163 | L<Crypt::OpenSSL::CA> supports enough of X509 parsing that we are able | ||
| 164 | to extract those from an existing PEM certificate. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | 1 | my $ca_dn = $ca_cert->get_subject_DN(); | |
| 169 | 1 | my $ca_keyid = $ca_cert->get_subject_keyid; | |
| 170 | |||
| 171 | =head2 Certificate Fields and Extensions | ||
| 172 | |||
| 173 | This time, let'use a Christmas-treeish set of extensions to | ||
| 174 | demonstrate the possibilities of the API. For the X509-savvy, here is | ||
| 175 | some babble about what L<Crypt::OpenSSL::CA> can do: the X509 version | ||
| 176 | is always X509v3. Long serial numbers are supported. DNs enjoy full | ||
| 177 | UTF-8 support. The validity period (notBefore and notAfter) can be of | ||
| 178 | arbitrary size, and transition from utcTime to generalizedTime is | ||
| 179 | handled properly. SubjectAltNames are fully supported regardless of | ||
| 180 | type and multiplicity; ditto for certificate policies. The signature | ||
| 181 | algorithm is RSA and the hash can be set to any of OpenSSL's supported | ||
| 182 | hashes. OpenSSL's algorithm for RSA key fingerprints (also known as | ||
| 183 | X509 KeyIDs) is available (but not mandatory) for the subject and | ||
| 184 | authority key identifiers. | ||
| 185 | |||
| 186 | =cut | ||
| 187 | |||
| 188 | 1 | my $user_cert = Crypt::OpenSSL::CA::X509->new($user_pubkey); | |
| 189 | |||
| 190 | 1 | my $user_dn = Crypt::OpenSSL::CA::X509_NAME->new_utf8 | |
| 191 | (C => "fr", O => "zoinxé", | ||
| 192 | OU => "☮☺⌨", # Peace, joy, coding :-) | ||
| 193 | CN => "test user cert"); | ||
| 194 | |||
| 195 | 1 | $user_cert->set_issuer_DN($ca_dn); | |
| 196 | 1 | $user_cert->set_subject_DN($user_dn); | |
| 197 | |||
| 198 | 1 | $user_cert->set_serial("0x1234567890abcdef1234567890ABCDEF"); | |
| 199 | |||
| 200 | 1 | $user_cert->set_notBefore("20080204114600Z"); | |
| 201 | 1 | $user_cert->set_notAfter("21060108000000Z"); | |
| 202 | 1 | $user_cert->set_extension("basicConstraints", "CA:FALSE", | |
| 203 | -critical => 1); | ||
| 204 | |||
| 205 | 1 | $user_cert->set_extension("authorityKeyIdentifier", | |
| 206 | { keyid => $ca_keyid }, | ||
| 207 | -critical => 0); # As per RFC3280 section 4.2.1.1 | ||
| 208 | 1 | $user_cert->set_extension( subjectKeyIdentifier => | |
| 209 | "00:DE:AD:BE:EF"); # Hey, why not? | ||
| 210 | |||
| 211 | 1 | $user_cert->set_extension(certificatePolicies => | |
| 212 | 'ia5org,1.2.3.4,1.5.6.7.8,@polsect', | ||
| 213 | -critical => 0, | ||
| 214 | polsect => { | ||
| 215 | policyIdentifier => '1.3.5.8', | ||
| 216 | "CPS.1" => 'http://my.host.name/', | ||
| 217 | "CPS.2" => 'http://my.your.name/', | ||
| 218 | "userNotice.1" => '@notice', | ||
| 219 | }, | ||
| 220 | notice => { | ||
| 221 | explicitText => "Explicit Text Here", | ||
| 222 | organization => "Organisation Name", | ||
| 223 | noticeNumbers => '1,2,3,4', | ||
| 224 | }); | ||
| 225 | |||
| 226 | 1 | $user_cert->set_extension | |
| 227 | (subjectAltName => 'email:johndoe@example.com,email:johndoe@example.net'); | ||
| 228 | |||
| 229 | 1 | my $fancy_digest_alg = "ripemd160"; # I'd use "sha256" myself, but | |
| 230 | # some old builds of OpenSSL don't have it. | ||
| 231 | 1 | warn "And here is a certificate using $fancy_digest_alg as the digest!\n"; | |
| 232 | |||
| 233 | 1 | our $user_cert_as_text = $user_cert->sign($ca_privkey, $fancy_digest_alg); | |
| 234 | 1 | print $user_cert_as_text; | |
| 235 | } | ||
| 236 | |||
| 237 | 1 | exit 0; | |