Skip to content

Commit 6588fad

Browse files
authored
Merge pull request #645 from rhenium/ky/pkey-document-traditional-pem
[DOC] enhance RDoc for exporting pkeys
2 parents 8ac40ba + d22769a commit 6588fad

File tree

7 files changed

+243
-52
lines changed

7 files changed

+243
-52
lines changed

ext/openssl/ossl.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd_)
207207

208208
while (1) {
209209
/*
210-
* when the flag is nonzero, this passphrase
210+
* when the flag is nonzero, this password
211211
* will be used to perform encryption; otherwise it will
212212
* be used to perform decryption.
213213
*/
@@ -674,23 +674,21 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
674674
*
675675
* key = OpenSSL::PKey::RSA.new 2048
676676
*
677-
* open 'private_key.pem', 'w' do |io| io.write key.to_pem end
678-
* open 'public_key.pem', 'w' do |io| io.write key.public_key.to_pem end
677+
* File.write 'private_key.pem', key.private_to_pem
678+
* File.write 'public_key.pem', key.public_to_pem
679679
*
680680
* === Exporting a Key
681681
*
682682
* Keys saved to disk without encryption are not secure as anyone who gets
683683
* ahold of the key may use it unless it is encrypted. In order to securely
684-
* export a key you may export it with a pass phrase.
684+
* export a key you may export it with a password.
685685
*
686686
* cipher = OpenSSL::Cipher.new 'aes-256-cbc'
687-
* pass_phrase = 'my secure pass phrase goes here'
687+
* password = 'my secure password goes here'
688688
*
689-
* key_secure = key.export cipher, pass_phrase
689+
* key_secure = key.private_to_pem cipher, password
690690
*
691-
* open 'private.secure.pem', 'w' do |io|
692-
* io.write key_secure
693-
* end
691+
* File.write 'private.secure.pem', key_secure
694692
*
695693
* OpenSSL::Cipher.ciphers returns a list of available ciphers.
696694
*
@@ -710,13 +708,13 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
710708
*
711709
* === Loading an Encrypted Key
712710
*
713-
* OpenSSL will prompt you for your pass phrase when loading an encrypted key.
714-
* If you will not be able to type in the pass phrase you may provide it when
711+
* OpenSSL will prompt you for your password when loading an encrypted key.
712+
* If you will not be able to type in the password you may provide it when
715713
* loading the key:
716714
*
717715
* key4_pem = File.read 'private.secure.pem'
718-
* pass_phrase = 'my secure pass phrase goes here'
719-
* key4 = OpenSSL::PKey.read key4_pem, pass_phrase
716+
* password = 'my secure password goes here'
717+
* key4 = OpenSSL::PKey.read key4_pem, password
720718
*
721719
* == RSA Encryption
722720
*
@@ -909,12 +907,12 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
909907
* not readable by other users.
910908
*
911909
* ca_key = OpenSSL::PKey::RSA.new 2048
912-
* pass_phrase = 'my secure pass phrase goes here'
910+
* password = 'my secure password goes here'
913911
*
914-
* cipher = OpenSSL::Cipher.new 'aes-256-cbc'
912+
* cipher = 'aes-256-cbc'
915913
*
916914
* open 'ca_key.pem', 'w', 0400 do |io|
917-
* io.write ca_key.export(cipher, pass_phrase)
915+
* io.write ca_key.private_to_pem(cipher, password)
918916
* end
919917
*
920918
* === CA Certificate

ext/openssl/ossl_kdf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static VALUE mKDF, eKDF;
2121
* (https://tools.ietf.org/html/rfc2898#section-5.2).
2222
*
2323
* === Parameters
24-
* pass :: The passphrase.
24+
* pass :: The password.
2525
* salt :: The salt. Salts prevent attacks based on dictionaries of common
2626
* passwords and attacks based on rainbow tables. It is a public
2727
* value that can be safely stored along with the password (e.g.

ext/openssl/ossl_pkey.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,18 @@ ossl_pkey_private_to_der(int argc, VALUE *argv, VALUE self)
875875
*
876876
* Serializes the private key to PEM-encoded PKCS #8 format. See #private_to_der
877877
* for more details.
878+
*
879+
* An unencrypted PEM-encoded key will look like:
880+
*
881+
* -----BEGIN PRIVATE KEY-----
882+
* [...]
883+
* -----END PRIVATE KEY-----
884+
*
885+
* An encrypted PEM-encoded key will look like:
886+
*
887+
* -----BEGIN ENCRYPTED PRIVATE KEY-----
888+
* [...]
889+
* -----END ENCRYPTED PRIVATE KEY-----
878890
*/
879891
static VALUE
880892
ossl_pkey_private_to_pem(int argc, VALUE *argv, VALUE self)
@@ -953,6 +965,12 @@ ossl_pkey_public_to_der(VALUE self)
953965
* pkey.public_to_pem -> string
954966
*
955967
* Serializes the public key to PEM-encoded X.509 SubjectPublicKeyInfo format.
968+
*
969+
* A PEM-encoded key will look like:
970+
*
971+
* -----BEGIN PUBLIC KEY-----
972+
* [...]
973+
* -----END PUBLIC KEY-----
956974
*/
957975
static VALUE
958976
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)

0 commit comments

Comments
 (0)