parent
caf2a7b494
commit
cee44de3d1
@ -0,0 +1,29 @@
|
||||
package org.mitre.jwt.encryption;
|
||||
|
||||
import org.mitre.jwe.model.Jwe;
|
||||
import org.mitre.jwt.encryption.impl.RsaDecrypter;
|
||||
|
||||
|
||||
public abstract class AbstractJweDecrypter implements JwtDecrypter {
|
||||
|
||||
@Override
|
||||
public Jwe decrypt(Jwe jwe) {
|
||||
String alg = jwe.getHeader().getAlgorithm();
|
||||
if(alg.equals("RS256") || alg.equals("RS384") || alg.equals("RS512")) {
|
||||
|
||||
RsaDecrypter decrypter = new RsaDecrypter(jwe);
|
||||
jwe.setCiphertext(decrypter.decryptCipherText(jwe).getBytes()); //TODO: When decrypting, should it return a jwe or jwt?
|
||||
jwe.setEncryptedKey(decrypter.decryptEncryptionKey(jwe));
|
||||
|
||||
} else if(alg.equals("HS256") || alg.equals("HS384") || alg.equals("HS512")){
|
||||
|
||||
throw new IllegalArgumentException("Cannot use Hmac for decryption");
|
||||
|
||||
} else {
|
||||
throw new IllegalArgumentException("Not a valid decrypting algorithm");
|
||||
}
|
||||
|
||||
return jwe;
|
||||
}
|
||||
|
||||
}
|
@ -1,41 +1,145 @@
|
||||
package org.mitre.jwt.encryption;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
|
||||
import org.mitre.jwe.model.Jwe;
|
||||
import org.mitre.jwt.signer.impl.HmacSigner;
|
||||
import org.mitre.jwt.signer.impl.RsaSigner;
|
||||
import org.mitre.jwe.model.JweHeader;
|
||||
import org.mitre.jwt.model.JwtClaims;
|
||||
|
||||
|
||||
public abstract class AbstractJweEncrypter implements JwtEncrypter {
|
||||
|
||||
private Jwe jwe;
|
||||
|
||||
private JweHeader header;
|
||||
|
||||
private JwtClaims claims;
|
||||
|
||||
private String signature;
|
||||
|
||||
private byte[] encryptedKey;
|
||||
|
||||
private byte[] cipherText;
|
||||
|
||||
public Jwe getJwe() {
|
||||
return jwe;
|
||||
}
|
||||
|
||||
public void setJwe(Jwe jwe) {
|
||||
this.jwe = jwe;
|
||||
}
|
||||
|
||||
public byte[] getEncryptecKey() {
|
||||
return encryptedKey;
|
||||
}
|
||||
|
||||
public void setEncryptedKey(byte[] encryptedKey) {
|
||||
this.encryptedKey = encryptedKey;
|
||||
}
|
||||
|
||||
public JweHeader getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public void setHeader(JweHeader header) {
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
public JwtClaims getClaims() {
|
||||
return claims;
|
||||
}
|
||||
|
||||
public void setClaims(JwtClaims claims) {
|
||||
this.claims = claims;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Jwe encrypt(Jwe jwe) {
|
||||
public String getSignature() {
|
||||
return signature;
|
||||
}
|
||||
|
||||
public void setSignature(String signature) {
|
||||
this.signature = signature;
|
||||
}
|
||||
|
||||
public byte[] getCipherText() {
|
||||
return cipherText;
|
||||
}
|
||||
|
||||
public void setCipherText(byte[] cipherText) {
|
||||
this.cipherText = cipherText;
|
||||
}
|
||||
|
||||
|
||||
public byte[] encryptKey(Jwe jwe){
|
||||
|
||||
RSAPublicKey publicKey = null; // TODO: placeholder
|
||||
RSAPrivateKey privateKey = null;
|
||||
|
||||
jwe.setCiphertext(encryptClaims(jwe));
|
||||
jwe.setEncryptedKey(encryptKey(jwe));
|
||||
Cipher cipher;
|
||||
try {
|
||||
cipher = Cipher.getInstance("RSA");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||
encryptedKey = cipher.doFinal(privateKey.getEncoded());
|
||||
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchPaddingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (InvalidKeyException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (BadPaddingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return encryptedKey;
|
||||
|
||||
String alg = jwe.getHeader().getAlgorithm();
|
||||
if(alg.equals("RS256") || alg.equals("RS384") || alg.equals("RS512")) {
|
||||
RsaSigner rsaSigner = new RsaSigner();
|
||||
try {
|
||||
jwe = (Jwe) rsaSigner.sign(jwe);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if(alg.equals("HS256") || alg.equals("HS384") || alg.equals("HS512")){
|
||||
HmacSigner hmacSigner = new HmacSigner();
|
||||
try {
|
||||
jwe = (Jwe) hmacSigner.sign(jwe);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Not a valid signing algorithm");
|
||||
}
|
||||
|
||||
public byte[] encryptClaims(Jwe jwe) {
|
||||
|
||||
RSAPublicKey publicKey = null; // TODO: placeholder
|
||||
|
||||
Cipher cipher;
|
||||
try {
|
||||
cipher = Cipher.getInstance("RSA");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||
cipherText = cipher.doFinal(claims.toString().getBytes());
|
||||
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchPaddingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (InvalidKeyException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (BadPaddingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return jwe;
|
||||
return cipherText;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
package org.mitre.jwt.encryption;
|
||||
|
||||
public class IntegrityProtection {
|
||||
|
||||
}
|
@ -1,11 +1,14 @@
|
||||
package org.mitre.jwt.encryption;
|
||||
|
||||
import org.mitre.jwt.model.Jwt;
|
||||
import org.mitre.jwe.model.Jwe;
|
||||
|
||||
public class JwtDecrypter {
|
||||
public interface JwtDecrypter {
|
||||
|
||||
public Jwe decrypt(Jwe jwe);
|
||||
|
||||
public String decryptCipherText(Jwe jwe);
|
||||
|
||||
public byte[] decryptEncryptionKey(Jwe jwe);
|
||||
|
||||
public JwtDecrypter(Jwt jwt) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,56 +0,0 @@
|
||||
package org.mitre.jwt.encryption.impl;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.Key;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PublicKey;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
|
||||
public class RsaJwtDecrypter {
|
||||
|
||||
PublicKey publicKey;
|
||||
byte[] wrappedKey;
|
||||
String algorithm;
|
||||
|
||||
public RsaJwtDecrypter(String algorithm, PublicKey publicKey, byte[] wrappedKey){
|
||||
setPublicKey(publicKey);
|
||||
setWrappedKey(wrappedKey);
|
||||
setAlgorithm(algorithm);
|
||||
}
|
||||
|
||||
public PublicKey getPublicKey() {
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
public void setPublicKey(PublicKey publicKey) {
|
||||
this.publicKey = publicKey;
|
||||
}
|
||||
|
||||
public byte[] getWrappedKey() {
|
||||
return wrappedKey;
|
||||
}
|
||||
|
||||
public void setWrappedKey(byte[] wrappedKey) {
|
||||
this.wrappedKey = wrappedKey;
|
||||
}
|
||||
|
||||
public String getAlgorithm() {
|
||||
return algorithm;
|
||||
}
|
||||
|
||||
public void setAlgorithm(String algorithm) {
|
||||
this.algorithm = algorithm;
|
||||
}
|
||||
|
||||
public Key keyDecrypter() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
|
||||
|
||||
Cipher cipher = Cipher.getInstance("RSA");
|
||||
cipher.init(Cipher.UNWRAP_MODE, publicKey);
|
||||
Key unwrappedKey = cipher.unwrap(wrappedKey, algorithm, Cipher.PRIVATE_KEY);
|
||||
|
||||
return unwrappedKey;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue