Search in sources :

Example 61 with GNSClientCommands

use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.

the class KeyLookup method parse.

@Override
public void parse(String commandText) throws Exception {
    try {
        StringTokenizer st = new StringTokenizer(commandText.trim());
        String alias;
        switch(st.countTokens()) {
            case 0:
                if (!module.isCurrentGuidSetAndVerified()) {
                    return;
                } else {
                    alias = module.getCurrentGuid().getEntityName();
                }
                break;
            case 1:
                alias = st.nextToken();
                break;
            default:
                wrongArguments();
                return;
        }
        GNSClientCommands gnsClient = module.getGnsClient();
        PublicKey pk;
        if (!StringUtil.isValidGuidString(alias)) {
            pk = gnsClient.publicKeyLookupFromAlias(alias);
        } else {
            pk = gnsClient.publicKeyLookupFromGuid(alias);
        }
        console.printString(alias + " public key is " + DatatypeConverter.printHexBinary(pk.getEncoded()));
        //console.printString(alias + " public key is " + ByteUtils.toHex(pk.getEncoded()));
        console.printNewline();
    } catch (IOException | ClientException e) {
        console.printString("Failed to access GNS ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) PublicKey(java.security.PublicKey) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException)

Example 62 with GNSClientCommands

use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.

the class PrivateKeyImport method parse.

@Override
public void parse(String commandText) throws Exception {
    GNSClientCommands gnsClient = module.getGnsClient();
    try {
        StringTokenizer st = new StringTokenizer(commandText.trim());
        if (st.countTokens() != 2) {
            wrongArguments();
            return;
        }
        String aliasName = st.nextToken();
        String filename = st.nextToken();
        PrivateKey privateKey = KeyPairUtils.getPrivateKeyFromPKCS8File(filename);
        String guid = gnsClient.lookupGuid(aliasName);
        if (guid == null) {
            console.printString("Alias " + aliasName + " is not in the GNS");
            console.printNewline();
            return;
        }
        PublicKey publicKey = gnsClient.publicKeyLookupFromGuid(guid);
        GuidEntry guidEntry = new GuidEntry(aliasName, guid, publicKey, privateKey);
        KeyPairUtils.saveKeyPair(module.getGnsInstance(), guidEntry.getEntityName(), guidEntry.getGuid(), new KeyPair(guidEntry.getPublicKey(), guidEntry.getPrivateKey()));
        console.printString("Private key for " + guidEntry.getEntityName() + " read from " + filename + " and saved in local preferences.");
    } catch (IOException | InvalidKeySpecException | NoSuchAlgorithmException | ClientException e) {
        console.printString("Failed to save keys ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) KeyPair(java.security.KeyPair) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 63 with GNSClientCommands

use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.

the class GuidCreate method parse.

@Override
public void parse(String commandText) throws Exception {
    GuidEntry accountGuid = module.getCurrentGuid();
    StringTokenizer st = new StringTokenizer(commandText.trim());
    if ((st.countTokens() != 1)) {
        wrongArguments();
        return;
    }
    String aliasName = st.nextToken();
    try {
        GNSClientCommands gnsClient = module.getGnsClient();
        try {
            gnsClient.lookupGuid(aliasName);
            printString("Alias " + aliasName + " already exists.\n");
            return;
        } catch (ClientException expected) {
        // The alias does not exists, that's good, let's create it
        }
        if (!module.isSilent()) {
            printString("Looking for alias " + aliasName + " GUID and certificates...\n");
        }
        GuidEntry myGuid = KeyPairUtils.getGuidEntry(module.getGnsInstance(), aliasName);
        if (myGuid != null) {
            try {
                PublicKey pk = gnsClient.publicKeyLookupFromGuid(myGuid.getGuid());
                if (myGuid.getPublicKey().equals(pk)) {
                    // We already have the key but the alias is missing in the GNS,
                    // re-add the alias
                    printString("Alias info found locally but missing in GNS, re-adding alias to the GNS\n");
                    gnsClient.guidCreate(myGuid, aliasName);
                } else {
                    printString("Old certificates found locally and not matching key in GNS, deleting local keys\n");
                    KeyPairUtils.removeKeyPair(module.getGnsInstance(), aliasName);
                }
            } catch (InvalidGuidException e) {
                KeyPairUtils.removeKeyPair(module.getGnsInstance(), aliasName);
            }
        }
        if (!module.isSilent()) {
            printString("Generating new GUID and keys for account " + accountGuid.getEntityName() + " \n");
        }
        myGuid = gnsClient.guidCreate(accountGuid, aliasName);
        if (!module.isSilent()) {
            printString("Created GUID " + myGuid.getGuid() + "\n");
        }
        if (module.getCurrentGuid() == null) {
            module.setCurrentGuidAndCheckForVerified(myGuid);
            module.setPromptString(ConsoleModule.CONSOLE_PROMPT + aliasName + ">");
        }
    } catch (IOException | ClientException e) {
        printString("Failed to create new guid ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) PublicKey(java.security.PublicKey) InvalidGuidException(edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 64 with GNSClientCommands

use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.

the class CreateDnsRecord 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 {
    if (args.length != 2) {
        System.out.println("Usage: edu.umass.cs.gnsclient.examples.CreateDnsRecord <host> <address>");
        System.exit(0);
    }
    client = new GNSClientCommands();
    try {
        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");
    createAnARecord(args[0], args[1]);
    client.close();
    System.exit(0);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) 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 65 with GNSClientCommands

use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.

the class CreateSomeRecordsForDns 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 {
    client = new GNSClientCommands();
    try {
        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");
    createAnARecord("westy.gns.", "173.236.153.191");
    createAnARecord("mobilityfirst.gns.", "128.119.240.104");
    client.close();
    System.exit(0);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) 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)

Aggregations

GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)70 IOException (java.io.IOException)55 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)50 StringTokenizer (java.util.StringTokenizer)35 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)28 JSONArray (org.json.JSONArray)13 JSONException (org.json.JSONException)11 DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)9 JSONObject (org.json.JSONObject)9 InvalidKeyException (java.security.InvalidKeyException)8 SignatureException (java.security.SignatureException)8 GNSClient (edu.umass.cs.gnsclient.client.GNSClient)6 PublicKey (java.security.PublicKey)6 HashSet (java.util.HashSet)6 BeforeClass (org.junit.BeforeClass)6 RandomString (edu.umass.cs.gnscommon.utils.RandomString)5 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5