Search in sources :

Example 1 with RSAPublicKey

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

the class IaikP11Slot method generateRSAKeypair0.

@Override
protected P11Identity generateRSAKeypair0(int keysize, BigInteger publicExponent, String label, P11NewKeyControl control) throws P11TokenException {
    long mech = PKCS11Constants.CKM_RSA_PKCS_KEY_PAIR_GEN;
    assertMechanismSupported(mech);
    RSAPrivateKey privateKey = new RSAPrivateKey();
    RSAPublicKey publicKey = new RSAPublicKey();
    setKeyAttributes(label, PKCS11Constants.CKK_RSA, control, publicKey, privateKey);
    publicKey.getModulusBits().setLongValue((long) keysize);
    if (publicExponent != null) {
        publicKey.getPublicExponent().setByteArrayValue(publicExponent.toByteArray());
    }
    return generateKeyPair(mech, privateKey, publicKey);
}
Also used : RSAPublicKey(iaik.pkcs.pkcs11.objects.RSAPublicKey) RSAPrivateKey(iaik.pkcs.pkcs11.objects.RSAPrivateKey)

Example 2 with RSAPublicKey

use of iaik.pkcs.pkcs11.objects.RSAPublicKey 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)

Aggregations

RSAPublicKey (iaik.pkcs.pkcs11.objects.RSAPublicKey)2 DSAPublicKey (iaik.pkcs.pkcs11.objects.DSAPublicKey)1 ECPublicKey (iaik.pkcs.pkcs11.objects.ECPublicKey)1 RSAPrivateKey (iaik.pkcs.pkcs11.objects.RSAPrivateKey)1 BigInteger (java.math.BigInteger)1 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)1 XiSecurityException (org.xipki.security.exception.XiSecurityException)1