Class: JOSE::JWE::ALG_RSA

Inherits:
Struct
  • Object
show all
Defined in:
lib/jose/jwe/alg_rsa.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rsa_oaep_mdObject

Returns the value of attribute rsa_oaep_md

Returns:

  • (Object)

    the current value of rsa_oaep_md


1
2
3
# File 'lib/jose/jwe/alg_rsa.rb', line 1

def rsa_oaep_md
  @rsa_oaep_md
end

#rsa_paddingObject

Returns the value of attribute rsa_padding

Returns:

  • (Object)

    the current value of rsa_padding


1
2
3
# File 'lib/jose/jwe/alg_rsa.rb', line 1

def rsa_padding
  @rsa_padding
end

Class Method Details

.from_map(fields) ⇒ Object

JOSE::JWE callbacks


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jose/jwe/alg_rsa.rb', line 5

def self.from_map(fields)
  rsa_padding = nil
  rsa_oaep_md = nil
  case fields['alg']
  when 'RSA1_5'
    rsa_padding = :rsa_pkcs1_padding
  when 'RSA-OAEP'
    rsa_padding = :rsa_pkcs1_oaep_padding
    rsa_oaep_md = OpenSSL::Digest::SHA1
  when 'RSA-OAEP-256'
    rsa_padding = :rsa_pkcs1_oaep_padding
    rsa_oaep_md = OpenSSL::Digest::SHA256
  else
    raise ArgumentError, "invalid 'alg' for JWE: #{fields['alg'].inspect}"
  end
  return new(rsa_padding, rsa_oaep_md), fields.except('alg')
end

Instance Method Details

#algorithmObject

API functions


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jose/jwe/alg_rsa.rb', line 55

def algorithm
  if rsa_padding == :rsa_pkcs1_padding
    'RSA1_5'
  elsif rsa_padding == :rsa_pkcs1_oaep_padding
    if rsa_oaep_md == OpenSSL::Digest::SHA1
      'RSA-OAEP'
    elsif rsa_oaep_md == OpenSSL::Digest::SHA256
      'RSA-OAEP-256'
    else
      raise ArgumentError, "unhandled JOSE::JWE::ALG_RSA rsa_oaep_md: #{rsa_oaep_md.inspect}"
    end
  else
    raise ArgumentError, "unhandled JOSE::JWE::ALG_RSA rsa_padding: #{rsa_padding.inspect}"
  end
end

#generate_key(fields, enc) ⇒ Object

JOSE::JWE::ALG callbacks


29
30
31
# File 'lib/jose/jwe/alg_rsa.rb', line 29

def generate_key(fields, enc)
  return JOSE::JWE::ALG.generate_key([:rsa, 2048], algorithm, enc.algorithm)
end

#key_decrypt(key, enc, encrypted_key) ⇒ Object


33
34
35
36
37
38
39
# File 'lib/jose/jwe/alg_rsa.rb', line 33

def key_decrypt(key, enc, encrypted_key)
  if key.is_a?(JOSE::JWK)
    return key.kty.decrypt_private(encrypted_key, rsa_padding: rsa_padding, rsa_oaep_md: rsa_oaep_md)
  else
    raise ArgumentError, "'key' must be a JOSE::JWK"
  end
end

#key_encrypt(key, enc, decrypted_key) ⇒ Object


41
42
43
44
45
46
47
# File 'lib/jose/jwe/alg_rsa.rb', line 41

def key_encrypt(key, enc, decrypted_key)
  if key.is_a?(JOSE::JWK)
    return key.kty.encrypt_public(decrypted_key, rsa_padding: rsa_padding, rsa_oaep_md: rsa_oaep_md), self
  else
    raise ArgumentError, "'key' must be a JOSE::JWK"
  end
end

#next_cek(key, enc) ⇒ Object


49
50
51
# File 'lib/jose/jwe/alg_rsa.rb', line 49

def next_cek(key, enc)
  return enc.next_cek
end

#to_map(fields) ⇒ Object


23
24
25
# File 'lib/jose/jwe/alg_rsa.rb', line 23

def to_map(fields)
  return fields.put('alg', algorithm)
end