Class: Saro::Dat::DatSignature

Inherits:
Object
  • Object
show all
Defined in:
lib/saro/dat/signature.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm, signing_key, verifying_key, config = nil) ⇒ DatSignature

Returns a new instance of DatSignature.



40
41
42
43
44
45
# File 'lib/saro/dat/signature.rb', line 40

def initialize(algorithm, signing_key, verifying_key, config = nil)
  @algorithm = algorithm
  @signing_key = signing_key
  @verifying_key = verifying_key
  @config = config || Saro::Dat.get_signature_config(algorithm)
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



38
39
40
# File 'lib/saro/dat/signature.rb', line 38

def algorithm
  @algorithm
end

#signing_keyObject (readonly)

Returns the value of attribute signing_key.



38
39
40
# File 'lib/saro/dat/signature.rb', line 38

def signing_key
  @signing_key
end

#verifying_keyObject (readonly)

Returns the value of attribute verifying_key.



38
39
40
# File 'lib/saro/dat/signature.rb', line 38

def verifying_key
  @verifying_key
end

Class Method Details

.generate(algorithm) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/saro/dat/signature.rb', line 92

def self.generate(algorithm)
  config = Saro::Dat.get_signature_config(algorithm)
  if config[:name] == "HMAC"
    key = OpenSSL::Random.random_bytes(config[:hmac_len])
    new(algorithm, key, key, config)
  else
    key = OpenSSL::PKey::EC.generate(config[:curve])
    begin
      key.check_key
    rescue OpenSSL::PKey::PKeyError, OpenSSL::PKey::ECError => e
      raise Saro::Dat::Error.new(Saro::Dat::ErrorCode::INTERNAL_UNKNOWN, "generated ecdsa key pair is invalid: #{e.message}", cause: e)
    end
    new(algorithm, key, key, config)
  end
end

.imports(algorithm, base64_str) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/saro/dat/signature.rb', line 108

def self.imports(algorithm, base64_str)
  config = Saro::Dat.get_signature_config(algorithm)
  bytes_data = Saro::Dat::Util.decode_base64_url(base64_str)

  if config[:name] == "HMAC"
    if bytes_data.bytesize != config[:hmac_len]
      raise Saro::Dat::Error.new(Saro::Dat::ErrorCode::KEY_INVALID, "hmac key must be #{config[:hmac_len]} bytes, got #{bytes_data.bytesize}")
    end
    new(algorithm, bytes_data, bytes_data, config)
  else
    private_len = config[:private_len]
    public_len = config[:public_len]

    signing_key = nil
    verifying_key = nil

    if bytes_data.bytesize == private_len + public_len
      private_bytes = bytes_data[0, private_len]
      public_bytes = bytes_data[private_len, public_len]
      
      d_value = OpenSSL::BN.new(private_bytes, 2)
      signing_key = create_ec_key(config[:curve], d_value, public_bytes)
      verifying_key = signing_key
    elsif bytes_data.bytesize == public_len
      verifying_key = create_ec_key(config[:curve], nil, bytes_data)
    else
      raise Saro::Dat::Error.new(Saro::Dat::ErrorCode::KEY_INVALID, "ecdsa key length matches neither private+public nor public")
    end

    new(algorithm, signing_key, verifying_key, config)
  end
end

Instance Method Details

#exports(verify_only = false) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/saro/dat/signature.rb', line 141

def exports(verify_only = false)
  if verify_only && !support_verify_only
    raise Saro::Dat::Error.new(Saro::Dat::ErrorCode::KEY_VERIFY_ONLY_UNSUPPORTED, @algorithm.to_s)
  end

  if @config[:name] == "HMAC"
    Saro::Dat::Util.encode_base64_url_str(@verifying_key)
  else
    if verify_only || !@signing_key&.private_key
      public_bytes = @verifying_key.public_key.to_octet_string(:uncompressed)
      Saro::Dat::Util.encode_base64_url_str(public_bytes)
    else
      d_value = @signing_key.private_key
      curve_size = (@signing_key.group.degree + 7) / 8
      d_bytes = d_value.to_s(2).rjust(curve_size, "\x00".b)
      
      public_bytes = @verifying_key.public_key.to_octet_string(:uncompressed)
      Saro::Dat::Util.encode_base64_url_str(d_bytes + public_bytes)
    end
  end
end

#pairObject



223
224
225
# File 'lib/saro/dat/signature.rb', line 223

def pair
  @config[:name] == "ECDSA"
end

#sign(body) ⇒ Object

Raises:



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/saro/dat/signature.rb', line 163

def sign(body)
  raise Saro::Dat::Error.new(Saro::Dat::ErrorCode::SIG_KEY_MISSING, "this key is verify-only") unless @signing_key
  body = normalize_body(body)
  raise Saro::Dat::Error.new(Saro::Dat::ErrorCode::CONFIG_ARGUMENT_INVALID, "body to sign is empty") if body.nil? || body.empty?

  if @config[:name] == "HMAC"
    OpenSSL::HMAC.digest(@config[:hash], @signing_key, body)
  else
    signature_der = @signing_key.dsa_sign_asn1(OpenSSL::Digest.digest(@config[:hash], body))
    der_to_raw_signature(signature_der)
  end
end

#signableObject



219
220
221
# File 'lib/saro/dat/signature.rb', line 219

def signable
  !@signing_key.nil?
end

#support_verify_onlyObject



227
228
229
# File 'lib/saro/dat/signature.rb', line 227

def support_verify_only
  @config[:name] == "ECDSA"
end

#verify(body, signature) ⇒ Object



176
177
178
# File 'lib/saro/dat/signature.rb', line 176

def verify(body, signature)
  verify_bytes(body, signature)
end

#verify_base64(body, signature_base64) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/saro/dat/signature.rb', line 180

def verify_base64(body, signature_base64)
  sig_bytes = begin
    Saro::Dat::Util.decode_base64_url(signature_base64)
  rescue Saro::Dat::Error => e
    raise Saro::Dat::Error.new(Saro::Dat::ErrorCode::SIG_MALFORMED, "signature is not base64url", cause: e)
  end
  verify_bytes(body, sig_bytes)
end