use of java.security.spec.ECPoint in project karaf by apache.
the class PublicKeyEncodingTest method testEC256.
@Test
public void testEC256() throws FailedLoginException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidParameterSpecException {
// ecdsa-sha2-nistp256 - generated using: ssh-keygen -t ecdsa
String storedKey = "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBL4+Vytknywh/XuOluxIqcHRoBsZHa12z+jpK" + "pwuGFlzlq3yatwC8DqUaywJjzSnoGKSge9GBjuFYwvHN17hq8U=";
String x = "-29742501866672735446035294501787338870744851402037490785638836399245997090445";
String y = "-47637824304079393691947094099226900728731860400898598466261954347951527449659";
KeyFactory keyFactory = KeyFactory.getInstance("EC");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec("secp256r1"));
ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
ECPoint pubPoint = new ECPoint(new BigInteger(x), new BigInteger(y));
KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
assertTrue(PublickeyLoginModule.equals(publicKey, storedKey));
// Make sure a different stored key does not work
String differentKey = "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDWwVudH/QYr/Nlkf/lZ0yMXVL+PvXBsGat8" + "+n07/Bw0xZGP5E8+x1wbkZVS6qx8XyPMI61NnCRLawB+UX3ZE/A=";
assertFalse(PublickeyLoginModule.equals(publicKey, differentKey));
}
use of java.security.spec.ECPoint in project karaf by apache.
the class PublicKeyEncodingTest method testEC256_2.
@Test
public void testEC256_2() throws FailedLoginException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidParameterSpecException {
// ecdsa-sha2-nistp256 - generated using: ssh-keygen -t ecdsa
String storedKey = "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDWwVudH/QYr/Nlkf/lZ0yMXVL+PvXBsGat8" + "+n07/Bw0xZGP5E8+x1wbkZVS6qx8XyPMI61NnCRLawB+UX3ZE/A=";
String x = "24284145843828879115537963613603143837878136357229118319568173718380870376500";
String y = "-26429272137078923303974425138822683171929812869671300956629169158527526562832";
KeyFactory keyFactory = KeyFactory.getInstance("EC");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec("secp256r1"));
ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
ECPoint pubPoint = new ECPoint(new BigInteger(x), new BigInteger(y));
KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
assertTrue(PublickeyLoginModule.equals(publicKey, storedKey));
// Make sure a different stored key does not work
String differentKey = "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBL4+Vytknywh/XuOluxIqcHRoBsZHa12z+jpK" + "pwuGFlzlq3yatwC8DqUaywJjzSnoGKSge9GBjuFYwvHN17hq8U=";
assertFalse(PublickeyLoginModule.equals(publicKey, differentKey));
}
use of java.security.spec.ECPoint in project karaf by apache.
the class PublicKeyEncodingTest method testEC521.
@Test
public void testEC521() throws FailedLoginException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidParameterSpecException {
// ecdsa-sha2-nistp521 - generated using: ssh-keygen -t ecdsa -b 521
String storedKey = "AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBADLxNQ1lf99/8WlEg1nYbDL+qcWY4KSFZG+" + "2euZ1hAO9m1ly4ByuqLhuf1M3JPhhOCMIn9ihrPcxplG2zjpOnhaugDdwGJn+qcwkZSXVWoZOxpYUaQRdfnSd5wAKo9XOPqvc/L3BeHK" + "mPPygLH7eW2MEz2qOWe7Bby9duELK+9Zn3ebOQ==";
String x = "273209377797440455675669012133614826094878213786507845287169633163915658072657502796285437529808606" + "0585712688028315849324172582722748448938768134500098005690";
String y = "297320154107898594969162703371411878757449109919929193169657424280609259087338914952452468191452153" + "1633519626430088785609447337443826933969196755052278553401";
KeyFactory keyFactory = KeyFactory.getInstance("EC");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec("secp521r1"));
ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
ECPoint pubPoint = new ECPoint(new BigInteger(x), new BigInteger(y));
KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
assertTrue(PublickeyLoginModule.equals(publicKey, storedKey));
// Make sure a different stored key does not work
String differentKey = "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBL4+Vytknywh/XuOluxIqcHRoBsZHa12z+jpK" + "pwuGFlzlq3yatwC8DqUaywJjzSnoGKSge9GBjuFYwvHN17hq8U=";
assertFalse(PublickeyLoginModule.equals(publicKey, differentKey));
}
use of java.security.spec.ECPoint in project karaf by apache.
the class PublicKeyLoginModuleTest method testUnknownKeyEC.
@Test
public void testUnknownKeyEC() throws Exception {
Properties options = getLoginModuleOptions();
PublickeyLoginModule module = new PublickeyLoginModule();
Subject subject = new Subject();
String x = "2145382594999641569030545431803328907795332312211583318014254232969998637145";
String y = "52282205184471090919696434245736603165041352971927370430120381994413951213993";
KeyFactory keyFactory = KeyFactory.getInstance("EC");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec("secp256r1"));
ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
ECPoint pubPoint = new ECPoint(new BigInteger(x), new BigInteger(y));
KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
module.initialize(subject, new NamePubkeyCallbackHandler("ec", publicKey), null, options);
assertEquals("Precondition", 0, subject.getPrincipals().size());
try {
module.login();
fail("Failure expected on an unknown user");
} catch (FailedLoginException ex) {
// expected
}
}
use of java.security.spec.ECPoint in project thingsboard by thingsboard.
the class LwM2mRPkCredentials method generatePublicKeyRPK.
private void generatePublicKeyRPK(String publX, String publY, String privS) {
try {
/*Get Elliptic Curve Parameter spec for secp256r1 */
AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
algoParameters.init(new ECGenParameterSpec("secp256r1"));
ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);
if (publX != null && !publX.isEmpty() && publY != null && !publY.isEmpty()) {
// Get point values
byte[] publicX = Hex.decodeHex(publX.toCharArray());
byte[] publicY = Hex.decodeHex(publY.toCharArray());
/* Create key specs */
KeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(publicX), new BigInteger(publicY)), parameterSpec);
/* Get keys */
this.serverPublicKey = KeyFactory.getInstance("EC").generatePublic(publicKeySpec);
}
if (privS != null && !privS.isEmpty()) {
/* Get point values */
byte[] privateS = Hex.decodeHex(privS.toCharArray());
/* Create key specs */
KeySpec privateKeySpec = new ECPrivateKeySpec(new BigInteger(privateS), parameterSpec);
/* Get keys */
this.serverPrivateKey = KeyFactory.getInstance("EC").generatePrivate(privateKeySpec);
}
} catch (GeneralSecurityException | IllegalArgumentException e) {
log.error("[{}] Failed generate Server KeyRPK", e.getMessage());
throw new RuntimeException(e);
}
}
Aggregations