Search in sources :

Example 66 with GeneralSecurityException

use of java.security.GeneralSecurityException in project okhttp by square.

the class OkHttpClient method systemDefaultTrustManager.

private X509TrustManager systemDefaultTrustManager() {
    try {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init((KeyStore) null);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
            throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
        }
        return (X509TrustManager) trustManagers[0];
    } catch (GeneralSecurityException e) {
        // The system has no TLS. Just give up.
        throw new AssertionError();
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) GeneralSecurityException(java.security.GeneralSecurityException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 67 with GeneralSecurityException

use of java.security.GeneralSecurityException in project camel by apache.

the class Olingo2Component method createOlingo2App.

private Olingo2AppWrapper createOlingo2App(Olingo2Configuration configuration) {
    Object clientBuilder = configuration.getHttpAsyncClientBuilder();
    if (clientBuilder == null) {
        HttpAsyncClientBuilder asyncClientBuilder = HttpAsyncClientBuilder.create();
        // apply simple configuration properties
        final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        requestConfigBuilder.setConnectTimeout(configuration.getConnectTimeout());
        requestConfigBuilder.setSocketTimeout(configuration.getSocketTimeout());
        final HttpHost proxy = configuration.getProxy();
        if (proxy != null) {
            requestConfigBuilder.setProxy(proxy);
        }
        // set default request config
        asyncClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
        SSLContextParameters sslContextParameters = configuration.getSslContextParameters();
        if (sslContextParameters == null) {
            // use defaults if not specified
            sslContextParameters = new SSLContextParameters();
        }
        try {
            asyncClientBuilder.setSSLContext(sslContextParameters.createSSLContext(getCamelContext()));
        } catch (GeneralSecurityException e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
        } catch (IOException e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
    }
    Olingo2AppImpl olingo2App;
    if (clientBuilder == null || clientBuilder instanceof HttpAsyncClientBuilder) {
        olingo2App = new Olingo2AppImpl(configuration.getServiceUri(), (HttpAsyncClientBuilder) clientBuilder);
    } else {
        olingo2App = new Olingo2AppImpl(configuration.getServiceUri(), (HttpClientBuilder) clientBuilder);
    }
    apiProxy = new Olingo2AppWrapper(olingo2App);
    apiProxy.getOlingo2App().setContentType(configuration.getContentType());
    apiProxy.getOlingo2App().setHttpHeaders(configuration.getHttpHeaders());
    return apiProxy;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) Olingo2AppImpl(org.apache.camel.component.olingo2.api.impl.Olingo2AppImpl) HttpHost(org.apache.http.HttpHost) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters)

Example 68 with GeneralSecurityException

use of java.security.GeneralSecurityException in project camel by apache.

the class XmlSignerProcessor method sign.

protected Document sign(final Message out) throws Exception {
    try {
        XMLSignatureFactory fac;
        // not work
        try {
            fac = XMLSignatureFactory.getInstance("DOM", "ApacheXMLDSig");
        } catch (NoSuchProviderException ex) {
            fac = XMLSignatureFactory.getInstance("DOM");
        }
        final Node node = getMessageBodyNode(out);
        if (getConfiguration().getKeyAccessor() == null) {
            throw new XmlSignatureNoKeyException("Key accessor is missing for XML signature generation. Specify a key accessor in the configuration.");
        }
        final KeySelector keySelector = getConfiguration().getKeyAccessor().getKeySelector(out);
        if (keySelector == null) {
            throw new XmlSignatureNoKeyException("Key selector is missing for XML signature generation. Specify a key selector in the configuration.");
        }
        SignatureType signatureType = determineSignatureType(out);
        final List<String> contentReferenceUris = getContentReferenceUris(out, signatureType, node);
        Node lastParent = null;
        // only in the detached case there can be several
        for (final String contentReferenceUri : contentReferenceUris) {
            // the method KeyAccessor.getKeyInfo must be called after the method KeyAccessor.getKeySelector, this is part of the interface contract!
            // and this method must be called within the loop over the content reference URIs, because for each signature the key info ID must be different
            final KeyInfo keyInfo = getConfiguration().getKeyAccessor().getKeyInfo(out, node, fac.getKeyInfoFactory());
            String signatureId = getConfiguration().getSignatureId();
            if (signatureId == null) {
                signatureId = "_" + UUID.randomUUID().toString();
            } else if (signatureId.isEmpty()) {
                // indicator that no signature Id attribute shall be generated
                signatureId = null;
            }
            // parent only relevant for enveloped or detached signature
            Node parent = getParentOfSignature(out, node, contentReferenceUri, signatureType);
            if (parent == null) {
                // for enveloping signature, create new document 
                parent = XmlSignatureHelper.newDocumentBuilder(Boolean.TRUE).newDocument();
            }
            lastParent = parent;
            XmlSignatureProperties.Input input = new InputBuilder().contentDigestAlgorithm(getDigestAlgorithmUri()).keyInfo(keyInfo).message(out).messageBodyNode(node).parent(parent).signatureAlgorithm(getConfiguration().getSignatureAlgorithm()).signatureFactory(fac).signatureId(signatureId).contentReferenceUri(contentReferenceUri).signatureType(signatureType).prefixForXmlSignatureNamespace(getConfiguration().getPrefixForXmlSignatureNamespace()).build();
            XmlSignatureProperties.Output properties = getSignatureProperties(input);
            // the signature properties can overwrite the signature Id
            if (properties != null && properties.getSignatureId() != null && !properties.getSignatureId().isEmpty()) {
                signatureId = properties.getSignatureId();
            }
            List<? extends XMLObject> objects = getObjects(input, properties);
            List<? extends Reference> refs = getReferences(input, properties, getKeyInfoId(keyInfo));
            SignedInfo si = createSignedInfo(fac, refs);
            DOMSignContext dsc = createAndConfigureSignContext(parent, keySelector);
            XMLSignature signature = fac.newXMLSignature(si, keyInfo, objects, signatureId, null);
            // generate the signature
            signature.sign(dsc);
        }
        return XmlSignatureHelper.getDocument(lastParent);
    } catch (XMLSignatureException se) {
        if (se.getCause() instanceof InvalidKeyException) {
            throw new XmlSignatureInvalidKeyException(se.getMessage(), se);
        } else {
            throw new XmlSignatureException(se);
        }
    } catch (GeneralSecurityException e) {
        // like NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchProviderException
        throw new XmlSignatureException(e);
    }
}
Also used : XmlSignatureInvalidKeyException(org.apache.camel.component.xmlsecurity.api.XmlSignatureInvalidKeyException) XMLSignatureFactory(javax.xml.crypto.dsig.XMLSignatureFactory) XmlSignatureProperties(org.apache.camel.component.xmlsecurity.api.XmlSignatureProperties) Node(org.w3c.dom.Node) GeneralSecurityException(java.security.GeneralSecurityException) SignatureType(org.apache.camel.component.xmlsecurity.api.SignatureType) KeySelector(javax.xml.crypto.KeySelector) InvalidKeyException(java.security.InvalidKeyException) XmlSignatureInvalidKeyException(org.apache.camel.component.xmlsecurity.api.XmlSignatureInvalidKeyException) SignedInfo(javax.xml.crypto.dsig.SignedInfo) XmlSignatureException(org.apache.camel.component.xmlsecurity.api.XmlSignatureException) KeyInfo(javax.xml.crypto.dsig.keyinfo.KeyInfo) XmlSignatureNoKeyException(org.apache.camel.component.xmlsecurity.api.XmlSignatureNoKeyException) DOMSignContext(javax.xml.crypto.dsig.dom.DOMSignContext) XMLSignature(javax.xml.crypto.dsig.XMLSignature) NoSuchProviderException(java.security.NoSuchProviderException) XMLSignatureException(javax.xml.crypto.dsig.XMLSignatureException)

Example 69 with GeneralSecurityException

use of java.security.GeneralSecurityException in project blade by biezhi.

the class HttpRequest method getTrustedFactory.

/**
	 * @return 返回SSL套接字工厂
	 * @throws HttpRequestException
	 */
private static SSLSocketFactory getTrustedFactory() throws HttpRequestException {
    if (TRUSTED_FACTORY == null) {
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

            public void checkClientTrusted(X509Certificate[] chain, String authType) {
            // Intentionally left blank
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) {
            // Intentionally left blank
            }
        } };
        try {
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, trustAllCerts, new SecureRandom());
            TRUSTED_FACTORY = context.getSocketFactory();
        } catch (GeneralSecurityException e) {
            IOException ioException = new IOException("Security exception configuring SSL context");
            ioException.initCause(e);
            throw new HttpRequestException(ioException);
        }
    }
    return TRUSTED_FACTORY;
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) SecureRandom(java.security.SecureRandom) X509Certificate(java.security.cert.X509Certificate)

Example 70 with GeneralSecurityException

use of java.security.GeneralSecurityException in project bigbluebutton by bigbluebutton.

the class HMAC_SHA1 method isValid.

@Override
public boolean isValid(String signature, String baseString) throws OAuthException {
    try {
        byte[] expected = computeSignature(baseString);
        byte[] actual = decodeBase64(signature);
        return Arrays.equals(expected, actual);
    } catch (GeneralSecurityException e) {
        throw new OAuthException(e);
    } catch (UnsupportedEncodingException e) {
        throw new OAuthException(e);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) OAuthException(net.oauth.OAuthException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

GeneralSecurityException (java.security.GeneralSecurityException)1171 IOException (java.io.IOException)435 Cipher (javax.crypto.Cipher)144 Test (org.junit.Test)136 X509Certificate (java.security.cert.X509Certificate)124 KeyStore (java.security.KeyStore)89 SSLContext (javax.net.ssl.SSLContext)84 SecretKeySpec (javax.crypto.spec.SecretKeySpec)80 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)72 ArrayList (java.util.ArrayList)72 File (java.io.File)61 InputStream (java.io.InputStream)57 Certificate (java.security.cert.Certificate)57 PublicKey (java.security.PublicKey)53 PrivateKey (java.security.PrivateKey)50 FileInputStream (java.io.FileInputStream)49 BigInteger (java.math.BigInteger)49 SecretKey (javax.crypto.SecretKey)48 IvParameterSpec (javax.crypto.spec.IvParameterSpec)43 SecureRandom (java.security.SecureRandom)42