Search in sources :

Example 6 with NoIdleSignerException

use of org.xipki.security.exception.NoIdleSignerException in project xipki by xipki.

the class CsrGenAction method generateRequest.

private PKCS10CertificationRequest generateRequest(ConcurrentContentSigner signer, SubjectPublicKeyInfo subjectPublicKeyInfo, X500Name subjectDn, Map<ASN1ObjectIdentifier, ASN1Encodable> attributes) throws XiSecurityException {
    ParamUtil.requireNonNull("signer", signer);
    ParamUtil.requireNonNull("subjectPublicKeyInfo", subjectPublicKeyInfo);
    ParamUtil.requireNonNull("subjectDn", subjectDn);
    PKCS10CertificationRequestBuilder csrBuilder = new PKCS10CertificationRequestBuilder(subjectDn, subjectPublicKeyInfo);
    if (CollectionUtil.isNonEmpty(attributes)) {
        for (ASN1ObjectIdentifier attrType : attributes.keySet()) {
            csrBuilder.addAttribute(attrType, attributes.get(attrType));
        }
    }
    ConcurrentBagEntrySigner signer0;
    try {
        signer0 = signer.borrowSigner();
    } catch (NoIdleSignerException ex) {
        throw new XiSecurityException(ex.getMessage(), ex);
    }
    try {
        return csrBuilder.build(signer0.value());
    } finally {
        signer.requiteSigner(signer0);
    }
}
Also used : XiSecurityException(org.xipki.security.exception.XiSecurityException) NoIdleSignerException(org.xipki.security.exception.NoIdleSignerException) PKCS10CertificationRequestBuilder(org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder) ConcurrentBagEntrySigner(org.xipki.security.ConcurrentBagEntrySigner) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 7 with NoIdleSignerException

use of org.xipki.security.exception.NoIdleSignerException in project xipki by xipki.

the class SecurityFactoryImpl method validateSigner.

private static void validateSigner(ConcurrentContentSigner signer, String signerType, SignerConf signerConf) throws ObjectCreationException {
    if (signer.getPublicKey() == null) {
        return;
    }
    String signatureAlgoName = signer.getAlgorithmName();
    try {
        byte[] dummyContent = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        Signature verifier = Signature.getInstance(signatureAlgoName, "BC");
        byte[] signatureValue = signer.sign(dummyContent);
        verifier.initVerify(signer.getPublicKey());
        verifier.update(dummyContent);
        boolean valid = verifier.verify(signatureValue);
        if (!valid) {
            StringBuilder sb = new StringBuilder();
            sb.append("private key and public key does not match, ");
            sb.append("key type='").append(signerType).append("'; ");
            String pwd = signerConf.getConfValue("password");
            if (pwd != null) {
                signerConf.putConfEntry("password", "****");
            }
            signerConf.putConfEntry("algo", signatureAlgoName);
            sb.append("conf='").append(signerConf.getConf());
            X509Certificate cert = signer.getCertificate();
            if (cert != null) {
                String subject = X509Util.getRfc4519Name(cert.getSubjectX500Principal());
                sb.append("', certificate subject='").append(subject).append("'");
            }
            throw new ObjectCreationException(sb.toString());
        }
    } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException | NoSuchProviderException | NoIdleSignerException ex) {
        throw new ObjectCreationException(ex.getMessage(), ex);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) X509Certificate(java.security.cert.X509Certificate) ObjectCreationException(org.xipki.common.ObjectCreationException) Signature(java.security.Signature) NoIdleSignerException(org.xipki.security.exception.NoIdleSignerException) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

NoIdleSignerException (org.xipki.security.exception.NoIdleSignerException)7 X509Certificate (java.security.cert.X509Certificate)5 ConcurrentBagEntrySigner (org.xipki.security.ConcurrentBagEntrySigner)5 CertificateException (java.security.cert.CertificateException)4 BigInteger (java.math.BigInteger)3 Date (java.util.Date)3 LinkedList (java.util.LinkedList)3 Certificate (org.bouncycastle.asn1.x509.Certificate)3 Extensions (org.bouncycastle.asn1.x509.Extensions)3 IOException (java.io.IOException)2 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)2 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)2 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)2 DERPrintableString (org.bouncycastle.asn1.DERPrintableString)2 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)2 X500Name (org.bouncycastle.asn1.x500.X500Name)2 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)2 CRLDistPoint (org.bouncycastle.asn1.x509.CRLDistPoint)2 Extension (org.bouncycastle.asn1.x509.Extension)2 IssuingDistributionPoint (org.bouncycastle.asn1.x509.IssuingDistributionPoint)2