Skip to content

Commit d22769a

Browse files
committed
[DOC] enhance RDoc for exporting pkeys
Describe the behavior of OpenSSL::PKey::{DH,DSA,EC,RSA}#to_pem and #to_der more clearly. They return a different result depending on whether the pkey is a public or private key. This was not documented adequately. Also, suggest the use of OpenSSL::PKey::PKey#private_to_pem and #public_to_pem instead, if possible.
1 parent 5631203 commit d22769a

File tree

5 files changed

+219
-26
lines changed

5 files changed

+219
-26
lines changed

ext/openssl/ossl_pkey.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,18 @@ ossl_pkey_private_to_der(int argc, VALUE *argv, VALUE self)
809809
*
810810
* Serializes the private key to PEM-encoded PKCS #8 format. See #private_to_der
811811
* for more details.
812+
*
813+
* An unencrypted PEM-encoded key will look like:
814+
*
815+
* -----BEGIN PRIVATE KEY-----
816+
* [...]
817+
* -----END PRIVATE KEY-----
818+
*
819+
* An encrypted PEM-encoded key will look like:
820+
*
821+
* -----BEGIN ENCRYPTED PRIVATE KEY-----
822+
* [...]
823+
* -----END ENCRYPTED PRIVATE KEY-----
812824
*/
813825
static VALUE
814826
ossl_pkey_private_to_pem(int argc, VALUE *argv, VALUE self)
@@ -858,6 +870,12 @@ ossl_pkey_public_to_der(VALUE self)
858870
* pkey.public_to_pem -> string
859871
*
860872
* Serializes the public key to PEM-encoded X.509 SubjectPublicKeyInfo format.
873+
*
874+
* A PEM-encoded key will look like:
875+
*
876+
* -----BEGIN PUBLIC KEY-----
877+
* [...]
878+
* -----END PUBLIC KEY-----
861879
*/
862880
static VALUE
863881
ossl_pkey_public_to_pem(VALUE self)

ext/openssl/ossl_pkey_dh.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,20 @@ ossl_dh_is_private(VALUE self)
216216
* dh.to_pem -> aString
217217
* dh.to_s -> aString
218218
*
219-
* Encodes this DH to its PEM encoding. Note that any existing per-session
220-
* public/private keys will *not* get encoded, just the Diffie-Hellman
221-
* parameters will be encoded.
219+
* Serializes the DH parameters to a PEM-encoding.
220+
*
221+
* Note that any existing per-session public/private keys will *not* get
222+
* encoded, just the Diffie-Hellman parameters will be encoded.
223+
*
224+
* PEM-encoded parameters will look like:
225+
*
226+
* -----BEGIN DH PARAMETERS-----
227+
* [...]
228+
* -----END DH PARAMETERS-----
229+
*
230+
* See also #public_to_pem (X.509 SubjectPublicKeyInfo) and
231+
* #private_to_pem (PKCS #8 PrivateKeyInfo or EncryptedPrivateKeyInfo) for
232+
* serialization with the private or public key components.
222233
*/
223234
static VALUE
224235
ossl_dh_export(VALUE self)
@@ -244,10 +255,14 @@ ossl_dh_export(VALUE self)
244255
* call-seq:
245256
* dh.to_der -> aString
246257
*
247-
* Encodes this DH to its DER encoding. Note that any existing per-session
248-
* public/private keys will *not* get encoded, just the Diffie-Hellman
249-
* parameters will be encoded.
250-
258+
* Serializes the DH parameters to a DER-encoding
259+
*
260+
* Note that any existing per-session public/private keys will *not* get
261+
* encoded, just the Diffie-Hellman parameters will be encoded.
262+
*
263+
* See also #public_to_der (X.509 SubjectPublicKeyInfo) and
264+
* #private_to_der (PKCS #8 PrivateKeyInfo or EncryptedPrivateKeyInfo) for
265+
* serialization with the private or public key components.
251266
*/
252267
static VALUE
253268
ossl_dh_to_der(VALUE self)

ext/openssl/ossl_pkey_dsa.c

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,58 @@ ossl_dsa_is_private(VALUE self)
211211
* dsa.to_pem([cipher, password]) -> aString
212212
* dsa.to_s([cipher, password]) -> aString
213213
*
214-
* Encodes this DSA to its PEM encoding.
214+
* Serializes a private or public key to a PEM-encoding.
215215
*
216-
* === Parameters
217-
* * _cipher_ is an OpenSSL::Cipher.
218-
* * _password_ is a string containing your password.
216+
* [When the key contains public components only]
219217
*
220-
* === Examples
221-
* DSA.to_pem -> aString
222-
* DSA.to_pem(cipher, 'mypassword') -> aString
218+
* Serializes it into an X.509 SubjectPublicKeyInfo.
219+
* The parameters _cipher_ and _password_ are ignored.
223220
*
221+
* A PEM-encoded key will look like:
222+
*
223+
* -----BEGIN PUBLIC KEY-----
224+
* [...]
225+
* -----END PUBLIC KEY-----
226+
*
227+
* Consider using #public_to_pem instead. This serializes the key into an
228+
* X.509 SubjectPublicKeyInfo regardless of whether it is a public key
229+
* or a private key.
230+
*
231+
* [When the key contains private components, and no parameters are given]
232+
*
233+
* Serializes it into a traditional \OpenSSL DSAPrivateKey.
234+
*
235+
* A PEM-encoded key will look like:
236+
*
237+
* -----BEGIN DSA PRIVATE KEY-----
238+
* [...]
239+
* -----END DSA PRIVATE KEY-----
240+
*
241+
* [When the key contains private components, and _cipher_ and _password_ are given]
242+
*
243+
* Serializes it into a traditional \OpenSSL DSAPrivateKey and encrypts it in
244+
* OpenSSL's traditional PEM encryption format.
245+
* _cipher_ must be a cipher name understood by OpenSSL::Cipher.new or an
246+
* instance of OpenSSL::Cipher.
247+
*
248+
* An encrypted PEM-encoded key will look like:
249+
*
250+
* -----BEGIN DSA PRIVATE KEY-----
251+
* Proc-Type: 4,ENCRYPTED
252+
* DEK-Info: AES-128-CBC,733F5302505B34701FC41F5C0746E4C0
253+
*
254+
* [...]
255+
* -----END DSA PRIVATE KEY-----
256+
*
257+
* Note that this format uses MD5 to derive the encryption key, and hence
258+
* will not be available on FIPS-compliant systems.
259+
*
260+
* <b>This method is kept for compatibility.</b>
261+
* This should only be used when the traditional, non-standard \OpenSSL format
262+
* is required.
263+
*
264+
* Consider using #public_to_pem (X.509 SubjectPublicKeyInfo) or #private_to_pem
265+
* (PKCS #8 PrivateKeyInfo or EncryptedPrivateKeyInfo) instead.
224266
*/
225267
static VALUE
226268
ossl_dsa_export(int argc, VALUE *argv, VALUE self)
@@ -238,8 +280,15 @@ ossl_dsa_export(int argc, VALUE *argv, VALUE self)
238280
* call-seq:
239281
* dsa.to_der -> aString
240282
*
241-
* Encodes this DSA to its DER encoding.
283+
* Serializes a private or public key to a DER-encoding.
284+
*
285+
* See #to_pem for details.
286+
*
287+
* <b>This method is kept for compatibility.</b>
288+
* This should only be used when the traditional, non-standard \OpenSSL format
289+
* is required.
242290
*
291+
* Consider using #public_to_der or #private_to_der instead.
243292
*/
244293
static VALUE
245294
ossl_dsa_to_der(VALUE self)

ext/openssl/ossl_pkey_ec.c

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,61 @@ static VALUE ossl_ec_key_is_private(VALUE self)
400400

401401
/*
402402
* call-seq:
403-
* key.export([cipher, pass_phrase]) => String
404-
* key.to_pem([cipher, pass_phrase]) => String
403+
* key.export([cipher, password]) => String
404+
* key.to_pem([cipher, password]) => String
405405
*
406-
* Outputs the EC key in PEM encoding. If _cipher_ and _pass_phrase_ are given
407-
* they will be used to encrypt the key. _cipher_ must be an OpenSSL::Cipher
408-
* instance. Note that encryption will only be effective for a private key,
409-
* public keys will always be encoded in plain text.
406+
* Serializes a private or public key to a PEM-encoding.
407+
*
408+
* [When the key contains public components only]
409+
*
410+
* Serializes it into an X.509 SubjectPublicKeyInfo.
411+
* The parameters _cipher_ and _password_ are ignored.
412+
*
413+
* A PEM-encoded key will look like:
414+
*
415+
* -----BEGIN PUBLIC KEY-----
416+
* [...]
417+
* -----END PUBLIC KEY-----
418+
*
419+
* Consider using #public_to_pem instead. This serializes the key into an
420+
* X.509 SubjectPublicKeyInfo regardless of whether it is a public key
421+
* or a private key.
422+
*
423+
* [When the key contains private components, and no parameters are given]
424+
*
425+
* Serializes it into a SEC 1/RFC 5915 ECPrivateKey.
426+
*
427+
* A PEM-encoded key will look like:
428+
*
429+
* -----BEGIN EC PRIVATE KEY-----
430+
* [...]
431+
* -----END EC PRIVATE KEY-----
432+
*
433+
* [When the key contains private components, and _cipher_ and _password_ are given]
434+
*
435+
* Serializes it into a SEC 1/RFC 5915 ECPrivateKey
436+
* and encrypts it in OpenSSL's traditional PEM encryption format.
437+
* _cipher_ must be a cipher name understood by OpenSSL::Cipher.new or an
438+
* instance of OpenSSL::Cipher.
439+
*
440+
* An encrypted PEM-encoded key will look like:
441+
*
442+
* -----BEGIN EC PRIVATE KEY-----
443+
* Proc-Type: 4,ENCRYPTED
444+
* DEK-Info: AES-128-CBC,733F5302505B34701FC41F5C0746E4C0
445+
*
446+
* [...]
447+
* -----END EC PRIVATE KEY-----
448+
*
449+
* Note that this format uses MD5 to derive the encryption key, and hence
450+
* will not be available on FIPS-compliant systems.
451+
*
452+
* <b>This method is kept for compatibility.</b>
453+
* This should only be used when the SEC 1/RFC 5915 ECPrivateKey format is
454+
* required.
455+
*
456+
* Consider using #public_to_pem (X.509 SubjectPublicKeyInfo) or #private_to_pem
457+
* (PKCS #8 PrivateKeyInfo or EncryptedPrivateKeyInfo) instead.
410458
*/
411459
static VALUE
412460
ossl_ec_key_export(int argc, VALUE *argv, VALUE self)
@@ -426,7 +474,15 @@ ossl_ec_key_export(int argc, VALUE *argv, VALUE self)
426474
* call-seq:
427475
* key.to_der => String
428476
*
429-
* See the OpenSSL documentation for i2d_ECPrivateKey_bio()
477+
* Serializes a private or public key to a DER-encoding.
478+
*
479+
* See #to_pem for details.
480+
*
481+
* <b>This method is kept for compatibility.</b>
482+
* This should only be used when the SEC 1/RFC 5915 ECPrivateKey format is
483+
* required.
484+
*
485+
* Consider using #public_to_der or #private_to_der instead.
430486
*/
431487
static VALUE
432488
ossl_ec_key_to_der(VALUE self)

ext/openssl/ossl_pkey_rsa.c

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,57 @@ can_export_rsaprivatekey(VALUE self)
221221
* rsa.to_pem([cipher, password]) => PEM-format String
222222
* rsa.to_s([cipher, password]) => PEM-format String
223223
*
224-
* Outputs this keypair in PEM encoding. If _cipher_ and _password_ are
225-
* given they will be used to encrypt the key. _cipher_ must be an
226-
* OpenSSL::Cipher instance.
224+
* Serializes a private or public key to a PEM-encoding.
225+
*
226+
* [When the key contains public components only]
227+
*
228+
* Serializes it into an X.509 SubjectPublicKeyInfo.
229+
* The parameters _cipher_ and _password_ are ignored.
230+
*
231+
* A PEM-encoded key will look like:
232+
*
233+
* -----BEGIN PUBLIC KEY-----
234+
* [...]
235+
* -----END PUBLIC KEY-----
236+
*
237+
* Consider using #public_to_pem instead. This serializes the key into an
238+
* X.509 SubjectPublicKeyInfo regardless of whether the key is a public key
239+
* or a private key.
240+
*
241+
* [When the key contains private components, and no parameters are given]
242+
*
243+
* Serializes it into a PKCS #1 RSAPrivateKey.
244+
*
245+
* A PEM-encoded key will look like:
246+
*
247+
* -----BEGIN RSA PRIVATE KEY-----
248+
* [...]
249+
* -----END RSA PRIVATE KEY-----
250+
*
251+
* [When the key contains private components, and _cipher_ and _password_ are given]
252+
*
253+
* Serializes it into a PKCS #1 RSAPrivateKey
254+
* and encrypts it in OpenSSL's traditional PEM encryption format.
255+
* _cipher_ must be a cipher name understood by OpenSSL::Cipher.new or an
256+
* instance of OpenSSL::Cipher.
257+
*
258+
* An encrypted PEM-encoded key will look like:
259+
*
260+
* -----BEGIN RSA PRIVATE KEY-----
261+
* Proc-Type: 4,ENCRYPTED
262+
* DEK-Info: AES-128-CBC,733F5302505B34701FC41F5C0746E4C0
263+
*
264+
* [...]
265+
* -----END RSA PRIVATE KEY-----
266+
*
267+
* Note that this format uses MD5 to derive the encryption key, and hence
268+
* will not be available on FIPS-compliant systems.
269+
*
270+
* <b>This method is kept for compatibility.</b>
271+
* This should only be used when the PKCS #1 RSAPrivateKey format is required.
272+
*
273+
* Consider using #public_to_pem (X.509 SubjectPublicKeyInfo) or #private_to_pem
274+
* (PKCS #8 PrivateKeyInfo or EncryptedPrivateKeyInfo) instead.
227275
*/
228276
static VALUE
229277
ossl_rsa_export(int argc, VALUE *argv, VALUE self)
@@ -238,7 +286,14 @@ ossl_rsa_export(int argc, VALUE *argv, VALUE self)
238286
* call-seq:
239287
* rsa.to_der => DER-format String
240288
*
241-
* Outputs this keypair in DER encoding.
289+
* Serializes a private or public key to a DER-encoding.
290+
*
291+
* See #to_pem for details.
292+
*
293+
* <b>This method is kept for compatibility.</b>
294+
* This should only be used when the PKCS #1 RSAPrivateKey format is required.
295+
*
296+
* Consider using #public_to_der or #private_to_der instead.
242297
*/
243298
static VALUE
244299
ossl_rsa_to_der(VALUE self)

0 commit comments

Comments
 (0)