Search in sources :

Example 1 with CompositePublicKey

use of org.bouncycastle.jcajce.CompositePublicKey in project jmulticard by ctt-gob-es.

the class JcaContentVerifierProviderBuilder method createCompositeVerifier.

private ContentVerifier createCompositeVerifier(AlgorithmIdentifier compAlgId, PublicKey publicKey) throws OperatorCreationException {
    if (publicKey instanceof CompositePublicKey) {
        List<PublicKey> pubKeys = ((CompositePublicKey) publicKey).getPublicKeys();
        ASN1Sequence keySeq = ASN1Sequence.getInstance(compAlgId.getParameters());
        Signature[] sigs = new Signature[keySeq.size()];
        for (int i = 0; i != keySeq.size(); i++) {
            AlgorithmIdentifier sigAlg = AlgorithmIdentifier.getInstance(keySeq.getObjectAt(i));
            if (pubKeys.get(i) != null) {
                sigs[i] = createSignature(sigAlg, (PublicKey) pubKeys.get(i));
            } else {
                sigs[i] = null;
            }
        }
        return new CompositeVerifier(sigs);
    } else {
        ASN1Sequence keySeq = ASN1Sequence.getInstance(compAlgId.getParameters());
        Signature[] sigs = new Signature[keySeq.size()];
        for (int i = 0; i != keySeq.size(); i++) {
            AlgorithmIdentifier sigAlg = AlgorithmIdentifier.getInstance(keySeq.getObjectAt(i));
            try {
                sigs[i] = createSignature(sigAlg, publicKey);
            } catch (Exception e) {
                sigs[i] = null;
            // continue
            }
        }
        return new CompositeVerifier(sigs);
    }
}
Also used : CompositePublicKey(org.bouncycastle.jcajce.CompositePublicKey) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) CompositePublicKey(org.bouncycastle.jcajce.CompositePublicKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) GeneralSecurityException(java.security.GeneralSecurityException) SignatureException(java.security.SignatureException) CertificateException(java.security.cert.CertificateException) RuntimeOperatorException(org.bouncycastle.operator.RuntimeOperatorException) CertificateEncodingException(java.security.cert.CertificateEncodingException) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 2 with CompositePublicKey

use of org.bouncycastle.jcajce.CompositePublicKey in project jmulticard by ctt-gob-es.

the class JcaContentVerifierProviderBuilder method build.

public ContentVerifierProvider build(final PublicKey publicKey) throws OperatorCreationException {
    return new ContentVerifierProvider() {

        public boolean hasAssociatedCertificate() {
            return false;
        }

        public X509CertificateHolder getAssociatedCertificate() {
            return null;
        }

        public ContentVerifier get(AlgorithmIdentifier algorithm) throws OperatorCreationException {
            if (algorithm.getAlgorithm().equals(MiscObjectIdentifiers.id_alg_composite)) {
                return createCompositeVerifier(algorithm, publicKey);
            }
            if (publicKey instanceof CompositePublicKey) {
                List<PublicKey> keys = ((CompositePublicKey) publicKey).getPublicKeys();
                for (int i = 0; i != keys.size(); i++) {
                    try {
                        Signature sig = createSignature(algorithm, (PublicKey) keys.get(i));
                        Signature rawSig = createRawSig(algorithm, (PublicKey) keys.get(i));
                        if (rawSig != null) {
                            return new RawSigVerifier(algorithm, sig, rawSig);
                        } else {
                            return new SigVerifier(algorithm, sig);
                        }
                    } catch (OperatorCreationException e) {
                    // skip incorrect keys
                    }
                }
                throw new OperatorCreationException("no matching algorithm found for key");
            } else {
                Signature sig = createSignature(algorithm, publicKey);
                Signature rawSig = createRawSig(algorithm, publicKey);
                if (rawSig != null) {
                    return new RawSigVerifier(algorithm, sig, rawSig);
                } else {
                    return new SigVerifier(algorithm, sig);
                }
            }
        }
    };
}
Also used : CompositePublicKey(org.bouncycastle.jcajce.CompositePublicKey) CompositePublicKey(org.bouncycastle.jcajce.CompositePublicKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) ContentVerifierProvider(org.bouncycastle.operator.ContentVerifierProvider) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Aggregations

PublicKey (java.security.PublicKey)2 Signature (java.security.Signature)2 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)2 CompositePublicKey (org.bouncycastle.jcajce.CompositePublicKey)2 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)2 GeneralSecurityException (java.security.GeneralSecurityException)1 SignatureException (java.security.SignatureException)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 CertificateException (java.security.cert.CertificateException)1 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)1 ContentVerifierProvider (org.bouncycastle.operator.ContentVerifierProvider)1 RuntimeOperatorException (org.bouncycastle.operator.RuntimeOperatorException)1