Class: SSHData::PrivateKey::ECDSA

Inherits:
Base
  • Object
show all
Defined in:
lib/ssh_data/private_key/ecdsa.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#algo, #comment, #public_key

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#issue_certificate

Constructor Details

#initialize(algo:, curve:, public_key:, private_key:, comment:) ⇒ ECDSA

Returns a new instance of ECDSA.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ssh_data/private_key/ecdsa.rb', line 38

def initialize(algo:, curve:, public_key:, private_key:, comment:)
  unless [PublicKey::ALGO_ECDSA256, PublicKey::ALGO_ECDSA384, PublicKey::ALGO_ECDSA521].include?(algo)
    raise DecodeError, "bad algorithm: #{algo.inspect}"
  end

  unless algo == "ecdsa-sha2-#{curve}"
    raise DecodeError, "bad curve: #{curve.inspect}"
  end

  @curve = curve
  @public_key_bytes = public_key
  @private_key_bytes = private_key

  super(algo: algo, comment: comment)

  @openssl = begin
    OpenSSL::PKey::EC.new(asn1.to_der)
  rescue ArgumentError
    raise DecodeError, "bad key data"
  end

  @public_key = PublicKey::ECDSA.new(
    algo: algo,
    curve: curve,
    public_key: public_key_bytes
  )
end

Instance Attribute Details

#curveObject (readonly)

Returns the value of attribute curve.



4
5
6
# File 'lib/ssh_data/private_key/ecdsa.rb', line 4

def curve
  @curve
end

#opensslObject (readonly)

Returns the value of attribute openssl.



4
5
6
# File 'lib/ssh_data/private_key/ecdsa.rb', line 4

def openssl
  @openssl
end

#private_key_bytesObject (readonly)

Returns the value of attribute private_key_bytes.



4
5
6
# File 'lib/ssh_data/private_key/ecdsa.rb', line 4

def private_key_bytes
  @private_key_bytes
end

#public_key_bytesObject (readonly)

Returns the value of attribute public_key_bytes.



4
5
6
# File 'lib/ssh_data/private_key/ecdsa.rb', line 4

def public_key_bytes
  @public_key_bytes
end

Class Method Details

.from_openssl(key) ⇒ Object

Import an openssl private key.

key - An OpenSSL::PKey::EC instance.

Returns a DSA instance.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ssh_data/private_key/ecdsa.rb', line 25

def self.from_openssl(key)
  curve = PublicKey::ECDSA::CURVE_FOR_OPENSSL_CURVE_NAME[key.group.curve_name]
  algo = "ecdsa-sha2-#{curve}"

  new(
    algo: algo,
    curve: curve,
    public_key: key.public_key.to_bn.to_s(2),
    private_key: key.private_key,
    comment: "",
  )
end

.generate(curve) ⇒ Object

Generate a new private key.

curve - The String curve to use. One of SSHData::PublicKey::NISTP256,

SSHData::PublicKey::NISTP384, or SSHData::PublicKey::NISTP521.

Returns a PublicKey::Base subclass instance.

Raises:



12
13
14
15
16
17
18
# File 'lib/ssh_data/private_key/ecdsa.rb', line 12

def self.generate(curve)
  openssl_curve = PublicKey::ECDSA::OPENSSL_CURVE_NAME_FOR_CURVE[curve]
  raise AlgorithmError, "unknown curve: #{curve}" if openssl_curve.nil?

  openssl_key = OpenSSL::PKey::EC.generate(openssl_curve)
  from_openssl(openssl_key)
end

Instance Method Details

#sign(signed_data, algo: nil) ⇒ Object

Make an SSH signature.

signed_data - The String message over which to calculated the signature.

Returns a binary String signature.

Raises:



71
72
73
74
75
76
77
# File 'lib/ssh_data/private_key/ecdsa.rb', line 71

def sign(signed_data, algo: nil)
  algo ||= self.algo
  raise AlgorithmError unless algo == self.algo
  openssl_sig = openssl.sign(public_key.digest.new, signed_data)
  raw_sig = PublicKey::ECDSA.ssh_signature(openssl_sig)
  Encoding.encode_signature(algo, raw_sig)
end