use of java.security.spec.DSAPublicKeySpec in project j2objc by google.
the class BasicChecker method makeInheritedParamsKey.
/**
* Internal method to create a new key with inherited key parameters.
*
* @param keyValueKey key from which to obtain key value
* @param keyParamsKey key from which to obtain key parameters
* @return new public key having value and parameters
* @throws CertPathValidatorException if keys are not appropriate types
* for this operation
*/
static PublicKey makeInheritedParamsKey(PublicKey keyValueKey, PublicKey keyParamsKey) throws CertPathValidatorException {
if (!(keyValueKey instanceof DSAPublicKey) || !(keyParamsKey instanceof DSAPublicKey))
throw new CertPathValidatorException("Input key is not " + "appropriate type for " + "inheriting parameters");
DSAParams params = ((DSAPublicKey) keyParamsKey).getParams();
if (params == null)
throw new CertPathValidatorException("Key parameters missing");
try {
BigInteger y = ((DSAPublicKey) keyValueKey).getY();
KeyFactory kf = KeyFactory.getInstance("DSA");
DSAPublicKeySpec ks = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG());
return kf.generatePublic(ks);
} catch (GeneralSecurityException e) {
throw new CertPathValidatorException("Unable to generate key with" + " inherited parameters: " + e.getMessage(), e);
}
}
use of java.security.spec.DSAPublicKeySpec in project j2objc by google.
the class DSAPublicKeySpecTest method testDSAPublicKeySpec.
/**
* Test for <code>DSAPublicKeySpec</code> ctor
*/
public final void testDSAPublicKeySpec() {
KeySpec ks = new DSAPublicKeySpec(// y
new BigInteger("1"), // p
new BigInteger("2"), // q
new BigInteger("3"), // g
new BigInteger("4"));
assertTrue(ks instanceof DSAPublicKeySpec);
}
use of java.security.spec.DSAPublicKeySpec in project j2objc by google.
the class DSAPublicKeySpecTest method testGetQ.
/**
* Test for <code>getQ</code> method
*/
public final void testGetQ() {
DSAPublicKeySpec dpks = new DSAPublicKeySpec(// y
new BigInteger("1"), // p
new BigInteger("2"), // q
new BigInteger("3"), // g
new BigInteger("4"));
assertEquals(3, dpks.getQ().intValue());
}
use of java.security.spec.DSAPublicKeySpec in project karaf by apache.
the class PublickeyLoginModule method equals.
public static boolean equals(PublicKey key, String storedKey) throws FailedLoginException {
try {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(storedKey)));
String identifier = readString(dis);
if (key instanceof DSAPublicKey) {
if (!"ssh-dss".equals(identifier)) {
return false;
}
BigInteger p = readBigInteger(dis);
BigInteger q = readBigInteger(dis);
BigInteger g = readBigInteger(dis);
BigInteger y = readBigInteger(dis);
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
PublicKey generatedPublicKey = keyFactory.generatePublic(publicKeySpec);
return key.equals(generatedPublicKey);
} else if (key instanceof RSAKey) {
if (!"ssh-rsa".equals(identifier)) {
return false;
}
BigInteger exponent = readBigInteger(dis);
BigInteger modulus = readBigInteger(dis);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
KeySpec publicKeySpec = new RSAPublicKeySpec(modulus, exponent);
PublicKey generatedPublicKey = keyFactory.generatePublic(publicKeySpec);
return key.equals(generatedPublicKey);
} else if (key instanceof ECPublicKey) {
String ecIdentifier = readString(dis);
if (!identifier.equals("ecdsa-sha2-" + ecIdentifier) || !nistSecMap.containsKey(ecIdentifier)) {
return false;
}
// Overall size of the x + y coordinates. We only support uncompressed points here, so
// to read x + y we ignore the "04" byte using (size - 1) / 2
int size = dis.readInt();
byte[] bytes = new byte[(size - 1) / 2];
dis.skipBytes(1);
dis.read(bytes, 0, bytes.length);
BigInteger x = new BigInteger(bytes);
dis.read(bytes, 0, bytes.length);
BigInteger y = new BigInteger(bytes);
KeyFactory keyFactory = KeyFactory.getInstance("EC");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec(nistSecMap.get(ecIdentifier)));
ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
ECPoint pubPoint = new ECPoint(x, y);
KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
PublicKey generatedPublicKey = keyFactory.generatePublic(keySpec);
return key.equals(generatedPublicKey);
} else {
throw new FailedLoginException("Unsupported key type " + key.getClass().toString());
}
} catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException | InvalidParameterSpecException e) {
throw new FailedLoginException("Unable to check public key");
}
}
use of java.security.spec.DSAPublicKeySpec in project robovm by robovm.
the class DSAKeyFactoryImpl method engineGetKeySpec.
/**
* This method returns a specification for the supplied key.
*
* The specification will be returned in the form of an object of the type
* specified by keySpec.
*
* @param key -
* either DSAPrivateKey or DSAPublicKey
* @param keySpec -
* either DSAPrivateKeySpec.class or DSAPublicKeySpec.class
*
* @return either a DSAPrivateKeySpec or a DSAPublicKeySpec
*
* @throws InvalidKeySpecException
* if "keySpec" is not a specification for DSAPublicKey or
* DSAPrivateKey
*/
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
BigInteger p, q, g, x, y;
if (key != null) {
if (keySpec == null) {
throw new NullPointerException("keySpec == null");
}
if (key instanceof DSAPrivateKey) {
DSAPrivateKey privateKey = (DSAPrivateKey) key;
if (keySpec.equals(DSAPrivateKeySpec.class)) {
x = privateKey.getX();
DSAParams params = privateKey.getParams();
p = params.getP();
q = params.getQ();
g = params.getG();
return (T) (new DSAPrivateKeySpec(x, p, q, g));
}
if (keySpec.equals(PKCS8EncodedKeySpec.class)) {
return (T) (new PKCS8EncodedKeySpec(key.getEncoded()));
}
throw new InvalidKeySpecException("'keySpec' is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec");
}
if (key instanceof DSAPublicKey) {
DSAPublicKey publicKey = (DSAPublicKey) key;
if (keySpec.equals(DSAPublicKeySpec.class)) {
y = publicKey.getY();
DSAParams params = publicKey.getParams();
p = params.getP();
q = params.getQ();
g = params.getG();
return (T) (new DSAPublicKeySpec(y, p, q, g));
}
if (keySpec.equals(X509EncodedKeySpec.class)) {
return (T) (new X509EncodedKeySpec(key.getEncoded()));
}
throw new InvalidKeySpecException("'keySpec' is neither DSAPublicKeySpec nor X509EncodedKeySpec");
}
}
throw new InvalidKeySpecException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
Aggregations