Search in sources :

Example 16 with InvalidKeySpecException

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

the class Select method aclCheckFilterFields.

/**
   * This filters individual fields if the cannot be accessed by the reader.
   *
   * @param packet
   * @param records
   * @param reader
   * @param app
   * @return
   */
private static JSONArray aclCheckFilterFields(SelectRequestPacket packet, JSONArray records, String reader, GNSApplicationInterface<String> app) {
    for (int i = 0; i < records.length(); i++) {
        try {
            JSONObject record = records.getJSONObject(i);
            String guid = record.getString(NameRecord.NAME.getName());
            // Look at the keys in the values map
            JSONObject valuesMap = record.getJSONObject(NameRecord.VALUES_MAP.getName());
            Iterator<?> keys = valuesMap.keys();
            while (keys.hasNext()) {
                String field = (String) keys.next();
                if (!InternalField.isInternalField(field)) {
                    LOGGER.log(Level.FINE, "{0} Checking: {1}", new Object[] { app.getNodeID(), field });
                    ResponseCode responseCode = NSAuthentication.signatureAndACLCheck(null, guid, field, null, reader, null, null, MetaDataTypeName.READ_WHITELIST, app, true);
                    if (!responseCode.isOKResult()) {
                        LOGGER.log(Level.FINE, "{0} Removing: {1}", new Object[] { app.getNodeID(), field });
                        // removing the offending field
                        keys.remove();
                    }
                }
            }
        } catch (JSONException | InvalidKeyException | InvalidKeySpecException | SignatureException | NoSuchAlgorithmException | FailedDBOperationException | UnsupportedEncodingException e) {
            // ignore json errros
            LOGGER.log(Level.FINE, "{0} Problem getting guid from json: {1}", new Object[] { app.getNodeID(), e.getMessage() });
        }
    }
    return records;
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 17 with InvalidKeySpecException

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

the class Select method aclCheckFilterForRecordsArray.

/**
   * This filters entire records if the query uses fields that cannot be accessed in the
   * returned record by the reader. Otherwise the user would be able to determine that
   * some GUIDS contain specific values for fields they can't access.
   *
   * @param packet
   * @param records
   * @param reader
   * @param app
   * @return
   */
private static JSONArray aclCheckFilterForRecordsArray(SelectRequestPacket packet, JSONArray records, String reader, GNSApplicationInterface<String> app) {
    JSONArray result = new JSONArray();
    for (int i = 0; i < records.length(); i++) {
        try {
            JSONObject record = records.getJSONObject(i);
            String guid = record.getString(NameRecord.NAME.getName());
            List<String> queryFields = getFieldsForQueryType(packet);
            ResponseCode responseCode = NSAuthentication.signatureAndACLCheck(null, guid, null, queryFields, reader, null, null, MetaDataTypeName.READ_WHITELIST, app, true);
            LOGGER.log(Level.FINE, "{0} ACL check for select: guid={0} queryFields={1} responsecode={2}", new Object[] { app.getNodeID(), guid, queryFields, responseCode });
            if (responseCode.isOKResult()) {
                result.put(record);
            }
        } catch (JSONException | InvalidKeyException | InvalidKeySpecException | SignatureException | NoSuchAlgorithmException | FailedDBOperationException | UnsupportedEncodingException e) {
            // ignore json errros
            LOGGER.log(Level.FINE, "{0} Problem getting guid from json: {1}", new Object[] { app.getNodeID(), e.getMessage() });
        }
    }
    return result;
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 18 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project OneSignal-Android-SDK by OneSignal.

the class Security method generatePublicKey.

/**
     * Generates a PublicKey instance from a string containing the
     * Base64-encoded public key.
     *
     * @param encodedPublicKey Base64-encoded public key
     * @throws IllegalArgumentException if encodedPublicKey is invalid
     */
public static PublicKey generatePublicKey(String encodedPublicKey) {
    try {
        byte[] decodedKey = Base64.decode(encodedPublicKey);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
        return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        Log.e(TAG, "Invalid key specification.");
        throw new IllegalArgumentException(e);
    } catch (Base64DecoderException e) {
        Log.e(TAG, "Base64 decoding failed.");
        throw new IllegalArgumentException(e);
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 19 with InvalidKeySpecException

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

the class GNSClientCommandsExample method main.

/**
   * @param args
   * @throws IOException
   * @throws InvalidKeySpecException
   * @throws NoSuchAlgorithmException
   * @throws ClientException
   * @throws InvalidKeyException
   * @throws SignatureException
   * @throws Exception
   */
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, ClientException, InvalidKeyException, SignatureException, Exception {
    /* Create the client that connects to a default reconfigurator as
		 * specified in gigapaxos properties file. */
    client = new GNSClientCommands();
    System.out.println("[Client connected to GNS]\n");
    try {
        /**
       * Create an account GUID if one doesn't already exists. The true
       * flag makes it verbosely print out what it is doing. The password
       * is for future use and is needed mainly if the keypair is
       * generated on the server in order to retrieve the private key.
       * lookupOrCreateAccountGuid "cheats" by bypassing email-based or
       * other verification mechanisms using a shared secret between the
       * server and the client.
       *
       */
        System.out.println("// account GUID creation\n" + "GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS," + " \"password\", true)");
        guid = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password", true);
    } catch (Exception | Error e) {
        System.out.println("Exception during accountGuid creation: " + e);
        e.printStackTrace();
        System.exit(1);
    }
    // Create a JSON Object to initialize our guid record
    JSONObject json = new JSONObject("{\"occupation\":\"busboy\"," + "\"friends\":[\"Joe\",\"Sam\",\"Billy\"]," + "\"gibberish\":{\"meiny\":\"bloop\",\"einy\":\"floop\"}," + "\"location\":\"work\",\"name\":\"frank\"}");
    // Write out the JSON Object
    client.update(guid, json);
    System.out.println("\n// record update\n" + "client.update(GUID, record) // record=" + json);
    // and read the entire object back in
    JSONObject result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Change a field
    client.update(guid, new JSONObject("{\"occupation\":\"rocket scientist\"}"));
    System.out.println("\n// field update\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue={\"occupation\":\"rocket scientist\"}");
    // and read the entire object back in
    result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Add a field
    client.update(guid, new JSONObject("{\"ip address\":\"127.0.0.1\"}"));
    System.out.println("\n// field add\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue= {\"ip address\":\"127.0.0.1\"}");
    // and read the entire object back in
    result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Remove a field
    client.fieldRemove(guid.getGuid(), "gibberish", guid);
    System.out.println("\n// field remove\n" + "client.fieldRemove(GUID, \"gibberish\")");
    // and read the entire object back in
    result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Add some more stuff to read back
    JSONObject newJson = new JSONObject();
    JSONObject subJson = new JSONObject();
    subJson.put("sally", "red");
    subJson.put("sammy", "green");
    JSONObject subsubJson = new JSONObject();
    subsubJson.put("right", "seven");
    subsubJson.put("left", "eight");
    subJson.put("sally", subsubJson);
    newJson.put("flapjack", subJson);
    client.update(guid, newJson);
    System.out.println("\n// field add with JSON value\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue=" + newJson);
    // Read a single field at the top level
    String resultString = client.fieldRead(guid, "flapjack");
    System.out.println("client.fieldRead(\"flapjack\") -> " + resultString);
    // Read a single field using dot notation
    resultString = client.fieldRead(guid, "flapjack.sally.right");
    System.out.println("\n// dotted field read\n" + "client.fieldRead(GUID, \"flapjack.sally.right\") -> " + resultString);
    // Update a field using dot notation
    JSONArray newValue = new JSONArray(Arrays.asList("One", "Ready", "Frap"));
    client.fieldUpdate(guid, "flapjack.sammy", newValue);
    System.out.println("\n// dotted field update\n" + "client.fieldUpdate(GUID, \"flapjack.sammy\", " + newValue);
    // Read the same field using dot notation
    resultString = client.fieldRead(guid, "flapjack.sammy");
    System.out.println("client.fieldRead(GUID, \"flapjack.sammy\") -> " + resultString);
    // Read two fields at a time
    resultString = client.fieldRead(guid, new ArrayList<String>(Arrays.asList("name", "occupation")));
    System.out.println("\n// multi-field read\n" + "client.fieldRead(GUID, [\"name\",\"occupation\"] -> " + resultString);
    // Read the entire object back in
    result = client.read(guid);
    System.out.println("\nclient.read(GUID) -> " + result.toString());
    // Delete created GUID
    client.accountGuidRemove(guid);
    System.out.println("\n// GUID delete\n" + "client.accountGuidRemove(GUID) // GUID=" + guid);
    // Try read the entire record
    try {
        result = client.read(guid);
    } catch (Exception e) {
        System.out.println("\n// non-existent GUID error (expected)\n" + "client.read(GUID) // GUID= " + guid + "\n  " + e.getMessage());
    }
    client.close();
    System.out.println("\nclient.close() // test successful");
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 20 with InvalidKeySpecException

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

the class HTTPClientExample method main.

/**
   * @param args
   * @throws IOException
   * @throws InvalidKeySpecException
   * @throws NoSuchAlgorithmException
   * @throws ClientException
   * @throws InvalidKeyException
   * @throws SignatureException
   * @throws Exception
   */
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, ClientException, InvalidKeyException, SignatureException, Exception {
    // Create the client will connect to GNS HTTP server running locally.	 
    client = new HttpClient("127.0.0.1", 8080);
    try {
        /**
       * Create an account GUID if one doesn't already exists. The true
       * flag makes it verbosely print out what it is doing. The password
       * is for future use.
       * lookupOrCreateAccountGuid "cheats" by bypassing email-based or
       * other verification mechanisms using a shared secret between the
       * server and the client.
       *
       */
        System.out.println("// account GUID creation\n" + "GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS," + " \"password\", true)");
        guid = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password", true);
    } catch (Exception | Error e) {
        System.out.println("Exception during accountGuid creation: " + e);
        e.printStackTrace();
        System.exit(1);
    }
    // Create a JSON Object to initialize our guid record
    JSONObject json = new JSONObject("{\"occupation\":\"busboy\"," + "\"friends\":[\"Joe\",\"Sam\",\"Billy\"]," + "\"gibberish\":{\"meiny\":\"bloop\",\"einy\":\"floop\"}," + "\"location\":\"work\",\"name\":\"frank\"}");
    // Write out the JSON Object
    client.update(guid, json);
    System.out.println("\n// record update\n" + "client.update(GUID, record) // record=" + json);
    // and read the entire object back in
    JSONObject result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Change a field
    client.update(guid, new JSONObject("{\"occupation\":\"rocket scientist\"}"));
    System.out.println("\n// field update\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue={\"occupation\":\"rocket scientist\"}");
    // and read the entire object back in
    result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Add a field
    client.update(guid, new JSONObject("{\"ip address\":\"127.0.0.1\"}"));
    System.out.println("\n// field add\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue= {\"ip address\":\"127.0.0.1\"}");
    // and read the entire object back in
    result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Remove a field
    client.fieldRemove(guid.getGuid(), "gibberish", guid);
    System.out.println("\n// field remove\n" + "client.fieldRemove(GUID, \"gibberish\")");
    // and read the entire object back in
    result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Add some more stuff to read back
    JSONObject newJson = new JSONObject();
    JSONObject subJson = new JSONObject();
    subJson.put("sally", "red");
    subJson.put("sammy", "green");
    JSONObject subsubJson = new JSONObject();
    subsubJson.put("right", "seven");
    subsubJson.put("left", "eight");
    subJson.put("sally", subsubJson);
    newJson.put("flapjack", subJson);
    client.update(guid, newJson);
    System.out.println("\n// field add with JSON value\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue=" + newJson);
    // Read a single field at the top level
    String resultString = client.fieldRead(guid, "flapjack");
    System.out.println("client.fieldRead(\"flapjack\") -> " + resultString);
    // Read a single field using dot notation
    resultString = client.fieldRead(guid, "flapjack.sally.right");
    System.out.println("\n// dotted field read\n" + "client.fieldRead(GUID, \"flapjack.sally.right\") -> " + resultString);
    // Update a field using dot notation
    JSONArray newValue = new JSONArray(Arrays.asList("One", "Ready", "Frap"));
    client.fieldUpdate(guid, "flapjack.sammy", newValue);
    System.out.println("\n// dotted field update\n" + "client.fieldUpdate(GUID, \"flapjack.sammy\", " + newValue);
    // Read the same field using dot notation
    resultString = client.fieldRead(guid, "flapjack.sammy");
    System.out.println("client.fieldRead(GUID, \"flapjack.sammy\") -> " + resultString);
    // Read two fields at a time
    resultString = client.fieldRead(guid, new ArrayList<String>(Arrays.asList("name", "occupation")));
    System.out.println("\n// multi-field read\n" + "client.fieldRead(GUID, [\"name\",\"occupation\"] -> " + resultString);
    // Read the entire object back in
    result = client.read(guid);
    System.out.println("\nclient.read(GUID) -> " + result.toString());
    // Delete created GUID
    client.accountGuidRemove(guid);
    System.out.println("\n// GUID delete\n" + "client.accountGuidRemove(GUID) // GUID=" + guid);
    // Try read the entire record
    try {
        result = client.read(guid);
    } catch (Exception e) {
        System.out.println("\n// non-existent GUID error (expected)\n" + "client.read(GUID) // GUID= " + guid + "\n  " + e.getMessage());
    }
    client.close();
    System.out.println("\nclient.close() // test successful");
}
Also used : JSONObject(org.json.JSONObject) HttpClient(edu.umass.cs.gnsclient.client.http.HttpClient) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

InvalidKeySpecException (java.security.spec.InvalidKeySpecException)483 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)306 KeyFactory (java.security.KeyFactory)199 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)155 InvalidKeyException (java.security.InvalidKeyException)116 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)108 IOException (java.io.IOException)98 PublicKey (java.security.PublicKey)90 PrivateKey (java.security.PrivateKey)77 SecretKeyFactory (javax.crypto.SecretKeyFactory)66 PBEKeySpec (javax.crypto.spec.PBEKeySpec)59 BigInteger (java.math.BigInteger)45 SignatureException (java.security.SignatureException)39 SecretKey (javax.crypto.SecretKey)38 BadPaddingException (javax.crypto.BadPaddingException)36 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)36 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)35 NoSuchProviderException (java.security.NoSuchProviderException)34 KeySpec (java.security.spec.KeySpec)32 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)30