Search in sources :

Example 21 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project GNS by MobilityFirst.

the class KeyPairUtils method getGuidEntry.

/**
   * Retrieves the public/private key pair for the given user.
   *
   * @param gnsName the name of the GNS instance (e.g. "server.gns.name:8080")
   * @param username the user name
   * @return the GNSProtocol.GUID.toString() entry if found, null otherwise
   */
public static GuidEntry getGuidEntry(String gnsName, String username) {
    if (username == null) {
        return null;
    }
    if (IS_ANDROID) {
        return KeyPairUtilsAndroid.getGuidEntryFromPreferences(gnsName, username);
    }
    createSingleton();
    String guid = keyStorageObj.get(generateKey(gnsName, username, GUID), "");
    String publicString = keyStorageObj.get(generateKey(gnsName, username, PUBLIC), "");
    String privateString = keyStorageObj.get(generateKey(gnsName, username, PRIVATE), "");
    if (!guid.isEmpty() && !publicString.isEmpty() && !privateString.isEmpty()) {
        try {
            byte[] encodedPublicKey = DatatypeConverter.parseHexBinary(publicString);
            //byte[] encodedPublicKey = ByteUtils.hexStringToByteArray(publicString);
            byte[] encodedPrivateKey = DatatypeConverter.parseHexBinary(privateString);
            //byte[] encodedPrivateKey = ByteUtils.hexStringToByteArray(privateString);
            KeyFactory keyFactory = KeyFactory.getInstance(GNSProtocol.RSA_ALGORITHM.toString());
            X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
            PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
            PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
            PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
            return new GuidEntry(username, guid, publicKey, privateKey);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | EncryptionException e) {
            System.out.println(e.toString());
            return null;
        }
    } else {
        return null;
    }
}
Also used : PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 22 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project GNS by MobilityFirst.

the class KeyPairUtilsAndroid method getAllGuids.

/**
   * Return the list of all GUIDs stored locally that belong to a particular GNS
   * instance
   *
   * @param gnsName the GNS host:port
   * @return all matching GUIDs
   */
public static List<GuidEntry> getAllGuids(String gnsName) {
    List<GuidEntry> guids = new LinkedList<>();
    File gnsFolder = new File(GNS_KEY_DIR);
    // Save the path as a string value
    String extStorageDirectory = gnsFolder.toString();
    File file = new File(extStorageDirectory, GNS_KEYS_FILENAME);
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            String aliasKey = line;
            String guid = br.readLine();
            String publicString = br.readLine();
            String privateString = br.readLine();
            if (aliasKey.contains(gnsName) && !publicString.isEmpty() && !privateString.isEmpty()) {
                try {
                    byte[] encodedPublicKey = DatatypeConverter.parseHexBinary(publicString);
                    byte[] encodedPrivateKey = DatatypeConverter.parseHexBinary(privateString);
                    //byte[] encodedPublicKey = ByteUtils.hexStringToByteArray(publicString);
                    //byte[] encodedPrivateKey = ByteUtils.hexStringToByteArray(privateString);
                    KeyFactory keyFactory = KeyFactory.getInstance(GNSProtocol.RSA_ALGORITHM.toString());
                    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
                    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
                    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
                    PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
                    // Strip gnsName from stored alias to only return the entity name
                    guids.add(new GuidEntry(aliasKey.substring(gnsName.length() + 1), guid, publicKey, privateKey));
                } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
                    Log.e(KeyPairUtilsAndroid.class.getName(), "Cannot decode keys", e);
                } catch (EncryptionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    // You'll need to add proper error handling here
    }
    return guids;
}
Also used : PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BufferedReader(java.io.BufferedReader) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) FileReader(java.io.FileReader) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) File(java.io.File) KeyFactory(java.security.KeyFactory)

Example 23 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project lucene-solr by apache.

the class CryptoKeys method getX509PublicKey.

/**
   * Create PublicKey from a .DER file
   */
public static PublicKey getX509PublicKey(byte[] buf) throws Exception {
    X509EncodedKeySpec spec = new X509EncodedKeySpec(buf);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return kf.generatePublic(spec);
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 24 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project karaf by apache.

the class KnownHostsManager method getKnownKeyInternal.

private PublicKey getKnownKeyInternal(SocketAddress remoteAddress, String checkAlgorithm, BufferedReader reader) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    String checkServerAddress = getAddressString(remoteAddress);
    String line = reader.readLine();
    while (line != null) {
        String[] lineParts = line.split(" ");
        String serverAddress = lineParts[0];
        String algorithm = lineParts[1];
        if (checkServerAddress.equals(serverAddress) && checkAlgorithm.equals(algorithm)) {
            byte[] key = Base64.getDecoder().decode(lineParts[2].getBytes());
            KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key);
            return keyFactory.generatePublic(keySpec);
        }
        line = reader.readLine();
    }
    return null;
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 25 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project cloudstack by apache.

the class SAMLUtils method savePublicKey.

public static String savePublicKey(PublicKey key) {
    try {
        KeyFactory keyFactory = SAMLUtils.getKeyFactory();
        if (keyFactory == null)
            return null;
        X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.class);
        return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()), Charset.forName("UTF-8"));
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create KeyFactory:" + e.getMessage());
    }
    return null;
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Aggregations

X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)343 KeyFactory (java.security.KeyFactory)228 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)154 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)142 PublicKey (java.security.PublicKey)129 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)82 PrivateKey (java.security.PrivateKey)50 RSAPublicKey (java.security.interfaces.RSAPublicKey)48 IOException (java.io.IOException)39 InvalidKeyException (java.security.InvalidKeyException)37 KeyPair (java.security.KeyPair)30 Cipher (javax.crypto.Cipher)26 Signature (java.security.Signature)25 EncodedKeySpec (java.security.spec.EncodedKeySpec)21 NoSuchProviderException (java.security.NoSuchProviderException)14 ECPublicKey (java.security.interfaces.ECPublicKey)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 SecretKey (javax.crypto.SecretKey)13 BigInteger (java.math.BigInteger)12 Key (java.security.Key)12