Search in sources :

Example 1 with JWEObject

use of com.nimbusds.jose.JWEObject in project perry by ca-cwds.

the class JwtService method generate.

public String generate(String id, String subject, Map<String, String> customJwtClaimsMap) {
    try {
        JWTClaimsSet claimsSet = prepareClaims(id, subject, customJwtClaimsMap);
        SignedJWT signedJWT = sign(claimsSet);
        String token;
        if (configuration.isEncryptionEnabled()) {
            JWEObject jweObject = encrypt(signedJWT);
            token = jweObject.serialize();
        } else {
            token = signedJWT.serialize();
        }
        return removeHeader(token);
    } catch (Exception e) {
        throw new JwtException(e);
    }
}
Also used : JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) JWEObject(com.nimbusds.jose.JWEObject) SignedJWT(com.nimbusds.jwt.SignedJWT) GeneralSecurityException(java.security.GeneralSecurityException)

Example 2 with JWEObject

use of com.nimbusds.jose.JWEObject in project fitpay-android-sdk by fitpay.

the class StringUtils method getDecryptedString.

/**
 * Get decrypted string
 *
 * @param type            key type
 * @param encryptedString encrypted string
 * @return decrypted string
 */
public static String getDecryptedString(@KeysManager.KeyType int type, String encryptedString) {
    KeysManager keysManager = KeysManager.getInstance();
    JWEObject jweObject;
    try {
        jweObject = JWEObject.parse(encryptedString);
        JWEHeader jweHeader = jweObject.getHeader();
        if (jweHeader.getKeyID() == null || jweHeader.getKeyID().equals(keysManager.getKeyId(type))) {
            jweObject.decrypt(new AESDecrypter(keysManager.getSecretKey(type)));
            if ("JWT".equals(jweObject.getHeader().getContentType())) {
                SignedJWT signedJwt = jweObject.getPayload().toSignedJWT();
                ECCKeyPair keyPair = keysManager.getPairForType(type);
                ECPublicKey key = null;
                if ("https://fit-pay.com".equals(signedJwt.getJWTClaimsSet().getIssuer())) {
                    key = (ECPublicKey) keysManager.getPublicKey("EC", Hex.hexStringToBytes(keyPair.getServerPublicKey()));
                } else {
                    key = (ECPublicKey) keysManager.getPublicKey("EC", Hex.hexStringToBytes(keyPair.getPublicKey()));
                }
                JWSVerifier verifier = new ECDSAVerifier(key);
                if (!signedJwt.verify(verifier)) {
                    throw new IllegalArgumentException("jwt did not pass signature validation");
                }
                return signedJwt.getJWTClaimsSet().getStringClaim("data");
            } else {
                return jweObject.getPayload().toString();
            }
        }
    } catch (Exception e) {
        FPLog.e(e);
    }
    return null;
}
Also used : ECDSAVerifier(com.nimbusds.jose.crypto.ECDSAVerifier) JWEHeader(com.nimbusds.jose.JWEHeader) ECPublicKey(java.security.interfaces.ECPublicKey) JWEObject(com.nimbusds.jose.JWEObject) JWSVerifier(com.nimbusds.jose.JWSVerifier) AESDecrypter(com.nimbusds.jose.crypto.AESDecrypter) SignedJWT(com.nimbusds.jwt.SignedJWT) ECCKeyPair(com.fitpay.android.api.models.security.ECCKeyPair) JOSEException(com.nimbusds.jose.JOSEException)

Example 3 with JWEObject

use of com.nimbusds.jose.JWEObject in project fitpay-android-sdk by fitpay.

the class StringUtils method getEncryptedString.

/**
 * Get encrypted string
 *
 * @param type            key type
 * @param decryptedString decrypted string
 * @return encrypted string
 */
public static String getEncryptedString(@KeysManager.KeyType int type, String decryptedString) {
    JWEAlgorithm alg = JWEAlgorithm.A256GCMKW;
    EncryptionMethod enc = EncryptionMethod.A256GCM;
    ECCKeyPair keyPair = KeysManager.getInstance().getPairForType(type);
    if (null == keyPair) {
        throw new IllegalStateException("No key pair available for type (type = " + type + ")");
    }
    JWEHeader.Builder jweHeaderBuilder = new JWEHeader.Builder(alg, enc).contentType("application/json").keyID(keyPair.getKeyId());
    JWEHeader header = jweHeaderBuilder.build();
    Payload payload = new Payload(decryptedString);
    JWEObject jweObject = new JWEObject(header, payload);
    try {
        JWEEncrypter encrypter = new AESEncrypter(KeysManager.getInstance().getSecretKey(type));
        jweObject.encrypt(encrypter);
    } catch (JOSEException e) {
        FPLog.e(e);
    }
    return jweObject.serialize();
}
Also used : AESEncrypter(com.nimbusds.jose.crypto.AESEncrypter) JWEHeader(com.nimbusds.jose.JWEHeader) JWEObject(com.nimbusds.jose.JWEObject) JWEAlgorithm(com.nimbusds.jose.JWEAlgorithm) Payload(com.nimbusds.jose.Payload) JWEEncrypter(com.nimbusds.jose.JWEEncrypter) EncryptionMethod(com.nimbusds.jose.EncryptionMethod) ECCKeyPair(com.fitpay.android.api.models.security.ECCKeyPair) JOSEException(com.nimbusds.jose.JOSEException)

Example 4 with JWEObject

use of com.nimbusds.jose.JWEObject in project perry by ca-cwds.

the class JwtService method decrypt.

private SignedJWT decrypt(String token) throws JwtException {
    try {
        SignedJWT signedJWT;
        JWEObject jweObject = JWEObject.parse(token);
        jweObject.decrypt(new DirectDecrypter(keyProvider.getEncryptingKey().getEncoded()));
        signedJWT = jweObject.getPayload().toSignedJWT();
        return signedJWT;
    } catch (Exception e) {
        throw new JwtException(e);
    }
}
Also used : JWEObject(com.nimbusds.jose.JWEObject) SignedJWT(com.nimbusds.jwt.SignedJWT) DirectDecrypter(com.nimbusds.jose.crypto.DirectDecrypter) GeneralSecurityException(java.security.GeneralSecurityException)

Example 5 with JWEObject

use of com.nimbusds.jose.JWEObject in project perry by ca-cwds.

the class JwtService method encrypt.

private JWEObject encrypt(SignedJWT signedJWT) throws JwtException {
    try {
        JWEObject jweObject = new JWEObject(jweHeader(), new Payload(signedJWT));
        jweObject.encrypt(new DirectEncrypter(keyProvider.getEncryptingKey().getEncoded()));
        return jweObject;
    } catch (Exception e) {
        throw new JwtException(e);
    }
}
Also used : DirectEncrypter(com.nimbusds.jose.crypto.DirectEncrypter) JWEObject(com.nimbusds.jose.JWEObject) Payload(com.nimbusds.jose.Payload) GeneralSecurityException(java.security.GeneralSecurityException)

Aggregations

JWEObject (com.nimbusds.jose.JWEObject)7 SignedJWT (com.nimbusds.jwt.SignedJWT)5 JOSEException (com.nimbusds.jose.JOSEException)4 GeneralSecurityException (java.security.GeneralSecurityException)3 ECCKeyPair (com.fitpay.android.api.models.security.ECCKeyPair)2 JWEHeader (com.nimbusds.jose.JWEHeader)2 Payload (com.nimbusds.jose.Payload)2 RSADecrypter (com.nimbusds.jose.crypto.RSADecrypter)2 RSASSAVerifier (com.nimbusds.jose.crypto.RSASSAVerifier)2 RSAKey (com.nimbusds.jose.jwk.RSAKey)2 ParseException (java.text.ParseException)2 EncryptionMethod (com.nimbusds.jose.EncryptionMethod)1 JWEAlgorithm (com.nimbusds.jose.JWEAlgorithm)1 JWEEncrypter (com.nimbusds.jose.JWEEncrypter)1 JWSVerifier (com.nimbusds.jose.JWSVerifier)1 AESDecrypter (com.nimbusds.jose.crypto.AESDecrypter)1 AESEncrypter (com.nimbusds.jose.crypto.AESEncrypter)1 DirectDecrypter (com.nimbusds.jose.crypto.DirectDecrypter)1 DirectEncrypter (com.nimbusds.jose.crypto.DirectEncrypter)1 ECDSAVerifier (com.nimbusds.jose.crypto.ECDSAVerifier)1