Module: OpenSSL::PKey

Defined in:
ext/openssl/ossl_pkey.c,
lib/openssl/pkey.rb,
ext/openssl/ossl_pkey.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, DSA, EC, PKey, PKeyError, RSA

Constant Summary collapse

DHError =

Alias of PKeyError. Before version 4.0.0, this was a subclass of PKeyError.

PKeyError
DSAError =

Alias of PKeyError. Before version 4.0.0, this was a subclass of PKeyError.

PKeyError
ECError =

Alias of PKeyError. Before version 4.0.0, this was a subclass of PKeyError.

PKeyError
RSAError =

Alias of PKeyError. Before version 4.0.0, this was a subclass of PKeyError.

PKeyError

Class Method Summary collapse

Class Method Details

.OpenSSL::PKey.generate_key(algo_name[, options]) ⇒ Object .OpenSSL::PKey.generate_key(pkey[, options]) ⇒ Object

Generates a new key (pair).

If a String is given as the first argument, it generates a new random key for the algorithm specified by the name just as ::generate_parameters does. If an OpenSSL::PKey::PKey is given instead, it generates a new random key for the same algorithm as the key, using the parameters the key contains.

See ::generate_parameters for the details of options and the given block.

Example

pkey_params = OpenSSL::PKey.generate_parameters("DSA", "dsa_paramgen_bits" => 2048)
pkey_params.priv_key #=> nil
pkey = OpenSSL::PKey.generate_key(pkey_params)
pkey.priv_key #=> #<OpenSSL::BN 6277...


496
497
498
499
500
# File 'ext/openssl/ossl_pkey.c', line 496

static VALUE
ossl_pkey_s_generate_key(int argc, VALUE *argv, VALUE self)
{
    return pkey_generate(argc, argv, self, 0);
}

.OpenSSL::PKey.generate_parameters(algo_name[, options]) ⇒ Object

Generates new parameters for the algorithm. algo_name is a String that represents the algorithm. The optional argument options is a Hash that specifies the options specific to the algorithm. The order of the options can be important.

A block can be passed optionally. The meaning of the arguments passed to the block varies depending on the implementation of the algorithm. The block may be called once or multiple times, or may not even be called.

For the supported options, see the documentation for the ‘openssl genpkey’ utility command.

Example

pkey = OpenSSL::PKey.generate_parameters("DSA", "dsa_paramgen_bits" => 2048)
p pkey.p.num_bits #=> 2048


470
471
472
473
474
# File 'ext/openssl/ossl_pkey.c', line 470

static VALUE
ossl_pkey_s_generate_parameters(int argc, VALUE *argv, VALUE self)
{
    return pkey_generate(argc, argv, self, 1);
}

.OpenSSL::PKey.new_raw_private_key(algo, string) ⇒ PKey

See the OpenSSL documentation for EVP_PKEY_new_raw_private_key()

Returns:



669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'ext/openssl/ossl_pkey.c', line 669

static VALUE
ossl_pkey_new_raw_private_key(VALUE self, VALUE type, VALUE key)
{
    EVP_PKEY *pkey;
    size_t keylen;

    StringValue(key);
    keylen = RSTRING_LEN(key);

#ifdef OSSL_USE_PROVIDER
    pkey = EVP_PKEY_new_raw_private_key_ex(NULL, StringValueCStr(type), NULL,
                                           (unsigned char *)RSTRING_PTR(key),
                                           keylen);
    if (!pkey)
        ossl_raise(ePKeyError, "EVP_PKEY_new_raw_private_key_ex");
#else
    int pkey_id = lookup_pkey_type(type);
    pkey = EVP_PKEY_new_raw_private_key(pkey_id, NULL, (unsigned char *)RSTRING_PTR(key), keylen);
    if (!pkey)
        ossl_raise(ePKeyError, "EVP_PKEY_new_raw_private_key");
#endif

    return ossl_pkey_wrap(pkey);
}

.OpenSSL::PKey.new_raw_public_key(algo, string) ⇒ PKey

See the OpenSSL documentation for EVP_PKEY_new_raw_public_key()

Returns:



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'ext/openssl/ossl_pkey.c', line 701

static VALUE
ossl_pkey_new_raw_public_key(VALUE self, VALUE type, VALUE key)
{
    EVP_PKEY *pkey;
    size_t keylen;

    StringValue(key);
    keylen = RSTRING_LEN(key);

#ifdef OSSL_USE_PROVIDER
    pkey = EVP_PKEY_new_raw_public_key_ex(NULL, StringValueCStr(type), NULL,
                                          (unsigned char *)RSTRING_PTR(key),
                                          keylen);
    if (!pkey)
        ossl_raise(ePKeyError, "EVP_PKEY_new_raw_public_key_ex");
#else
    int pkey_id = lookup_pkey_type(type);
    pkey = EVP_PKEY_new_raw_public_key(pkey_id, NULL, (unsigned char *)RSTRING_PTR(key), keylen);
    if (!pkey)
        ossl_raise(ePKeyError, "EVP_PKEY_new_raw_public_key");
#endif

    return ossl_pkey_wrap(pkey);
}

.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:



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'ext/openssl/ossl_pkey.c', line 230

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);
    bio = ossl_obj2bio(&data);
    pkey = ossl_pkey_read_generic(bio, ossl_pem_passwd_value(pass));
    BIO_free(bio);
    if (!pkey)
        ossl_raise(ePKeyError, "Could not parse PKey");
    return ossl_pkey_wrap(pkey);
}