Example usage for org.bouncycastle.jce.provider X509CertParser engineRead

List of usage examples for org.bouncycastle.jce.provider X509CertParser engineRead

Introduction

In this page you can find the example usage for org.bouncycastle.jce.provider X509CertParser engineRead.

Prototype

public Object engineRead() throws StreamParsingException 

Source Link

Usage

From source file:it.zero11.acme.Acme.java

License:Apache License

private X509Certificate extractCertificate(final String[] domains, InputStream inputStream)
        throws StreamParsingException {
    X509CertParser certParser = new X509CertParser();
    certParser.engineInit(inputStream);//  w ww. ja  v a2 s.co  m
    X509Certificate certificate = (X509Certificate) certParser.engineRead();
    certificateStorage.saveCertificate(domains, certificate);
    return certificate;
}

From source file:it.zero11.acme.storage.impl.DefaultCertificateStorage.java

License:Apache License

@Override
public void saveCertificate(String[] domains, X509Certificate certificate) {
    for (String domain : domains) {
        try (OutputStream outputStream = new FileOutputStream(domain + ".crt")) {
            X509Utils.savePEM(outputStream, certificate);
        } catch (IOException e) {
            throw new CertificateStorageException(e);
        }//  w  ww. j  av  a  2  s.  com
    }
    if (saveCAIntermediateCertificate) {
        try {
            String caIntermediateCertificateURL = X509Utils.getCACertificateURL(certificate);
            if (caIntermediateCertificateURL != null) {
                X509CertificateObject caIntermediateCertificate;
                try (InputStream is = new URL(caIntermediateCertificateURL).openStream()) {
                    X509CertParser certParser = new X509CertParser();
                    certParser.engineInit(is);
                    caIntermediateCertificate = (X509CertificateObject) certParser.engineRead();
                }
                for (String domain : domains) {
                    try (OutputStream outputStream = new FileOutputStream(domain + ".chain.crt")) {
                        X509Utils.savePEM(outputStream, caIntermediateCertificate);
                    }
                }
            }
        } catch (IOException | StreamParsingException e) {
            throw new CertificateStorageException(e);
        }
    }
}

From source file:org.apache.directory.server.integration.http.HttpServer.java

License:Apache License

private void configureServerThroughCode() {
    try {/* w  w w  . j a v a2  s.  co m*/
        jetty = new Server();

        if (httpTransport != null) {
            ServerConnector httpConnector = new ServerConnector(jetty);
            httpConnector.setPort(httpTransport.getPort());
            httpConnector.setHost(httpTransport.getAddress());
            jetty.addConnector(httpConnector);
        }

        if (httpsTransport != null) {
            // load the admin entry to get the private key and certificate
            Dn adminDn = dirService.getDnFactory().create(ServerDNConstants.ADMIN_SYSTEM_DN);
            Entry adminEntry = dirService.getAdminSession().lookup(adminDn, SchemaConstants.ALL_USER_ATTRIBUTES,
                    SchemaConstants.ALL_OPERATIONAL_ATTRIBUTES);

            File confDir = dirService.getInstanceLayout().getConfDirectory();
            File ksFile = new File(confDir, "httpserver.generated.ks");

            String password = UUID.randomUUID().toString();

            KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
            ks.load(null, null);

            X509CertParser parser = new X509CertParser();

            parser.engineInit(
                    new ByteArrayInputStream(adminEntry.get(TlsKeyGenerator.USER_CERTIFICATE_AT).getBytes()));

            X509Certificate cert = (X509Certificate) parser.engineRead();

            ks.setCertificateEntry("cert", cert);

            KeyPair keyPair = TlsKeyGenerator.getKeyPair(adminEntry);
            ks.setKeyEntry("privatekey", keyPair.getPrivate(), password.toCharArray(),
                    new Certificate[] { cert });

            try (OutputStream stream = Files.newOutputStream(ksFile.toPath())) {
                ks.store(stream, password.toCharArray());
            }

            SslContextFactory sslContextFactory = new SslContextFactory();
            sslContextFactory.setKeyStoreType(KeyStore.getDefaultType());
            sslContextFactory.setKeyStorePath(ksFile.getAbsolutePath());
            sslContextFactory.setKeyStorePassword(password);
            sslContextFactory.setKeyManagerPassword(password);

            HttpConfiguration httpsConfiguration = new HttpConfiguration();
            httpsConfiguration.setSecureScheme(HTTPS_TRANSPORT_ID);
            httpsConfiguration.setSecurePort(httpsTransport.getPort());
            httpsConfiguration.addCustomizer(new SecureRequestCustomizer());

            ServerConnector httpsConnector = new ServerConnector(jetty,
                    new SslConnectionFactory(sslContextFactory, "http/1.1"),
                    new HttpConnectionFactory(httpsConfiguration));
            httpsConnector.setPort(httpsTransport.getPort());
            httpsConnector.setHost(httpsTransport.getAddress());

            jetty.addConnector(httpsConnector);
        }

        HandlerList handlers = new HandlerList();
        for (WebApp w : webApps) {
            WebAppContext webapp = new WebAppContext();
            webapp.setWar(w.getWarFile());
            webapp.setContextPath(w.getContextPath());
            handlers.addHandler(webapp);

            webapp.setParentLoaderPriority(true);
        }

        // add web apps from the webapps directory inside directory service's working directory
        // the exploded or archived wars
        File webAppDir = new File(dirService.getInstanceLayout().getInstanceDirectory(), "webapps");

        FilenameFilter webAppFilter = new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.endsWith(".war");
            }
        };

        if (webAppDir.exists()) {
            File[] appList = webAppDir.listFiles(webAppFilter);
            for (File app : appList) {
                WebAppContext webapp = new WebAppContext();
                webapp.setWar(app.getAbsolutePath());
                String ctxName = app.getName();
                int pos = ctxName.indexOf('.');
                if (pos > 0) {
                    ctxName = ctxName.substring(0, pos);
                }

                webapp.setContextPath("/" + ctxName);
                handlers.addHandler(webapp);

                webapp.setParentLoaderPriority(true);
            }
        }

        jetty.setHandler(handlers);

        configured = true;
    } catch (Exception e) {
        LOG.error(I18n.err(I18n.ERR_121), e);
    }

}