Example usage for java.security NoSuchAlgorithmException NoSuchAlgorithmException

List of usage examples for java.security NoSuchAlgorithmException NoSuchAlgorithmException

Introduction

In this page you can find the example usage for java.security NoSuchAlgorithmException NoSuchAlgorithmException.

Prototype

public NoSuchAlgorithmException(Throwable cause) 

Source Link

Document

Creates a NoSuchAlgorithmException with the specified cause and a detail message of (cause==null ?

Usage

From source file:org.eclipse.smarthome.binding.mqtt.handler.BrokerHandler.java

/**
 * Reads the thing configuration related to public key or certificate pinning, creates an appropriate a
 * {@link PinningSSLContextProvider} and assigns it to the {@link MqttBrokerConnection} instance.
 * The instance need to be set before calling this method. If the SHA-256 algorithm is not supported
 * by the platform, this method will do nothing.
 *
 * @throws IllegalArgumentException Throws this exception, if provided hash values cannot be
 *             assigned to the {@link PinningSSLContextProvider}.
 *//*w w  w  . j  a v a2 s.  com*/
protected void assignSSLContextProvider(BrokerHandlerConfig config, MqttBrokerConnection connection,
        PinnedCallback callback) throws IllegalArgumentException {
    final PinTrustManager trustManager = new PinTrustManager();

    connection.setSSLContextProvider(new PinningSSLContextProvider(trustManager));
    trustManager.setCallback(callback);

    if (config.certificatepin) {
        try {
            Pin pin;
            if (StringUtils.isBlank(config.certificate)) {
                pin = Pin.LearningPin(PinType.CERTIFICATE_TYPE);
            } else {
                String[] split = config.certificate.split(":");
                if (split.length != 2) {
                    throw new NoSuchAlgorithmException("Algorithm is missing");
                }
                pin = Pin.CheckingPin(PinType.CERTIFICATE_TYPE, new PinMessageDigest(split[0]),
                        HexUtils.hexToBytes(split[1]));
            }
            trustManager.addPinning(pin);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e);
        }
    }
    if (config.publickeypin) {
        try {
            Pin pin;
            if (StringUtils.isBlank(config.publickey)) {
                pin = Pin.LearningPin(PinType.PUBLIC_KEY_TYPE);
            } else {
                String[] split = config.publickey.split(":");
                if (split.length != 2) {
                    throw new NoSuchAlgorithmException("Algorithm is missing");
                }
                pin = Pin.CheckingPin(PinType.PUBLIC_KEY_TYPE, new PinMessageDigest(split[0]),
                        HexUtils.hexToBytes(split[1]));
            }
            trustManager.addPinning(pin);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

From source file:org.ejbca.core.protocol.ws.EjbcaWS.java

@Override
public CertificateResponse certificateRequest(final UserDataVOWS userdata, final String requestData,
        final int requestType, final String hardTokenSN, final String responseType)
        throws AuthorizationDeniedException, NotFoundException, UserDoesntFullfillEndEntityProfile,
        ApprovalException, WaitingForApprovalException, EjbcaException {
    final IPatternLogger logger = TransactionLogger.getPatternLogger();
    try {/*  w  w  w  .  j a  v  a2 s  .co  m*/
        if (log.isDebugEnabled()) {
            log.debug("CertReq for user '" + userdata.getUsername() + "'.");
        }
        setUserDataVOWS(userdata);
        final EjbcaWSHelper ejbcawshelper = new EjbcaWSHelper(wsContext, authorizationSession, caAdminSession,
                caSession, certificateProfileSession, certificateStoreSession, endEntityAccessSession,
                endEntityProfileSession, hardTokenSession, endEntityManagementSession, webAuthenticationSession,
                cryptoTokenManagementSession);
        final AuthenticationToken admin = ejbcawshelper.getAdmin(false);
        logAdminName(admin, logger);
        enrichUserDataWithRawSubjectDn(userdata);
        final EndEntityInformation endEntityInformation = ejbcawshelper.convertUserDataVOWS(admin, userdata);
        int responseTypeInt = CertificateConstants.CERT_RES_TYPE_CERTIFICATE;
        if (!responseType.equalsIgnoreCase(CertificateHelper.RESPONSETYPE_CERTIFICATE)) {
            if (responseType.equalsIgnoreCase(CertificateHelper.RESPONSETYPE_PKCS7)) {
                responseTypeInt = CertificateConstants.CERT_RES_TYPE_PKCS7;
            } else if (responseType.equalsIgnoreCase(CertificateHelper.RESPONSETYPE_PKCS7WITHCHAIN)) {
                responseTypeInt = CertificateConstants.CERT_RES_TYPE_PKCS7WITHCHAIN;
            } else {
                throw new NoSuchAlgorithmException("Bad responseType:" + responseType);
            }
        }

        return new CertificateResponse(responseType, certificateRequestSession.processCertReq(admin,
                endEntityInformation, requestData, requestType, hardTokenSN, responseTypeInt));
    } catch (CADoesntExistsException t) {
        logger.paramPut(TransactionTags.ERROR_MESSAGE.toString(), t.toString());
        throw new EjbcaException(t);
    } catch (AuthorizationDeniedException t) {
        logger.paramPut(TransactionTags.ERROR_MESSAGE.toString(), t.toString());
        throw t;
    } catch (NotFoundException t) {
        logger.paramPut(TransactionTags.ERROR_MESSAGE.toString(), t.toString());
        throw t;
    } catch (CertificateExtensionException e) {
        throw EjbcaWSHelper.getInternalException(e, logger);
    } catch (InvalidKeyException e) {
        throw EjbcaWSHelper.getEjbcaException(e, logger, ErrorCode.INVALID_KEY, Level.ERROR);
    } catch (IllegalKeyException e) {
        // Don't log a bad error for this (user's key length too small)
        throw EjbcaWSHelper.getEjbcaException(e, logger, ErrorCode.ILLEGAL_KEY, Level.DEBUG);
    } catch (AuthStatusException e) {
        // Don't log a bad error for this (user wrong status)
        throw EjbcaWSHelper.getEjbcaException(e, logger, ErrorCode.USER_WRONG_STATUS, Level.DEBUG);
    } catch (AuthLoginException e) {
        throw EjbcaWSHelper.getEjbcaException(e, logger, ErrorCode.LOGIN_ERROR, Level.ERROR);
    } catch (SignatureException e) {
        throw EjbcaWSHelper.getEjbcaException(e, logger, ErrorCode.SIGNATURE_ERROR, Level.ERROR);
    } catch (SignRequestSignatureException e) {
        throw EjbcaWSHelper.getEjbcaException(e.getMessage(), logger, null, Level.ERROR);
    } catch (InvalidKeySpecException e) {
        throw EjbcaWSHelper.getEjbcaException(e, logger, ErrorCode.INVALID_KEY_SPEC, Level.ERROR);
    } catch (NoSuchAlgorithmException e) {
        throw EjbcaWSHelper.getInternalException(e, logger);
    } catch (NoSuchProviderException e) {
        throw EjbcaWSHelper.getInternalException(e, logger);
    } catch (CertificateException e) {
        throw EjbcaWSHelper.getInternalException(e, logger);
    } catch (CreateException e) {
        throw EjbcaWSHelper.getInternalException(e, logger);
    } catch (IOException e) {
        throw EjbcaWSHelper.getInternalException(e, logger);
    } catch (CesecoreException e) {
        // Will convert the CESecore exception to an EJBCA exception with the same error code
        throw EjbcaWSHelper.getEjbcaException(e, null, e.getErrorCode(), null);
    } catch (FinderException e) {
        throw new NotFoundException(e.getMessage());
    } catch (RuntimeException e) { // EJBException, ClassCastException, ...
        throw EjbcaWSHelper.getInternalException(e, logger);
    } finally {
        logger.writeln();
        logger.flush();
    }
}

From source file:org.globus.workspace.common.SecurityUtil.java

public static void checkMD5() throws NoSuchAlgorithmException {
    if (md5 == null) {
        throw new NoSuchAlgorithmException("MD5 digester was not initialized");
    }//from w  w  w .j a  v a2  s .c  om
}

From source file:org.globus.workspace.groupauthz.HashUtil.java

private static void checkMD5() throws NoSuchAlgorithmException {
    if (md5 == null) {
        throw new NoSuchAlgorithmException("MD5 digester was not initialized");
    }//from www  . jav  a 2  s  . c  o  m
}

From source file:org.imogene.client.ssl.EasyX509TrustManager.java

/**
 * Constructor for EasyX509TrustManager.
 *//*from  w  ww . ja  v  a2s .  co m*/
public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
    super();
    TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    factory.init(keystore);
    TrustManager[] trustmanagers = factory.getTrustManagers();
    if (trustmanagers.length == 0) {
        throw new NoSuchAlgorithmException("no trust manager found"); //$NON-NLS-1$
    }
    this.standardTrustManager = (X509TrustManager) trustmanagers[0];
}

From source file:org.jivesoftware.sparkimpl.updater.EasyX509TrustManager.java

public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
    super();//from w w w. jav a  2s. com
    TrustManagerFactory factory = TrustManagerFactory.getInstance("SunX509");
    factory.init(keystore);
    TrustManager[] trustmanagers = factory.getTrustManagers();
    if (trustmanagers.length == 0) {
        throw new NoSuchAlgorithmException("SunX509 trust manager not supported");
    }
    this.standardTrustManager = (X509TrustManager) trustmanagers[0];
}

From source file:org.lockss.util.urlconn.PermissiveX509TrustManager.java

/**
 * Constructor for PermissiveX509TrustManager.
 *///  w w w.ja v  a 2 s  .c o m
public PermissiveX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
    super();
    TrustManagerFactory factory = TrustManagerFactory.getInstance("SunX509");
    factory.init(keystore);
    TrustManager[] trustmanagers = factory.getTrustManagers();
    if (trustmanagers.length == 0) {
        throw new NoSuchAlgorithmException("SunX509 trust manager not supported");
    }
    this.standardTrustManager = (X509TrustManager) trustmanagers[0];
}

From source file:org.nuxeo.common.codec.Crypto.java

protected SecretKey getSecretKey(String algorithm, byte[] key) throws NoSuchAlgorithmException {
    if (!initialized) {
        throw new RuntimeException("The Crypto object has been cleared.");
    }/*from  w  w w .  j  ava2 s.c o m*/
    if (AES_ECB_PKCS5PADDING.equals(algorithm)) {
        algorithm = AES; // AES_ECB_PKCS5PADDING is the default for AES
    } else if (DES_ECB_PKCS5PADDING.equals(algorithm)) {
        algorithm = DES; // DES_ECB_PKCS5PADDING is the default for DES
    }
    if (!secretKeys.containsKey(algorithm)) {
        if (secretKey.length == 0) {
            throw new NoSuchAlgorithmException("Unsupported algorithm: " + algorithm);
        }
        if (AES.equals(algorithm)) { // default for AES
            key = Arrays.copyOf(getSHA1Digest(key), 16); // use a 128 bits secret key
            secretKeys.put(AES, new SecretKeySpec(key, AES));
        } else if (DES.equals(algorithm)) { // default for DES
            key = Arrays.copyOf(getSHA1Digest(key), 8); // use a 64 bits secret key
            secretKeys.put(DES, new SecretKeySpec(key, DES));
        } else {
            throw new NoSuchAlgorithmException("Unsupported algorithm: " + algorithm);
        }
    }
    return secretKeys.get(algorithm);
}

From source file:org.opensaml.xml.security.SecurityHelper.java

/**
 * Generates a random Java JCE symmetric Key object from the specified XML Encryption algorithm URI.
 * //from   www .j  a  v  a  2 s.  c  om
 * @param algoURI The XML Encryption algorithm URI
 * @return a randomly-generated symmetric Key
 * @throws NoSuchAlgorithmException thrown if the specified algorithm is invalid
 * @throws KeyException thrown if the length of the key to generate could not be determined
 */
public static SecretKey generateSymmetricKey(String algoURI) throws NoSuchAlgorithmException, KeyException {
    String jceAlgorithmName = getKeyAlgorithmFromURI(algoURI);
    if (DatatypeHelper.isEmpty(jceAlgorithmName)) {
        log.error("Mapping from algorithm URI '" + algoURI
                + "' to key algorithm not available, key generation failed");
        throw new NoSuchAlgorithmException("Algorithm URI'" + algoURI + "' is invalid for key generation");
    }
    Integer keyLength = getKeyLengthFromURI(algoURI);
    if (keyLength == null) {
        log.error("Key length could not be determined from algorithm URI, can't generate key");
        throw new KeyException("Key length not determinable from algorithm URI, could not generate new key");
    }
    KeyGenerator keyGenerator = KeyGenerator.getInstance(jceAlgorithmName);
    keyGenerator.init(keyLength);
    return keyGenerator.generateKey();
}

From source file:org.opensaml.xml.security.XMLSecurityHelper.java

/**
 * Generates a random Java JCE symmetric Key object from the specified XML Encryption algorithm URI.
 * //from w w w. j a  v a2 s.  c  om
 * @param algoURI The XML Encryption algorithm URI
 * @return a randomly-generated symmetric Key
 * @throws NoSuchAlgorithmException thrown if the specified algorithm is invalid
 * @throws KeyException thrown if the length of the key to generate could not be determined
 */
public static SecretKey generateSymmetricKey(String algoURI) throws NoSuchAlgorithmException, KeyException {
    Logger log = getLogger();
    String jceAlgorithmName = getKeyAlgorithmFromURI(algoURI);
    if (StringSupport.isNullOrEmpty(jceAlgorithmName)) {
        log.error("Mapping from algorithm URI '" + algoURI
                + "' to key algorithm not available, key generation failed");
        throw new NoSuchAlgorithmException("Algorithm URI'" + algoURI + "' is invalid for key generation");
    }
    Integer keyLength = getKeyLengthFromURI(algoURI);
    if (keyLength == null) {
        log.error("Key length could not be determined from algorithm URI, can't generate key");
        throw new KeyException("Key length not determinable from algorithm URI, could not generate new key");
    }
    KeyGenerator keyGenerator = KeyGenerator.getInstance(jceAlgorithmName);
    keyGenerator.init(keyLength);
    return keyGenerator.generateKey();
}