Class: JOSE::JWE::ALG_PBES2

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bitsObject

Returns the value of attribute bits

Returns:

  • (Object)

    the current value of bits



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

def bits
  @bits
end

#hmacObject

Returns the value of attribute hmac

Returns:

  • (Object)

    the current value of hmac



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

def hmac
  @hmac
end

#iterObject

Returns the value of attribute iter

Returns:

  • (Object)

    the current value of iter



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

def iter
  @iter
end

#saltObject

Returns the value of attribute salt

Returns:

  • (Object)

    the current value of salt



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

def salt
  @salt
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
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jose/jwe/alg_pbes2.rb', line 5

def self.from_map(fields)
  bits = nil
  hmac = nil
  case fields['alg']
  when 'PBES2-HS256+A128KW'
    bits = 128
    hmac = OpenSSL::Digest::SHA256
  when 'PBES2-HS384+A192KW'
    bits = 192
    hmac = OpenSSL::Digest::SHA384
  when 'PBES2-HS512+A256KW'
    bits = 256
    hmac = OpenSSL::Digest::SHA512
  else
    raise ArgumentError, "invalid 'alg' for JWE: #{fields['alg'].inspect}"
  end
  iter = nil
  if fields['p2c'].is_a?(Integer) and fields['p2c'] >= 0
    iter = fields['p2c']
  else
    raise ArgumentError, "invalid 'p2c' for JWE: #{fields['p2c'].inspect}"
  end
  salt = nil
  if fields.has_key?('p2s') and fields['p2s'].is_a?(String)
    salt = wrap_salt(fields['alg'], JOSE.urlsafe_decode64(fields['p2s']))
  else
    raise ArgumentError, "invalid 'p2s' for JWE: #{fields['p2s'].inspect}"
  end
  return new(hmac, bits, salt, iter), fields.except('alg', 'p2c', 'p2s')
end

Instance Method Details

#key_decrypt(key, enc, encrypted_key) ⇒ Object

JOSE::JWE::ALG callbacks



53
54
55
56
57
58
59
60
# File 'lib/jose/jwe/alg_pbes2.rb', line 53

def key_decrypt(key, enc, encrypted_key)
  if key.is_a?(JOSE::JWK)
    key = key.kty.derive_key
  end
  derived_key = OpenSSL::PKCS5.pbkdf2_hmac(key, salt, iter, bits.div(8) + (bits % 8), hmac.new)
  decrypted_key = JOSE::JWA::AES_KW.unwrap(encrypted_key, derived_key)
  return decrypted_key
end

#key_encrypt(key, enc, decrypted_key) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/jose/jwe/alg_pbes2.rb', line 62

def key_encrypt(key, enc, decrypted_key)
  if key.is_a?(JOSE::JWK)
    key = key.kty.derive_key
  end
  derived_key = OpenSSL::PKCS5.pbkdf2_hmac(key, salt, iter, bits.div(8) + (bits % 8), hmac.new)
  encrypted_key = JOSE::JWA::AES_KW.wrap(decrypted_key, derived_key)
  return encrypted_key, self
end

#next_cek(key, enc) ⇒ Object



71
72
73
# File 'lib/jose/jwe/alg_pbes2.rb', line 71

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

#to_map(fields) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jose/jwe/alg_pbes2.rb', line 36

def to_map(fields)
  alg = if hmac == OpenSSL::Digest::SHA256
    'PBES2-HS256+A128KW'
  elsif hmac == OpenSSL::Digest::SHA384
    'PBES2-HS384+A192KW'
  elsif hmac == OpenSSL::Digest::SHA512
    'PBES2-HS512+A256KW'
  else
    raise ArgumentError, "unhandled JOSE::JWE::ALG_PBES2 hmac: #{hmac.inspect}"
  end
  p2c = iter
  p2s = JOSE.urlsafe_encode64(unwrap_salt(alg, salt))
  return fields.put('alg', alg).put('p2c', p2c).put('p2s', p2s)
end