| 1 |
NAME |
| 2 |
Convert::PEM - Read/write encrypted ASN.1 PEM files |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Convert::PEM; |
| 6 |
my $pem = Convert::PEM->new( |
| 7 |
Name => "DSA PRIVATE KEY", |
| 8 |
Macro => "DSAPrivateKey", |
| 9 |
ASN => qq( |
| 10 |
DSAPrivateKey SEQUENCE { |
| 11 |
version INTEGER, |
| 12 |
p INTEGER, |
| 13 |
q INTEGER, |
| 14 |
g INTEGER, |
| 15 |
pub_key INTEGER, |
| 16 |
priv_key INTEGER |
| 17 |
} |
| 18 |
)); |
| 19 |
|
| 20 |
my $keyfile = 'private-key.pem'; |
| 21 |
my $pwd = 'foobar'; |
| 22 |
|
| 23 |
my $pkey = $pem->read( |
| 24 |
Filename => $keyfile, |
| 25 |
Password => $pwd |
| 26 |
); |
| 27 |
|
| 28 |
$pem->write( |
| 29 |
Content => $pkey, |
| 30 |
Password => $pwd, |
| 31 |
Filename => $keyfile |
| 32 |
); |
| 33 |
|
| 34 |
DESCRIPTION |
| 35 |
*Convert::PEM* reads and writes PEM files containing ASN.1-encoded |
| 36 |
objects. The files can optionally be encrypted using a symmetric cipher |
| 37 |
algorithm, such as 3DES. An unencrypted PEM file might look something |
| 38 |
like this: |
| 39 |
|
| 40 |
-----BEGIN DH PARAMETERS----- |
| 41 |
MB4CGQDUoLoCULb9LsYm5+/WN992xxbiLQlEuIsCAQM= |
| 42 |
-----END DH PARAMETERS----- |
| 43 |
|
| 44 |
The string beginning "MB4C..." is the Base64-encoded, ASN.1-encoded |
| 45 |
"object." |
| 46 |
|
| 47 |
An encrypted file would have headers describing the type of encryption |
| 48 |
used, and the initialization vector: |
| 49 |
|
| 50 |
-----BEGIN DH PARAMETERS----- |
| 51 |
Proc-Type: 4,ENCRYPTED |
| 52 |
DEK-Info: DES-EDE3-CBC,C814158661DC1449 |
| 53 |
|
| 54 |
AFAZFbnQNrGjZJ/ZemdVSoZa3HWujxZuvBHzHNoesxeyqqidFvnydA== |
| 55 |
-----END DH PARAMETERS----- |
| 56 |
|
| 57 |
The two headers ("Proc-Type" and "DEK-Info") indicate information about |
| 58 |
the type of encryption used, and the string starting with "AFAZ..." is |
| 59 |
the Base64-encoded, encrypted, ASN.1-encoded contents of this "object." |
| 60 |
|
| 61 |
The initialization vector ("C814158661DC1449") is chosen randomly. |
| 62 |
|
| 63 |
USAGE |
| 64 |
$pem = Convert::PEM->new( %arg ) |
| 65 |
Constructs a new *Convert::PEM* object designed to read/write an object |
| 66 |
of a specific type (given in *%arg*, see below). Returns the new object |
| 67 |
on success, "undef" on failure (see *ERROR HANDLING* for details). |
| 68 |
|
| 69 |
*%arg* can contain: |
| 70 |
|
| 71 |
* Name |
| 72 |
|
| 73 |
The name of the object; when decoding a PEM-encoded stream, the name |
| 74 |
in the encoding will be checked against the value of *Name*. |
| 75 |
Similarly, when encoding an object, the value of *Name* will be used |
| 76 |
as the name of the object in the PEM-encoded content. For example, |
| 77 |
given the string "FOO BAR", the output from *encode* will start with |
| 78 |
a header like: |
| 79 |
|
| 80 |
-----BEGIN FOO BAR----- |
| 81 |
|
| 82 |
*Name* is a required argument. |
| 83 |
|
| 84 |
* ASN |
| 85 |
|
| 86 |
An ASN.1 description of the content to be either encoded or decoded. |
| 87 |
|
| 88 |
*ASN* is an optional argument. |
| 89 |
|
| 90 |
* Macro |
| 91 |
|
| 92 |
If your ASN.1 description (in the *ASN* parameter) includes more |
| 93 |
than one ASN.1 macro definition, you will want to use the *Macro* |
| 94 |
parameter to specify which definition to use when encoding/decoding |
| 95 |
objects. For example, if your ASN.1 description looks like this: |
| 96 |
|
| 97 |
Foo ::= SEQUENCE { |
| 98 |
x INTEGER, |
| 99 |
bar Bar |
| 100 |
} |
| 101 |
|
| 102 |
Bar ::= INTEGER |
| 103 |
|
| 104 |
If you want to encode/decode a "Foo" object, you will need to tell |
| 105 |
*Convert::PEM* to use the "Foo" macro definition by using the |
| 106 |
*Macro* parameter and setting the value to "Foo". |
| 107 |
|
| 108 |
*Macro* is an optional argument when an ASN.1 description is |
| 109 |
provided. |
| 110 |
|
| 111 |
* InForm |
| 112 |
|
| 113 |
Specify what type of file to expect when using the *read* method. |
| 114 |
Value may be either PEM or DER. Default is "PEM". |
| 115 |
|
| 116 |
If "DER" is specified, encryption options are ignored when using the |
| 117 |
*read* method and file is read as an unencrypted blob. This option |
| 118 |
does not affect the *decode* behavior. |
| 119 |
|
| 120 |
*InForm* is an optional argument. |
| 121 |
|
| 122 |
* OutForm |
| 123 |
|
| 124 |
Specify what type of file the *write* method should output. Value |
| 125 |
may be either PEM or DER. Default is "PEM". |
| 126 |
|
| 127 |
If "DER" is specified, encryption options are ignored when using the |
| 128 |
*write* method and the file is written as an uncrypted blob. This |
| 129 |
option does not affect the *encode* behavior. |
| 130 |
|
| 131 |
*OutForm* is an optional argument. |
| 132 |
|
| 133 |
$obj = $pem->decode(%args) |
| 134 |
Decodes, and, optionally, decrypts a PEM file, returning the object as |
| 135 |
decoded by *Convert::ASN1*. The difference between this method and |
| 136 |
*read* is that *read* reads the contents of a PEM file on disk; this |
| 137 |
method expects you to pass the PEM contents as an argument. |
| 138 |
|
| 139 |
If an error occurs while reading the file or decrypting/decoding the |
| 140 |
contents, the function returns *undef*, and you should check the error |
| 141 |
message using the *errstr* method (below). |
| 142 |
|
| 143 |
*%args* can contain: |
| 144 |
|
| 145 |
* Content |
| 146 |
|
| 147 |
The PEM contents. |
| 148 |
|
| 149 |
* Password |
| 150 |
|
| 151 |
The password with which the file contents were encrypted. |
| 152 |
|
| 153 |
If the file is encrypted, this is a mandatory argument (well, it's |
| 154 |
not strictly mandatory, but decryption isn't going to work without |
| 155 |
it). Otherwise it's not necessary. |
| 156 |
|
| 157 |
$blob = $pem->encode(%args) |
| 158 |
Constructs the contents for the PEM file from an object: ASN.1-encodes |
| 159 |
the object, optionally encrypts those contents. |
| 160 |
|
| 161 |
Returns *undef* on failure (encryption failure, file-writing failure, |
| 162 |
etc.); in this case you should check the error message using the |
| 163 |
*errstr* method (below). On success returns the constructed PEM string. |
| 164 |
|
| 165 |
*%args* can contain: |
| 166 |
|
| 167 |
* Content |
| 168 |
|
| 169 |
This method requires either Content or DER. An error will be |
| 170 |
generated if one of these arguments are not present. |
| 171 |
|
| 172 |
A hash reference that will be passed to *Convert::ASN1::encode*, and |
| 173 |
which should correspond to the ASN.1 description you gave to the |
| 174 |
*new* method. The hash reference should have the exact same format |
| 175 |
as that returned from the *read* method. |
| 176 |
|
| 177 |
This is required unless DER is specified. |
| 178 |
|
| 179 |
* DER |
| 180 |
|
| 181 |
A string containing actual binary of the contents to be encoded. |
| 182 |
This bypasses ASN.1 encoding. |
| 183 |
|
| 184 |
May be used in lieu of Content. If specified, will override Content. |
| 185 |
|
| 186 |
* Password |
| 187 |
|
| 188 |
A password used to encrypt the contents of the PEM file. This is an |
| 189 |
optional argument; if not provided the contents will be unencrypted. |
| 190 |
|
| 191 |
* Cipher |
| 192 |
|
| 193 |
The Cipher to use if a password is provided. This is an optional |
| 194 |
argument; if not provided, the default of DES-EDE3-CBC will be used |
| 195 |
or the cipher configured is $Convert::PEM::DefaultCipher. See below |
| 196 |
for a list of supported ciphers. |
| 197 |
|
| 198 |
$obj = $pem->read(%args) |
| 199 |
Reads, decodes, and, optionally, decrypts a PEM file, returning the |
| 200 |
object as decoded by *Convert::ASN1* (or binary blob if ASN.1 |
| 201 |
description was not provided). This is implemented as a wrapper around |
| 202 |
*decode*, with the bonus of reading the PEM file from disk for you. |
| 203 |
|
| 204 |
If an error occurs while reading the file or decrypting/decoding the |
| 205 |
contents, the function returns *undef*, and you should check the error |
| 206 |
message using the *errstr* method (below). |
| 207 |
|
| 208 |
In addition to the arguments that can be passed to the *decode* method |
| 209 |
(minus the *Content* argument), *%args* can contain: |
| 210 |
|
| 211 |
* Filename |
| 212 |
|
| 213 |
The location of the PEM file that you wish to read. |
| 214 |
|
| 215 |
* InForm |
| 216 |
|
| 217 |
Specify what file type to read. Description can be found under *new* |
| 218 |
method. If specified, will override InForm provided in *new* method. |
| 219 |
|
| 220 |
$pem->write(%args) |
| 221 |
Constructs the contents for the PEM file from an object: ASN.1-encodes |
| 222 |
the object, optionally encrypts those contents; then writes the file to |
| 223 |
disk. This is implemented as a wrapper around *encode*, with the bonus |
| 224 |
of writing the file to disk for you. |
| 225 |
|
| 226 |
Returns *undef* on failure (encryption failure, file-writing failure, |
| 227 |
etc.); in this case you should check the error message using the |
| 228 |
*errstr* method (below). On success returns the constructed PEM string. |
| 229 |
|
| 230 |
In addition to the arguments for *encode*, *%args* can contain: |
| 231 |
|
| 232 |
* Filename |
| 233 |
|
| 234 |
The location on disk where you'd like the PEM file written. |
| 235 |
|
| 236 |
* OutForm |
| 237 |
|
| 238 |
Specify format to write out. Description can be found under *new* |
| 239 |
method. If specified, will override OutForm provided in *new* |
| 240 |
method. |
| 241 |
|
| 242 |
$pem->from_der(%args) |
| 243 |
Method used internally, but may be accessed directly decode an ASN.1 |
| 244 |
string into a perl structure object. If the Convert::PEM object has no |
| 245 |
ASN.1 definition, this method has no effect. |
| 246 |
|
| 247 |
* DER |
| 248 |
|
| 249 |
Binary string to convert to an object. |
| 250 |
|
| 251 |
This option is required. |
| 252 |
|
| 253 |
* Macro |
| 254 |
|
| 255 |
If the object has an ASN definition, a Macro may be specified. If |
| 256 |
specified, it will override the object's Macro if one exists. |
| 257 |
|
| 258 |
Macro is an optional argument. |
| 259 |
|
| 260 |
$pem->to_der(%args) |
| 261 |
Method used internally, but may be accessed directly to encode Content |
| 262 |
into binary data. If the Convert::PEM object has no ASN.1 definition, |
| 263 |
this method has no effect. |
| 264 |
|
| 265 |
* Content |
| 266 |
|
| 267 |
An object to be ASN.1 encoded to a binary string. |
| 268 |
|
| 269 |
* Macro |
| 270 |
|
| 271 |
If the object has an ASN definition, a Macro may be specified. If |
| 272 |
specified, it will override the object's Macro if one exists. |
| 273 |
|
| 274 |
Macro is an optional argument. |
| 275 |
|
| 276 |
$pem->errstr |
| 277 |
Returns the value of the last error that occurred. This should only be |
| 278 |
considered meaningful when you've received *undef* from one of the |
| 279 |
functions above; in all other cases its relevance is undefined. |
| 280 |
|
| 281 |
$pem->asn |
| 282 |
Returns the *Convert::ASN1* object used internally to decode and encode |
| 283 |
ASN.1 representations. This is useful when you wish to interact directly |
| 284 |
with that object; for example, if you need to call *configure* on that |
| 285 |
object to set the type of big-integer class to be used when |
| 286 |
decoding/encoding big integers: |
| 287 |
|
| 288 |
$pem->asn->configure( decode => { bigint => 'Math::Pari' }, |
| 289 |
encode => { bigint => 'Math::Pari' } ); |
| 290 |
|
| 291 |
CONFIGURATION |
| 292 |
To support any encryption/decryption, the appropriate cipher module |
| 293 |
needs to be installed. |
| 294 |
|
| 295 |
Some settings may be viewed or configured through variables or methods. |
| 296 |
|
| 297 |
Configuration settings are global to the package. If a setting is |
| 298 |
changed, it affects all Convert::PEM objects. |
| 299 |
|
| 300 |
$Convert::PEM::DefaultCipher *or* $OBJ->DefaultCipher(*[NEW_CIPHER]*) |
| 301 |
Used to configure a default cipher when writing to the disk. When using |
| 302 |
the method form " $OBJ-"DefaultCipher([NEW_CIPHER]) >, if NEW_CIPHER is |
| 303 |
not specified, will return the current setting. If the specified cipher |
| 304 |
is not recognized/valid, an error will be raised. |
| 305 |
|
| 306 |
To list supported ciphers, use "Convert::PEM::list_ciphers". Here is a |
| 307 |
list of supported Ciphers: |
| 308 |
|
| 309 |
* DES-CBC |
| 310 |
|
| 311 |
* DES-EDE3-CBC |
| 312 |
|
| 313 |
* AES-128-CBC |
| 314 |
|
| 315 |
* AES-192-CBC |
| 316 |
|
| 317 |
* AES-256-CBC |
| 318 |
|
| 319 |
* CAMELLIA-128-CBC |
| 320 |
|
| 321 |
* CAMELLIA-192-CBC |
| 322 |
|
| 323 |
* CAMELLIA-256-CBC |
| 324 |
|
| 325 |
* IDEA-CBC |
| 326 |
|
| 327 |
* SEED-CBC |
| 328 |
|
| 329 |
Convert::PEM->has_cipher(*$cipher_name*) |
| 330 |
Will see if the cipher is supported and is configured with an encryption |
| 331 |
module. |
| 332 |
|
| 333 |
Convert::PEM->has_cipher_module(*$cipher_name*) |
| 334 |
Will see if the cipher is supported and if the configured encryption |
| 335 |
module is usable. If it is not usable, will return "undef". If it is |
| 336 |
usable, will return the name of the cipher module. |
| 337 |
|
| 338 |
Convert::PEM->set_cipher_module($cipher,$module[,$all]) |
| 339 |
This function/method is used to specify a module name for a supported |
| 340 |
cipher. It accepts 2 or 3 arguments. |
| 341 |
|
| 342 |
Convert::PEM->set_cipher_module(<cipher_name>, <module_name>[,0]) |
| 343 |
or |
| 344 |
$OBJ->set_cipher_module(<cipher_name>, <module_name>[,0]) |
| 345 |
|
| 346 |
"cipher_name" |
| 347 |
A supported cipher name. Use Convert::PEM::list_ciphers() to |
| 348 |
retrieve a list of supported ciphers. |
| 349 |
|
| 350 |
"module_name" |
| 351 |
A cipher module. The module must support the following methods: |
| 352 |
|
| 353 |
$cipher_object = Cipher->new($key) |
| 354 |
$cipher_object->encrypt($plaintext) |
| 355 |
$cipher_object->decrypt($ciphertext) |
| 356 |
$cipher_object->blocksize() |
| 357 |
|
| 358 |
"all" |
| 359 |
An optional boolean argument. If true will replace the modules for |
| 360 |
all supported ciphers matching the cipher being set. Default is |
| 361 |
true. If setting a cipher, only set this to false if it is desired |
| 362 |
to use a separate cipher for different key lengths of the same |
| 363 |
algorithm. |
| 364 |
|
| 365 |
Convert::PEM->list_cipher_modules([$cipher_name]) |
| 366 |
If a *cipher_name* is provided, will return the module configured for |
| 367 |
the matching cipher name or "undef" if cipher is not supported. If |
| 368 |
*cipher_name* is not provided, will return a list of modules names |
| 369 |
configured as an array in array context or as a colon separated list in |
| 370 |
scalar context. |
| 371 |
|
| 372 |
Here is a list of the cipher modules used by default. |
| 373 |
|
| 374 |
* Crypt::DES |
| 375 |
|
| 376 |
* Crypt::DES_EDE3 |
| 377 |
|
| 378 |
* Crypt::Rijndael - "AES-128-CBC, AES-192-CBC and AES-256-CBC" |
| 379 |
|
| 380 |
* Crypt::Camellia - "CAMELLIA-128-CBC, CAMELLIA-192-CBC and |
| 381 |
CAMELLIA-256-CBC" |
| 382 |
|
| 383 |
* Crypt::IDEA |
| 384 |
|
| 385 |
* Crypt::SEED |
| 386 |
|
| 387 |
ERROR HANDLING |
| 388 |
If an error occurs in any of the above methods, the method will return |
| 389 |
"undef". You should then call the method *errstr* to determine the |
| 390 |
source of the error: |
| 391 |
|
| 392 |
$pem->errstr |
| 393 |
|
| 394 |
In the case that you do not yet have a *Convert::PEM* object (that is, |
| 395 |
if an error occurs while creating a *Convert::PEM* object), the error |
| 396 |
can be obtained as a class method: |
| 397 |
|
| 398 |
Convert::PEM->errstr |
| 399 |
|
| 400 |
For example, if you try to decode an encrypted object, and you do not |
| 401 |
give a passphrase to decrypt the object: |
| 402 |
|
| 403 |
my $obj = $pem->read( Filename => "encrypted.pem" ) |
| 404 |
or die "Decryption failed: ", $pem->errstr; |
| 405 |
|
| 406 |
LICENSE |
| 407 |
Convert::PEM is free software; you may redistribute it and/or modify it |
| 408 |
under the same terms as Perl itself. |
| 409 |
|
| 410 |
AUTHOR & COPYRIGHTS |
| 411 |
Except where otherwise noted, Convert::PEM is Copyright Benjamin Trott, |
| 412 |
[email protected]. All rights reserved. |
| 413 |
|