Search in sources :

Example 26 with P11TokenException

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

the class EmulatorP11Identity method rsaPkcsSign.

private byte[] rsaPkcsSign(byte[] contentToSign, HashAlgo hashAlgo) throws P11TokenException {
    int modulusBitLen = getSignatureKeyBitLength();
    byte[] paddedHash;
    try {
        if (hashAlgo == null) {
            paddedHash = SignerUtil.EMSA_PKCS1_v1_5_encoding(contentToSign, modulusBitLen);
        } else {
            byte[] hash = hashAlgo.hash(contentToSign);
            paddedHash = SignerUtil.EMSA_PKCS1_v1_5_encoding(hash, modulusBitLen, hashAlgo);
        }
    } catch (XiSecurityException ex) {
        throw new P11TokenException("XiSecurityException: " + ex.getMessage(), ex);
    }
    return rsaX509Sign(paddedHash);
}
Also used : XiSecurityException(org.xipki.security.exception.XiSecurityException) P11TokenException(org.xipki.security.exception.P11TokenException)

Example 27 with P11TokenException

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

the class ProxyP11Slot method getPublicKey.

private PublicKey getPublicKey(P11ObjectIdentifier objectId) throws P11UnknownEntityException, P11TokenException {
    P11EntityIdentifier entityId = new P11EntityIdentifier(slotId, objectId);
    byte[] resp = module.send(P11ProxyConstants.ACTION_GET_PUBLICKEY, new Asn1P11EntityIdentifier(entityId));
    if (resp == null) {
        return null;
    }
    SubjectPublicKeyInfo pkInfo = SubjectPublicKeyInfo.getInstance(resp);
    try {
        return KeyUtil.generatePublicKey(pkInfo);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
        throw new P11TokenException("could not generate Public Key from SubjectPublicKeyInfo:" + ex.getMessage(), ex);
    }
}
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) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo)

Example 28 with P11TokenException

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

the class ProxyP11Slot method getObjectIdsFromServer.

private List<P11ObjectIdentifier> getObjectIdsFromServer(short action) throws P11TokenException {
    Asn1P11SlotIdentifier asn1SlotId = new Asn1P11SlotIdentifier(slotId);
    byte[] resp = module.send(action, asn1SlotId);
    List<Asn1P11ObjectIdentifier> asn1ObjectIds;
    try {
        asn1ObjectIds = Asn1P11ObjectIdentifiers.getInstance(resp).getObjectIds();
    } catch (BadAsn1ObjectException ex) {
        throw new P11TokenException("bad ASN1 object: " + ex.getMessage(), ex);
    }
    List<P11ObjectIdentifier> objectIds = new ArrayList<>(asn1ObjectIds.size());
    for (Asn1P11ObjectIdentifier asn1Id : asn1ObjectIds) {
        objectIds.add(asn1Id.getObjectId());
    }
    return objectIds;
}
Also used : Asn1P11ObjectIdentifier(org.xipki.p11proxy.msg.Asn1P11ObjectIdentifier) P11TokenException(org.xipki.security.exception.P11TokenException) ArrayList(java.util.ArrayList) P11ObjectIdentifier(org.xipki.security.pkcs11.P11ObjectIdentifier) Asn1P11ObjectIdentifier(org.xipki.p11proxy.msg.Asn1P11ObjectIdentifier) Asn1P11SlotIdentifier(org.xipki.p11proxy.msg.Asn1P11SlotIdentifier) BadAsn1ObjectException(org.xipki.security.exception.BadAsn1ObjectException)

Example 29 with P11TokenException

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

the class EmulatorP11Slot method savePkcs11Entry.

private static void savePkcs11Entry(File dir, byte[] id, String label, byte[] value) throws P11TokenException {
    ParamUtil.requireNonNull("dir", dir);
    ParamUtil.requireNonNull("id", id);
    ParamUtil.requireNonBlank("label", label);
    ParamUtil.requireNonNull("value", value);
    String hexId = hex(id);
    String str = StringUtil.concat(PROP_ID, "=", hexId, "\n", PROP_LABEL, "=", label, "\n", PROP_SHA1SUM, "=", HashAlgo.SHA1.hexHash(value), "\n");
    try {
        IoUtil.save(new File(dir, hexId + INFO_FILE_SUFFIX), str.getBytes());
        IoUtil.save(new File(dir, hexId + VALUE_FILE_SUFFIX), value);
    } catch (IOException ex) {
        throw new P11TokenException("could not save certificate");
    }
}
Also used : P11TokenException(org.xipki.security.exception.P11TokenException) DEROctetString(org.bouncycastle.asn1.DEROctetString) IOException(java.io.IOException) File(java.io.File)

Example 30 with P11TokenException

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

the class EmulatorP11Slot method savePkcs11PrivateKey.

private void savePkcs11PrivateKey(byte[] id, String label, PrivateKey privateKey) throws P11TokenException {
    PKCS8EncryptedPrivateKeyInfo encryptedPrivKeyInfo = privateKeyCryptor.encrypt(privateKey);
    byte[] encoded;
    try {
        encoded = encryptedPrivKeyInfo.getEncoded();
    } catch (IOException ex) {
        LogUtil.error(LOG, ex);
        throw new P11TokenException("could not encode PrivateKey");
    }
    savePkcs11Entry(privKeyDir, id, label, encoded);
}
Also used : P11TokenException(org.xipki.security.exception.P11TokenException) PKCS8EncryptedPrivateKeyInfo(org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo) IOException(java.io.IOException)

Aggregations

P11TokenException (org.xipki.security.exception.P11TokenException)57 TokenException (iaik.pkcs.pkcs11.TokenException)16 XiSecurityException (org.xipki.security.exception.XiSecurityException)16 IOException (java.io.IOException)11 Session (iaik.pkcs.pkcs11.Session)10 P11EntityIdentifier (org.xipki.security.pkcs11.P11EntityIdentifier)10 ECPrivateKey (iaik.pkcs.pkcs11.objects.ECPrivateKey)9 SecretKey (iaik.pkcs.pkcs11.objects.SecretKey)9 ValuedSecretKey (iaik.pkcs.pkcs11.objects.ValuedSecretKey)9 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)9 DSAPrivateKey (iaik.pkcs.pkcs11.objects.DSAPrivateKey)8 PrivateKey (iaik.pkcs.pkcs11.objects.PrivateKey)8 RSAPrivateKey (iaik.pkcs.pkcs11.objects.RSAPrivateKey)8 SM2PrivateKey (iaik.pkcs.pkcs11.objects.SM2PrivateKey)8 DEROctetString (org.bouncycastle.asn1.DEROctetString)8 P11ObjectIdentifier (org.xipki.security.pkcs11.P11ObjectIdentifier)8 ECPublicKey (iaik.pkcs.pkcs11.objects.ECPublicKey)7 DSAPublicKey (iaik.pkcs.pkcs11.objects.DSAPublicKey)6 PublicKey (iaik.pkcs.pkcs11.objects.PublicKey)6 RSAPublicKey (iaik.pkcs.pkcs11.objects.RSAPublicKey)6