Package com.nimbusds.jose

Examples of com.nimbusds.jose.JOSEException


    } catch (Exception e) {

      // java.security.NoSuchAlgorithmException
      // java.security.InvalidKeyException
      // javax.crypto.IllegalBlockSizeException
      throw new JOSEException("Couldn't encrypt Content Encryption Key (CEK): " + e.getMessage(), e);
    }
  }
View Full Code Here


      cipher.init(Cipher.DECRYPT_MODE, priv);
      byte[] secretKeyBytes = cipher.doFinal(encryptedCEK);

      if (8 * secretKeyBytes.length != keyLength) {

        throw new JOSEException("CEK key length mismatch: " +
                          secretKeyBytes.length + " != " + keyLength);
      }

      return new SecretKeySpec(secretKeyBytes, "AES");

    } catch (Exception e) {

      // java.security.NoSuchAlgorithmException
      // java.security.InvalidKeyException
      // javax.crypto.IllegalBlockSizeException
      throw new JOSEException("Couldn't decrypt Content Encryption Key (CEK): " + e.getMessage(), e);
    }
  }
View Full Code Here

      // JWA mandates salt length must equal hash
      pssSpec = new PSSParameterSpec("SHA512", "MGF1", MGF1ParameterSpec.SHA512, 64, 1);

    } else {
     
      throw new JOSEException("Unsupported RSASSA algorithm, must be RS256, RS384, RS512, PS256, PS384 or PS512");
    }

    Signature signature;

    try {
      if (provider != null) {
        signature = Signature.getInstance(internalAlgName, provider);
      } else {
        signature = Signature.getInstance(internalAlgName);
      }

    } catch (NoSuchAlgorithmException e) {

      throw new JOSEException("Unsupported RSASSA algorithm: " + e.getMessage(), e);
    }


    if (pssSpec != null) {

      try {
        signature.setParameter(pssSpec);

      } catch (InvalidAlgorithmParameterException e) {

        throw new JOSEException("Invalid RSASSA-PSS salt length parameter: " + e.getMessage(), e);
      }
    }


    return signature;
View Full Code Here

    throws JOSEException {

    // Validate required JWE parts
    if (encryptedKey == null) {

      throw new JOSEException("The encrypted key must not be null");
   

    if (iv == null) {

      throw new JOSEException("The initialization vector (IV) must not be null");
    }

    if (authTag == null) {

      throw new JOSEException("The authentication tag must not be null");
    }

    if (! critParamChecker.headerPasses(header)) {

      throw new JOSEException("Unsupported critical header parameter");
    }
   

    // Derive the content encryption key
    JWEAlgorithm alg = header.getAlgorithm();

    SecretKey cek;

    if (alg.equals(JWEAlgorithm.RSA1_5)) {

      int keyLength = header.getEncryptionMethod().cekBitLength();

      SecureRandom randomGen = getSecureRandom();
      SecretKey randomCEK = AES.generateKey(keyLength, randomGen);

      try {
        cek = RSA1_5.decryptCEK(privateKey, encryptedKey.decode(), keyLength, keyEncryptionProvider);
     
      } catch (Exception e) {

        // Protect against MMA attack by generating random CEK on failure,
        // see http://www.ietf.org/mail-archive/web/jose/current/msg01832.html
        cek = randomCEK;
      }
   
    } else if (alg.equals(JWEAlgorithm.RSA_OAEP)) {

      cek = RSA_OAEP.decryptCEK(privateKey, encryptedKey.decode(), keyEncryptionProvider);

    } else if (alg.equals(JWEAlgorithm.RSA_OAEP_256)) {
     
      cek = RSA_OAEP_256.decryptCEK(privateKey, encryptedKey.decode(), keyEncryptionProvider);
     
    } else {
   
      throw new JOSEException("Unsupported JWE algorithm, must be RSA1_5 or RSA_OAEP");
    }

    // Compose the AAD
    byte[] aad = StringUtils.toByteArray(header.toBase64URL().toString());

    // Decrypt the cipher text according to the JWE enc
    EncryptionMethod enc = header.getEncryptionMethod();

    byte[] plainText;

    if (enc.equals(EncryptionMethod.A128CBC_HS256) ||
        enc.equals(EncryptionMethod.A192CBC_HS384) ||
        enc.equals(EncryptionMethod.A256CBC_HS512)    ) {

      plainText = AESCBC.decryptAuthenticated(
        cek,
        iv.decode(),
        cipherText.decode(),
        aad,
        authTag.decode(),
        contentEncryptionProvider,
        macProvider);

    } else if (enc.equals(EncryptionMethod.A128GCM) ||
         enc.equals(EncryptionMethod.A192GCM) ||
         enc.equals(EncryptionMethod.A256GCM)    ) {

      plainText = AESGCM.decrypt(
        cek,
        iv.decode(),
        cipherText.decode(),
        aad,
        authTag.decode(),
        contentEncryptionProvider);

    } else if (enc.equals(EncryptionMethod.A128CBC_HS256_DEPRECATED) ||
         enc.equals(EncryptionMethod.A256CBC_HS512_DEPRECATED)    ) {

      plainText = AESCBC.decryptWithConcatKDF(
        header,
        cek,
        encryptedKey,
        iv,
        cipherText,
        authTag,
        contentEncryptionProvider,
        macProvider);

    } else {

      throw new JOSEException("Unsupported encryption method, must be A128CBC_HS256, A192CBC_HS384, A256CBC_HS512, A128GCM, A192GCM or A256GCM");
    }


    // Apply decompression if requested
    return DeflateHelper.applyDecompression(header, plainText);
View Full Code Here

      return "HMACSHA512";

    } else {
     
      throw new JOSEException("Unsupported HMAC algorithm, must be HS256, HS384 or HS512");
    }
  }
View Full Code Here

      return 132;
   
    } else {

      throw new JOSEException("Unsupported ECDSA algorithm, must be ES256, ES384 or ES512");
    }
  }
View Full Code Here

      oid = SECObjectIdentifiers.secp521r1;
      digest = new SHA512Digest();

    } else {
      throw new JOSEException("Unsupported ECDSA algorithm, must be ES256, ES384 or ES512");
    }

    X9ECParameters x9ECParams = SECNamedCurves.getByOID(oid);

    return new ECDSAParameters(x9ECParams, digest);
View Full Code Here

    try {
      return KeyGenerator.getInstance("AES", BouncyCastleProviderSingleton.getInstance());

    } catch (NoSuchAlgorithmException e) {

      throw new JOSEException(e.getMessage(), e);
    }
  }
View Full Code Here

        cipher.init(Cipher.DECRYPT_MODE, keyspec, ivSpec);
      }

    } catch (Exception e) {

      throw new JOSEException(e.getMessage(), e);
    }

    return cipher;
  }
View Full Code Here

    try {
      return cipher.doFinal(plainText)
   
    } catch (Exception e) {

      throw new JOSEException(e.getMessage(), e);
    }
  }
View Full Code Here

TOP

Related Classes of com.nimbusds.jose.JOSEException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.