Search in sources :

Example 96 with ECPoint

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));
}
Also used : ECParameterSpec(java.security.spec.ECParameterSpec) PublicKey(java.security.PublicKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) BigInteger(java.math.BigInteger) ECPoint(java.security.spec.ECPoint) KeyFactory(java.security.KeyFactory) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) AlgorithmParameters(java.security.AlgorithmParameters) Test(org.junit.Test)

Example 97 with ECPoint

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));
}
Also used : ECParameterSpec(java.security.spec.ECParameterSpec) PublicKey(java.security.PublicKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) BigInteger(java.math.BigInteger) ECPoint(java.security.spec.ECPoint) KeyFactory(java.security.KeyFactory) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) AlgorithmParameters(java.security.AlgorithmParameters) Test(org.junit.Test)

Example 98 with ECPoint

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));
}
Also used : ECParameterSpec(java.security.spec.ECParameterSpec) PublicKey(java.security.PublicKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) BigInteger(java.math.BigInteger) ECPoint(java.security.spec.ECPoint) KeyFactory(java.security.KeyFactory) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) AlgorithmParameters(java.security.AlgorithmParameters) Test(org.junit.Test)

Example 99 with ECPoint

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
    }
}
Also used : PublicKey(java.security.PublicKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) Properties(org.apache.felix.utils.properties.Properties) ECPoint(java.security.spec.ECPoint) Subject(javax.security.auth.Subject) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) FailedLoginException(javax.security.auth.login.FailedLoginException) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) NamePubkeyCallbackHandler(org.apache.karaf.jaas.modules.NamePubkeyCallbackHandler) KeyFactory(java.security.KeyFactory) AlgorithmParameters(java.security.AlgorithmParameters) Test(org.junit.Test)

Example 100 with ECPoint

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);
    }
}
Also used : ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) KeySpec(java.security.spec.KeySpec) GeneralSecurityException(java.security.GeneralSecurityException) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

ECPoint (java.security.spec.ECPoint)111 ECParameterSpec (java.security.spec.ECParameterSpec)56 BigInteger (java.math.BigInteger)54 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)36 ECPublicKey (java.security.interfaces.ECPublicKey)31 EllipticCurve (java.security.spec.EllipticCurve)31 KeyPair (java.security.KeyPair)20 ECPrivateKey (java.security.interfaces.ECPrivateKey)20 PublicKey (java.security.PublicKey)17 ECFieldFp (java.security.spec.ECFieldFp)17 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)16 Test (org.junit.Test)16 KeyFactory (java.security.KeyFactory)15 KeyPairGenerator (java.security.KeyPairGenerator)14 AlgorithmParameters (java.security.AlgorithmParameters)13 GeneralSecurityException (java.security.GeneralSecurityException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 KeySpec (java.security.spec.KeySpec)11 IOException (java.io.IOException)10 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)9