gluu
公開メンバ関数 | 非公開変数類 | 全メンバ一覧
org.xdi.oxauth.model.jwe.JweDecrypterImpl クラス
org.xdi.oxauth.model.jwe.JweDecrypterImpl の継承関係図
Inheritance graph
org.xdi.oxauth.model.jwe.JweDecrypterImpl 連携図
Collaboration graph

公開メンバ関数

 JweDecrypterImpl (byte[] sharedSymmetricKey)
 
 JweDecrypterImpl (RSAPrivateKey rsaPrivateKey)
 
 JweDecrypterImpl (PrivateKey privateKey)
 
byte [] decryptEncryptionKey (String encodedEncryptedKey) throws InvalidJweException
 
String decryptCipherText (String encodedCipherText, byte[] contentMasterKey, byte[] initializationVector, byte[] authenticationTag, byte[] additionalAuthenticatedData) throws InvalidJweException
 
KeyEncryptionAlgorithm getKeyEncryptionAlgorithm ()
 
void setKeyEncryptionAlgorithm (KeyEncryptionAlgorithm keyEncryptionAlgorithm)
 
BlockEncryptionAlgorithm getBlockEncryptionAlgorithm ()
 
void setBlockEncryptionAlgorithm (BlockEncryptionAlgorithm blockEncryptionAlgorithm)
 
Jwe decrypt (String encryptedJwe) throws InvalidJweException
 

非公開変数類

PrivateKey privateKey
 
RSAPrivateKey rsaPrivateKey
 
byte [] sharedSymmetricKey
 

詳解

著者
Javier Rojas Blum
バージョン
July 31, 2016

構築子と解体子

◆ JweDecrypterImpl() [1/3]

org.xdi.oxauth.model.jwe.JweDecrypterImpl.JweDecrypterImpl ( byte []  sharedSymmetricKey)
inline
45  {
46  if (sharedSymmetricKey != null) {
48  }
49  }
byte [] sharedSymmetricKey
Definition: JweDecrypterImpl.java:43

◆ JweDecrypterImpl() [2/3]

org.xdi.oxauth.model.jwe.JweDecrypterImpl.JweDecrypterImpl ( RSAPrivateKey  rsaPrivateKey)
inline
51  {
53  }
RSAPrivateKey rsaPrivateKey
Definition: JweDecrypterImpl.java:42

◆ JweDecrypterImpl() [3/3]

org.xdi.oxauth.model.jwe.JweDecrypterImpl.JweDecrypterImpl ( PrivateKey  privateKey)
inline
55  {
56  this.privateKey = privateKey;
57  }
PrivateKey privateKey
Definition: JweDecrypterImpl.java:41

関数詳解

◆ decrypt()

Jwe org.xdi.oxauth.model.jwe.AbstractJweDecrypter.decrypt ( String  encryptedJwe) throws InvalidJweException
inlineinherited

org.xdi.oxauth.model.jwe.JweDecrypterを実装しています。

48  {
49  try {
50  if (StringUtils.isBlank(encryptedJwe)) {
51  return null;
52  }
53 
54  String[] jweParts = encryptedJwe.split("\\.");
55  if (jweParts.length != 5) {
56  throw new InvalidJwtException("Invalid JWS format.");
57  }
58 
59  String encodedHeader = jweParts[0];
60  String encodedEncryptedKey = jweParts[1];
61  String encodedInitializationVector = jweParts[2];
62  String encodedCipherText = jweParts[3];
63  String encodedIntegrityValue = jweParts[4];
64 
65  Jwe jwe = new Jwe();
66  jwe.setEncodedHeader(encodedHeader);
67  jwe.setEncodedEncryptedKey(encodedEncryptedKey);
68  jwe.setEncodedInitializationVector(encodedInitializationVector);
69  jwe.setEncodedCiphertext(encodedCipherText);
70  jwe.setEncodedIntegrityValue(encodedIntegrityValue);
71 
72  jwe.setHeader(new JwtHeader(encodedHeader));
73 
74  keyEncryptionAlgorithm = KeyEncryptionAlgorithm.fromName(
75  jwe.getHeader().getClaimAsString(JwtHeaderName.ALGORITHM));
76  blockEncryptionAlgorithm = BlockEncryptionAlgorithm.fromName(
77  jwe.getHeader().getClaimAsString(JwtHeaderName.ENCRYPTION_METHOD));
78 
79  byte[] contentMasterKey = decryptEncryptionKey(encodedEncryptedKey);
80  byte[] initializationVector = Base64Util.base64urldecode(encodedInitializationVector);
81  byte[] authenticationTag = Base64Util.base64urldecode(encodedIntegrityValue);
82  byte[] additionalAuthenticatedData = jwe.getAdditionalAuthenticatedData().getBytes(Util.UTF8_STRING_ENCODING);
83 
84  String plainText = decryptCipherText(encodedCipherText, contentMasterKey, initializationVector,
85  authenticationTag, additionalAuthenticatedData);
86  jwe.setClaims(new JwtClaims(plainText));
87 
88  return jwe;
89  } catch (InvalidJwtException e) {
90  throw new InvalidJweException(e);
91  } catch (UnsupportedEncodingException e) {
92  throw new InvalidJweException(e);
93  }
94  }
abstract String decryptCipherText(String encodedCipherText, byte[] contentMasterKey, byte[] initializationVector, byte[] authenticationTag, byte[] additionalAuthenticatedData)
BlockEncryptionAlgorithm blockEncryptionAlgorithm
Definition: AbstractJweDecrypter.java:29
static BlockEncryptionAlgorithm fromName(String name)
Definition: BlockEncryptionAlgorithm.java:83
abstract byte [] decryptEncryptionKey(String encodedEncryptedKey)
KeyEncryptionAlgorithm keyEncryptionAlgorithm
Definition: AbstractJweDecrypter.java:28
static KeyEncryptionAlgorithm fromName(String name)
Definition: KeyEncryptionAlgorithm.java:51

◆ decryptCipherText()

String org.xdi.oxauth.model.jwe.JweDecrypterImpl.decryptCipherText ( String  encodedCipherText,
byte []  contentMasterKey,
byte []  initializationVector,
byte []  authenticationTag,
byte []  additionalAuthenticatedData 
) throws InvalidJweException
inline
132  {
133  if (getBlockEncryptionAlgorithm() == null) {
134  throw new InvalidJweException("The block encryption algorithm is null");
135  }
136  if (contentMasterKey == null) {
137  throw new InvalidJweException("The content master key (CMK) is null");
138  }
139  if (initializationVector == null) {
140  throw new InvalidJweException("The initialization vector is null");
141  }
142  if (authenticationTag == null) {
143  throw new InvalidJweException("The authentication tag is null");
144  }
145  if (additionalAuthenticatedData == null) {
146  throw new InvalidJweException("The additional authentication data is null");
147  }
148 
149  try {
150  if (getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A128GCM
151  || getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A256GCM) {
152  final int MAC_SIZE_BITS = 128;
153  byte[] cipherText = Base64Util.base64urldecode(encodedCipherText);
154 
155  KeyParameter key = new KeyParameter(contentMasterKey);
156  AEADParameters aeadParameters = new AEADParameters(key, MAC_SIZE_BITS, initializationVector, additionalAuthenticatedData);
157  SecretKeySpec sks = new SecretKeySpec(contentMasterKey, "AES");
158 
159  BlockCipher blockCipher = new AESEngine();
160  CipherParameters params = new KeyParameter(sks.getEncoded());
161  blockCipher.init(false, params);
162  GCMBlockCipher aGCMBlockCipher = new GCMBlockCipher(blockCipher);
163  aGCMBlockCipher.init(false, aeadParameters);
164  byte[] input = new byte[cipherText.length + authenticationTag.length];
165  System.arraycopy(cipherText, 0, input, 0, cipherText.length);
166  System.arraycopy(authenticationTag, 0, input, cipherText.length, authenticationTag.length);
167  int len = aGCMBlockCipher.getOutputSize(input.length);
168  byte[] out = new byte[len];
169  int outOff = aGCMBlockCipher.processBytes(input, 0, input.length, out, 0);
170  aGCMBlockCipher.doFinal(out, outOff);
171 
172  String plaintext = new String(out, Charset.forName(Util.UTF8_STRING_ENCODING));
173 
174  return plaintext;
175  } else if (getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A128CBC_PLUS_HS256
176  || getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A256CBC_PLUS_HS512) {
177  byte[] cipherText = Base64Util.base64urldecode(encodedCipherText);
178 
179  byte[] cek = KeyDerivationFunction.generateCek(contentMasterKey, getBlockEncryptionAlgorithm());
180  Cipher cipher = Cipher.getInstance(getBlockEncryptionAlgorithm().getAlgorithm());
181  IvParameterSpec ivParameter = new IvParameterSpec(initializationVector);
182  cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(cek, "AES"), ivParameter);
183  byte[] decodedPlainTextBytes = cipher.doFinal(cipherText);
184  String decodedPlainText = new String(decodedPlainTextBytes, Charset.forName(Util.UTF8_STRING_ENCODING));
185 
186  // Integrity check
187  String securedInputValue = new String(additionalAuthenticatedData, Charset.forName(Util.UTF8_STRING_ENCODING))
188  + "." + encodedCipherText;
189  byte[] cik = KeyDerivationFunction.generateCik(contentMasterKey, getBlockEncryptionAlgorithm());
190  SecretKey secretKey = new SecretKeySpec(cik, getBlockEncryptionAlgorithm().getIntegrityValueAlgorithm());
191  Mac mac = Mac.getInstance(getBlockEncryptionAlgorithm().getIntegrityValueAlgorithm());
192  mac.init(secretKey);
193  byte[] integrityValue = mac.doFinal(securedInputValue.getBytes(Util.UTF8_STRING_ENCODING));
194  if (!Arrays.equals(integrityValue, authenticationTag)) {
195  throw new InvalidJweException("The authentication tag is not valid");
196  }
197 
198  return decodedPlainText;
199  } else {
200  throw new InvalidJweException("The block encryption algorithm is not supported");
201  }
202  } catch (InvalidCipherTextException e) {
203  throw new InvalidJweException(e);
204  } catch (NoSuchPaddingException e) {
205  throw new InvalidJweException(e);
206  } catch (BadPaddingException e) {
207  throw new InvalidJweException(e);
208  } catch (InvalidAlgorithmParameterException e) {
209  throw new InvalidJweException(e);
210  } catch (NoSuchAlgorithmException e) {
211  throw new InvalidJweException(e);
212  } catch (IllegalBlockSizeException e) {
213  throw new InvalidJweException(e);
214  } catch (UnsupportedEncodingException e) {
215  throw new InvalidJweException(e);
216  } catch (NoSuchProviderException e) {
217  throw new InvalidJweException(e);
218  } catch (InvalidKeyException e) {
219  throw new InvalidJweException(e);
220  } catch (InvalidParameterException e) {
221  throw new InvalidJweException(e);
222  }
223  }
A128CBC_PLUS_HS256
Definition: BlockEncryptionAlgorithm.java:14
A256GCM
Definition: BlockEncryptionAlgorithm.java:17
A256CBC_PLUS_HS512
Definition: BlockEncryptionAlgorithm.java:15
BlockEncryptionAlgorithm getBlockEncryptionAlgorithm()
Definition: AbstractJweDecrypter.java:39
A128GCM
Definition: BlockEncryptionAlgorithm.java:16

◆ decryptEncryptionKey()

byte [] org.xdi.oxauth.model.jwe.JweDecrypterImpl.decryptEncryptionKey ( String  encodedEncryptedKey) throws InvalidJweException
inline
60  {
61  if (getKeyEncryptionAlgorithm() == null) {
62  throw new InvalidJweException("The key encryption algorithm is null");
63  }
64  if (encodedEncryptedKey == null) {
65  throw new InvalidJweException("The encoded encryption key is null");
66  }
67 
68  try {
69  if (getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.RSA_OAEP
70  || getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.RSA1_5) {
71  if (rsaPrivateKey == null && privateKey == null) {
72  throw new InvalidJweException("The RSA private key is null");
73  }
74 
75  //Cipher cipher = Cipher.getInstance(getKeyEncryptionAlgorithm().getAlgorithm(), "BC");
76  Cipher cipher = Cipher.getInstance(getKeyEncryptionAlgorithm().getAlgorithm());
77 
78  if (rsaPrivateKey != null) {
79  KeyFactory keyFactory = KeyFactory.getInstance(getKeyEncryptionAlgorithm().getFamily(), "BC");
80  RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
81  java.security.interfaces.RSAPrivateKey privKey = (java.security.interfaces.RSAPrivateKey) keyFactory.generatePrivate(privKeySpec);
82  cipher.init(Cipher.DECRYPT_MODE, privKey);
83  } else {
84  cipher.init(Cipher.DECRYPT_MODE, privateKey);
85  }
86 
87  byte[] decryptedKey = cipher.doFinal(Base64Util.base64urldecode(encodedEncryptedKey));
88 
89  return decryptedKey;
90  } else if (getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.A128KW
91  || getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.A256KW) {
92  if (sharedSymmetricKey == null) {
93  throw new InvalidJweException("The shared symmetric key is null");
94  }
95  if (sharedSymmetricKey.length != 16) { // 128 bit
96  MessageDigest sha = MessageDigest.getInstance("SHA-256");
98  sharedSymmetricKey = Arrays.copyOf(sharedSymmetricKey, 16);
99  }
100  byte[] encryptedKey = Base64Util.base64urldecode(encodedEncryptedKey);
101  SecretKeySpec keyEncryptionKey = new SecretKeySpec(sharedSymmetricKey, "AES");
102  AESWrapEngine aesWrapEngine = new AESWrapEngine();
103  CipherParameters params = new KeyParameter(keyEncryptionKey.getEncoded());
104  aesWrapEngine.init(false, params);
105  byte[] decryptedKey = aesWrapEngine.unwrap(encryptedKey, 0, encryptedKey.length);
106 
107  return decryptedKey;
108  } else {
109  throw new InvalidJweException("The key encryption algorithm is not supported");
110  }
111  } catch (NoSuchPaddingException e) {
112  throw new InvalidJweException(e);
113  } catch (NoSuchAlgorithmException e) {
114  throw new InvalidJweException(e);
115  } catch (IllegalBlockSizeException e) {
116  throw new InvalidJweException(e);
117  } catch (BadPaddingException e) {
118  throw new InvalidJweException(e);
119  } catch (NoSuchProviderException e) {
120  throw new InvalidJweException(e);
121  } catch (InvalidKeyException e) {
122  throw new InvalidJweException(e);
123  } catch (InvalidKeySpecException e) {
124  throw new InvalidJweException(e);
125  } catch (InvalidCipherTextException e) {
126  throw new InvalidJweException(e);
127  }
128  }
byte [] sharedSymmetricKey
Definition: JweDecrypterImpl.java:43
RSA_OAEP
Definition: KeyEncryptionAlgorithm.java:15
BigInteger getPrivateExponent()
Definition: RSAPrivateKey.java:48
A256KW
Definition: KeyEncryptionAlgorithm.java:17
BigInteger getModulus()
Definition: RSAPrivateKey.java:40
RSAPrivateKey rsaPrivateKey
Definition: JweDecrypterImpl.java:42
A128KW
Definition: KeyEncryptionAlgorithm.java:16
RSA1_5
Definition: KeyEncryptionAlgorithm.java:14
KeyEncryptionAlgorithm getKeyEncryptionAlgorithm()
Definition: AbstractJweDecrypter.java:31
PrivateKey privateKey
Definition: JweDecrypterImpl.java:41

◆ getBlockEncryptionAlgorithm()

BlockEncryptionAlgorithm org.xdi.oxauth.model.jwe.AbstractJweDecrypter.getBlockEncryptionAlgorithm ( )
inlineinherited

org.xdi.oxauth.model.jwe.JweDecrypterを実装しています。

39  {
41  }
BlockEncryptionAlgorithm blockEncryptionAlgorithm
Definition: AbstractJweDecrypter.java:29

◆ getKeyEncryptionAlgorithm()

KeyEncryptionAlgorithm org.xdi.oxauth.model.jwe.AbstractJweDecrypter.getKeyEncryptionAlgorithm ( )
inlineinherited

org.xdi.oxauth.model.jwe.JweDecrypterを実装しています。

31  {
33  }
KeyEncryptionAlgorithm keyEncryptionAlgorithm
Definition: AbstractJweDecrypter.java:28

◆ setBlockEncryptionAlgorithm()

void org.xdi.oxauth.model.jwe.AbstractJweDecrypter.setBlockEncryptionAlgorithm ( BlockEncryptionAlgorithm  blockEncryptionAlgorithm)
inlineinherited

org.xdi.oxauth.model.jwe.JweDecrypterを実装しています。

43  {
45  }
BlockEncryptionAlgorithm blockEncryptionAlgorithm
Definition: AbstractJweDecrypter.java:29

◆ setKeyEncryptionAlgorithm()

void org.xdi.oxauth.model.jwe.AbstractJweDecrypter.setKeyEncryptionAlgorithm ( KeyEncryptionAlgorithm  keyEncryptionAlgorithm)
inlineinherited

org.xdi.oxauth.model.jwe.JweDecrypterを実装しています。

35  {
37  }
KeyEncryptionAlgorithm keyEncryptionAlgorithm
Definition: AbstractJweDecrypter.java:28

メンバ詳解

◆ privateKey

PrivateKey org.xdi.oxauth.model.jwe.JweDecrypterImpl.privateKey
private

◆ rsaPrivateKey

RSAPrivateKey org.xdi.oxauth.model.jwe.JweDecrypterImpl.rsaPrivateKey
private

◆ sharedSymmetricKey

byte [] org.xdi.oxauth.model.jwe.JweDecrypterImpl.sharedSymmetricKey
private

このクラス詳解は次のファイルから抽出されました: