1Convert::PEM(3) User Contributed Perl Documentation Convert::PEM(3)
2
3
4
6 Convert::PEM - Read/write encrypted ASN.1 PEM files
7
9 use Convert::PEM;
10 my $pem = Convert::PEM->new(
11 Name => "DSA PRIVATE KEY",
12 ASN => qq(
13 DSAPrivateKey SEQUENCE {
14 version INTEGER,
15 p INTEGER,
16 q INTEGER,
17 g INTEGER,
18 pub_key INTEGER,
19 priv_key INTEGER
20 }
21 ));
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
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
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
74 name 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
87 decoded.
88
89 ASN is a required argument.
90
91 · Macro
92
93 If your ASN.1 description (in the ASN parameter) includes more than
94 one ASN.1 macro definition, you will want to use the Macro
95 parameter to specify which definition to use when encoding/decoding
96 objects. For example, if your ASN.1 description looks like this:
97
98 Foo ::= SEQUENCE {
99 x INTEGER,
100 bar Bar
101 }
102
103 Bar ::= INTEGER
104
105 If you want to encode/decode a "Foo" object, you will need to tell
106 Convert::PEM to use the "Foo" macro definition by using the Macro
107 parameter and setting the value to "Foo".
108
109 Macro is an optional argument.
110
111 $obj = $pem->decode(%args)
112 Decodes, and, optionally, decrypts a PEM file, returning the object as
113 decoded by Convert::ASN1. The difference between this method and read
114 is that read reads the contents of a PEM file on disk; this method
115 expects you to pass the PEM contents as an argument.
116
117 If an error occurs while reading the file or decrypting/decoding the
118 contents, the function returns undef, and you should check the error
119 message using the errstr method (below).
120
121 %args can contain:
122
123 · Content
124
125 The PEM contents.
126
127 · Password
128
129 The password with which the file contents were encrypted.
130
131 If the file is encrypted, this is a mandatory argument (well, it's
132 not strictly mandatory, but decryption isn't going to work without
133 it). Otherwise it's not necessary.
134
135 $blob = $pem->encode(%args)
136 Constructs the contents for the PEM file from an object: ASN.1-encodes
137 the object, optionally encrypts those contents.
138
139 Returns undef on failure (encryption failure, file-writing failure,
140 etc.); in this case you should check the error message using the errstr
141 method (below). On success returns the constructed PEM string.
142
143 %args can contain:
144
145 · Content
146
147 A hash reference that will be passed to Convert::ASN1::encode, and
148 which should correspond to the ASN.1 description you gave to the
149 new method. The hash reference should have the exact same format as
150 that returned from the read method.
151
152 This argument is mandatory.
153
154 · Password
155
156 A password used to encrypt the contents of the PEM file. This is an
157 optional argument; if not provided the contents will be
158 unencrypted.
159
160 $obj = $pem->read(%args)
161 Reads, decodes, and, optionally, decrypts a PEM file, returning the
162 object as decoded by Convert::ASN1. This is implemented as a wrapper
163 around decode, with the bonus of reading the PEM file from disk for
164 you.
165
166 If an error occurs while reading the file or decrypting/decoding the
167 contents, the function returns undef, and you should check the error
168 message using the errstr method (below).
169
170 In addition to the arguments that can be passed to the decode method
171 (minus the Content method), %args can contain:
172
173 · Filename
174
175 The location of the PEM file that you wish to read.
176
177 $pem->write(%args)
178 Constructs the contents for the PEM file from an object: ASN.1-encodes
179 the object, optionally encrypts those contents; then writes the file to
180 disk. This is implemented as a wrapper around encode, with the bonus of
181 writing the file to disk for you.
182
183 Returns undef on failure (encryption failure, file-writing failure,
184 etc.); in this case you should check the error message using the errstr
185 method (below). On success returns the constructed PEM string.
186
187 In addition to the arguments for encode, %args can contain:
188
189 · Filename
190
191 The location on disk where you'd like the PEM file written.
192
193 $pem->errstr
194 Returns the value of the last error that occurred. This should only be
195 considered meaningful when you've received undef from one of the
196 functions above; in all other cases its relevance is undefined.
197
198 $pem->asn
199 Returns the Convert::ASN1 object used internally to decode and encode
200 ASN.1 representations. This is useful when you wish to interact
201 directly with that object; for example, if you need to call configure
202 on that object to set the type of big-integer class to be used when
203 decoding/encoding big integers:
204
205 $pem->asn->configure( decode => { bigint => 'Math::Pari' },
206 encode => { bigint => 'Math::Pari' } );
207
209 If an error occurs in any of the above methods, the method will return
210 "undef". You should then call the method errstr to determine the source
211 of the error:
212
213 $pem->errstr
214
215 In the case that you do not yet have a Convert::PEM object (that is, if
216 an error occurs while creating a Convert::PEM object), the error can be
217 obtained as a class method:
218
219 Convert::PEM->errstr
220
221 For example, if you try to decode an encrypted object, and you do not
222 give a passphrase to decrypt the object:
223
224 my $obj = $pem->read( Filename => "encrypted.pem" )
225 or die "Decryption failed: ", $pem->errstr;
226
228 Benjamin Trott, ben@rhumba.pair.com
229
230 Except where otherwise noted, Convert::PEM is Copyright 2001 Benjamin
231 Trott. All rights reserved. Convert::PEM is free software; you may
232 redistribute it and/or modify it under the same terms as Perl itself.
233
234
235
236perl v5.12.0 2005-05-25 Convert::PEM(3)