Search in sources :

Example 6 with PublicKey

use of iaik.pkcs.pkcs11.objects.PublicKey in project xipki by xipki.

the class IaikP11Slot method generatePublicKey.

// method getObjects
private static java.security.PublicKey generatePublicKey(PublicKey p11Key) throws XiSecurityException {
    if (p11Key instanceof RSAPublicKey) {
        RSAPublicKey rsaP11Key = (RSAPublicKey) p11Key;
        byte[] expBytes = rsaP11Key.getPublicExponent().getByteArrayValue();
        BigInteger exp = new BigInteger(1, expBytes);
        byte[] modBytes = rsaP11Key.getModulus().getByteArrayValue();
        BigInteger mod = new BigInteger(1, modBytes);
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(mod, exp);
        try {
            return KeyUtil.generateRSAPublicKey(keySpec);
        } catch (InvalidKeySpecException ex) {
            throw new XiSecurityException(ex.getMessage(), ex);
        }
    } else if (p11Key instanceof DSAPublicKey) {
        DSAPublicKey dsaP11Key = (DSAPublicKey) p11Key;
        // p
        BigInteger prime = new BigInteger(1, dsaP11Key.getPrime().getByteArrayValue());
        BigInteger subPrime = new BigInteger(1, // q
        dsaP11Key.getSubprime().getByteArrayValue());
        // g
        BigInteger base = new BigInteger(1, dsaP11Key.getBase().getByteArrayValue());
        // y
        BigInteger value = new BigInteger(1, dsaP11Key.getValue().getByteArrayValue());
        DSAPublicKeySpec keySpec = new DSAPublicKeySpec(value, prime, subPrime, base);
        try {
            return KeyUtil.generateDSAPublicKey(keySpec);
        } catch (InvalidKeySpecException ex) {
            throw new XiSecurityException(ex.getMessage(), ex);
        }
    } else if (p11Key instanceof ECPublicKey) {
        ECPublicKey ecP11Key = (ECPublicKey) p11Key;
        byte[] encodedAlgorithmIdParameters = ecP11Key.getEcdsaParams().getByteArrayValue();
        byte[] encodedPoint = DEROctetString.getInstance(ecP11Key.getEcPoint().getByteArrayValue()).getOctets();
        try {
            return KeyUtil.createECPublicKey(encodedAlgorithmIdParameters, encodedPoint);
        } catch (InvalidKeySpecException ex) {
            throw new XiSecurityException(ex.getMessage(), ex);
        }
    } else {
        throw new XiSecurityException("unknown publicKey class " + p11Key.getClass().getName());
    }
}
Also used : XiSecurityException(org.xipki.security.exception.XiSecurityException) RSAPublicKey(iaik.pkcs.pkcs11.objects.RSAPublicKey) ECPublicKey(iaik.pkcs.pkcs11.objects.ECPublicKey) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DSAPublicKey(iaik.pkcs.pkcs11.objects.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 7 with PublicKey

use of iaik.pkcs.pkcs11.objects.PublicKey in project xipki by xipki.

the class IaikP11Slot method generateDSAKeypair0.

@Override
protected // CHECKSTYLE:SKIP
P11Identity generateDSAKeypair0(BigInteger p, BigInteger q, BigInteger g, String label, P11NewKeyControl control) throws P11TokenException {
    long mech = PKCS11Constants.CKM_DSA_KEY_PAIR_GEN;
    assertMechanismSupported(mech);
    DSAPrivateKey privateKey = new DSAPrivateKey();
    DSAPublicKey publicKey = new DSAPublicKey();
    setKeyAttributes(label, PKCS11Constants.CKK_DSA, control, publicKey, privateKey);
    publicKey.getPrime().setByteArrayValue(p.toByteArray());
    publicKey.getSubprime().setByteArrayValue(q.toByteArray());
    publicKey.getBase().setByteArrayValue(g.toByteArray());
    return generateKeyPair(mech, privateKey, publicKey);
}
Also used : DSAPrivateKey(iaik.pkcs.pkcs11.objects.DSAPrivateKey) DSAPublicKey(iaik.pkcs.pkcs11.objects.DSAPublicKey)

Example 8 with PublicKey

use of iaik.pkcs.pkcs11.objects.PublicKey in project xipki by xipki.

the class IaikP11Slot method generateECKeypair0.

@Override
protected P11Identity generateECKeypair0(ASN1ObjectIdentifier curveId, String label, P11NewKeyControl control) throws P11TokenException {
    long mech = PKCS11Constants.CKM_EC_KEY_PAIR_GEN;
    assertMechanismSupported(mech);
    ECPrivateKey privateKey = new ECPrivateKey();
    ECPublicKey publicKey = new ECPublicKey();
    setKeyAttributes(label, PKCS11Constants.CKK_EC, control, publicKey, privateKey);
    byte[] encodedCurveId;
    try {
        encodedCurveId = curveId.getEncoded();
    } catch (IOException ex) {
        throw new P11TokenException(ex.getMessage(), ex);
    }
    try {
        publicKey.getEcdsaParams().setByteArrayValue(encodedCurveId);
        return generateKeyPair(mech, privateKey, publicKey);
    } catch (P11TokenException ex) {
        X9ECParameters ecParams = ECNamedCurveTable.getByOID(curveId);
        if (ecParams == null) {
            throw new IllegalArgumentException("could not get X9ECParameters for curve " + curveId.getId());
        }
        try {
            publicKey.getEcdsaParams().setByteArrayValue(ecParams.getEncoded());
        } catch (IOException ex2) {
            throw new P11TokenException(ex.getMessage(), ex);
        }
        return generateKeyPair(mech, privateKey, publicKey);
    }
}
Also used : ECPrivateKey(iaik.pkcs.pkcs11.objects.ECPrivateKey) ECPublicKey(iaik.pkcs.pkcs11.objects.ECPublicKey) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) P11TokenException(org.xipki.security.exception.P11TokenException) IOException(java.io.IOException)

Aggregations

DSAPublicKey (iaik.pkcs.pkcs11.objects.DSAPublicKey)4 ECPublicKey (iaik.pkcs.pkcs11.objects.ECPublicKey)4 RSAPublicKey (iaik.pkcs.pkcs11.objects.RSAPublicKey)4 DSAPrivateKey (iaik.pkcs.pkcs11.objects.DSAPrivateKey)3 ECPrivateKey (iaik.pkcs.pkcs11.objects.ECPrivateKey)3 RSAPrivateKey (iaik.pkcs.pkcs11.objects.RSAPrivateKey)3 SM2PrivateKey (iaik.pkcs.pkcs11.objects.SM2PrivateKey)3 SM2PublicKey (iaik.pkcs.pkcs11.objects.SM2PublicKey)3 P11TokenException (org.xipki.security.exception.P11TokenException)3 Session (iaik.pkcs.pkcs11.Session)2 TokenException (iaik.pkcs.pkcs11.TokenException)2 PrivateKey (iaik.pkcs.pkcs11.objects.PrivateKey)2 PublicKey (iaik.pkcs.pkcs11.objects.PublicKey)2 DEROctetString (org.bouncycastle.asn1.DEROctetString)2 XiSecurityException (org.xipki.security.exception.XiSecurityException)2 P11EntityIdentifier (org.xipki.security.pkcs11.P11EntityIdentifier)2 P11ObjectIdentifier (org.xipki.security.pkcs11.P11ObjectIdentifier)2 KeyPair (iaik.pkcs.pkcs11.objects.KeyPair)1 SecretKey (iaik.pkcs.pkcs11.objects.SecretKey)1 ValuedSecretKey (iaik.pkcs.pkcs11.objects.ValuedSecretKey)1