Search in sources :

Example 1 with RuntimeOperatorException

use of org.bouncycastle.operator.RuntimeOperatorException in project dgc-gateway by eu-digital-green-certificates.

the class SignerInformationService method certificateSignedByCa.

private boolean certificateSignedByCa(X509CertificateHolder certificate, TrustedPartyEntity caCertificateEntity) {
    X509Certificate caCertificate = trustedPartyService.getX509CertificateFromEntity(caCertificateEntity);
    ContentVerifierProvider verifier;
    try {
        verifier = new JcaContentVerifierProviderBuilder().build(caCertificate);
    } catch (OperatorCreationException e) {
        DgcMdc.put("certHash", caCertificateEntity.getThumbprint());
        log.error("Failed to instantiate JcaContentVerifierProvider from cert");
        return false;
    }
    try {
        return certificate.isSignatureValid(verifier);
    } catch (CertException | RuntimeOperatorException e) {
        return false;
    }
}
Also used : JcaContentVerifierProviderBuilder(org.bouncycastle.operator.jcajce.JcaContentVerifierProviderBuilder) RuntimeOperatorException(org.bouncycastle.operator.RuntimeOperatorException) CertException(org.bouncycastle.cert.CertException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) X509Certificate(java.security.cert.X509Certificate) ContentVerifierProvider(org.bouncycastle.operator.ContentVerifierProvider)

Example 2 with RuntimeOperatorException

use of org.bouncycastle.operator.RuntimeOperatorException in project xipki by xipki.

the class FpIdCalculator method hash.

/**
 * Hash the data and returns the first 8 bytes of the hash value.
 * @param data data over which the hash value is calculated.
 * @return long represented of the first 8 bytes
 */
public static long hash(byte[] data) {
    ParamUtil.requireNonNull("data", data);
    ConcurrentBagEntry<Digest> md0 = null;
    for (int i = 0; i < 3; i++) {
        try {
            md0 = MDS.borrow(10, TimeUnit.SECONDS);
            break;
        } catch (InterruptedException ex) {
        // CHECKSTYLE:SKIP
        }
    }
    if (md0 == null) {
        throw new RuntimeOperatorException("could not get idle MessageDigest");
    }
    try {
        Digest md = md0.value();
        md.reset();
        md.update(data, 0, data.length);
        byte[] bytes = new byte[md.getDigestSize()];
        md.doFinal(bytes, 0);
        return bytesToLong(bytes);
    } finally {
        MDS.requite(md0);
    }
}
Also used : RuntimeOperatorException(org.bouncycastle.operator.RuntimeOperatorException) Digest(org.bouncycastle.crypto.Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest)

Example 3 with RuntimeOperatorException

use of org.bouncycastle.operator.RuntimeOperatorException in project xipki by xipki.

the class HashCalculator method hash.

public static byte[] hash(HashAlgo hashAlgo, byte[]... datas) {
    notNull(hashAlgo, "hashAlgo");
    notNull(datas, "datas");
    if (!MDS_MAP.containsKey(hashAlgo)) {
        throw new IllegalArgumentException("unknown hash algo " + hashAlgo);
    }
    ConcurrentBag<ConcurrentBagEntry<Digest>> mds = MDS_MAP.get(hashAlgo);
    ConcurrentBagEntry<Digest> md0 = null;
    for (int i = 0; i < 3; i++) {
        try {
            md0 = mds.borrow(10, TimeUnit.SECONDS);
            break;
        } catch (InterruptedException ex) {
        // CHECKSTYLE:SKIP
        }
    }
    if (md0 == null) {
        throw new RuntimeOperatorException("could not get idle MessageDigest");
    }
    try {
        Digest md = md0.value();
        md.reset();
        for (byte[] data : datas) {
            if (data != null && data.length > 0) {
                md.update(data, 0, data.length);
            }
        }
        byte[] bytes = new byte[md.getDigestSize()];
        md.doFinal(bytes, 0);
        return bytes;
    } finally {
        mds.requite(md0);
    }
}
Also used : RuntimeOperatorException(org.bouncycastle.operator.RuntimeOperatorException) Digest(org.bouncycastle.crypto.Digest) ConcurrentBagEntry(org.xipki.util.concurrent.ConcurrentBagEntry)

Example 4 with RuntimeOperatorException

use of org.bouncycastle.operator.RuntimeOperatorException in project xipki by xipki.

the class HashCalculator method hash.

// method hash
public static byte[] hash(HashAlgo hashAlgo, byte[] data, int offset, int len) {
    notNull(hashAlgo, "hashAlgo");
    notNull(data, "data");
    if (data.length - offset < len) {
        throw new IndexOutOfBoundsException("data.length - offset < len");
    }
    if (!MDS_MAP.containsKey(hashAlgo)) {
        throw new IllegalArgumentException("unknown hash algo " + hashAlgo);
    }
    ConcurrentBag<ConcurrentBagEntry<Digest>> mds = MDS_MAP.get(hashAlgo);
    ConcurrentBagEntry<Digest> md0 = null;
    for (int i = 0; i < 3; i++) {
        try {
            md0 = mds.borrow(10, TimeUnit.SECONDS);
            break;
        } catch (InterruptedException ex) {
        // CHECKSTYLE:SKIP
        }
    }
    if (md0 == null) {
        throw new RuntimeOperatorException("could not get idle MessageDigest");
    }
    try {
        Digest md = md0.value();
        md.reset();
        md.update(data, offset, len);
        byte[] bytes = new byte[md.getDigestSize()];
        md.doFinal(bytes, 0);
        return bytes;
    } finally {
        mds.requite(md0);
    }
}
Also used : RuntimeOperatorException(org.bouncycastle.operator.RuntimeOperatorException) Digest(org.bouncycastle.crypto.Digest) ConcurrentBagEntry(org.xipki.util.concurrent.ConcurrentBagEntry)

Aggregations

RuntimeOperatorException (org.bouncycastle.operator.RuntimeOperatorException)4 Digest (org.bouncycastle.crypto.Digest)3 ConcurrentBagEntry (org.xipki.util.concurrent.ConcurrentBagEntry)2 X509Certificate (java.security.cert.X509Certificate)1 CertException (org.bouncycastle.cert.CertException)1 SHA1Digest (org.bouncycastle.crypto.digests.SHA1Digest)1 ContentVerifierProvider (org.bouncycastle.operator.ContentVerifierProvider)1 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)1 JcaContentVerifierProviderBuilder (org.bouncycastle.operator.jcajce.JcaContentVerifierProviderBuilder)1