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

公開メンバ関数

 JweEncrypterImpl (KeyEncryptionAlgorithm keyEncryptionAlgorithm, BlockEncryptionAlgorithm blockEncryptionAlgorithm, byte[] sharedSymmetricKey)
 
 JweEncrypterImpl (KeyEncryptionAlgorithm keyEncryptionAlgorithm, BlockEncryptionAlgorithm blockEncryptionAlgorithm, PublicKey publicKey)
 
String generateEncryptedKey (byte[] contentMasterKey) throws InvalidJweException
 
Pair< String, String > generateCipherTextAndIntegrityValue (byte[] contentMasterKey, byte[] initializationVector, byte[] additionalAuthenticatedData, byte[] plainText) throws InvalidJweException
 
KeyEncryptionAlgorithm getKeyEncryptionAlgorithm ()
 
BlockEncryptionAlgorithm getBlockEncryptionAlgorithm ()
 
Jwe encrypt (Jwe jwe) throws InvalidJweException
 

非公開変数類

PublicKey publicKey
 
byte [] sharedSymmetricKey
 

詳解

著者
Javier Rojas Blum
バージョン
August 17, 2016

構築子と解体子

◆ JweEncrypterImpl() [1/2]

org.xdi.oxauth.model.jwe.JweEncrypterImpl.JweEncrypterImpl ( KeyEncryptionAlgorithm  keyEncryptionAlgorithm,
BlockEncryptionAlgorithm  blockEncryptionAlgorithm,
byte []  sharedSymmetricKey 
)
inline
42  {
44  if (sharedSymmetricKey != null) {
46  }
47  }
KeyEncryptionAlgorithm keyEncryptionAlgorithm
Definition: AbstractJweEncrypter.java:26
byte [] sharedSymmetricKey
Definition: JweEncrypterImpl.java:40
BlockEncryptionAlgorithm blockEncryptionAlgorithm
Definition: AbstractJweEncrypter.java:27

◆ JweEncrypterImpl() [2/2]

org.xdi.oxauth.model.jwe.JweEncrypterImpl.JweEncrypterImpl ( KeyEncryptionAlgorithm  keyEncryptionAlgorithm,
BlockEncryptionAlgorithm  blockEncryptionAlgorithm,
PublicKey  publicKey 
)
inline
49  {
51  this.publicKey = publicKey;
52  }
KeyEncryptionAlgorithm keyEncryptionAlgorithm
Definition: AbstractJweEncrypter.java:26
PublicKey publicKey
Definition: JweEncrypterImpl.java:39
BlockEncryptionAlgorithm blockEncryptionAlgorithm
Definition: AbstractJweEncrypter.java:27

関数詳解

◆ encrypt()

Jwe org.xdi.oxauth.model.jwe.AbstractJweEncrypter.encrypt ( Jwe  jwe) throws InvalidJweException
inlineinherited

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

43  {
44  try {
45  jwe.setEncodedHeader(jwe.getHeader().toBase64JsonObject());
46 
47  byte[] contentMasterKey = new byte[blockEncryptionAlgorithm.getCmkLength() / 8];
48  SecureRandom random = new SecureRandom();
49  random.nextBytes(contentMasterKey);
50 
51  String encodedEncryptedKey = generateEncryptedKey(contentMasterKey);
52  jwe.setEncodedEncryptedKey(encodedEncryptedKey);
53 
54  byte[] initializationVector = new byte[blockEncryptionAlgorithm.getInitVectorLength() / 8];
55  random.nextBytes(initializationVector);
56  String encodedInitializationVector = Base64Util.base64urlencode(initializationVector);
57  jwe.setEncodedInitializationVector(encodedInitializationVector);
58 
59  Pair<String, String> result = generateCipherTextAndIntegrityValue(contentMasterKey, initializationVector,
60  jwe.getAdditionalAuthenticatedData().getBytes(Util.UTF8_STRING_ENCODING),
61  jwe.getClaims().toBase64JsonObject().getBytes(Util.UTF8_STRING_ENCODING));
62  jwe.setEncodedCiphertext(result.getFirst());
63  jwe.setEncodedIntegrityValue(result.getSecond());
64 
65  return jwe;
66  } catch (InvalidJwtException e) {
67  throw new InvalidJweException(e);
68  } catch (UnsupportedEncodingException e) {
69  throw new InvalidJweException(e);
70  }
71  }
int getInitVectorLength()
Definition: BlockEncryptionAlgorithm.java:75
abstract String generateEncryptedKey(byte[] contentMasterKey)
abstract Pair< String, String > generateCipherTextAndIntegrityValue(byte[] contentMasterKey, byte[] initializationVector, byte[] additionalAuthenticatedData, byte[] plainText)
int getCmkLength()
Definition: BlockEncryptionAlgorithm.java:71
BlockEncryptionAlgorithm blockEncryptionAlgorithm
Definition: AbstractJweEncrypter.java:27

◆ generateCipherTextAndIntegrityValue()

Pair<String, String> org.xdi.oxauth.model.jwe.JweEncrypterImpl.generateCipherTextAndIntegrityValue ( byte []  contentMasterKey,
byte []  initializationVector,
byte []  additionalAuthenticatedData,
byte []  plainText 
) throws InvalidJweException
inline
119  {
120  if (getBlockEncryptionAlgorithm() == null) {
121  throw new InvalidJweException("The block encryption algorithm is null");
122  }
123  if (contentMasterKey == null) {
124  throw new InvalidJweException("The content master key (CMK) is null");
125  }
126  if (initializationVector == null) {
127  throw new InvalidJweException("The initialization vector is null");
128  }
129  if (additionalAuthenticatedData == null) {
130  throw new InvalidJweException("The additional authentication data is null");
131  }
132  if (plainText == null) {
133  throw new InvalidJweException("The plain text to encrypt is null");
134  }
135 
136  try {
137  if (getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A128GCM
138  || getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A256GCM) {
139  SecretKey secretKey = new SecretKeySpec(contentMasterKey, "AES");
140  KeyParameter key = new KeyParameter(contentMasterKey);
141  final int MAC_SIZE_BITS = 128;
142  AEADParameters aeadParameters = new AEADParameters(key, MAC_SIZE_BITS, initializationVector,
143  additionalAuthenticatedData);
144 
145  final int macSize = aeadParameters.getMacSize() / 8;
146  BlockCipher blockCipher = new AESEngine();
147  CipherParameters params = new KeyParameter(secretKey.getEncoded());
148  blockCipher.init(true, params);
149  GCMBlockCipher aGCMBlockCipher = new GCMBlockCipher(blockCipher);
150  aGCMBlockCipher.init(true, aeadParameters);
151  int len = aGCMBlockCipher.getOutputSize(plainText.length);
152  byte[] out = new byte[len];
153  int outOff = aGCMBlockCipher.processBytes(plainText, 0, plainText.length, out, 0);
154  outOff += aGCMBlockCipher.doFinal(out, outOff);
155  byte[] cipherText = new byte[outOff - macSize];
156  System.arraycopy(out, 0, cipherText, 0, cipherText.length);
157  byte[] authenticationTag = new byte[macSize];
158  System.arraycopy(out, outOff - macSize, authenticationTag, 0, authenticationTag.length);
159 
160  String encodedCipherText = Base64Util.base64urlencode(cipherText);
161  String encodedAuthenticationTag = Base64Util.base64urlencode(authenticationTag);
162 
163  return new Pair<String, String>(encodedCipherText, encodedAuthenticationTag);
164  } else if (getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A128CBC_PLUS_HS256
165  || getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A256CBC_PLUS_HS512) {
166  byte[] cek = KeyDerivationFunction.generateCek(contentMasterKey, getBlockEncryptionAlgorithm());
167  IvParameterSpec parameters = new IvParameterSpec(initializationVector);
168  Cipher cipher = Cipher.getInstance(getBlockEncryptionAlgorithm().getAlgorithm(), "BC");
169  //Cipher cipher = Cipher.getInstance(getBlockEncryptionAlgorithm().getAlgorithm());
170  SecretKeySpec secretKeySpec = new SecretKeySpec(cek, "AES");
171  cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, parameters);
172  byte[] cipherText = cipher.doFinal(plainText);
173 
174  String encodedCipherText = Base64Util.base64urlencode(cipherText);
175 
176  String securedInputValue = new String(additionalAuthenticatedData, Charset.forName(Util.UTF8_STRING_ENCODING))
177  + "." + encodedCipherText;
178 
179  byte[] cik = KeyDerivationFunction.generateCik(contentMasterKey, getBlockEncryptionAlgorithm());
180  SecretKey secretKey = new SecretKeySpec(cik, getBlockEncryptionAlgorithm().getIntegrityValueAlgorithm());
181  Mac mac = Mac.getInstance(getBlockEncryptionAlgorithm().getIntegrityValueAlgorithm());
182  mac.init(secretKey);
183  byte[] integrityValue = mac.doFinal(securedInputValue.getBytes(Util.UTF8_STRING_ENCODING));
184 
185  String encodedIntegrityValue = Base64Util.base64urlencode(integrityValue);
186 
187  return new Pair<String, String>(encodedCipherText, encodedIntegrityValue);
188  } else {
189  throw new InvalidJweException("The block encryption algorithm is not supported");
190  }
191  } catch (InvalidCipherTextException e) {
192  throw new InvalidJweException(e);
193  } catch (NoSuchAlgorithmException e) {
194  throw new InvalidJweException(e);
195  } catch (UnsupportedEncodingException e) {
196  throw new InvalidJweException(e);
197  } catch (NoSuchProviderException e) {
198  throw new InvalidJweException(e);
199  } catch (IllegalBlockSizeException e) {
200  throw new InvalidJweException(e);
201  } catch (InvalidKeyException e) {
202  throw new InvalidJweException(e);
203  } catch (BadPaddingException e) {
204  throw new InvalidJweException(e);
205  } catch (InvalidAlgorithmParameterException e) {
206  throw new InvalidJweException(e);
207  } catch (NoSuchPaddingException e) {
208  throw new InvalidJweException(e);
209  } catch (InvalidParameterException e) {
210  throw new InvalidJweException(e);
211  }
212  }
A128CBC_PLUS_HS256
Definition: BlockEncryptionAlgorithm.java:14
A256GCM
Definition: BlockEncryptionAlgorithm.java:17
A256CBC_PLUS_HS512
Definition: BlockEncryptionAlgorithm.java:15
BlockEncryptionAlgorithm getBlockEncryptionAlgorithm()
Definition: AbstractJweEncrypter.java:38
A128GCM
Definition: BlockEncryptionAlgorithm.java:16

◆ generateEncryptedKey()

String org.xdi.oxauth.model.jwe.JweEncrypterImpl.generateEncryptedKey ( byte []  contentMasterKey) throws InvalidJweException
inline
55  {
56  if (getKeyEncryptionAlgorithm() == null) {
57  throw new InvalidJweException("The key encryption algorithm is null");
58  }
59  if (contentMasterKey == null) {
60  throw new InvalidJweException("The content master key (CMK) is null");
61  }
62 
63  try {
64  if (getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.RSA_OAEP
65  || getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.RSA1_5) {
66  if (publicKey != null) {
67  Cipher cipher = Cipher.getInstance(getKeyEncryptionAlgorithm().getAlgorithm(), "BC");
68  //Cipher cipher = Cipher.getInstance(getKeyEncryptionAlgorithm().getAlgorithm());
69 
70  cipher.init(Cipher.ENCRYPT_MODE, publicKey);
71  byte[] encryptedKey = cipher.doFinal(contentMasterKey);
72 
73  String encodedEncryptedKey = Base64Util.base64urlencode(encryptedKey);
74  return encodedEncryptedKey;
75  } else {
76  throw new InvalidJweException("The RSA public key is null");
77  }
78 
79  } else if (getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.A128KW
80  || getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.A256KW) {
81  if (sharedSymmetricKey == null) {
82  throw new InvalidJweException("The shared symmetric key is null");
83  }
84  if (sharedSymmetricKey.length != 16) { // 128 bit
85  MessageDigest sha = MessageDigest.getInstance("SHA-256");
87  sharedSymmetricKey = Arrays.copyOf(sharedSymmetricKey, 16);
88  }
89 
90  SecretKeySpec keyEncryptionKey = new SecretKeySpec(sharedSymmetricKey, "AES");
91  AESWrapEngine aesWrapEngine = new AESWrapEngine();
92  CipherParameters params = new KeyParameter(keyEncryptionKey.getEncoded());
93  aesWrapEngine.init(true, params);
94  byte[] wrappedKey = aesWrapEngine.wrap(contentMasterKey, 0, contentMasterKey.length);
95 
96  String encodedEncryptedKey = Base64Util.base64urlencode(wrappedKey);
97  return encodedEncryptedKey;
98  } else {
99  throw new InvalidJweException("The key encryption algorithm is not supported");
100  }
101  } catch (NoSuchPaddingException e) {
102  throw new InvalidJweException(e);
103  } catch (NoSuchAlgorithmException e) {
104  throw new InvalidJweException(e);
105  } catch (IllegalBlockSizeException e) {
106  throw new InvalidJweException(e);
107  } catch (BadPaddingException e) {
108  throw new InvalidJweException(e);
109  } catch (InvalidKeyException e) {
110  throw new InvalidJweException(e);
111  } catch (NoSuchProviderException e) {
112  throw new InvalidJweException(e);
113  }
114  }
KeyEncryptionAlgorithm getKeyEncryptionAlgorithm()
Definition: AbstractJweEncrypter.java:34
PublicKey publicKey
Definition: JweEncrypterImpl.java:39
byte [] sharedSymmetricKey
Definition: JweEncrypterImpl.java:40
RSA_OAEP
Definition: KeyEncryptionAlgorithm.java:15
A256KW
Definition: KeyEncryptionAlgorithm.java:17
A128KW
Definition: KeyEncryptionAlgorithm.java:16
RSA1_5
Definition: KeyEncryptionAlgorithm.java:14

◆ getBlockEncryptionAlgorithm()

BlockEncryptionAlgorithm org.xdi.oxauth.model.jwe.AbstractJweEncrypter.getBlockEncryptionAlgorithm ( )
inlineinherited
38  {
40  }
BlockEncryptionAlgorithm blockEncryptionAlgorithm
Definition: AbstractJweEncrypter.java:27

◆ getKeyEncryptionAlgorithm()

KeyEncryptionAlgorithm org.xdi.oxauth.model.jwe.AbstractJweEncrypter.getKeyEncryptionAlgorithm ( )
inlineinherited
34  {
36  }
KeyEncryptionAlgorithm keyEncryptionAlgorithm
Definition: AbstractJweEncrypter.java:26

メンバ詳解

◆ publicKey

PublicKey org.xdi.oxauth.model.jwe.JweEncrypterImpl.publicKey
private

◆ sharedSymmetricKey

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

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