Search in sources :

Example 11 with EncryptionException

use of edu.umass.cs.gnscommon.exceptions.client.EncryptionException in project GNS by MobilityFirst.

the class GNSClientCommands method publicKeyLookupFromGuid.

/**
   * Get the public key for a given guid.
   *
   * @param guid
   * @return Public key for {@code guid}
   * @throws edu.umass.cs.gnscommon.exceptions.client.ClientException
   * if a protocol error occurs or the list cannot be parsed
   * @throws java.io.IOException
   * if a communication error occurs
   */
// Note: publicKeyLookupFromGUID is implemented incorrectly in GNSCommand
public PublicKey publicKeyLookupFromGuid(String guid) throws ClientException, IOException {
    JSONObject guidInfo = lookupGuidRecord(guid);
    try {
        String key = guidInfo.getString(GNSProtocol.GUID_RECORD_PUBLICKEY.toString());
        byte[] encodedPublicKey = Base64.decode(key);
        KeyFactory keyFactory = KeyFactory.getInstance(GNSProtocol.RSA_ALGORITHM.toString());
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
        return keyFactory.generatePublic(publicKeySpec);
    } catch (JSONException e) {
        throw new ClientException("Failed to parse LOOKUP_USER response", e);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        throw new EncryptionException("Public key encryption failed", e);
    }
}
Also used : JSONObject(org.json.JSONObject) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) JSONException(org.json.JSONException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 12 with EncryptionException

use of edu.umass.cs.gnscommon.exceptions.client.EncryptionException in project GNS by MobilityFirst.

the class KeyPairUtilsAndroid method readGuidEntryFromFile.

private static GuidEntry readGuidEntryFromFile(String gnsName, String username) {
    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);
    String aliasKey = gnsName + "@" + username;
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            if (line.equals(aliasKey)) {
                // found the correct alias, read next three lines for guid, public
                // key,
                // private key
                String guid = br.readLine();
                String publicString = br.readLine();
                String privateString = br.readLine();
                if (!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);
                        return new GuidEntry(username, guid, publicKey, privateKey);
                    } catch (NoSuchAlgorithmException | InvalidKeySpecException | EncryptionException e) {
                        Log.e(KeyPairUtilsAndroid.class.getName(), "Cannot decode keys", e);
                    }
                }
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    // You'll need to add proper error handling here
    }
    return null;
}
Also used : PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) 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)

Aggregations

EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 KeyFactory (java.security.KeyFactory)7 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)7 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)6 IOException (java.io.IOException)6 File (java.io.File)5 JSONException (org.json.JSONException)5 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)4 PrivateKey (java.security.PrivateKey)4 PublicKey (java.security.PublicKey)4 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)4 FileInputStream (java.io.FileInputStream)3 ObjectInputStream (java.io.ObjectInputStream)3 GNSClient (edu.umass.cs.gnsclient.client.GNSClient)2 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)2 BufferedReader (java.io.BufferedReader)2 FileOutputStream (java.io.FileOutputStream)2 FileReader (java.io.FileReader)2