Module: OpenSSL::PKey

Defined in:
ossl_pkey.c,
lib/openssl/pkey.rb,
ossl_pkey.c,
ossl_pkey_dh.c,
ossl_pkey_ec.c,
ossl_pkey_dsa.c,
ossl_pkey_rsa.c

Overview

Asymmetric Public Key Algorithms

Asymmetric public key algorithms solve the problem of establishing and sharing secret keys to en-/decrypt messages. The key in such an algorithm consists of two parts: a public key that may be distributed to others and a private key that needs to remain secret.

Messages encrypted with a public key can only be decrypted by recipients that are in possession of the associated private key. Since public key algorithms are considerably slower than symmetric key algorithms (cf. OpenSSL::Cipher) they are often used to establish a symmetric key shared between two parties that are in possession of each other’s public key.

Asymmetric algorithms offer a lot of nice features that are used in a lot of different areas. A very common application is the creation and validation of digital signatures. To sign a document, the signatory generally uses a message digest algorithm (cf. OpenSSL::Digest) to compute a digest of the document that is then encrypted (i.e. signed) using the private key. Anyone in possession of the public key may then verify the signature by computing the message digest of the original document on their own, decrypting the signature using the signatory’s public key and comparing the result to the message digest they previously computed. The signature is valid if and only if the decrypted signature is equal to this message digest.

The PKey module offers support for three popular public/private key algorithms:

  • RSA (OpenSSL::PKey::RSA)

  • DSA (OpenSSL::PKey::DSA)

  • Elliptic Curve Cryptography (OpenSSL::PKey::EC)

Each of these implementations is in fact a sub-class of the abstract PKey class which offers the interface for supporting digital signatures in the form of PKey#sign and PKey#verify.

Diffie-Hellman Key Exchange

Finally PKey also features OpenSSL::PKey::DH, an implementation of the Diffie-Hellman key exchange protocol based on discrete logarithms in finite fields, the same basis that DSA is built on. The Diffie-Hellman protocol can be used to exchange (symmetric) keys over insecure channels without needing any prior joint knowledge between the participating parties. As the security of DH demands relatively long “public keys” (i.e. the part that is overtly transmitted between participants) DH tends to be quite slow. If security or speed is your primary concern, OpenSSL::PKey::EC offers another implementation of the Diffie-Hellman protocol.

Defined Under Namespace

Classes: DH, DHError, DSA, DSAError, EC, ECError, PKey, PKeyError, RSA, RSAError

Class Method Summary collapse

Class Method Details

.OpenSSL::PKey.read(string[, pwd ]) ⇒ PKey .OpenSSL::PKey.read(io[, pwd ]) ⇒ PKey

Reads a DER or PEM encoded string from string or io and returns an instance of the appropriate PKey class.

Parameters

  • _string+ is a DER- or PEM-encoded string containing an arbitrary private

or public key.
  • io is an instance of IO containing a DER- or PEM-encoded

arbitrary private or public key.
  • pwd is an optional password in case string or io is an encrypted

PEM resource.

Overloads:

  • .OpenSSL::PKey.read(string[, pwd ]) ⇒ PKey

    Returns:

  • .OpenSSL::PKey.read(io[, pwd ]) ⇒ PKey

    Returns:



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'ossl_pkey.c', line 159

static VALUE
ossl_pkey_new_from_data(int argc, VALUE *argv, VALUE self)
{
    EVP_PKEY *pkey;
    BIO *bio;
    VALUE data, pass;

    rb_scan_args(argc, argv, "11", &data, &pass);
    pass = ossl_pem_passwd_value(pass);

    bio = ossl_obj2bio(&data);
    if ((pkey = d2i_PrivateKey_bio(bio, NULL)))
	goto ok;
    OSSL_BIO_reset(bio);
    if ((pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, ossl_pem_passwd_cb, (void *)pass)))
	goto ok;
    OSSL_BIO_reset(bio);
    if ((pkey = d2i_PUBKEY_bio(bio, NULL)))
	goto ok;
    OSSL_BIO_reset(bio);
    /* PEM_read_bio_PrivateKey() also parses PKCS #8 formats */
    if ((pkey = PEM_read_bio_PrivateKey(bio, NULL, ossl_pem_passwd_cb, (void *)pass)))
	goto ok;
    OSSL_BIO_reset(bio);
    if ((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)))
	goto ok;

    BIO_free(bio);
    ossl_raise(ePKeyError, "Could not parse PKey");

ok:
    BIO_free(bio);
    return ossl_pkey_new(pkey);
}