Search in sources :

Example 1 with ContentVerifierProvider

use of com.github.zhenwei.pkix.operator.ContentVerifierProvider 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 ContentVerifierProvider

use of com.github.zhenwei.pkix.operator.ContentVerifierProvider in project LinLong-Java by zhenwei1108.

the class JcaContentVerifierProviderBuilder method build.

public ContentVerifierProvider build(final X509Certificate certificate) throws OperatorCreationException {
    final X509CertificateHolder certHolder;
    try {
        certHolder = new JcaX509CertificateHolder(certificate);
    } catch (CertificateEncodingException e) {
        throw new OperatorCreationException("cannot process certificate: " + e.getMessage(), e);
    }
    return new ContentVerifierProvider() {

        public boolean hasAssociatedCertificate() {
            return true;
        }

        public X509CertificateHolder getAssociatedCertificate() {
            return certHolder;
        }

        public ContentVerifier get(AlgorithmIdentifier algorithm) throws OperatorCreationException {
            if (algorithm.getAlgorithm().equals(MiscObjectIdentifiers.id_alg_composite)) {
                return createCompositeVerifier(algorithm, certificate.getPublicKey());
            } else {
                Signature sig;
                try {
                    sig = helper.createSignature(algorithm);
                    sig.initVerify(certificate.getPublicKey());
                } catch (GeneralSecurityException e) {
                    throw new OperatorCreationException("exception on setup: " + e, e);
                }
                Signature rawSig = createRawSig(algorithm, certificate.getPublicKey());
                if (rawSig != null) {
                    return new RawSigVerifier(algorithm, sig, rawSig);
                } else {
                    return new SigVerifier(algorithm, sig);
                }
            }
        }
    };
}
Also used : X509CertificateHolder(com.github.zhenwei.pkix.cert.X509CertificateHolder) JcaX509CertificateHolder(com.github.zhenwei.pkix.cert.jcajce.JcaX509CertificateHolder) Signature(java.security.Signature) GeneralSecurityException(java.security.GeneralSecurityException) CertificateEncodingException(java.security.cert.CertificateEncodingException) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) JcaX509CertificateHolder(com.github.zhenwei.pkix.cert.jcajce.JcaX509CertificateHolder) ContentVerifierProvider(com.github.zhenwei.pkix.operator.ContentVerifierProvider) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 3 with ContentVerifierProvider

use of com.github.zhenwei.pkix.operator.ContentVerifierProvider in project LinLong-Java by zhenwei1108.

the class BcContentVerifierProviderBuilder method build.

public ContentVerifierProvider build(final X509CertificateHolder certHolder) throws OperatorCreationException {
    return new ContentVerifierProvider() {

        public boolean hasAssociatedCertificate() {
            return true;
        }

        public X509CertificateHolder getAssociatedCertificate() {
            return certHolder;
        }

        public ContentVerifier get(AlgorithmIdentifier algorithm) throws OperatorCreationException {
            try {
                AsymmetricKeyParameter publicKey = extractKeyParameters(certHolder.getSubjectPublicKeyInfo());
                BcSignerOutputStream stream = createSignatureStream(algorithm, publicKey);
                return new SigVerifier(algorithm, stream);
            } catch (IOException e) {
                throw new OperatorCreationException("exception on setup: " + e, e);
            }
        }
    };
}
Also used : AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) IOException(java.io.IOException) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) ContentVerifierProvider(com.github.zhenwei.pkix.operator.ContentVerifierProvider) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Aggregations

AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 ContentVerifierProvider (com.github.zhenwei.pkix.operator.ContentVerifierProvider)3 OperatorCreationException (com.github.zhenwei.pkix.operator.OperatorCreationException)3 Signature (java.security.Signature)2 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)1 X509CertificateHolder (com.github.zhenwei.pkix.cert.X509CertificateHolder)1 JcaX509CertificateHolder (com.github.zhenwei.pkix.cert.jcajce.JcaX509CertificateHolder)1 CompositePublicKey (com.github.zhenwei.provider.jcajce.CompositePublicKey)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 PublicKey (java.security.PublicKey)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1