Search in sources :

Example 1 with ECCKeyPair

use of com.fitpay.android.api.models.security.ECCKeyPair 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 2 with ECCKeyPair

use of com.fitpay.android.api.models.security.ECCKeyPair 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 3 with ECCKeyPair

use of com.fitpay.android.api.models.security.ECCKeyPair in project fitpay-android-sdk by fitpay.

the class KeysManager method createPairForType.

public ECCKeyPair createPairForType(@KeyType int type) throws Exception {
    removePairForType(type);
    ECCKeyPair keyPair = createECCKeyPair();
    mKeysMap.put(type, keyPair);
    return keyPair;
}
Also used : ECCKeyPair(com.fitpay.android.api.models.security.ECCKeyPair)

Example 4 with ECCKeyPair

use of com.fitpay.android.api.models.security.ECCKeyPair in project fitpay-android-sdk by fitpay.

the class KeysManager method createECCKeyPair.

// Create the public and private keys
private ECCKeyPair createECCKeyPair() throws Exception {
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(ALGORITHM, SecurityProvider.getInstance().getProvider());
    keyGenerator.initialize(new ECGenParameterSpec(EC_CURVE), new SecureRandom());
    KeyPair keyPair = keyGenerator.generateKeyPair();
    ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
    ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
    ECCKeyPair eccKeyPair = new ECCKeyPair();
    eccKeyPair.setKeyId(UUID.randomUUID().toString());
    eccKeyPair.setPrivateKey(Hex.bytesToHexString(privateKey.getEncoded()));
    eccKeyPair.setPublicKey(Hex.bytesToHexString(publicKey.getEncoded()));
    return eccKeyPair;
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) ECCKeyPair(com.fitpay.android.api.models.security.ECCKeyPair) ECPublicKey(java.security.interfaces.ECPublicKey) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) ECCKeyPair(com.fitpay.android.api.models.security.ECCKeyPair)

Example 5 with ECCKeyPair

use of com.fitpay.android.api.models.security.ECCKeyPair in project fitpay-android-sdk by fitpay.

the class KeysManager method getSecretKey.

public SecretKey getSecretKey(@KeyType int type) {
    ECCKeyPair keyPair = getPairForType(type);
    SecretKey secretKey = keyPair.getSecretKey();
    if (secretKey == null) {
        secretKey = createSecretKey(keyPair.getPrivateKey(), keyPair.getServerPublicKey());
        keyPair.setSecretKey(secretKey);
    }
    return secretKey;
}
Also used : SecretKey(javax.crypto.SecretKey) ECCKeyPair(com.fitpay.android.api.models.security.ECCKeyPair)

Aggregations

ECCKeyPair (com.fitpay.android.api.models.security.ECCKeyPair)6 JOSEException (com.nimbusds.jose.JOSEException)2 JWEHeader (com.nimbusds.jose.JWEHeader)2 JWEObject (com.nimbusds.jose.JWEObject)2 ECPublicKey (java.security.interfaces.ECPublicKey)2 ApiCallback (com.fitpay.android.api.callbacks.ApiCallback)1 EncryptionMethod (com.nimbusds.jose.EncryptionMethod)1 JWEAlgorithm (com.nimbusds.jose.JWEAlgorithm)1 JWEEncrypter (com.nimbusds.jose.JWEEncrypter)1 JWSVerifier (com.nimbusds.jose.JWSVerifier)1 Payload (com.nimbusds.jose.Payload)1 AESDecrypter (com.nimbusds.jose.crypto.AESDecrypter)1 AESEncrypter (com.nimbusds.jose.crypto.AESEncrypter)1 ECDSAVerifier (com.nimbusds.jose.crypto.ECDSAVerifier)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1 KeyPair (java.security.KeyPair)1 KeyPairGenerator (java.security.KeyPairGenerator)1 SecureRandom (java.security.SecureRandom)1 ECPrivateKey (java.security.interfaces.ECPrivateKey)1 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)1