use of com.github.zhenwei.provider.jcajce.spec.OpenSSHPrivateKeySpec in project LinLong-Java by zhenwei1108.
the class KeyFactorySpi method engineGetKeySpec.
protected KeySpec engineGetKeySpec(Key key, Class spec) throws InvalidKeySpecException {
if ((spec.isAssignableFrom(KeySpec.class) || spec.isAssignableFrom(java.security.spec.ECPublicKeySpec.class)) && key instanceof ECPublicKey) {
ECPublicKey k = (ECPublicKey) key;
if (k.getParams() != null) {
return new java.security.spec.ECPublicKeySpec(k.getW(), k.getParams());
} else {
ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
return new java.security.spec.ECPublicKeySpec(k.getW(), EC5Util.convertSpec(EC5Util.convertCurve(implicitSpec.getCurve(), implicitSpec.getSeed()), implicitSpec));
}
} else if ((spec.isAssignableFrom(KeySpec.class) || spec.isAssignableFrom(java.security.spec.ECPrivateKeySpec.class)) && key instanceof ECPrivateKey) {
ECPrivateKey k = (ECPrivateKey) key;
if (k.getParams() != null) {
return new java.security.spec.ECPrivateKeySpec(k.getS(), k.getParams());
} else {
ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
return new java.security.spec.ECPrivateKeySpec(k.getS(), EC5Util.convertSpec(EC5Util.convertCurve(implicitSpec.getCurve(), implicitSpec.getSeed()), implicitSpec));
}
} else if (spec.isAssignableFrom(com.github.zhenwei.provider.jce.spec.ECPublicKeySpec.class) && key instanceof ECPublicKey) {
ECPublicKey k = (ECPublicKey) key;
if (k.getParams() != null) {
return new com.github.zhenwei.provider.jce.spec.ECPublicKeySpec(EC5Util.convertPoint(k.getParams(), k.getW()), EC5Util.convertSpec(k.getParams()));
} else {
ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
return new com.github.zhenwei.provider.jce.spec.ECPublicKeySpec(EC5Util.convertPoint(k.getParams(), k.getW()), implicitSpec);
}
} else if (spec.isAssignableFrom(com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec.class) && key instanceof ECPrivateKey) {
ECPrivateKey k = (ECPrivateKey) key;
if (k.getParams() != null) {
return new com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec(k.getS(), EC5Util.convertSpec(k.getParams()));
} else {
ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
return new com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec(k.getS(), implicitSpec);
}
} else if (spec.isAssignableFrom(OpenSSHPublicKeySpec.class) && key instanceof ECPublicKey) {
if (key instanceof BCECPublicKey) {
BCECPublicKey bcPk = (BCECPublicKey) key;
ECParameterSpec sc = bcPk.getParameters();
try {
return new OpenSSHPublicKeySpec(OpenSSHPublicKeyUtil.encodePublicKey(new ECPublicKeyParameters(bcPk.getQ(), new ECDomainParameters(sc.getCurve(), sc.getG(), sc.getN(), sc.getH(), sc.getSeed()))));
} catch (IOException e) {
throw new IllegalArgumentException("unable to produce encoding: " + e.getMessage());
}
} else {
throw new IllegalArgumentException("invalid key type: " + key.getClass().getName());
}
} else if (spec.isAssignableFrom(OpenSSHPrivateKeySpec.class) && key instanceof ECPrivateKey) {
if (key instanceof BCECPrivateKey) {
try {
return new OpenSSHPrivateKeySpec(PrivateKeyInfo.getInstance(key.getEncoded()).parsePrivateKey().toASN1Primitive().getEncoded());
} catch (IOException e) {
throw new IllegalArgumentException("cannot encoded key: " + e.getMessage());
}
} else {
throw new IllegalArgumentException("invalid key type: " + key.getClass().getName());
}
}
return super.engineGetKeySpec(key, spec);
}
use of com.github.zhenwei.provider.jcajce.spec.OpenSSHPrivateKeySpec in project LinLong-Java by zhenwei1108.
the class KeyFactorySpi method engineGetKeySpec.
protected KeySpec engineGetKeySpec(Key key, Class spec) throws InvalidKeySpecException {
if (spec.isAssignableFrom(OpenSSHPrivateKeySpec.class) && key instanceof BCEdDSAPrivateKey) {
try {
//
// The DEROctetString at element 2 is an encoded DEROctetString with the private key value
// within it.
//
ASN1Sequence seq = ASN1Sequence.getInstance(key.getEncoded());
ASN1OctetString val = ASN1OctetString.getInstance(seq.getObjectAt(2));
byte[] encoding = ASN1OctetString.getInstance(ASN1Primitive.fromByteArray(val.getOctets())).getOctets();
return new OpenSSHPrivateKeySpec(OpenSSHPrivateKeyUtil.encodePrivateKey(new Ed25519PrivateKeyParameters(encoding)));
} catch (IOException ex) {
throw new InvalidKeySpecException(ex.getMessage(), ex.getCause());
}
} else if (spec.isAssignableFrom(OpenSSHPublicKeySpec.class) && key instanceof BCEdDSAPublicKey) {
try {
byte[] encoding = key.getEncoded();
if (!Arrays.areEqual(Ed25519Prefix, 0, Ed25519Prefix.length, encoding, 0, encoding.length - Ed25519PublicKeyParameters.KEY_SIZE)) {
throw new InvalidKeySpecException("Invalid Ed25519 public key encoding");
}
Ed25519PublicKeyParameters publicKey = new Ed25519PublicKeyParameters(encoding, Ed25519Prefix.length);
return new OpenSSHPublicKeySpec(OpenSSHPublicKeyUtil.encodePublicKey(publicKey));
} catch (IOException ex) {
throw new InvalidKeySpecException(ex.getMessage(), ex.getCause());
}
} else if (spec.isAssignableFrom(RawEncodedKeySpec.class)) {
if (key instanceof XDHPublicKey) {
return new RawEncodedKeySpec(((XDHPublicKey) key).getUEncoding());
}
if (key instanceof EdDSAPublicKey) {
return new RawEncodedKeySpec(((EdDSAPublicKey) key).getPointEncoding());
}
}
return super.engineGetKeySpec(key, spec);
}
Aggregations