Search in sources :

Example 1 with CryptoException

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

the class CryptoPrimitives method setProperties.

// /* (non-Javadoc)
// * @see org.hyperledger.fabric.sdk.security.CryptoSuite#setProperties(java.util.Properties)
// */
// @Override
void setProperties(Properties properties) throws CryptoException, InvalidArgumentException {
    if (properties == null) {
        throw new InvalidArgumentException("properties must not be null");
    }
    // if (properties != null) {
    hashAlgorithm = Optional.ofNullable(properties.getProperty(Config.HASH_ALGORITHM)).orElse(hashAlgorithm);
    String secLevel = Optional.ofNullable(properties.getProperty(Config.SECURITY_LEVEL)).orElse(Integer.toString(securityLevel));
    securityLevel = Integer.parseInt(secLevel);
    if (properties.containsKey(Config.SECURITY_CURVE_MAPPING)) {
        securityCurveMapping = Config.parseSecurityCurveMappings(properties.getProperty(Config.SECURITY_CURVE_MAPPING));
    } else {
        securityCurveMapping = config.getSecurityCurveMapping();
    }
    final String providerName = properties.containsKey(Config.SECURITY_PROVIDER_CLASS_NAME) ? properties.getProperty(Config.SECURITY_PROVIDER_CLASS_NAME) : config.getSecurityProviderClassName();
    try {
        SECURITY_PROVIDER = setUpExplictProvider(providerName);
    } catch (Exception e) {
        throw new InvalidArgumentException(format("Getting provider for class name: %s", providerName), e);
    }
    CERTIFICATE_FORMAT = Optional.ofNullable(properties.getProperty(Config.CERTIFICATE_FORMAT)).orElse(CERTIFICATE_FORMAT);
    DEFAULT_SIGNATURE_ALGORITHM = Optional.ofNullable(properties.getProperty(Config.SIGNATURE_ALGORITHM)).orElse(DEFAULT_SIGNATURE_ALGORITHM);
    resetConfiguration();
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) KeyStoreException(java.security.KeyStoreException) CertPathValidatorException(java.security.cert.CertPathValidatorException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException)

Example 2 with CryptoException

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

the class CryptoPrimitives method ecdsaSignToBytes.

/**
 * Sign data with the specified elliptic curve private key.
 *
 * @param privateKey elliptic curve private key.
 * @param data       data to sign
 * @return the signed data.
 * @throws CryptoException
 */
private byte[] ecdsaSignToBytes(ECPrivateKey privateKey, byte[] data) throws CryptoException {
    try {
        X9ECParameters params = ECNamedCurveTable.getByName(curveName);
        BigInteger curveN = params.getN();
        Signature sig = SECURITY_PROVIDER == null ? Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM) : Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM, SECURITY_PROVIDER);
        sig.initSign(privateKey);
        sig.update(data);
        byte[] signature = sig.sign();
        BigInteger[] sigs = decodeECDSASignature(signature);
        sigs = preventMalleability(sigs, curveN);
        ByteArrayOutputStream s = new ByteArrayOutputStream();
        DERSequenceGenerator seq = new DERSequenceGenerator(s);
        seq.addObject(new ASN1Integer(sigs[0]));
        seq.addObject(new ASN1Integer(sigs[1]));
        seq.close();
        return s.toByteArray();
    } catch (Exception e) {
        throw new CryptoException("Could not sign the message using private key", e);
    }
}
Also used : X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) Signature(java.security.Signature) BigInteger(java.math.BigInteger) DERSequenceGenerator(org.bouncycastle.asn1.DERSequenceGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) KeyStoreException(java.security.KeyStoreException) CertPathValidatorException(java.security.cert.CertPathValidatorException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException)

Example 3 with CryptoException

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

the class CryptoPrimitives method addCACertificateToTrustStore.

/**
 * addCACertificateToTrustStore adds a CA cert to the set of certificates used for signature validation
 *
 * @param caCertPem an X.509 certificate in PEM format
 * @param alias     an alias associated with the certificate. Used as shorthand for the certificate during crypto operations
 * @throws CryptoException
 * @throws InvalidArgumentException
 */
public void addCACertificateToTrustStore(File caCertPem, String alias) throws CryptoException, InvalidArgumentException {
    if (caCertPem == null) {
        throw new InvalidArgumentException("The certificate cannot be null");
    }
    if (alias == null || alias.isEmpty()) {
        throw new InvalidArgumentException("You must assign an alias to a certificate when adding to the trust store");
    }
    BufferedInputStream bis;
    try {
        bis = new BufferedInputStream(new ByteArrayInputStream(FileUtils.readFileToByteArray(caCertPem)));
        Certificate caCert = cf.generateCertificate(bis);
        addCACertificateToTrustStore(caCert, alias);
    } catch (CertificateException | IOException e) {
        throw new CryptoException("Unable to add CA certificate to trust store. Error: " + e.getMessage(), e);
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 4 with CryptoException

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

the class CryptoPrimitives method verify.

@Override
public boolean verify(byte[] pemCertificate, String signatureAlgorithm, byte[] signature, byte[] plainText) throws CryptoException {
    boolean isVerified = false;
    if (plainText == null || signature == null || pemCertificate == null) {
        return false;
    }
    if (config.extraLogLevel(10)) {
        if (null != diagnosticFileDumper) {
            StringBuilder sb = new StringBuilder(10000);
            sb.append("plaintext in hex: " + DatatypeConverter.printHexBinary(plainText));
            sb.append("\n");
            sb.append("signature in hex: " + DatatypeConverter.printHexBinary(signature));
            sb.append("\n");
            sb.append("PEM cert in hex: " + DatatypeConverter.printHexBinary(pemCertificate));
            logger.trace("verify :  " + diagnosticFileDumper.createDiagnosticFile(sb.toString()));
        }
    }
    try {
        X509Certificate certificate = getX509Certificate(pemCertificate);
        if (certificate != null) {
            isVerified = validateCertificate(certificate);
            if (isVerified) {
                // only proceed if cert is trusted
                Signature sig = Signature.getInstance(signatureAlgorithm);
                sig.initVerify(certificate);
                sig.update(plainText);
                isVerified = sig.verify(signature);
            }
        }
    } catch (InvalidKeyException e) {
        CryptoException ex = new CryptoException("Cannot verify signature. Error is: " + e.getMessage() + "\r\nCertificate: " + DatatypeConverter.printHexBinary(pemCertificate), e);
        logger.error(ex.getMessage(), ex);
        throw ex;
    } catch (NoSuchAlgorithmException | SignatureException e) {
        CryptoException ex = new CryptoException("Cannot verify. Signature algorithm is invalid. Error is: " + e.getMessage(), e);
        logger.error(ex.getMessage(), ex);
        throw ex;
    }
    return isVerified;
}
Also used : Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) X509Certificate(java.security.cert.X509Certificate)

Example 5 with CryptoException

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

the class CryptoPrimitives method createTrustStore.

private void createTrustStore() throws CryptoException {
    try {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);
        setTrustStore(keyStore);
    } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | InvalidArgumentException e) {
        throw new CryptoException("Cannot create trust store. Error: " + e.getMessage(), e);
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) KeyStore(java.security.KeyStore)

Aggregations

CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)20 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 CertificateException (java.security.cert.CertificateException)11 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)11 KeyStoreException (java.security.KeyStoreException)10 IOException (java.io.IOException)9 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)6 InvalidKeyException (java.security.InvalidKeyException)6 SignatureException (java.security.SignatureException)6 CertPathValidatorException (java.security.cert.CertPathValidatorException)6 X509Certificate (java.security.cert.X509Certificate)5 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Certificate (java.security.cert.Certificate)4 Test (org.junit.Test)4 BufferedInputStream (java.io.BufferedInputStream)3 KeyStore (java.security.KeyStore)3 PrivateKey (java.security.PrivateKey)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 BigInteger (java.math.BigInteger)2