Search in sources :

Example 1 with CMCException

use of com.github.zhenwei.pkix.cmc.CMCException in project LinLong-Java by zhenwei1108.

the class ESTService method handleEnrollResponse.

/**
 * Handles the enroll response, deals with status codes and setting of delays.
 *
 * @param resp The response.
 * @return An EnrollmentResponse.
 * @throws IOException
 */
protected EnrollmentResponse handleEnrollResponse(ESTResponse resp) throws IOException {
    ESTRequest req = resp.getOriginalRequest();
    Store<X509CertificateHolder> enrolled = null;
    if (resp.getStatusCode() == 202) {
        // Received but not ready.
        String rt = resp.getHeader("Retry-After");
        if (rt == null) {
            throw new ESTException("Got Status 202 but not Retry-After header from: " + req.getURL().toString());
        }
        long notBefore = -1;
        try {
            notBefore = System.currentTimeMillis() + (Long.parseLong(rt) * 1000);
        } catch (NumberFormatException nfe) {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
                dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
                notBefore = dateFormat.parse(rt).getTime();
            } catch (Exception ex) {
                throw new ESTException("Unable to parse Retry-After header:" + req.getURL().toString() + " " + ex.getMessage(), null, resp.getStatusCode(), resp.getInputStream());
            }
        }
        return new EnrollmentResponse(null, notBefore, req, resp.getSource());
    } else if (resp.getStatusCode() == 200) {
        ASN1InputStream ain = new ASN1InputStream(resp.getInputStream());
        SimplePKIResponse spkr = null;
        try {
            spkr = new SimplePKIResponse(ContentInfo.getInstance(ain.readObject()));
        } catch (CMCException e) {
            throw new ESTException(e.getMessage(), e.getCause());
        }
        enrolled = spkr.getCertificates();
        return new EnrollmentResponse(enrolled, -1, null, resp.getSource());
    }
    throw new ESTException("Simple Enroll: " + req.getURL().toString(), null, resp.getStatusCode(), resp.getInputStream());
}
Also used : ASN1InputStream(com.github.zhenwei.core.asn1.ASN1InputStream) DERPrintableString(com.github.zhenwei.core.asn1.DERPrintableString) IOException(java.io.IOException) CMCException(com.github.zhenwei.pkix.cmc.CMCException) SimplePKIResponse(com.github.zhenwei.pkix.cmc.SimplePKIResponse) CMCException(com.github.zhenwei.pkix.cmc.CMCException) X509CertificateHolder(com.github.zhenwei.pkix.cert.X509CertificateHolder) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

ASN1InputStream (com.github.zhenwei.core.asn1.ASN1InputStream)1 DERPrintableString (com.github.zhenwei.core.asn1.DERPrintableString)1 X509CertificateHolder (com.github.zhenwei.pkix.cert.X509CertificateHolder)1 CMCException (com.github.zhenwei.pkix.cmc.CMCException)1 SimplePKIResponse (com.github.zhenwei.pkix.cmc.SimplePKIResponse)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1