Search in sources :

Example 1 with EnrollmentException

use of org.hyperledger.fabric_ca.sdk.exception.EnrollmentException in project fabric-sdk-java by hyperledger.

the class MockHFCAClient method httpPost.

@Override
JsonObject httpPost(String url, String body, User admin) throws Exception {
    JsonObject response;
    if (httpPostResponse == null) {
        response = super.httpPost(url, body, admin);
    } else {
        JsonReader reader = Json.createReader(new StringReader(httpPostResponse));
        response = (JsonObject) reader.read();
        // TODO: HFCAClient could do with some minor refactoring to avoid duplicating this code here!!
        JsonObject result = response.getJsonObject("result");
        if (result == null) {
            EnrollmentException e = new EnrollmentException(format("POST request to %s failed request body %s " + "Body of response did not contain result", url, body), new Exception());
            throw e;
        }
    }
    return response;
}
Also used : EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) JsonReader(javax.json.JsonReader) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) MalformedURLException(java.net.MalformedURLException) EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException)

Example 2 with EnrollmentException

use of org.hyperledger.fabric_ca.sdk.exception.EnrollmentException in project fabric-sdk-java by hyperledger.

the class HFCAClient method enroll.

/**
 * Enroll the user with member service
 *
 * @param user   Identity name to enroll
 * @param secret Secret returned via registration
 * @param req    Enrollment request with the following fields: hosts, profile, csr, label, keypair
 * @return enrollment
 * @throws EnrollmentException
 * @throws InvalidArgumentException
 */
public Enrollment enroll(String user, String secret, EnrollmentRequest req) throws EnrollmentException, InvalidArgumentException {
    logger.debug(format("url:%s enroll user: %s", url, user));
    if (Utils.isNullOrEmpty(user)) {
        throw new InvalidArgumentException("enrollment user is not set");
    }
    if (Utils.isNullOrEmpty(secret)) {
        throw new InvalidArgumentException("enrollment secret is not set");
    }
    if (cryptoSuite == null) {
        throw new InvalidArgumentException("Crypto primitives not set.");
    }
    setUpSSL();
    try {
        String pem = req.getCsr();
        KeyPair keypair = req.getKeyPair();
        if (null != pem && keypair == null) {
            throw new InvalidArgumentException("If certificate signing request is supplied the key pair needs to be supplied too.");
        }
        if (keypair == null) {
            logger.debug("[HFCAClient.enroll] Generating keys...");
            // generate ECDSA keys: signing and encryption keys
            keypair = cryptoSuite.keyGen();
            logger.debug("[HFCAClient.enroll] Generating keys...done!");
        }
        if (pem == null) {
            String csr = cryptoSuite.generateCertificationRequest(user, keypair);
            req.setCSR(csr);
        }
        if (caName != null && !caName.isEmpty()) {
            req.setCAName(caName);
        }
        String body = req.toJson();
        String responseBody = httpPost(url + HFCA_ENROLL, body, new UsernamePasswordCredentials(user, secret));
        logger.debug("response:" + responseBody);
        JsonReader reader = Json.createReader(new StringReader(responseBody));
        JsonObject jsonst = (JsonObject) reader.read();
        boolean success = jsonst.getBoolean("success");
        logger.debug(format("[HFCAClient] enroll success:[%s]", success));
        if (!success) {
            throw new EnrollmentException(format("FabricCA failed enrollment for user %s response success is false.", user));
        }
        JsonObject result = jsonst.getJsonObject("result");
        if (result == null) {
            throw new EnrollmentException(format("FabricCA failed enrollment for user %s - response did not contain a result", user));
        }
        Base64.Decoder b64dec = Base64.getDecoder();
        String signedPem = new String(b64dec.decode(result.getString("Cert").getBytes(UTF_8)));
        logger.debug(format("[HFCAClient] enroll returned pem:[%s]", signedPem));
        JsonArray messages = jsonst.getJsonArray("messages");
        if (messages != null && !messages.isEmpty()) {
            JsonObject jo = messages.getJsonObject(0);
            String message = format("Enroll request response message [code %d]: %s", jo.getInt("code"), jo.getString("message"));
            logger.info(message);
        }
        logger.debug("Enrollment done.");
        return new HFCAEnrollment(keypair, signedPem);
    } catch (EnrollmentException ee) {
        logger.error(format("url:%s, user:%s  error:%s", url, user, ee.getMessage()), ee);
        throw ee;
    } catch (Exception e) {
        EnrollmentException ee = new EnrollmentException(format("Url:%s, Failed to enroll user %s ", url, user), e);
        logger.error(e.getMessage(), e);
        throw ee;
    }
}
Also used : KeyPair(java.security.KeyPair) Base64(java.util.Base64) EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException) JsonObject(javax.json.JsonObject) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) URISyntaxException(java.net.URISyntaxException) RegistrationException(org.hyperledger.fabric_ca.sdk.exception.RegistrationException) KeyStoreException(java.security.KeyStoreException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) GenerateCRLException(org.hyperledger.fabric_ca.sdk.exception.GenerateCRLException) KeyManagementException(java.security.KeyManagementException) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) RevocationException(org.hyperledger.fabric_ca.sdk.exception.RevocationException) ParseException(org.apache.http.ParseException) MalformedURLException(java.net.MalformedURLException) InfoException(org.hyperledger.fabric_ca.sdk.exception.InfoException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) JsonArray(javax.json.JsonArray) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader)

Example 3 with EnrollmentException

use of org.hyperledger.fabric_ca.sdk.exception.EnrollmentException in project fabric-sdk-java by hyperledger.

the class HFCAClient method reenroll.

/**
 * Re-Enroll the user with member service
 *
 * @param user User to be re-enrolled
 * @param req  Enrollment request with the following fields: hosts, profile, csr, label
 * @return enrollment
 * @throws EnrollmentException
 * @throws InvalidArgumentException
 */
public Enrollment reenroll(User user, EnrollmentRequest req) throws EnrollmentException, InvalidArgumentException {
    if (cryptoSuite == null) {
        throw new InvalidArgumentException("Crypto primitives not set.");
    }
    if (user == null) {
        throw new InvalidArgumentException("reenrollment user is missing");
    }
    if (user.getEnrollment() == null) {
        throw new InvalidArgumentException("reenrollment user is not a valid user object");
    }
    logger.debug(format("re-enroll user: %s, url: %s", user.getName(), url));
    try {
        setUpSSL();
        PublicKey publicKey = cryptoSuite.bytesToCertificate(user.getEnrollment().getCert().getBytes(StandardCharsets.UTF_8)).getPublicKey();
        KeyPair keypair = new KeyPair(publicKey, user.getEnrollment().getKey());
        // generate CSR
        String pem = cryptoSuite.generateCertificationRequest(user.getName(), keypair);
        // build request body
        req.setCSR(pem);
        if (caName != null && !caName.isEmpty()) {
            req.setCAName(caName);
        }
        String body = req.toJson();
        // build authentication header
        JsonObject result = httpPost(url + HFCA_REENROLL, body, user);
        // get new cert from response
        Base64.Decoder b64dec = Base64.getDecoder();
        String signedPem = new String(b64dec.decode(result.getString("Cert").getBytes(UTF_8)));
        logger.debug(format("[HFCAClient] re-enroll returned pem:[%s]", signedPem));
        logger.debug(format("reenroll user %s done.", user.getName()));
        return new HFCAEnrollment(keypair, signedPem);
    } catch (EnrollmentException ee) {
        logger.error(ee.getMessage(), ee);
        throw ee;
    } catch (Exception e) {
        EnrollmentException ee = new EnrollmentException(format("Failed to re-enroll user %s", user), e);
        logger.error(e.getMessage(), e);
        throw ee;
    }
}
Also used : KeyPair(java.security.KeyPair) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) Base64(java.util.Base64) EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException) PublicKey(java.security.PublicKey) JsonObject(javax.json.JsonObject) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) URISyntaxException(java.net.URISyntaxException) RegistrationException(org.hyperledger.fabric_ca.sdk.exception.RegistrationException) KeyStoreException(java.security.KeyStoreException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) GenerateCRLException(org.hyperledger.fabric_ca.sdk.exception.GenerateCRLException) KeyManagementException(java.security.KeyManagementException) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) RevocationException(org.hyperledger.fabric_ca.sdk.exception.RevocationException) ParseException(org.apache.http.ParseException) MalformedURLException(java.net.MalformedURLException) InfoException(org.hyperledger.fabric_ca.sdk.exception.InfoException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException)

Example 4 with EnrollmentException

use of org.hyperledger.fabric_ca.sdk.exception.EnrollmentException in project fabric-sdk-java by hyperledger.

the class HFCAClient method info.

/**
 * Return information on the Fabric Certificate Authority.
 * No credentials are needed for this API.
 *
 * @return {@link HFCAInfo}
 * @throws InfoException
 * @throws InvalidArgumentException
 */
public HFCAInfo info() throws InfoException, InvalidArgumentException {
    logger.debug(format("info url:%s", url));
    if (cryptoSuite == null) {
        throw new InvalidArgumentException("Crypto primitives not set.");
    }
    setUpSSL();
    try {
        JsonObjectBuilder factory = Json.createObjectBuilder();
        if (caName != null) {
            factory.add(HFCAClient.FABRIC_CA_REQPROP, caName);
        }
        JsonObject body = factory.build();
        String responseBody = httpPost(url + HFCA_INFO, body.toString(), (UsernamePasswordCredentials) null);
        logger.debug("response:" + responseBody);
        JsonReader reader = Json.createReader(new StringReader(responseBody));
        JsonObject jsonst = (JsonObject) reader.read();
        boolean success = jsonst.getBoolean("success");
        logger.debug(format("[HFCAClient] enroll success:[%s]", success));
        if (!success) {
            throw new EnrollmentException(format("FabricCA failed info %s", url));
        }
        JsonObject result = jsonst.getJsonObject("result");
        if (result == null) {
            throw new InfoException(format("FabricCA info error  - response did not contain a result url %s", url));
        }
        String caName = result.getString("CAName");
        String caChain = result.getString("CAChain");
        String version = null;
        if (result.containsKey("Version")) {
            version = result.getString("Version");
        }
        return new HFCAInfo(caName, caChain, version);
    } catch (Exception e) {
        InfoException ee = new InfoException(format("Url:%s, Failed to get info", url), e);
        logger.error(e.getMessage(), e);
        throw ee;
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) JsonReader(javax.json.JsonReader) InfoException(org.hyperledger.fabric_ca.sdk.exception.InfoException) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) JsonObjectBuilder(javax.json.JsonObjectBuilder) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) URISyntaxException(java.net.URISyntaxException) RegistrationException(org.hyperledger.fabric_ca.sdk.exception.RegistrationException) KeyStoreException(java.security.KeyStoreException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) GenerateCRLException(org.hyperledger.fabric_ca.sdk.exception.GenerateCRLException) KeyManagementException(java.security.KeyManagementException) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) RevocationException(org.hyperledger.fabric_ca.sdk.exception.RevocationException) ParseException(org.apache.http.ParseException) MalformedURLException(java.net.MalformedURLException) InfoException(org.hyperledger.fabric_ca.sdk.exception.InfoException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException)

Aggregations

MalformedURLException (java.net.MalformedURLException)4 JsonObject (javax.json.JsonObject)4 EnrollmentException (org.hyperledger.fabric_ca.sdk.exception.EnrollmentException)4 InvalidArgumentException (org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException)4 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 URISyntaxException (java.net.URISyntaxException)3 KeyManagementException (java.security.KeyManagementException)3 KeyStoreException (java.security.KeyStoreException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 UnrecoverableKeyException (java.security.UnrecoverableKeyException)3 CertificateException (java.security.cert.CertificateException)3 JsonReader (javax.json.JsonReader)3 ParseException (org.apache.http.ParseException)3 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)3 AffiliationException (org.hyperledger.fabric_ca.sdk.exception.AffiliationException)3 GenerateCRLException (org.hyperledger.fabric_ca.sdk.exception.GenerateCRLException)3 HTTPException (org.hyperledger.fabric_ca.sdk.exception.HTTPException)3 IdentityException (org.hyperledger.fabric_ca.sdk.exception.IdentityException)3 InfoException (org.hyperledger.fabric_ca.sdk.exception.InfoException)3