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;
}
}
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;
}
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);
}
}
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);
}
}
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);
}
}
Aggregations