class 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.
Accessor methods for the Diffie-Hellman parameters
DH#p : The prime (an OpenSSL::BN) of the Diffie-Hellman parameters.
DH#g : The generator (an OpenSSL::BN) g of the Diffie-Hellman parameters.
DH#pub_key : The per-session public key (an OpenSSL::BN) matching the private key. This needs to be passed to DH#compute_key.
DH#priv_key : The per-session private key, an OpenSSL::BN.
Example of a key exchange
# you may send the parameters (der) and own public key (pub1) publicly # to the participating party dh1 = OpenSSL::PKey::DH.new(2048) der = dh1.to_der pub1 = dh1.pub_key # the other party generates its per-session key pair dhparams = OpenSSL::PKey::DH.new(der) dh2 = OpenSSL::PKey.generate_key(dhparams) pub2 = dh2.pub_key symm_key1 = dh1.compute_key(pub2) symm_key2 = dh2.compute_key(pub1) puts symm_key1 == symm_key2 # => true
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5640
def self.generate: (Integer size, ?Integer generator) -> instance
Creates a new DH instance from scratch by generating random parameters and a key pair.
See also OpenSSL::PKey.generate_parameters and OpenSSL::PKey.generate_key.
size-
The desired key size in bits.
generator-
The generator.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5904
def initialize: (Integer size, ?Integer generator) -> void
| (String pem) -> void
| () -> void
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: # 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
Public Instance Methods
(bn pub_bn) → String
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5656
def compute_key: (bn pub_bn) -> String
Returns a String containing a shared secret computed from the other party’s public value.
This method is provided for backwards compatibility, and calls #derive internally.
Parameters
-
pub_bn is a
OpenSSL::BN, not theDHinstance returned byDH#public_keyas that contains theDHparameters only.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5679
def export: () -> String
Serializes the DH parameters to a PEM-encoding.
Note that any existing per-session public/private keys will not get encoded, just the Diffie-Hellman parameters will be encoded.
PEM-encoded parameters will look like:
-----BEGIN DH PARAMETERS----- [...] -----END DH PARAMETERS-----
See also public_to_pem (X.509 SubjectPublicKeyInfo) and private_to_pem (PKCS
#8 PrivateKeyInfo or EncryptedPrivateKeyInfo) for serialization with the private or public key components.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5681
def g: () -> BN?
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5710
def generate_key!: () -> self
Generates a private and public key unless a private key already exists. If this DH instance was generated from public DH parameters (e.g. by encoding the result of DH#public_key), then this method needs to be called first in order to generate the per-session keys before performing the actual key exchange.
<strong>Deprecated in version 3.0</strong>. This method is incompatible with OpenSSL 3.0.0 or later.
See also OpenSSL::PKey.generate_key.
Example: # DEPRECATED USAGE: This will not work on OpenSSL 3.0 or later dh0 = OpenSSL::PKey::DH.new(2048) dh = dh0.public_key # public_key only copies the DH parameters (contrary to the name) dh.generate_key! puts dh.private? # => true puts dh0.pub_key == dh.pub_key #=> false
# With OpenSSL::PKey.generate_key dh0 = OpenSSL::PKey::DH.new(2048) dh = OpenSSL::PKey.generate_key(dh0) puts dh0.pub_key == dh.pub_key #=> false
(self) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5913
def initialize_copy: (self) -> void
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5712
def p: () -> BN
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5722
def params: () -> Hash[String, BN?]
Stores all parameters of key to a Hash.
The hash has keys ‘p’, ‘q’, ‘g’, ‘pub_key’, and ‘priv_key’.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5734
def params_ok?: () -> bool
Validates the Diffie-Hellman parameters associated with this instance. It checks whether a safe prime and a suitable generator are used. If this is not the case, false is returned.
See also the man page EVP_PKEY_param_check(3).
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5736
def priv_key: () -> BN
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5745
def private?: () -> bool
Indicates whether this DH instance has a private key associated with it or not. The private key may be retrieved with DH#priv_key.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5747
def pub_key: () -> BN
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5756
def public?: () -> bool
Indicates whether this DH instance has a public key associated with it or not. The public key may be retrieved with DH#pub_key.
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5780
def public_key: () -> self
Returns a new DH instance that carries just the DH parameters.
Contrary to the method name, the returned DH object contains only parameters and not the public key.
This method is provided for backwards compatibility. In most cases, there is no need to call this method.
For the purpose of re-generating the key pair while keeping the parameters, check OpenSSL::PKey.generate_key.
Example: # OpenSSL::PKey::DH.generate by default generates a random key pair dh1 = OpenSSL::PKey::DH.generate(2048) p dh1.priv_key #=> #<OpenSSL::BN 1288347...> dhcopy = dh1.public_key p dhcopy.priv_key #=> nil
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5782
def q: () -> BN
(bn pub_key, bn? priv_key) → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5790
def set_key: (bn pub_key, bn? priv_key) -> self
(bn p, bn q, bn g) → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5798
def set_pqg: (bn p, bn q, bn g) -> self
Sets p, q, g to the DH instance.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/openssl/0/openssl.rbs, line 5813
def to_der: () -> String
Serializes the DH parameters to a DER-encoding
Note that any existing per-session public/private keys will not get encoded, just the Diffie-Hellman parameters will be encoded.
See also public_to_der (X.509 SubjectPublicKeyInfo) and private_to_der (PKCS
#8 PrivateKeyInfo or EncryptedPrivateKeyInfo) for serialization with the private or public key components.
Serializes the DH parameters to a PEM-encoding.
Note that any existing per-session public/private keys will not get encoded, just the Diffie-Hellman parameters will be encoded.
PEM-encoded parameters will look like:
-----BEGIN DH PARAMETERS----- [...] -----END DH PARAMETERS-----
See also public_to_pem (X.509 SubjectPublicKeyInfo) and private_to_pem (PKCS
#8 PrivateKeyInfo or EncryptedPrivateKeyInfo) for serialization with the private or public key components.
Serializes the DH parameters to a PEM-encoding.
Note that any existing per-session public/private keys will not get encoded, just the Diffie-Hellman parameters will be encoded.
PEM-encoded parameters will look like:
-----BEGIN DH PARAMETERS----- [...] -----END DH PARAMETERS-----
See also public_to_pem (X.509 SubjectPublicKeyInfo) and private_to_pem (PKCS
#8 PrivateKeyInfo or EncryptedPrivateKeyInfo) for serialization with the private or public key components.