Search in sources :

Example 1 with CompositePublicKey

use of com.github.zhenwei.provider.jcajce.CompositePublicKey in project LinLong-Java by zhenwei1108.

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(com.github.zhenwei.provider.jcajce.CompositePublicKey) CompositePublicKey(com.github.zhenwei.provider.jcajce.CompositePublicKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) ContentVerifierProvider(com.github.zhenwei.pkix.operator.ContentVerifierProvider) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 2 with CompositePublicKey

use of com.github.zhenwei.provider.jcajce.CompositePublicKey in project LinLong-Java by zhenwei1108.

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(com.github.zhenwei.provider.jcajce.CompositePublicKey) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) CompositePublicKey(com.github.zhenwei.provider.jcajce.CompositePublicKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) GeneralSecurityException(java.security.GeneralSecurityException) RuntimeOperatorException(com.github.zhenwei.pkix.operator.RuntimeOperatorException) SignatureException(java.security.SignatureException) CertificateException(java.security.cert.CertificateException) CertificateEncodingException(java.security.cert.CertificateEncodingException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 3 with CompositePublicKey

use of com.github.zhenwei.provider.jcajce.CompositePublicKey in project LinLong-Java by zhenwei1108.

the class X509CertificateImpl method doVerify.

private void doVerify(PublicKey key, SignatureCreator signatureCreator) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, SignatureException, NoSuchProviderException {
    if (key instanceof CompositePublicKey && X509SignatureUtil.isCompositeAlgorithm(c.getSignatureAlgorithm())) {
        List<PublicKey> pubKeys = ((CompositePublicKey) key).getPublicKeys();
        ASN1Sequence keySeq = ASN1Sequence.getInstance(c.getSignatureAlgorithm().getParameters());
        ASN1Sequence sigSeq = ASN1Sequence.getInstance(DERBitString.getInstance(c.getSignature()).getBytes());
        boolean success = false;
        for (int i = 0; i != pubKeys.size(); i++) {
            if (pubKeys.get(i) == null) {
                continue;
            }
            AlgorithmIdentifier sigAlg = AlgorithmIdentifier.getInstance(keySeq.getObjectAt(i));
            String sigName = X509SignatureUtil.getSignatureName(sigAlg);
            Signature signature = signatureCreator.createSignature(sigName);
            SignatureException sigExc = null;
            try {
                checkSignature((PublicKey) pubKeys.get(i), signature, sigAlg.getParameters(), DERBitString.getInstance(sigSeq.getObjectAt(i)).getBytes());
                success = true;
            } catch (SignatureException e) {
                sigExc = e;
            }
            if (sigExc != null) {
                throw sigExc;
            }
        }
        if (!success) {
            throw new InvalidKeyException("no matching key found");
        }
    } else if (X509SignatureUtil.isCompositeAlgorithm(c.getSignatureAlgorithm())) {
        ASN1Sequence keySeq = ASN1Sequence.getInstance(c.getSignatureAlgorithm().getParameters());
        ASN1Sequence sigSeq = ASN1Sequence.getInstance(DERBitString.getInstance(c.getSignature()).getBytes());
        boolean success = false;
        for (int i = 0; i != sigSeq.size(); i++) {
            AlgorithmIdentifier sigAlg = AlgorithmIdentifier.getInstance(keySeq.getObjectAt(i));
            String sigName = X509SignatureUtil.getSignatureName(sigAlg);
            SignatureException sigExc = null;
            try {
                Signature signature = signatureCreator.createSignature(sigName);
                checkSignature(key, signature, sigAlg.getParameters(), DERBitString.getInstance(sigSeq.getObjectAt(i)).getBytes());
                success = true;
            } catch (InvalidKeyException e) {
            // ignore
            } catch (NoSuchAlgorithmException e) {
            // ignore
            } catch (SignatureException e) {
                sigExc = e;
            }
            if (sigExc != null) {
                throw sigExc;
            }
        }
        if (!success) {
            throw new InvalidKeyException("no matching key found");
        }
    } else {
        String sigName = X509SignatureUtil.getSignatureName(c.getSignatureAlgorithm());
        Signature signature = signatureCreator.createSignature(sigName);
        if (key instanceof CompositePublicKey) {
            List<PublicKey> keys = ((CompositePublicKey) key).getPublicKeys();
            for (int i = 0; i != keys.size(); i++) {
                try {
                    checkSignature((PublicKey) keys.get(i), signature, c.getSignatureAlgorithm().getParameters(), this.getSignature());
                    // found the match!
                    return;
                } catch (InvalidKeyException e) {
                // continue;
                }
            }
            throw new InvalidKeyException("no matching signature found");
        } else {
            checkSignature(key, signature, c.getSignatureAlgorithm().getParameters(), this.getSignature());
        }
    }
}
Also used : CompositePublicKey(com.github.zhenwei.provider.jcajce.CompositePublicKey) PublicKey(java.security.PublicKey) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) DERBitString(com.github.zhenwei.core.asn1.DERBitString) ASN1String(com.github.zhenwei.core.asn1.ASN1String) ASN1IA5String(com.github.zhenwei.core.asn1.ASN1IA5String) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) CompositePublicKey(com.github.zhenwei.provider.jcajce.CompositePublicKey) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) Signature(java.security.Signature) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with CompositePublicKey

use of com.github.zhenwei.provider.jcajce.CompositePublicKey in project LinLong-Java by zhenwei1108.

the class X509CRLImpl method doVerify.

private void doVerify(PublicKey key, SignatureCreator sigCreator) throws CRLException, NoSuchAlgorithmException, InvalidKeyException, SignatureException, NoSuchProviderException {
    if (!c.getSignatureAlgorithm().equals(c.getTBSCertList().getSignature())) {
        throw new CRLException("Signature algorithm on CertificateList does not match TBSCertList.");
    }
    if (key instanceof CompositePublicKey && X509SignatureUtil.isCompositeAlgorithm(c.getSignatureAlgorithm())) {
        List<PublicKey> pubKeys = ((CompositePublicKey) key).getPublicKeys();
        ASN1Sequence keySeq = ASN1Sequence.getInstance(c.getSignatureAlgorithm().getParameters());
        ASN1Sequence sigSeq = ASN1Sequence.getInstance(DERBitString.getInstance(c.getSignature()).getBytes());
        boolean success = false;
        for (int i = 0; i != pubKeys.size(); i++) {
            if (pubKeys.get(i) == null) {
                continue;
            }
            AlgorithmIdentifier sigAlg = AlgorithmIdentifier.getInstance(keySeq.getObjectAt(i));
            String sigName = X509SignatureUtil.getSignatureName(sigAlg);
            Signature signature = sigCreator.createSignature(sigName);
            SignatureException sigExc = null;
            try {
                checkSignature((PublicKey) pubKeys.get(i), signature, sigAlg.getParameters(), DERBitString.getInstance(sigSeq.getObjectAt(i)).getBytes());
                success = true;
            } catch (SignatureException e) {
                sigExc = e;
            }
            if (sigExc != null) {
                throw sigExc;
            }
        }
        if (!success) {
            throw new InvalidKeyException("no matching key found");
        }
    } else if (X509SignatureUtil.isCompositeAlgorithm(c.getSignatureAlgorithm())) {
        ASN1Sequence keySeq = ASN1Sequence.getInstance(c.getSignatureAlgorithm().getParameters());
        ASN1Sequence sigSeq = ASN1Sequence.getInstance(DERBitString.getInstance(c.getSignature()).getBytes());
        boolean success = false;
        for (int i = 0; i != sigSeq.size(); i++) {
            AlgorithmIdentifier sigAlg = AlgorithmIdentifier.getInstance(keySeq.getObjectAt(i));
            String sigName = X509SignatureUtil.getSignatureName(sigAlg);
            SignatureException sigExc = null;
            try {
                Signature signature = sigCreator.createSignature(sigName);
                checkSignature(key, signature, sigAlg.getParameters(), DERBitString.getInstance(sigSeq.getObjectAt(i)).getBytes());
                success = true;
            } catch (InvalidKeyException e) {
            // ignore
            } catch (NoSuchAlgorithmException e) {
            // ignore
            } catch (SignatureException e) {
                sigExc = e;
            }
            if (sigExc != null) {
                throw sigExc;
            }
        }
        if (!success) {
            throw new InvalidKeyException("no matching key found");
        }
    } else {
        Signature sig = sigCreator.createSignature(getSigAlgName());
        if (sigAlgParams == null) {
            checkSignature(key, sig, null, this.getSignature());
        } else {
            try {
                checkSignature(key, sig, ASN1Primitive.fromByteArray(sigAlgParams), this.getSignature());
            } catch (IOException e) {
                throw new SignatureException("cannot decode signature parameters: " + e.getMessage());
            }
        }
    }
}
Also used : CompositePublicKey(com.github.zhenwei.provider.jcajce.CompositePublicKey) PublicKey(java.security.PublicKey) ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) DERBitString(com.github.zhenwei.core.asn1.DERBitString) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) IssuingDistributionPoint(com.github.zhenwei.core.asn1.x509.IssuingDistributionPoint) CRLDistPoint(com.github.zhenwei.core.asn1.x509.CRLDistPoint) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) CompositePublicKey(com.github.zhenwei.provider.jcajce.CompositePublicKey) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) Signature(java.security.Signature) CRLException(java.security.cert.CRLException)

Aggregations

AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)4 CompositePublicKey (com.github.zhenwei.provider.jcajce.CompositePublicKey)4 PublicKey (java.security.PublicKey)4 Signature (java.security.Signature)4 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)3 SignatureException (java.security.SignatureException)3 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)2 DERBitString (com.github.zhenwei.core.asn1.DERBitString)2 OperatorCreationException (com.github.zhenwei.pkix.operator.OperatorCreationException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ASN1BitString (com.github.zhenwei.core.asn1.ASN1BitString)1 ASN1IA5String (com.github.zhenwei.core.asn1.ASN1IA5String)1 ASN1String (com.github.zhenwei.core.asn1.ASN1String)1 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 CRLDistPoint (com.github.zhenwei.core.asn1.x509.CRLDistPoint)1 IssuingDistributionPoint (com.github.zhenwei.core.asn1.x509.IssuingDistributionPoint)1 ContentVerifierProvider (com.github.zhenwei.pkix.operator.ContentVerifierProvider)1 RuntimeOperatorException (com.github.zhenwei.pkix.operator.RuntimeOperatorException)1 IOException (java.io.IOException)1