class method
OpenSSL::PKey::DH.new
(::Integer size, ?::Integer generator) -> ::OpenSSL::PKey::DH
(::String pem) -> ::OpenSSL::PKey::DH
() -> ::OpenSSL::PKey::DH
DH.new → dh
DH.new(string) → dh
DH.new(size [, generator]) → dh
ext/openssl/ossl_pkey_dh.c
View on GitHub ↗
static VALUE
ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
{
EVP_PKEY *pkey;
int type;
DH *dh;
BIO *in = NULL;
VALUE arg;
TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
if (pkey)
rb_raise(rb_eTypeError, "pkey already initialized");
/* The DH.new(size, generator) form is handled by lib/openssl/pkey.rb */
if (rb_scan_args(argc, argv, "01", &arg) == 0) {
#ifdef OSSL_HAVE_IMMUTABLE_PKEY
rb_raise(rb_eArgError, "OpenSSL::PKey::DH.new cannot be called " \
"without arguments; pkeys are immutable with OpenSSL 3.0");
#else
dh = DH_new();
if (!dh)
ossl_raise(ePKeyError, "DH_new");
goto legacy;
#endif
}
arg = ossl_to_der_if_possible(arg);
in = ossl_obj2bio(&arg);
/*
* On OpenSSL <= 1.1.1 and current versions of LibreSSL, the generic
* routine does not support DER-encoded parameters
*/
dh = d2i_DHparams_bio(in, NULL);
if (dh)
goto legacy;
OSSL_BIO_reset(in);
pkey = ossl_pkey_read_generic(in, Qnil);
BIO_free(in);
if (!pkey)
ossl_raise(ePKeyError, "could not parse pkey");
type = EVP_PKEY_base_id(pkey);
if (type != EVP_PKEY_DH) {
EVP_PKEY_free(pkey);
rb_raise(ePKeyError, "incorrect pkey type: %s", OBJ_nid2sn(type));
}
RTYPEDDATA_DATA(self) = pkey;
return self;
legacy:
BIO_free(in);
pkey = EVP_PKEY_new();
if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
EVP_PKEY_free(pkey);
DH_free(dh);
ossl_raise(ePKeyError, "EVP_PKEY_assign_DH");
}
RTYPEDDATA_DATA(self) = pkey;
return self;
}
Creates a new instance of OpenSSL::PKey::DH.
If called without arguments, an empty instance without any parameter or key components is created. Use set_pqg to manually set the parameters afterwards (and optionally set_key to set private and public key components). This form is not compatible with OpenSSL 3.0 or later.
If a String is given, tries to parse it as a DER- or PEM- encoded parameters. See also OpenSSL::PKey.read which can parse keys of any kinds.
The DH.new(size [, generator]) form is an alias of DH.generate.
string-
A
Stringthat contains the DER or PEM encoded key. size-
See
DH.generate. generator-
See
DH.generate.
Examples:
Example
# Creating an instance from scratch # Note that this is deprecated and will result in ArgumentError when # using OpenSSL 3.0 or later. dh = OpenSSL::PKey::DH.new dh.set_pqg(bn_p, nil, bn_g) # Generating a parameters and a key pair dh = OpenSSL::PKey::DH.new(2048) # An alias of OpenSSL::PKey::DH.generate(2048) # Reading DH parameters from a PEM-encoded string dh_params = OpenSSL::PKey::DH.new(File.read('parameters.pem')) # loads parameters only dh = OpenSSL::PKey.generate_key(dh_params) # generates a key pair