Search in sources :

Example 1 with RuntimeOperatorException

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

the class JcaEACSignatureVerifierBuilder method build.

public EACSignatureVerifier build(final ASN1ObjectIdentifier usageOid, PublicKey pubKey) throws OperatorCreationException {
    Signature sig;
    try {
        sig = helper.getSignature(usageOid);
        sig.initVerify(pubKey);
    } catch (NoSuchAlgorithmException e) {
        throw new OperatorCreationException("unable to find algorithm: " + e.getMessage(), e);
    } catch (NoSuchProviderException e) {
        throw new OperatorCreationException("unable to find provider: " + e.getMessage(), e);
    } catch (InvalidKeyException e) {
        throw new OperatorCreationException("invalid key: " + e.getMessage(), e);
    }
    final SignatureOutputStream sigStream = new SignatureOutputStream(sig);
    return new EACSignatureVerifier() {

        public ASN1ObjectIdentifier getUsageIdentifier() {
            return usageOid;
        }

        public OutputStream getOutputStream() {
            return sigStream;
        }

        public boolean verify(byte[] expected) {
            try {
                if (usageOid.on(EACObjectIdentifiers.id_TA_ECDSA)) {
                    try {
                        byte[] reencoded = derEncode(expected);
                        return sigStream.verify(reencoded);
                    } catch (Exception e) {
                        return false;
                    }
                } else {
                    return sigStream.verify(expected);
                }
            } catch (SignatureException e) {
                throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e);
            }
        }
    };
}
Also used : RuntimeOperatorException(com.github.zhenwei.pkix.operator.RuntimeOperatorException) EACSignatureVerifier(com.github.zhenwei.pkix.eac.operator.EACSignatureVerifier) Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeyException(java.security.InvalidKeyException) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) OperatorStreamException(com.github.zhenwei.pkix.operator.OperatorStreamException) RuntimeOperatorException(com.github.zhenwei.pkix.operator.RuntimeOperatorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 2 with RuntimeOperatorException

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

the class PKMACBuilder method genCalculator.

private MacCalculator genCalculator(final PBMParameter params, char[] password) throws CRMFException {
    // From RFC 4211
    // 
    // 1.  Generate a random salt value S
    // 
    // 2.  Append the salt to the pw.  K = pw || salt.
    // 
    // 3.  Hash the value of K.  K = HASH(K)
    // 
    // 4.  Iter = Iter - 1.  If Iter is greater than zero.  Goto step 3.
    // 
    // 5.  Compute an HMAC as documented in [HMAC].
    // 
    // MAC = HASH( K XOR opad, HASH( K XOR ipad, data) )
    // 
    // Where opad and ipad are defined in [HMAC].
    byte[] pw = Strings.toUTF8ByteArray(password);
    byte[] salt = params.getSalt().getOctets();
    byte[] K = new byte[pw.length + salt.length];
    System.arraycopy(pw, 0, K, 0, pw.length);
    System.arraycopy(salt, 0, K, pw.length, salt.length);
    calculator.setup(params.getOwf(), params.getMac());
    int iter = params.getIterationCount().intValueExact();
    do {
        K = calculator.calculateDigest(K);
    } while (--iter > 0);
    final byte[] key = K;
    return new MacCalculator() {

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return new AlgorithmIdentifier(CMPObjectIdentifiers.passwordBasedMac, params);
        }

        public GenericKey getKey() {
            return new GenericKey(getAlgorithmIdentifier(), key);
        }

        public OutputStream getOutputStream() {
            return bOut;
        }

        public byte[] getMac() {
            try {
                return calculator.calculateMac(key, bOut.toByteArray());
            } catch (CRMFException e) {
                throw new RuntimeOperatorException("exception calculating mac: " + e.getMessage(), e);
            }
        }
    };
}
Also used : RuntimeOperatorException(com.github.zhenwei.pkix.operator.RuntimeOperatorException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) MacCalculator(com.github.zhenwei.pkix.operator.MacCalculator) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 3 with RuntimeOperatorException

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

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

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

the class JcaEACSignerBuilder method build.

public EACSigner build(final ASN1ObjectIdentifier usageOid, PrivateKey privKey) throws OperatorCreationException {
    Signature sig;
    try {
        sig = helper.getSignature(usageOid);
        sig.initSign(privKey);
    } catch (NoSuchAlgorithmException e) {
        throw new OperatorCreationException("unable to find algorithm: " + e.getMessage(), e);
    } catch (NoSuchProviderException e) {
        throw new OperatorCreationException("unable to find provider: " + e.getMessage(), e);
    } catch (InvalidKeyException e) {
        throw new OperatorCreationException("invalid key: " + e.getMessage(), e);
    }
    final SignatureOutputStream sigStream = new SignatureOutputStream(sig);
    return new EACSigner() {

        public ASN1ObjectIdentifier getUsageIdentifier() {
            return usageOid;
        }

        public OutputStream getOutputStream() {
            return sigStream;
        }

        public byte[] getSignature() {
            try {
                byte[] signature = sigStream.getSignature();
                if (usageOid.on(EACObjectIdentifiers.id_TA_ECDSA)) {
                    return reencode(signature);
                }
                return signature;
            } catch (SignatureException e) {
                throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e);
            }
        }
    };
}
Also used : RuntimeOperatorException(com.github.zhenwei.pkix.operator.RuntimeOperatorException) Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EACSigner(com.github.zhenwei.pkix.eac.operator.EACSigner) SignatureException(java.security.SignatureException) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

RuntimeOperatorException (com.github.zhenwei.pkix.operator.RuntimeOperatorException)5 OperatorCreationException (com.github.zhenwei.pkix.operator.OperatorCreationException)4 Signature (java.security.Signature)4 SignatureException (java.security.SignatureException)4 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 TeeOutputStream (com.github.zhenwei.core.util.io.TeeOutputStream)2 ContentSigner (com.github.zhenwei.pkix.operator.ContentSigner)2 CompositePrivateKey (com.github.zhenwei.provider.jcajce.CompositePrivateKey)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 GeneralSecurityException (java.security.GeneralSecurityException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 NoSuchProviderException (java.security.NoSuchProviderException)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 EACSignatureVerifier (com.github.zhenwei.pkix.eac.operator.EACSignatureVerifier)1 EACSigner (com.github.zhenwei.pkix.eac.operator.EACSigner)1