Search in sources :

Example 6 with P11TokenException

use of org.xipki.security.pkcs11.P11TokenException in project xipki by xipki.

the class IaikP11Slot method generateKeyPair.

private P11Identity generateKeyPair(long mech, PrivateKey privateKey, PublicKey publicKey) throws P11TokenException {
    final String label = toString(privateKey.getLabel());
    byte[] id = null;
    try {
        KeyPair keypair;
        Session session = borrowWritableSession();
        try {
            if (labelExists(session, label)) {
                throw new IllegalArgumentException("label " + label + " exists, please specify another one");
            }
            id = generateKeyId(session);
            privateKey.getId().setByteArrayValue(id);
            publicKey.getId().setByteArrayValue(id);
            try {
                keypair = session.generateKeyPair(Mechanism.get(mech), publicKey, privateKey);
            } catch (TokenException ex) {
                throw new P11TokenException("could not generate keypair " + Pkcs11Functions.mechanismCodeToString(mech), ex);
            }
            P11ObjectIdentifier objId = new P11ObjectIdentifier(id, label);
            P11EntityIdentifier entityId = new P11EntityIdentifier(slotId, objId);
            java.security.PublicKey jcePublicKey;
            try {
                jcePublicKey = generatePublicKey(keypair.getPublicKey());
            } catch (XiSecurityException ex) {
                throw new P11TokenException("could not generate public key " + objId, ex);
            }
            PrivateKey privateKey2 = getPrivateKeyObject(session, id, label.toCharArray());
            if (privateKey2 == null) {
                throw new P11TokenException("could not read the generated private key");
            }
            return new IaikP11Identity(this, entityId, privateKey2, jcePublicKey, null);
        } finally {
            returnWritableSession(session);
        }
    } catch (P11TokenException | RuntimeException ex) {
        try {
            removeObjects(id, label);
        } catch (Throwable th) {
            LogUtil.error(LOG, th, "could not remove objects");
        }
        throw ex;
    }
}
Also used : KeyPair(iaik.pkcs.pkcs11.objects.KeyPair) RSAPrivateKey(iaik.pkcs.pkcs11.objects.RSAPrivateKey) ECPrivateKey(iaik.pkcs.pkcs11.objects.ECPrivateKey) SM2PrivateKey(iaik.pkcs.pkcs11.objects.SM2PrivateKey) PrivateKey(iaik.pkcs.pkcs11.objects.PrivateKey) DSAPrivateKey(iaik.pkcs.pkcs11.objects.DSAPrivateKey) P11TokenException(org.xipki.security.exception.P11TokenException) P11EntityIdentifier(org.xipki.security.pkcs11.P11EntityIdentifier) DEROctetString(org.bouncycastle.asn1.DEROctetString) XiSecurityException(org.xipki.security.exception.XiSecurityException) P11TokenException(org.xipki.security.exception.P11TokenException) TokenException(iaik.pkcs.pkcs11.TokenException) P11ObjectIdentifier(org.xipki.security.pkcs11.P11ObjectIdentifier) Session(iaik.pkcs.pkcs11.Session)

Example 7 with P11TokenException

use of org.xipki.security.pkcs11.P11TokenException in project xipki by xipki.

the class IaikP11Slot method importSecretKey0.

@Override
protected P11Identity importSecretKey0(long keyType, byte[] keyValue, String label, P11NewKeyControl control) throws P11TokenException {
    ValuedSecretKey template = new ValuedSecretKey(keyType);
    template.getToken().setBooleanValue(true);
    template.getLabel().setCharArrayValue(label.toCharArray());
    template.getSign().setBooleanValue(true);
    template.getSensitive().setBooleanValue(true);
    template.getExtractable().setBooleanValue(control.isExtractable());
    template.getValue().setByteArrayValue(keyValue);
    SecretKey key;
    Session session = borrowWritableSession();
    try {
        if (labelExists(session, label)) {
            throw new IllegalArgumentException("label " + label + " exists, please specify another one");
        }
        byte[] id = generateKeyId(session);
        template.getId().setByteArrayValue(id);
        try {
            key = (SecretKey) session.createObject(template);
        } catch (TokenException ex) {
            throw new P11TokenException("could not create secret key", ex);
        }
        P11ObjectIdentifier objId = new P11ObjectIdentifier(id, label);
        P11EntityIdentifier entityId = new P11EntityIdentifier(slotId, objId);
        return new IaikP11Identity(this, entityId, key);
    } finally {
        returnWritableSession(session);
    }
}
Also used : ValuedSecretKey(iaik.pkcs.pkcs11.objects.ValuedSecretKey) ValuedSecretKey(iaik.pkcs.pkcs11.objects.ValuedSecretKey) SecretKey(iaik.pkcs.pkcs11.objects.SecretKey) P11TokenException(org.xipki.security.exception.P11TokenException) TokenException(iaik.pkcs.pkcs11.TokenException) P11TokenException(org.xipki.security.exception.P11TokenException) P11EntityIdentifier(org.xipki.security.pkcs11.P11EntityIdentifier) P11ObjectIdentifier(org.xipki.security.pkcs11.P11ObjectIdentifier) Session(iaik.pkcs.pkcs11.Session)

Example 8 with P11TokenException

use of org.xipki.security.pkcs11.P11TokenException in project xipki by xipki.

the class ProxyP11Slot method parseGenerateKeypairResult.

private P11Identity parseGenerateKeypairResult(byte[] resp) throws P11TokenException {
    if (resp == null) {
        throw new P11TokenException("server returned no result");
    }
    Asn1P11EntityIdentifier ei;
    try {
        ei = Asn1P11EntityIdentifier.getInstance(resp);
    } catch (BadAsn1ObjectException ex) {
        throw new P11TokenException("invalid ASN1 object Asn1P11EntityIdentifier: " + ex.getMessage(), ex);
    }
    if (!slotId.equals(ei.getSlotId().getSlotId())) {
        throw new P11TokenException("");
    }
    P11EntityIdentifier entityId = ei.getEntityId();
    PublicKey publicKey = getPublicKey(entityId.getObjectId());
    return new ProxyP11Identity(this, entityId, publicKey, null);
}
Also used : Asn1P11EntityIdentifier(org.xipki.p11proxy.msg.Asn1P11EntityIdentifier) PublicKey(java.security.PublicKey) P11TokenException(org.xipki.security.exception.P11TokenException) Asn1P11EntityIdentifier(org.xipki.p11proxy.msg.Asn1P11EntityIdentifier) P11EntityIdentifier(org.xipki.security.pkcs11.P11EntityIdentifier) BadAsn1ObjectException(org.xipki.security.exception.BadAsn1ObjectException)

Example 9 with P11TokenException

use of org.xipki.security.pkcs11.P11TokenException in project xipki by xipki.

the class ProxyP11Slot method parseGenerateSecretKeyResult.

private P11Identity parseGenerateSecretKeyResult(byte[] resp) throws P11TokenException {
    if (resp == null) {
        throw new P11TokenException("server returned no result");
    }
    Asn1P11EntityIdentifier ei;
    try {
        ei = Asn1P11EntityIdentifier.getInstance(resp);
    } catch (BadAsn1ObjectException ex) {
        throw new P11TokenException("invalid ASN1 object Asn1P11EntityIdentifier: " + ex.getMessage(), ex);
    }
    if (!slotId.equals(ei.getSlotId().getSlotId())) {
        throw new P11TokenException("");
    }
    P11EntityIdentifier entityId = ei.getEntityId();
    return new ProxyP11Identity(this, entityId);
}
Also used : Asn1P11EntityIdentifier(org.xipki.p11proxy.msg.Asn1P11EntityIdentifier) P11TokenException(org.xipki.security.exception.P11TokenException) Asn1P11EntityIdentifier(org.xipki.p11proxy.msg.Asn1P11EntityIdentifier) P11EntityIdentifier(org.xipki.security.pkcs11.P11EntityIdentifier) BadAsn1ObjectException(org.xipki.security.exception.BadAsn1ObjectException)

Example 10 with P11TokenException

use of org.xipki.security.pkcs11.P11TokenException in project xipki by xipki.

the class ProxyP11Slot method updateCertificate0.

@Override
protected void updateCertificate0(P11ObjectIdentifier objectId, X509Certificate newCert) throws P11TokenException, CertificateException {
    Asn1EntityIdAndCert asn1 = new Asn1EntityIdAndCert(new P11EntityIdentifier(slotId, objectId), newCert);
    module.send(P11ProxyConstants.ACTION_UPDATE_CERT, asn1);
}
Also used : Asn1EntityIdAndCert(org.xipki.p11proxy.msg.Asn1EntityIdAndCert) Asn1P11EntityIdentifier(org.xipki.p11proxy.msg.Asn1P11EntityIdentifier) P11EntityIdentifier(org.xipki.security.pkcs11.P11EntityIdentifier)

Aggregations

P11TokenException (org.xipki.security.exception.P11TokenException)15 P11EntityIdentifier (org.xipki.security.pkcs11.P11EntityIdentifier)11 P11TokenException (org.xipki.security.pkcs11.P11TokenException)11 P11ObjectIdentifier (org.xipki.security.pkcs11.P11ObjectIdentifier)9 XiSecurityException (org.xipki.security.exception.XiSecurityException)8 P11CryptService (org.xipki.security.pkcs11.P11CryptService)7 P11Module (org.xipki.security.pkcs11.P11Module)6 P11SlotIdentifier (org.xipki.security.pkcs11.P11SlotIdentifier)6 TokenException (iaik.pkcs.pkcs11.TokenException)4 PublicKey (java.security.PublicKey)4 DEROctetString (org.bouncycastle.asn1.DEROctetString)4 Asn1P11EntityIdentifier (org.xipki.p11proxy.msg.Asn1P11EntityIdentifier)4 P11Params (org.xipki.security.pkcs11.P11Params)4 P11Slot (org.xipki.security.pkcs11.P11Slot)4 Mechanism (iaik.pkcs.pkcs11.Mechanism)3 Session (iaik.pkcs.pkcs11.Session)3 PKCS11Exception (iaik.pkcs.pkcs11.wrapper.PKCS11Exception)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 BadAsn1ObjectException (org.xipki.security.exception.BadAsn1ObjectException)3