Search in sources :

Example 1 with ContentSigner

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

the class JcaSimpleSignerInfoGeneratorBuilder method build.

public SignerInfoGenerator build(String algorithmName, PrivateKey privateKey, X509Certificate certificate) throws OperatorCreationException, CertificateEncodingException {
    privateKey = CMSUtils.cleanPrivateKey(privateKey);
    ContentSigner contentSigner = helper.createContentSigner(algorithmName, privateKey);
    return configureAndBuild().build(contentSigner, new JcaX509CertificateHolder(certificate));
}
Also used : ContentSigner(com.github.zhenwei.pkix.operator.ContentSigner) JcaX509CertificateHolder(com.github.zhenwei.pkix.cert.jcajce.JcaX509CertificateHolder)

Example 2 with ContentSigner

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

the class JcaSimpleSignerInfoGeneratorBuilder method build.

public SignerInfoGenerator build(String algorithmName, PrivateKey privateKey, X509CertificateHolder certificate) throws OperatorCreationException {
    privateKey = CMSUtils.cleanPrivateKey(privateKey);
    ContentSigner contentSigner = helper.createContentSigner(algorithmName, privateKey);
    return configureAndBuild().build(contentSigner, certificate);
}
Also used : ContentSigner(com.github.zhenwei.pkix.operator.ContentSigner)

Example 3 with ContentSigner

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

the class JcaContentSignerBuilder method build.

public ContentSigner build(PrivateKey privateKey) throws OperatorCreationException {
    if (privateKey instanceof CompositePrivateKey) {
        return buildComposite((CompositePrivateKey) privateKey);
    }
    try {
        final Signature sig = helper.createSignature(sigAlgId);
        final AlgorithmIdentifier signatureAlgId = sigAlgId;
        if (random != null) {
            sig.initSign(privateKey, random);
        } else {
            sig.initSign(privateKey);
        }
        return new ContentSigner() {

            private OutputStream stream = OutputStreamFactory.createStream(sig);

            public AlgorithmIdentifier getAlgorithmIdentifier() {
                return signatureAlgId;
            }

            public OutputStream getOutputStream() {
                return stream;
            }

            public byte[] getSignature() {
                try {
                    return sig.sign();
                } catch (SignatureException e) {
                    throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e);
                }
            }
        };
    } catch (GeneralSecurityException e) {
        throw new OperatorCreationException("cannot create signer: " + e.getMessage(), e);
    }
}
Also used : RuntimeOperatorException(com.github.zhenwei.pkix.operator.RuntimeOperatorException) Signature(java.security.Signature) TeeOutputStream(com.github.zhenwei.core.util.io.TeeOutputStream) OutputStream(java.io.OutputStream) GeneralSecurityException(java.security.GeneralSecurityException) ContentSigner(com.github.zhenwei.pkix.operator.ContentSigner) CompositePrivateKey(com.github.zhenwei.provider.jcajce.CompositePrivateKey) SignatureException(java.security.SignatureException) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 4 with ContentSigner

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

the class JcaContentSignerBuilder method buildComposite.

private ContentSigner buildComposite(CompositePrivateKey privateKey) throws OperatorCreationException {
    try {
        List<PrivateKey> privateKeys = privateKey.getPrivateKeys();
        final ASN1Sequence sigAlgIds = ASN1Sequence.getInstance(sigAlgId.getParameters());
        final Signature[] sigs = new Signature[sigAlgIds.size()];
        for (int i = 0; i != sigAlgIds.size(); i++) {
            sigs[i] = helper.createSignature(AlgorithmIdentifier.getInstance(sigAlgIds.getObjectAt(i)));
            if (random != null) {
                sigs[i].initSign(privateKeys.get(i), random);
            } else {
                sigs[i].initSign(privateKeys.get(i));
            }
        }
        OutputStream sStream = OutputStreamFactory.createStream(sigs[0]);
        for (int i = 1; i != sigs.length; i++) {
            sStream = new TeeOutputStream(sStream, OutputStreamFactory.createStream(sigs[i]));
        }
        final OutputStream sigStream = sStream;
        return new ContentSigner() {

            OutputStream stream = sigStream;

            public AlgorithmIdentifier getAlgorithmIdentifier() {
                return sigAlgId;
            }

            public OutputStream getOutputStream() {
                return stream;
            }

            public byte[] getSignature() {
                try {
                    ASN1EncodableVector sigV = new ASN1EncodableVector();
                    for (int i = 0; i != sigs.length; i++) {
                        sigV.add(new DERBitString(sigs[i].sign()));
                    }
                    return new DERSequence(sigV).getEncoded(ASN1Encoding.DER);
                } catch (IOException e) {
                    throw new RuntimeOperatorException("exception encoding signature: " + e.getMessage(), e);
                } catch (SignatureException e) {
                    throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e);
                }
            }
        };
    } catch (GeneralSecurityException e) {
        throw new OperatorCreationException("cannot create signer: " + e.getMessage(), e);
    }
}
Also used : TeeOutputStream(com.github.zhenwei.core.util.io.TeeOutputStream) PrivateKey(java.security.PrivateKey) CompositePrivateKey(com.github.zhenwei.provider.jcajce.CompositePrivateKey) TeeOutputStream(com.github.zhenwei.core.util.io.TeeOutputStream) OutputStream(java.io.OutputStream) GeneralSecurityException(java.security.GeneralSecurityException) ContentSigner(com.github.zhenwei.pkix.operator.ContentSigner) DERBitString(com.github.zhenwei.core.asn1.DERBitString) IOException(java.io.IOException) SignatureException(java.security.SignatureException) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) DERSequence(com.github.zhenwei.core.asn1.DERSequence) RuntimeOperatorException(com.github.zhenwei.pkix.operator.RuntimeOperatorException) Signature(java.security.Signature) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException)

Example 5 with ContentSigner

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

the class JcaSimpleSignerInfoGeneratorBuilder method build.

public SignerInfoGenerator build(String algorithmName, PrivateKey privateKey, byte[] keyIdentifier) throws OperatorCreationException {
    privateKey = CMSUtils.cleanPrivateKey(privateKey);
    ContentSigner contentSigner = helper.createContentSigner(algorithmName, privateKey);
    return configureAndBuild().build(contentSigner, keyIdentifier);
}
Also used : ContentSigner(com.github.zhenwei.pkix.operator.ContentSigner)

Aggregations

ContentSigner (com.github.zhenwei.pkix.operator.ContentSigner)5 TeeOutputStream (com.github.zhenwei.core.util.io.TeeOutputStream)2 OperatorCreationException (com.github.zhenwei.pkix.operator.OperatorCreationException)2 RuntimeOperatorException (com.github.zhenwei.pkix.operator.RuntimeOperatorException)2 CompositePrivateKey (com.github.zhenwei.provider.jcajce.CompositePrivateKey)2 OutputStream (java.io.OutputStream)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Signature (java.security.Signature)2 SignatureException (java.security.SignatureException)2 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)1 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)1 DERBitString (com.github.zhenwei.core.asn1.DERBitString)1 DERSequence (com.github.zhenwei.core.asn1.DERSequence)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 JcaX509CertificateHolder (com.github.zhenwei.pkix.cert.jcajce.JcaX509CertificateHolder)1 IOException (java.io.IOException)1 PrivateKey (java.security.PrivateKey)1