/*************************************************************************
* *
* EJBCA Community: The OpenSource Certificate Authority *
* *
* This software is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or any later version. *
* *
* See terms of license at gnu.org. *
* *
*************************************************************************/
package org.ejbca.samples;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.KeyPair;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.bouncycastle.asn1.DEROutputStream;
import org.bouncycastle.asn1.DERSet;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.cesecore.certificates.util.AlgorithmConstants;
import org.cesecore.keys.util.KeyTools;
import org.cesecore.util.Base64;
import org.cesecore.util.CertTools;
import org.cesecore.util.CryptoProviderTools;
/**
* Example how a certificate can be fetched programmatically using HTTP/S. The sample generates a
* certificate request and uses POST to the same servlet as used from a browser through
* http://127.0.0.1:8080/ejbca/publicweb/apply/apply_man.jsp.
* The servlet url used in the example is
* http://127.0.0.1:8080/ejbca/publicweb/apply/certreq.
* The certificate reply containing a PEM-formatted certificate is printed to the screen.
*
* NOTE: Support for SSL has been commented out in this sample, since it requires JSSE. This sample
* class generates a PKCS10 request and POSTs to the CAs web interface. The reply is received and
* printed to stdout. Takes arguments:
*
*
* -
* requesturl - URL to the CA web (servlet where requests are POSTed),
* http://127.0.0.1/apply/apply_man.jsp.
*
* -
* username - username of a user registered with the CA with status NEW.
*
* -
* password - password for the above user.
*
*
*
* @version $Id: HttpGetCert.java 19901 2014-09-30 14:29:38Z anatom $
*/
public class HttpGetCert {
private static Logger log = Logger.getLogger(HttpGetCert.class);
/**
* Constructor
*/
public HttpGetCert() {
log.trace(">HttpGetCert:");
// Use for SSL connections
/*
System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
*/
log.trace("setSSLTrustedServerCert:");
CertificateFactory cf = CertTools.getCertificateFactory();
webcert = (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(cert));
if ( CertTools.isSelfSigned( webcert ) )
throw new IllegalArgumentException("Webcert certificate is not self signed (not a root CA certificate).");
log.trace("getSSLFactory" );
SSLContext ctx = SSLContext.getInstance( "SSL" );
KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
String proxyHost = null;
String proxyPort = null;
// if we are behind a proxy, there must be set
if (proxyHost != null)
System.setProperty("https.proxyHost", proxyHost);
if (proxyPort != null)
System.setProperty("https.proxyPort", proxyPort);
if (webcert == null)
throw new IllegalArgumentException("Server certificate must be set for SSL communication");
// If we must use client certificates here, we should read some certs and keys and create a keystore to put in the KeyManagerFactory
// Make a truststore to verify the server
KeyStore trustks = KeyStore.getInstance( "jks" );
trustks.load( null, new String("foo123").toCharArray() );
trustks.setCertificateEntry( "trustedRootCA", webcert);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init( trustks );
ctx.init( null, tmf.getTrustManagers(), null );
log.trace( "sendHttpReq: request=" + request.toString() + ", username=" + username +
", password=" + password);
if (requestUrl == null) {
throw new IllegalArgumentException("requesturl can not be null.");
}
log.debug("Sending request to: " + requestUrl);
URL url = new URL(requestUrl);
HttpURLConnection con = (HttpURLConnection) getUrlConnection(url);
// we are going to do a POST
con.setDoOutput(true);
con.setRequestMethod("POST");
// POST it
PrintWriter out = new PrintWriter(con.getOutputStream());
out.println("pkcs10req=" + URLEncoder.encode(request,"UTF-8") + "&user=" +
URLEncoder.encode(username,"UTF-8") + "&password=" + URLEncoder.encode(password,"UTF-8") +
"&submit=Submit+Query");
out.close();
// Read the reqponse
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
} finally {
if (in != null) {
in.close();
}
}
if (con.getResponseCode() == 200) {
log.debug("Received certificate reply.");
} else {
throw new Exception("Error sending PKCS10-request.");
}
// We are done, disconnect
con.disconnect();
log.trace("