Package com.nimbusds.jose

Examples of com.nimbusds.jose.JOSEException


    try {
      verifier.initVerify(publicKey);

    } catch (InvalidKeyException e) {

      throw new JOSEException("Invalid public RSA key: " + e.getMessage(), e);
    }

    try {
      verifier.update(signedContent);
      return verifier.verify(signature.decode());
View Full Code Here


    super(SUPPORTED_ALGORITHMS, SUPPORTED_ENCRYPTION_METHODS);

    if (! key.getAlgorithm().equals("AES")) {

      throw new JOSEException("The algorithm of the shared symmetric key must be AES");
    }
   

    byte[] keyBytes = key.getEncoded();

    if (keyBytes.length != 16 &&
        keyBytes.length != 24 &&
        keyBytes.length != 32 &&
        keyBytes.length != 48 &&
        keyBytes.length != 64) {

      throw new JOSEException("The length of the shared symmetric key must be 128 bits (16 bytes), 192 bits (24 bytes), 256 bits (32 bytes), 384 bits (48 bytes) or 512 bites (64 bytes)");
    }

    cek = key;
  }
View Full Code Here

    throws JOSEException {

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

      throw new JOSEException("Unexpected encrypted key, must be omitted");
   

    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");
    }
   

    JWEAlgorithm alg = header.getAlgorithm();

    if (! alg.equals(JWEAlgorithm.DIR)) {

      throw new JOSEException("Unsupported algorithm, must be \"dir\"");
    }

    if (! critParamChecker.headerPasses(header)) {

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

    // 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(getKey(), 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(getKey(), iv.decode(), cipherText.decode(), aad, authTag.decode(), contentEncryptionProvider);

    } else {

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


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

    Base64URL encryptedKey; // The second JWE part

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

      if(kek.getEncoded().length != 16){
        throw new JOSEException("The Key Encryption Key (KEK) length must be 128 bits for A128KW encryption");
      }
      algFamily = AlgFamily.AESKW;

    } else if (alg.equals(JWEAlgorithm.A192KW)) {

      if(kek.getEncoded().length != 24){
        throw new JOSEException("The Key Encryption Key (KEK) length must be 192 bits for A192KW encryption");
      }
      algFamily = AlgFamily.AESKW;

    } else if (alg.equals(JWEAlgorithm.A256KW)) {

      if (kek.getEncoded().length != 32) {
        throw new JOSEException("The Key Encryption Key (KEK) length must be 256 bits for A256KW encryption");
      }
      algFamily = AlgFamily.AESKW;

    } else if (alg.equals(JWEAlgorithm.A128GCMKW)) {

      if(kek.getEncoded().length != 16){
        throw new JOSEException("The Key Encryption Key (KEK) length must be 128 bits for A128GCMKW encryption");
      }
      algFamily = AlgFamily.AESGCMKW;

    } else if (alg.equals(JWEAlgorithm.A192GCMKW)) {

      if(kek.getEncoded().length != 24){
        throw new JOSEException("The Key Encryption Key (KEK) length must be 192 bits for A192GCMKW encryption");
      }
      algFamily = AlgFamily.AESGCMKW;

    } else if (alg.equals(JWEAlgorithm.A256GCMKW)) {

      if(kek.getEncoded().length != 32){
        throw new JOSEException("The Key Encryption Key (KEK) length must be 256 bits for A256GCMKW encryption");
      }
      algFamily = AlgFamily.AESGCMKW;

    } else {

      throw new JOSEException("Unsupported JWE algorithm, must be A128KW, A192KW, A256KW, A128GCMKW, A192GCMKW orA256GCMKW");
    }

    // We need to work on the header
    JWEHeader modifiableHeader;

    switch (algFamily) {

      case AESKW:
        encryptedKey = Base64URL.encode(AESKW.encryptCEK(cek, kek));
        modifiableHeader = header; // simply copy ref
        break;

      case AESGCMKW:
        keyIV = AESGCM.generateIV(randomGen);
        authCiphCEK = AESGCMKW.encryptCEK(cek, keyIV, kek, keyEncryptionProvider);
        encryptedKey = Base64URL.encode(authCiphCEK.getCipherText());

        // Add iv and tag to the header
        modifiableHeader = new JWEHeader.Builder(header).
          iv(Base64URL.encode(keyIV)).
          authTag(Base64URL.encode(authCiphCEK.getAuthenticationTag())).
          build();
        break;

      default:
        // This should never happen
        throw new JOSEException("Unsupported JWE algorithm, must be A128KW, A192KW, A256KW, A128GCMKW, A192GCMKW orA256GCMKW");
    }

    // Apply compression if instructed
    byte[] plainText = DeflateHelper.applyCompression(modifiableHeader, bytes);

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

    // Encrypt the plain text according to the JWE enc
    byte[] iv;
    AuthenticatedCipherText authCipherText;

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

      iv = AESCBC.generateIV(randomGen);

      authCipherText = AESCBC.encryptAuthenticated(
        cek, iv, plainText, aad,
        contentEncryptionProvider, macProvider);

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

      iv = AESGCM.generateIV(randomGen);

      authCipherText = AESGCM.encrypt(
        cek, iv, plainText, aad,
        contentEncryptionProvider);

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

      iv = AESCBC.generateIV(randomGen);

      authCipherText = AESCBC.encryptWithConcatKDF(
        modifiableHeader, cek, encryptedKey, iv, plainText,
        contentEncryptionProvider, macProvider);

    } else {

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

    return new JWECryptoParts(
      modifiableHeader,
      encryptedKey,
View Full Code Here

    } 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

      // java.security.NoSuchAlgorithmException
      // java.security.InvalidKeyException
      // javax.crypto.IllegalBlockSizeException
      // javax.crypto.BadPaddingException
      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

      System.arraycopy(signatureBytes, 0, rBytes, 0, rBytes.length);
      System.arraycopy(signatureBytes, rBytes.length, sBytes, 0, sBytes.length);

    } catch (Exception e) {

      throw new JOSEException("Invalid ECDSA signature format: " + e.getMessage(), e);
    }

    BigInteger r = new BigInteger(1, rBytes);
    BigInteger s = new BigInteger(1, sBytes);
View Full Code Here

   
    JWSVerifier verifier = jwsVerifiers.get(alg);
   
    if (verifier == null) {

      throw new JOSEException("Unsupported JWS algorithm: " + alg);
    }
     
   
    boolean verified;

    try {
      verified = signedJWT.verify(verifier);

    } catch (IllegalStateException e) {

      throw new JOSEException(e.getMessage(), e);
    }
   
    if (! verified) {

      throw new JOSEException("Bad JWS signature");
    }
   
    return signedJWT.getJWTClaimsSet();
  }
View Full Code Here

   
    JWEDecrypter decrypter = jweDecrypters.get(alg);
   
    if (decrypter == null) {

      throw new JOSEException("Unsupported JWE algorithm: " + alg);
    }
   
   
    try {
      encryptedJWT.decrypt(decrypter);

    } catch (IllegalStateException e) {

      throw new JOSEException(e.getMessage(), e);
    }
   
    return encryptedJWT.getJWTClaimsSet();
  }
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.