Example usage for org.bouncycastle.asn1.pkcs CertificationRequest getEncoded

List of usage examples for org.bouncycastle.asn1.pkcs CertificationRequest getEncoded

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs CertificationRequest getEncoded.

Prototype

public byte[] getEncoded() throws IOException 

Source Link

Document

Return the default BER or DER encoding for this object.

Usage

From source file:com.adaptris.security.certificate.CertRequestHandler.java

License:Apache License

/**
 * Create a certificate request based on the provided certificate.
 * <p>/*from   w  w  w. j  av a  2  s. c o  m*/
 * The certificate request is returned as a printable string.
 * <p>
 * The default implementation returns a PEM string which is bounded by <code>-----BEGIN NEW CERTIFICATE REQUEST-----</code> and
 * <code>-----END NEW CERTIFICATE REQUEST-----</code> with each line terminated by a \n, e.g.
 * <p>
 * 
 * <pre>
 * {@code 
 * -----BEGIN NEW CERTIFICATE REQUEST-----
 * MIIC1zCCAb8CAQAwgZMxGTAXBgNVBAMTEEdoaWxhaW5lIFd5bnlhcmQxFTATBgNV
 * BAsTDElyaXMgU3VwcG9ydDENMAsGA1UEChMESXJpczEQMA4GA1UEBxMHRGF0Y2hl
 * dDESMBAGA1UECBMJQmVya3NoaXJlMQswCQYDVQQGEwJVSzEdMBsGCSqGSIb3DQEJ
 * ARYOZ2x3QGlyaXMuY28udWswggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB
 * AQCRQMSX5HCu25aQ+dtozxoGP1QtYC6JXuyGQkQ2MX2JsF1y+WM4NWUvhZvCKjmg
 * jsQL2c/rBzXifZCDRzMuHymOcmWXBaFJJr3b2XZQykzhKZKvTx6X1oU7PPcf+ws0
 * fNRQ3c1ZekPRYTUmSKyswJwIJDmpx4folU348taHwqcnK/LizekgjwrcZwSmFkk6
 * zhGTji2Ris4NMGvm/gDPGGmstxXTQIT62zOP1c61IhkKqxTtMZqmuB2HgQ8MRAza
 * xAxm7uoGbSCgUrWOjb92BHYxvQw8LKKkqKmKWRmDPPQKlBP4iR7vPUqXlb4G/keN
 * jR2EdByCJbkCUCwuVU124lJbAgMBAAEwDQYJKoZIhvcNAQEEBQADggEBADHpHomV
 * ZItNOeXjBDQGWNX9SpA9QV5IvEzm4u5mRI+XFsgYXvybeLMYC6Vrpxl9INVI6hpx
 * Nblq09Cq8lHQusyJNVEW3ibc73T5OZrCSnPTD7DUoKpwLwkDOwze7NHu+7NceUm1
 * pHCdiVe9Q3AC3+qlIdOhXGB3L5/Tn+8rYFNMAV8TQl7yRAz0g4lm+CdXQWaozTLY
 * /1MifSidgYoOq2lCE6l6JsVUv7mBTgaA52GuL0XvfopMOJrEuVUfFy7xVZMqD92L
 * ThcZUaIq5/Z3PUGLi8txXXb0Ga81SkLAHpBljgED0pV06EsrDz/N+12aH75zDcE+
 * Odt/GdYzfpeYvBg=
 * -----END NEW CERTIFICATE REQUEST-----
 * }
 *  </pre>
 * 
 * @param c the certificate
 * @param key the private key
 * @return the PEM encoded string
 * @throws AdaptrisSecurityException if any error occurs
 * @see AdaptrisSecurityException
 */
public static String createRequest(Certificate c, PrivateKey key) throws AdaptrisSecurityException {

    String pemRequest = null;
    ByteArrayOutputStream out = null;
    try {

        CertificationRequest req = createCertRequest(c, key);
        out = new ByteArrayOutputStream();
        out.write("-----BEGIN NEW CERTIFICATE REQUEST-----".getBytes());
        out.write(req.getEncoded());
        out.write("-----END NEW CERTIFICATE REQUEST-----".getBytes());
        out.close();
        pemRequest = out.toString();
    } catch (Exception e) {
        throw new CertException(e);
    }
    return pemRequest;
}

From source file:com.adaptris.security.certificate.CertRequestHandler.java

License:Apache License

/**
 * Create a CertificateRequest based on the provided certificate and write it
 * to the supplied outputStream.// w w  w . j ava 2 s  .  c  om
 * <p>
 * The default implementation writes out the request as a DER encoded ASN.1
 * data structure
 * 
 * @param c the certificate
 * @param out the OutputStream to write to
 * @param key the Private key
 * @throws AdaptrisSecurityException if any error occurs
 * @see AdaptrisSecurityException
 */
public static void createRequest(Certificate c, PrivateKey key, OutputStream out)
        throws AdaptrisSecurityException {
    try {

        CertificationRequest req = createCertRequest(c, key);
        out.write(req.getEncoded());
    } catch (Exception e) {
        throw new CertException(e);
    }
}

From source file:me.it_result.ca.scep.ScepServer.java

License:Open Source License

public void authorizeManually(CertificationRequest csr, AuthorizationOutcome authorization) throws CAException {
    try {/*from  w w w  .  j a v  a  2 s .c o  m*/
        byte[] csrBytes = csr.getEncoded();
        String alias = Utils.sha1(csrBytes);
        Database db = getContext().getDatabase();
        if (db.readBytes(alias, ScepServlet.MANUAL_AUTHORIZATION_CSR_PROPERTY) == null)
            throw new CAException("The csr is not scheduled for manual authorization");
        if (authorization == AuthorizationOutcome.ACCEPT)
            getContext().getCA().signCertificate(csrBytes);
        else if (authorization == AuthorizationOutcome.REJECT)
            db.writeBytes(alias, ScepServlet.REJECTED_CSR_PROPERTY, csrBytes);
        db.removeProperty(alias, ScepServlet.MANUAL_AUTHORIZATION_CSR_PROPERTY);
    } catch (Exception e) {
        throw new CAException(e);
    }
}

From source file:me.it_result.ca.scep.ScepServlet.java

License:Open Source License

@Override
protected List<X509Certificate> doEnroll(CertificationRequest certificationRequest)
        throws OperationFailureException {
    // Is csr signed already?
    try {/*  ww w . j av a  2  s . c o  m*/
        for (X509Certificate cert : ca().listCertificates()) {
            String certAlias = Utils.generateAlias(cert.getSubjectX500Principal());
            String csrAlias = Utils
                    .generateAlias(certificationRequest.getCertificationRequestInfo().getSubject());
            // TODO: compare keys, etc?
            if (certAlias.equals(csrAlias))
                return Collections.singletonList(cert);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    // Was csr manually rejected?
    boolean rejected;
    try {
        Database db = getDatabase();
        byte[] csrBytes = certificationRequest.getEncoded();
        String alias = Utils.sha1(csrBytes);
        rejected = db.readBytes(alias, REJECTED_CSR_PROPERTY) != null;
        db.removeProperty(alias, REJECTED_CSR_PROPERTY);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (rejected)
        throw new OperationFailureException(FailInfo.badMessageCheck);
    // execute request
    AuthorizationOutcome outcome = authorize(certificationRequest);
    if (outcome == AuthorizationOutcome.REJECT)
        throw new OperationFailureException(FailInfo.badMessageCheck);
    try {
        byte[] csrBytes = certificationRequest.getEncoded();
        if (outcome == AuthorizationOutcome.ACCEPT) {
            X509Certificate certificate = ca().signCertificate(csrBytes);
            return Collections.singletonList(certificate);
        } else {
            String alias = Utils.sha1(csrBytes);
            getDatabase().writeBytes(alias, MANUAL_AUTHORIZATION_CSR_PROPERTY, csrBytes);
            return Collections.emptyList();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.deviceconnect.android.ssl.CertificateAuthorityClient.java

License:MIT License

void executeCertificateRequest(final CertificationRequest request, final CertificateRequestCallback callback) {
    try {/*from www .ja v a 2s  . c  om*/
        ICertificateAuthority localCA = fetchLocalCA(callback);
        if (localCA == null) {
            return;
        }
        byte[] cert = localCA.requestCertificate(request.getEncoded());
        if (cert == null) {
            mLogger.log(Level.SEVERE, "end-point certificate is null.");
            callback.onError();
            return;
        }
        byte[] rootCert = localCA.getRootCertificate();
        if (rootCert == null) {
            mLogger.log(Level.SEVERE, "root certificate is null.");
            callback.onError();
            return;
        }
        callback.onCreate(decodeX509Certificate(cert), decodeX509Certificate(rootCert));
    } catch (InterruptedException e) {
        mLogger.log(Level.SEVERE, "Failed to generate server certificate.", e);
        callback.onError();
    } catch (RemoteException e) {
        mLogger.log(Level.SEVERE, "Failed to generate server certificate.", e);
        callback.onError();
    } catch (IOException e) {
        mLogger.log(Level.SEVERE, "Failed to generate server certificate.", e);
        callback.onError();
    } catch (CertificateException e) {
        mLogger.log(Level.SEVERE, "Failed to generate server certificate.", e);
        callback.onError();
    }
}