Java Code Examples for org.apache.xml.security.algorithms.JCEMapper#translateURItoJCEID()

The following examples show how to use org.apache.xml.security.algorithms.JCEMapper#translateURItoJCEID() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SignatureUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public static MessageDigest getDigestInstance(String algorithmURI) throws NoSuchAlgorithmException {
   String algorithmID = JCEMapper.translateURItoJCEID(algorithmURI);
   if (algorithmID == null) {
      throw new NoSuchAlgorithmException("Could not translate algorithmURI [" + algorithmURI + "]");
   } else {
      String provider = JCEMapper.getProviderId();

      try {
         MessageDigest md;
         if (provider == null) {
            md = MessageDigest.getInstance(algorithmID);
         } else {
            md = MessageDigest.getInstance(algorithmID, provider);
         }

         return md;
      } catch (NoSuchProviderException var5) {
         throw new NoSuchAlgorithmException("Could not find provider for [" + algorithmID + "]", var5);
      }
   }
}
 
Example 2
Source File: SignatureUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public static MessageDigest getDigestInstance(String algorithmURI) throws NoSuchAlgorithmException {
   String algorithmID = JCEMapper.translateURItoJCEID(algorithmURI);
   if (algorithmID == null) {
      throw new NoSuchAlgorithmException("Could not translate algorithmURI [" + algorithmURI + "]");
   } else {
      String provider = JCEMapper.getProviderId();

      try {
         MessageDigest md;
         if (provider == null) {
            md = MessageDigest.getInstance(algorithmID);
         } else {
            md = MessageDigest.getInstance(algorithmID, provider);
         }

         return md;
      } catch (NoSuchProviderException var5) {
         throw new NoSuchAlgorithmException("Could not find provider for [" + algorithmID + "]", var5);
      }
   }
}
 
Example 3
Source File: SignatureUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public static MessageDigest getDigestInstance(String algorithmURI) throws NoSuchAlgorithmException {
   String algorithmID = JCEMapper.translateURItoJCEID(algorithmURI);
   if (algorithmID == null) {
      throw new NoSuchAlgorithmException("Could not translate algorithmURI [" + algorithmURI + "]");
   } else {
      String provider = JCEMapper.getProviderId();

      try {
         MessageDigest md;
         if (provider == null) {
            md = MessageDigest.getInstance(algorithmID);
         } else {
            md = MessageDigest.getInstance(algorithmID, provider);
         }

         return md;
      } catch (NoSuchProviderException var5) {
         throw new NoSuchAlgorithmException("Could not find provider for [" + algorithmID + "]", var5);
      }
   }
}
 
Example 4
Source File: SignatureUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public static MessageDigest getDigestInstance(String algorithmURI) throws NoSuchAlgorithmException {
   String algorithmID = JCEMapper.translateURItoJCEID(algorithmURI);
   if (algorithmID == null) {
      throw new NoSuchAlgorithmException("Could not translate algorithmURI [" + algorithmURI + "]");
   } else {
      String provider = JCEMapper.getProviderId();

      try {
         MessageDigest md;
         if (provider == null) {
            md = MessageDigest.getInstance(algorithmID);
         } else {
            md = MessageDigest.getInstance(algorithmID, provider);
         }

         return md;
      } catch (NoSuchProviderException var5) {
         throw new NoSuchAlgorithmException("Could not find provider for [" + algorithmID + "]", var5);
      }
   }
}
 
Example 5
Source File: SignatureUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public static MessageDigest getDigestInstance(String algorithmURI) throws NoSuchAlgorithmException {
   String algorithmID = JCEMapper.translateURItoJCEID(algorithmURI);
   if (algorithmID == null) {
      throw new NoSuchAlgorithmException("Could not translate algorithmURI [" + algorithmURI + "]");
   } else {
      String provider = JCEMapper.getProviderId();

      try {
         MessageDigest md;
         if (provider == null) {
            md = MessageDigest.getInstance(algorithmID);
         } else {
            md = MessageDigest.getInstance(algorithmID, provider);
         }

         return md;
      } catch (NoSuchProviderException var5) {
         throw new NoSuchAlgorithmException("Could not find provider for [" + algorithmID + "]", var5);
      }
   }
}
 
Example 6
Source File: DefaultMessageDigestProvider.java    From xades4j with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public MessageDigest getEngine(String digestAlgorithmURI) throws UnsupportedAlgorithmException
{

    String digestAlgorithmName = JCEMapper.translateURItoJCEID(digestAlgorithmURI);
    if (null == digestAlgorithmName) {
        throw new UnsupportedAlgorithmException("Digest algorithm not supported by the provider", digestAlgorithmURI);
    }
    try {
        return this.messageDigestProvider == null ?
                MessageDigest.getInstance(digestAlgorithmName) :
                MessageDigest.getInstance(digestAlgorithmName, this.messageDigestProvider);
    } catch (NoSuchAlgorithmException nsae) {
        throw new UnsupportedAlgorithmException(nsae.getMessage(), digestAlgorithmURI, nsae);
    } catch (NoSuchProviderException nspe) {
        // We checked that the provider existed on construction, but throw anyway
        throw new UnsupportedAlgorithmException("Provider not available", digestAlgorithmURI, nspe);
    }
}
 
Example 7
Source File: SAML2ReaderWriter.java    From syncope with Apache License 2.0 5 votes vote down vote up
public void init() {
    X509KeyInfoGeneratorFactory keyInfoGeneratorFactory = new X509KeyInfoGeneratorFactory();
    keyInfoGeneratorFactory.setEmitEntityCertificate(true);
    keyInfoGenerator = keyInfoGeneratorFactory.newInstance();

    // Try to load a signature algorithm
    if (loader.getSignatureAlgorithm() != null) {
        SignatureAlgorithm loadedSignatureAlgorithm =
                SignatureAlgorithm.valueOf(loader.getSignatureAlgorithm());
        sigAlgo = loadedSignatureAlgorithm.getAlgorithm();
        jceSigAlgo = JCEMapper.translateURItoJCEID(sigAlgo);
        if (jceSigAlgo == null) {
            LOG.warn("Signature algorithm {} is not valid. Using default algorithm instead.",
                    loader.getSignatureAlgorithm());
            sigAlgo = null;
        }
    }

    if (sigAlgo == null) {
        sigAlgo = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1;
        String pubKeyAlgo = loader.getCredential().getPublicKey().getAlgorithm();
        if (pubKeyAlgo.equalsIgnoreCase("DSA")) {
            sigAlgo = SignatureConstants.ALGO_ID_SIGNATURE_DSA_SHA1;
        } else if (pubKeyAlgo.equalsIgnoreCase("EC")) {
            sigAlgo = SignatureConstants.ALGO_ID_SIGNATURE_ECDSA_SHA1;
        }
        jceSigAlgo = JCEMapper.translateURItoJCEID(sigAlgo);
    }

    callbackHandler = new SAMLSPCallbackHandler(loader.getKeyPass());
}
 
Example 8
Source File: SamlRedirectBindingFilter.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * Sign a request according to the redirect binding spec for Web SSO
 */
private void signRequest(
    String authnRequest,
    String relayState,
    UriBuilder ub
) throws Exception {
    Crypto crypto = getSignatureCrypto();
    if (crypto == null) {
        LOG.warning("No crypto instance of properties file configured for signature");
        throw ExceptionUtils.toInternalServerErrorException(null, null);
    }
    String signatureUser = getSignatureUsername();
    if (signatureUser == null) {
        LOG.warning("No user configured for signature");
        throw ExceptionUtils.toInternalServerErrorException(null, null);
    }
    CallbackHandler callbackHandler = getCallbackHandler();
    if (callbackHandler == null) {
        LOG.warning("No CallbackHandler configured to supply a password for signature");
        throw ExceptionUtils.toInternalServerErrorException(null, null);
    }

    CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
    cryptoType.setAlias(signatureUser);
    X509Certificate[] issuerCerts = crypto.getX509Certificates(cryptoType);
    if (issuerCerts == null) {
        throw new Exception(
            "No issuer certs were found to sign the request using name: " + signatureUser
        );
    }

    String sigAlgo = getSignatureAlgorithm();
    String pubKeyAlgo = issuerCerts[0].getPublicKey().getAlgorithm();
    LOG.fine("automatic sig algo detection: " + pubKeyAlgo);
    if ("DSA".equalsIgnoreCase(pubKeyAlgo)) {
        sigAlgo = SSOConstants.DSA_SHA1;
    }

    LOG.fine("Using Signature algorithm " + sigAlgo);
    ub.queryParam(SSOConstants.SIG_ALG, URLEncoder.encode(sigAlgo, StandardCharsets.UTF_8.name()));

    // Get the password
    WSPasswordCallback[] cb = {new WSPasswordCallback(signatureUser, WSPasswordCallback.SIGNATURE)};
    callbackHandler.handle(cb);
    String password = cb[0].getPassword();

    // Get the private key
    PrivateKey privateKey = crypto.getPrivateKey(signatureUser, password);

    // Sign the request
    String jceSigAlgo = JCEMapper.translateURItoJCEID(sigAlgo);
    Signature signature = Signature.getInstance(jceSigAlgo);
    signature.initSign(privateKey);

    String requestToSign =
        SSOConstants.SAML_REQUEST + "=" + authnRequest + "&"
        + SSOConstants.RELAY_STATE + "=" + relayState + "&"
        + SSOConstants.SIG_ALG + "=" + URLEncoder.encode(sigAlgo, StandardCharsets.UTF_8.name());

    signature.update(requestToSign.getBytes(StandardCharsets.UTF_8));
    byte[] signBytes = signature.sign();

    String encodedSignature = Base64.getEncoder().encodeToString(signBytes);

    // Clean the private key from memory when we're done
    try {
        privateKey.destroy();
    } catch (DestroyFailedException ex) {
        // ignore
    }

    ub.queryParam(SSOConstants.SIGNATURE, URLEncoder.encode(encodedSignature, StandardCharsets.UTF_8.name()));

}