Search in sources :

Example 6 with EncryptionException

use of edu.umass.cs.gnscommon.exceptions.client.EncryptionException 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 7 with EncryptionException

use of edu.umass.cs.gnscommon.exceptions.client.EncryptionException 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 8 with EncryptionException

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

the class GuidEntry method generateKeyPair.

private static KeyPair generateKeyPair(String encodedPublic, String encodedPrivate) throws EncryptionException {
    byte[] encodedPublicKey = Base64.decode(encodedPublic);
    byte[] encodedPrivateKey = Base64.decode(encodedPrivate);
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(GNSProtocol.RSA_ALGORITHM.toString());
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
        PublicKey thePublicKey = keyFactory.generatePublic(publicKeySpec);
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
        PrivateKey thePrivateKey = keyFactory.generatePrivate(privateKeySpec);
        return new KeyPair(thePublicKey, thePrivateKey);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        throw new EncryptionException("Failed to generate keypair", e);
    }
}
Also used : KeyPair(java.security.KeyPair) 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 9 with EncryptionException

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

the class HttpClient method publicKeyLookupFromGuid.

/**
   * Get the public key for a given GNSProtocol.GUID.toString()
   *
   * @param guid
   * @return the publickey
   * @throws InvalidGuidException
   * @throws ClientException
   * @throws IOException
   */
public PublicKey publicKeyLookupFromGuid(String guid) throws InvalidGuidException, 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 10 with EncryptionException

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

the class CommandUtils method checkResponse.

/**
   *
   * @param command
   *
   * @param responsePacket
   * @return Response as a string.
   * @throws ClientException
   */
public static ResponsePacket checkResponse(ResponsePacket responsePacket, CommandPacket command) throws ClientException {
    ResponseCode code = responsePacket.getErrorCode();
    String returnValue = responsePacket.getReturnValue();
    // wants to return a null value.
    if (code.isOKResult()) {
        return (returnValue.startsWith(GNSProtocol.NULL_RESPONSE.toString())) ? null : //returnValue;
        responsePacket;
    }
    // else error
    String errorSummary = code + ": " + returnValue + //+ ": " + responsePacket.getSummary()
    (command != null ? " for command " + command.getSummary() : "");
    switch(code) {
        case SIGNATURE_ERROR:
            throw new EncryptionException(code, errorSummary);
        case BAD_GUID_ERROR:
        case BAD_ACCESSOR_ERROR:
        case BAD_ACCOUNT_ERROR:
            throw new InvalidGuidException(code, errorSummary);
        case FIELD_NOT_FOUND_ERROR:
            throw new FieldNotFoundException(code, errorSummary);
        case ACCESS_ERROR:
            throw new AclException(code, errorSummary);
        case VERIFICATION_ERROR:
            throw new VerificationException(code, errorSummary);
        case ALREADY_VERIFIED_EXCEPTION:
            throw new VerificationException(code, errorSummary);
        case DUPLICATE_ID_EXCEPTION:
            //case DUPLICATE_NAME_EXCEPTION:
            throw new DuplicateNameException(code, errorSummary);
        case DUPLICATE_FIELD_EXCEPTION:
            throw new InvalidFieldException(code, errorSummary);
        case ACTIVE_REPLICA_EXCEPTION:
            throw new InvalidGuidException(code, errorSummary);
        case NONEXISTENT_NAME_EXCEPTION:
            throw new InvalidGuidException(code, errorSummary);
        case TIMEOUT:
        case RECONFIGURATION_EXCEPTION:
            throw new ClientException(code, errorSummary);
        default:
            throw new ClientException(code, "Error received with an unknown response code: " + errorSummary);
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) InvalidGuidException(edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException) VerificationException(edu.umass.cs.gnscommon.exceptions.client.VerificationException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) AclException(edu.umass.cs.gnscommon.exceptions.client.AclException) InvalidFieldException(edu.umass.cs.gnscommon.exceptions.client.InvalidFieldException)

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