Search in sources :

Example 11 with SignatureException

use of java.security.SignatureException in project bigbluebutton by bigbluebutton.

the class TurnServer method calculateRFC2104HMAC.

/**
   * Computes RFC 2104-compliant HMAC signature.
   * * @param data
   * The data to be signed.
   * @param key
   * The signing key.
   * @return
   * The Base64-encoded RFC 2104-compliant HMAC signature.
   * @throws
   * java.security.SignatureException when signature generation fails
   */
private String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException {
    String result;
    try {
        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
        // get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        // compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes());
        // base64-encode the hmac
        result = new String(Base64.encodeBase64(rawHmac));
    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }
    return result;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SignatureException(java.security.SignatureException) Mac(javax.crypto.Mac) SignatureException(java.security.SignatureException)

Example 12 with SignatureException

use of java.security.SignatureException in project GNS by MobilityFirst.

the class ClientAsynchExample 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
    GNSClientCommands client = new GNSClientCommands(null);
    GuidEntry accountGuidEntry = null;
    try {
        // Create a guid (which is also an account guid)
        accountGuidEntry = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password", true);
    } catch (Exception e) {
        System.out.println("Exception during accountGuid creation: " + e);
        e.printStackTrace();
        System.exit(1);
    }
    System.out.println("Client connected to GNS");
    JSONObject command;
    if (args.length > 0 && args[0].equals("-write")) {
        JSONObject json = new JSONObject("{\"occupation\":\"busboy\"," + "\"friends\":[\"Joe\",\"Sam\",\"Billy\"]," + "\"gibberish\":{\"meiny\":\"bloop\",\"einy\":\"floop\"}," + "\"location\":\"work\",\"name\":\"frank\"}");
        command = createAndSignCommand(CommandType.ReplaceUserJSON, accountGuidEntry, GNSProtocol.GUID.toString(), accountGuidEntry.getGuid(), GNSProtocol.USER_JSON.toString(), json.toString(), GNSProtocol.WRITER.toString(), accountGuidEntry.getGuid());
    } else {
        command = createAndSignCommand(CommandType.Read, accountGuidEntry, GNSProtocol.GUID.toString(), accountGuidEntry.getGuid(), GNSProtocol.FIELD.toString(), "occupation", GNSProtocol.READER.toString(), accountGuidEntry.getGuid());
    }
    // Create the command packet with a bogus id
    // arun: can not change request ID
    CommandPacket commandPacket = new CommandPacket((long) (Math.random() * Long.MAX_VALUE), command);
    // Keep track of what we've sent for the other thread to look at.
    Set<Long> pendingIds = Collections.newSetFromMap(new ConcurrentHashMap<Long, Boolean>());
    // Create and run another thread to pick up the responses
    Runnable companion = new Runnable() {

        @Override
        public void run() {
            lookForResponses(client, pendingIds);
        }
    };
    //Does this on Android as of 9/16:
    //ERROR: ClientAsynchExample.java:114: Lambda coming from jar file need their interfaces 
    //on the classpath to be compiled, unknown interfaces are java.lang.Runnable
    //    Runnable companion = () -> {
    //      lookForResponses(client, pendingIds);
    //    };
    new Thread(companion).start();
    while (true) {
        //long id = client.generateNextRequestID();
        // Important to set the new request id each time
        //commandPacket.setClientRequestId(id);
        // Record what we're sending
        pendingIds.add(commandPacket.getRequestID());
        // arun: disabled
        if (true) {
            throw new RuntimeException("disabled");
        }
        // Actually send out the packet
        //client.sendCommandPacketAsynch(commandPacket);
        // if you generate them too fast you'll clog things up 
        ThreadUtils.sleep(100);
    }
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) JSONObject(org.json.JSONObject) CommandPacket(edu.umass.cs.gnscommon.packets.CommandPacket) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) 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 13 with SignatureException

use of java.security.SignatureException in project GNS by MobilityFirst.

the class SimpleClientExample 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. Connects to a default reconfigurator as specified in gigapaxos.properties file.
    client = new GNSClientCommands();
    try {
        // Create an account guid if one doesn't already exists.
        // The true makes it verbosely print out what it is doing.
        // The password is for future use.
        // Note that lookupOrCreateAccountGuid "cheats" by bypassing the account verification
        // mechanisms.
        accountGuid = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, PASSWORD, true);
    } catch (Exception e) {
        System.out.println("Exception during accountGuid creation: " + e);
        System.exit(1);
    }
    System.out.println("Client connected to GNS");
    // Retrive the GUID using the account id
    String guid = client.lookupGuid(ACCOUNT_ALIAS);
    System.out.println("Retrieved GUID for " + ACCOUNT_ALIAS + ": " + guid);
    // Get the public key from the GNS
    PublicKey publicKey = client.publicKeyLookupFromGuid(guid);
    System.out.println("Retrieved public key: " + publicKey.toString());
    // Use the GuidEntry create an new record in the GNS
    client.fieldUpdate(accountGuid, "homestate", "Florida");
    System.out.println("Added homestate -> Florida record to the GNS for GUID " + accountGuid.getGuid());
    // Retrive that record from the GNS
    String result = client.fieldRead(accountGuid.getGuid(), "homestate", accountGuid);
    System.out.println("Result of read location: " + result);
    System.exit(0);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) PublicKey(java.security.PublicKey) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException)

Example 14 with SignatureException

use of java.security.SignatureException in project GNS by MobilityFirst.

the class CryptoUtils method signDigestOfMessage.

/**
     * Signs a digest of a message using private key of the given guid.
     *
     * @param guidEntry
     * @param message
     * @return a signed digest of the message string encoded as a hex string
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws SignatureException
     * @throws java.io.UnsupportedEncodingException
     *
     * arun: This method need to be synchronized over the signature
     * instance, otherwise it will result in corrupted signatures.
     */
public static String signDigestOfMessage(GuidEntry guidEntry, String message) throws ClientException {
    try {
        Signature signatureInstance = getSignatureInstance();
        synchronized (signatureInstance) {
            signatureInstance.initSign(guidEntry.getPrivateKey());
            // iOS client uses UTF-8 - should switch to ISO-8859-1 to be consistent with
            // secret key version
            signatureInstance.update(message.getBytes("UTF-8"));
            byte[] signedString = signatureInstance.sign();
            // We used to encode this as a hex so we could send it with the html without
            // encoding. Not really necessary anymore for the socket based client,
            // but the iOS client does as well so we need to keep it like this.
            // Also note that the secret based method doesn't do this - it just returns a string
            // using the ISO-8859-1 charset.
            String result = DatatypeConverter.printHexBinary(signedString);
            //String result = ByteUtils.toHex(signedString);
            return result;
        }
    } catch (InvalidKeyException | UnsupportedEncodingException | SignatureException e) {
        throw new ClientException("Error encoding message", e);
    }
}
Also used : Signature(java.security.Signature) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SignatureException(java.security.SignatureException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) InvalidKeyException(java.security.InvalidKeyException)

Example 15 with SignatureException

use of java.security.SignatureException 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)

Aggregations

SignatureException (java.security.SignatureException)329 InvalidKeyException (java.security.InvalidKeyException)171 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)132 Signature (java.security.Signature)132 IOException (java.io.IOException)75 PublicKey (java.security.PublicKey)53 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)39 X509Certificate (java.security.cert.X509Certificate)33 BigInteger (java.math.BigInteger)32 CertificateException (java.security.cert.CertificateException)31 NoSuchProviderException (java.security.NoSuchProviderException)29 PrivateKey (java.security.PrivateKey)25 ByteArrayInputStream (java.io.ByteArrayInputStream)17 KeyFactory (java.security.KeyFactory)15 ArrayList (java.util.ArrayList)15 MySignature1 (org.apache.harmony.security.tests.support.MySignature1)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)12 GeneralSecurityException (java.security.GeneralSecurityException)12 MessageDigest (java.security.MessageDigest)12