Search in sources :

Example 11 with NAEKey

use of com.ingrian.security.nae.NAEKey in project CipherTrust_Application_Protection by thalescpl-io.

the class KMIPDeleteAttributeSample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        usage();
    }
    // add Ingrian provider to the list of JCE providers
    Security.addProvider(new IngrianProvider());
    KMIPSession session = null;
    try {
        // create NAE Session: pass in NAE client certificate information - client key and
        // keystore password
        session = KMIPSession.getSession(new NAEClientCertificate(args[0], args[1].toCharArray()));
        /* This Set<String> collection will hold the unique identifiers of the keys
             * matching the criteria (algorithm = RSA, length=2048
             */
        Set<String> managedObjectIdentifiers;
        /* this KMIPAttributes object will contain the KMIPAttribute(s) and the
             * values to match for the keys being searched for on the server
             * 
             */
        KMIPAttributes locateAttributes = new KMIPAttributes();
        // add CryptographicAlgorithm and length to the attributes to be matched
        locateAttributes.add(KMIPAttribute.CryptographicAlgorithm, Algorithm.rsa);
        locateAttributes.add(KMIPAttribute.CryptographicLength, 2048);
        /* Add a custom KMIP integer attribute at index 0 with the value 1 */
        locateAttributes.add("x-int1", 0, 1);
        /* this is also the sole attribute to be deleted. */
        KMIPAttributes deleteAttributes = new KMIPAttributes();
        deleteAttributes.add("x-int1", 0, 1);
        /* Locate all RSA keys with a length of 2048 and x-int1 = 1 */
        managedObjectIdentifiers = session.locate(locateAttributes);
        if (managedObjectIdentifiers != null) {
            System.out.println("\n\nFound " + managedObjectIdentifiers.size() + " managed objects matching criteria.");
            System.out.println("\n\nKeys with attributes rsa, 2048 and custom attribute x-int=1");
            for (String uid : managedObjectIdentifiers) {
                System.out.println("\n\nManaged Object UniqueIdentifier: \t" + uid);
                Object thingee = session.getManagedObject(uid);
                /* Convert each key into the proper type of object
                     * representing the managed key */
                if ((thingee instanceof NAEPublicKey) || (thingee instanceof NAEPrivateKey) || (thingee instanceof NAESecretKey)) {
                    NAEKey key;
                    if (thingee instanceof NAEPublicKey)
                        key = (NAEPublicKey) thingee;
                    else if (thingee instanceof NAEPrivateKey)
                        key = (NAEPrivateKey) thingee;
                    else
                        key = (NAESecretKey) thingee;
                    System.out.println("\tName: \t" + key.getName());
                    /* delete the x-int1 attribute */
                    key.deleteKMIPAttributes(deleteAttributes);
                }
            }
        }
    } catch (Exception e) {
        System.out.println("The Cause is " + e.getMessage() + ".");
        e.printStackTrace();
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : KMIPAttributes(com.ingrian.security.nae.KMIPAttributes) NAEKey(com.ingrian.security.nae.NAEKey) NAEPrivateKey(com.ingrian.security.nae.NAEPrivateKey) NAESecretKey(com.ingrian.security.nae.NAESecretKey) NAEPublicKey(com.ingrian.security.nae.NAEPublicKey) NAEClientCertificate(com.ingrian.security.nae.NAEClientCertificate) IngrianProvider(com.ingrian.security.nae.IngrianProvider) KMIPSession(com.ingrian.security.nae.KMIPSession)

Example 12 with NAEKey

use of com.ingrian.security.nae.NAEKey in project CipherTrust_Application_Protection by thalescpl-io.

the class KMIPGetCustomAttribute method main.

public static void main(String[] args) throws Exception {
    if (args.length != 4) {
        usage();
    }
    // add Ingrian provider to the list of JCE providers
    Security.addProvider(new IngrianProvider());
    KMIPSession session = null;
    try {
        // Create session to KMIP port based on authentication by an
        // NAEClientCertificate
        session = KMIPSession.getSession(new NAEClientCertificate(args[0], args[1].toCharArray()));
        // KMIPAttribute set to hold unique Key Manager identifiers for
        // located keys
        Set<String> managedObjectIdentifiers;
        // This instance of KMIPAttributes will be used as the KMIP
        // attributes and
        // values to be searched for
        KMIPAttributes locateAttributes = new KMIPAttributes();
        locateAttributes.add(KMIPAttribute.CryptographicAlgorithm, Algorithm.rsa);
        locateAttributes.add(KMIPAttribute.CryptographicLength, 2048);
        // This instance of KMIPAttributes will specify the set of KMIP
        // attributes
        // to be returned from the Key Manager
        // KMIPAttributes addAttributes = new KMIPAttributes();
        // addAttributes.add("x-String", 1, "Hello");
        KMIPAttributes getAttributes = new KMIPAttributes();
        getAttributes.add(KMIPAttribute.ApplicationSpecificInformation);
        // implied
        getAttributes.add(KMIPAttribute.CryptographicAlgorithm);
        // null
        // value
        getAttributes.add(KMIPAttribute.CryptographicLength);
        getAttributes.add(KMIPAttribute.ObjectType);
        getAttributes.add(KMIPAttribute.ContactInformation);
        getAttributes.add(KMIPAttribute.Digest);
        getAttributes.add(KMIPAttribute.InitialDate);
        getAttributes.add(KMIPAttribute.Link);
        getAttributes.add(KMIPAttribute.ObjectGroup);
        String custattrib = args[3];
        if (custattrib.contains("#")) {
            String[] attrs = custattrib.split("#");
            for (String atr : attrs) {
                getAttributes.add(atr);
            }
        } else {
            getAttributes.add(custattrib);
        }
        // Locate the keys with matching attributes
        managedObjectIdentifiers = session.locate(locateAttributes);
        if (managedObjectIdentifiers != null) {
            // for each object found, query all the non-custom attributes
            for (String uid : managedObjectIdentifiers) {
                Object serverManagedObject = session.getManagedObject(uid);
                if (serverManagedObject == null)
                    // not a key
                    continue;
                if (isKey(serverManagedObject)) {
                    // NAEKey is the superclass of public/private and secret
                    // keys
                    NAEKey key;
                    if (serverManagedObject instanceof NAEPublicKey)
                        key = (NAEPublicKey) serverManagedObject;
                    else if (serverManagedObject instanceof NAEPrivateKey)
                        key = (NAEPrivateKey) serverManagedObject;
                    else
                        key = (NAESecretKey) serverManagedObject;
                    locateAttributes.getAttributes();
                    // retrieve and print the key's attributes
                    if (key.getName().equals(args[2])) {
                        // key.addKMIPAttributes(addAttributes);
                        System.out.println("\tName: \t" + key.getName());
                        KMIPAttributes returnedAttributes = getAttrs(key, getAttributes);
                        // printKeyInfo(returnedAttributes);
                        printCustomAttribute(returnedAttributes);
                    }
                } else if (serverManagedObject instanceof KMIPSecretData) {
                    // KMIPSecretData managed objects do not inherit from
                    // NAEKey
                    // coerce to a KMIPSecretData and print the name of the
                    // object
                    System.out.println(((KMIPSecretData) serverManagedObject).getName());
                }
            }
        }
    } catch (Exception e) {
        System.out.println("The Cause is " + e.getMessage() + ".");
        e.printStackTrace();
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : KMIPAttributes(com.ingrian.security.nae.KMIPAttributes) NAEKey(com.ingrian.security.nae.NAEKey) NAEPrivateKey(com.ingrian.security.nae.NAEPrivateKey) NAEPublicKey(com.ingrian.security.nae.NAEPublicKey) KMIPSecretData(com.ingrian.security.nae.KMIPSecretData) NAEClientCertificate(com.ingrian.security.nae.NAEClientCertificate) NAEException(com.ingrian.security.nae.NAEException) IngrianProvider(com.ingrian.security.nae.IngrianProvider) KMIPSession(com.ingrian.security.nae.KMIPSession)

Example 13 with NAEKey

use of com.ingrian.security.nae.NAEKey in project CipherTrust_Application_Protection by thalescpl-io.

the class KMIPGetSample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        usage();
    }
    // add Ingrian provider to the list of JCE providers
    Security.addProvider(new IngrianProvider());
    KMIPSession session = null;
    try {
        // Create session to KMIP port based on authentication by an NAEClientCertificate
        session = KMIPSession.getSession(new NAEClientCertificate(args[0], args[1].toCharArray()));
        // KMIPAttribute set to hold unique Key Manager identifiers for located keys
        Set<String> managedObjectIdentifiers;
        // This instance of KMIPAttributes will be used as the KMIP attributes and
        // values to be searched for
        KMIPAttributes locateAttributes = new KMIPAttributes();
        locateAttributes.add(KMIPAttribute.CryptographicAlgorithm, Algorithm.rsa);
        locateAttributes.add(KMIPAttribute.CryptographicLength, 2048);
        // This instance of KMIPAttributes will specify the set of KMIP attributes
        // to be returned from the Key Manager
        KMIPAttributes getAttributes = new KMIPAttributes();
        getAttributes.add(KMIPAttribute.ApplicationSpecificInformation);
        // implied null value
        getAttributes.add(KMIPAttribute.CryptographicAlgorithm);
        getAttributes.add(KMIPAttribute.CryptographicLength);
        getAttributes.add(KMIPAttribute.ObjectType);
        getAttributes.add(KMIPAttribute.ContactInformation);
        getAttributes.add(KMIPAttribute.Digest);
        getAttributes.add(KMIPAttribute.InitialDate);
        getAttributes.add(KMIPAttribute.Link);
        getAttributes.add(KMIPAttribute.ObjectGroup);
        // Locate the keys with matching attributes
        managedObjectIdentifiers = session.locate(locateAttributes);
        if (managedObjectIdentifiers != null) {
            System.out.println("\n\nFound " + managedObjectIdentifiers.size() + " managed objects matching key Locate criteria.");
            System.out.println("\n\nKeys with attributes rsa, 2048 and their attibutes");
            // for each object found, query all the non-custom attributes
            for (String uid : managedObjectIdentifiers) {
                System.out.println("\n\nManaged Object UniqueIdentifier: \t" + uid);
                Object serverManagedObject = session.getManagedObject(uid);
                // not a key
                if (serverManagedObject == null)
                    continue;
                if (isKey(serverManagedObject)) {
                    // NAEKey is the superclass of public/private and secret keys
                    NAEKey key;
                    if (serverManagedObject instanceof NAEPublicKey)
                        key = (NAEPublicKey) serverManagedObject;
                    else if (serverManagedObject instanceof NAEPrivateKey)
                        key = (NAEPrivateKey) serverManagedObject;
                    else
                        key = (NAESecretKey) serverManagedObject;
                    System.out.println("\tName: \t" + key.getName());
                    // retrieve and print the key's attributes
                    KMIPAttributes returnedAttributes = getAttrs(key, getAttributes);
                    printKeyInfo(returnedAttributes);
                } else if (serverManagedObject instanceof KMIPSecretData) {
                    // KMIPSecretData managed objects do not inherit from NAEKey
                    // coerce to a KMIPSecretData and print the name of the object
                    System.out.println(((KMIPSecretData) serverManagedObject).getName());
                }
            }
        }
    } catch (Exception e) {
        System.out.println("The Cause is " + e.getMessage() + ".");
        e.printStackTrace();
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : KMIPAttributes(com.ingrian.security.nae.KMIPAttributes) NAEKey(com.ingrian.security.nae.NAEKey) NAEPrivateKey(com.ingrian.security.nae.NAEPrivateKey) NAEPublicKey(com.ingrian.security.nae.NAEPublicKey) KMIPSecretData(com.ingrian.security.nae.KMIPSecretData) NAEClientCertificate(com.ingrian.security.nae.NAEClientCertificate) NAEException(com.ingrian.security.nae.NAEException) IngrianProvider(com.ingrian.security.nae.IngrianProvider) KMIPSession(com.ingrian.security.nae.KMIPSession)

Example 14 with NAEKey

use of com.ingrian.security.nae.NAEKey in project CipherTrust_Application_Protection by thalescpl-io.

the class KMIPLocateSample method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        usage();
    }
    // add Ingrian provider to the list of JCE providers
    Security.addProvider(new IngrianProvider());
    KMIPSession session = null;
    try {
        // create NAE Session: pass in NAE Client Certificate clicnt key and keystore password
        session = KMIPSession.getSession(new NAEClientCertificate(args[0], args[1].toCharArray()));
        // This set holds the managed object unique identifiers (UIDs)
        Set<String> managedObjectIdentifiers;
        // Locate keys with crypto algorithm = aes and crypto length = 256
        KMIPAttributes queryAttributes = new KMIPAttributes();
        /* 
             * IMPORTANT-In case of locate by name it is compulsory to pass argument for keyName as below 
             *  [-Name locateKeyName] where locateKeyName will be value of userInput.
             * */
        if (args.length > 3) {
            if (args[2] != null && "-Name".equals(args[2])) {
                queryAttributes.add(new Attribute(KMIPAttribute.Name, args[3]));
            }
        }
        // Have the session locate the keys matching the queryAttributes:
        managedObjectIdentifiers = session.locate(queryAttributes);
        // loop through the UIDs of the matching managed objects
        System.out.println("Total Keys: " + managedObjectIdentifiers.size());
        for (String uid : managedObjectIdentifiers) {
            System.out.println("Managed object Unique Identifier: " + uid);
            // get the objects as Java client NAEKeys or KMIPSecretData objects
            // (Note: Secret Data doesn't have KMIP attributes of
            // algorithm or length, and will not be found by this query,
            // but is included here for completeness.
            byte[] keyMaterial = null;
            Object managedObject = session.getManagedObject(uid);
            // not a key
            if (managedObject == null)
                continue;
            if (managedObject instanceof NAEPublicKey) {
                System.out.println(((NAEPublicKey) managedObject).getName());
                keyMaterial = ((NAEKey) managedObject).export();
            } else if (managedObject instanceof NAEPrivateKey) {
                System.out.println(((NAEPrivateKey) managedObject).getName());
                keyMaterial = ((NAEKey) managedObject).export();
            } else if (managedObject instanceof NAESecretKey) {
                System.out.println(((NAESecretKey) managedObject).getName());
                keyMaterial = ((NAEKey) managedObject).export();
            } else if (managedObject instanceof KMIPSecretData) {
                System.out.println(((KMIPSecretData) managedObject).getName());
                keyMaterial = ((KMIPSecretData) managedObject).export();
            } else if (managedObject instanceof NAECertificate) {
                System.out.println(((NAECertificate) managedObject).getName());
                keyMaterial = ((NAECertificate) managedObject).certificateExport();
            }
            System.out.println("Key Material = " + TTLVUtil.toHexString(keyMaterial));
        }
    } catch (Exception e) {
        System.out.println("The Cause is " + e.getMessage() + ".");
        e.printStackTrace();
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : KMIPAttributes(com.ingrian.security.nae.KMIPAttributes) NAEKey(com.ingrian.security.nae.NAEKey) NAEPrivateKey(com.ingrian.security.nae.NAEPrivateKey) Attribute(com.ingrian.internal.kmip.api.Attribute) KMIPAttribute(com.ingrian.security.nae.KMIPAttributeNames.KMIPAttribute) NAESecretKey(com.ingrian.security.nae.NAESecretKey) NAECertificate(com.ingrian.security.nae.NAECertificate) NAEPublicKey(com.ingrian.security.nae.NAEPublicKey) KMIPSecretData(com.ingrian.security.nae.KMIPSecretData) NAEClientCertificate(com.ingrian.security.nae.NAEClientCertificate) IngrianProvider(com.ingrian.security.nae.IngrianProvider) KMIPSession(com.ingrian.security.nae.KMIPSession)

Example 15 with NAEKey

use of com.ingrian.security.nae.NAEKey in project CipherTrust_Application_Protection by thalescpl-io.

the class KMIPModifySample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        usage();
    }
    // add Ingrian provider to the list of JCE providers
    Security.addProvider(new IngrianProvider());
    // get the list of all registered JCE providers
    Provider[] providers = Security.getProviders();
    for (int i = 0; i < providers.length; i++) System.out.println(providers[i].getInfo());
    KMIPSession session = null;
    try {
        // create a KMIPSession: pass in NAE client X.509 key and keyStore password
        session = KMIPSession.getSession(new NAEClientCertificate(args[0], args[1].toCharArray()));
        // create key KMIPAttribute object with a list of attributes to match
        Set<String> managedObjectIdentifiers;
        KMIPAttributes locateAttributes = new KMIPAttributes();
        locateAttributes.add(KMIPAttribute.CryptographicAlgorithm, Algorithm.rsa);
        locateAttributes.add(KMIPAttribute.CryptographicLength, 2048);
        KMIPAttributes getAttributes = new KMIPAttributes();
        getAttributes.add(KMIPAttribute.Name);
        managedObjectIdentifiers = session.locate(locateAttributes);
        if (managedObjectIdentifiers != null) {
            System.out.println("\n\nFound " + managedObjectIdentifiers.size() + " managed objects matching criteria.");
            System.out.println("\n\nKeys with attributes rsa, 2048 and object group");
            for (String uid : managedObjectIdentifiers) {
                System.out.println("\n\nManaged Object UniqueIdentifier: \t" + uid);
                Object managedObject = session.getManagedObject(uid);
                // not a key
                if (managedObject == null)
                    continue;
                if ((managedObject instanceof NAEPublicKey) || (managedObject instanceof NAEPrivateKey) || (managedObject instanceof NAESecretKey)) {
                    NAEKey key;
                    if (managedObject instanceof NAEPublicKey)
                        key = (NAEPublicKey) managedObject;
                    else if (managedObject instanceof NAEPrivateKey)
                        key = (NAEPrivateKey) managedObject;
                    else
                        key = (NAESecretKey) managedObject;
                    System.out.println("\tName: \t" + key.getName());
                    // Retrieve a KMIP attribute - in this case, Name.
                    KMIPAttributes returnedAttributes = key.getKMIPAttributes(getAttributes);
                    KMIPNameAttribute name = returnedAttributes.getNameAttribute();
                    System.out.println("Name attribute: " + name.getNameValue().getNameValue());
                    // Modify the Application Specific Information for this key - if it has any
                    KMIPAttributes modAttributes = new KMIPAttributes();
                    String ts = timestamp();
                    modAttributes.add(new KMIPApplicationSpecificInformation("namespace-" + ts, ts), 0);
                    try {
                        // throws NAE error if the key does not already have attribute being modified
                        key.modifyKMIPAttributes(modAttributes);
                    } catch (NAEException nae) {
                        if (!nae.getMessage().contains("Object does not have the specified attribute"))
                            throw nae;
                    }
                } else if (managedObject instanceof KMIPSecretData) {
                    System.out.println(((KMIPSecretData) managedObject).getName());
                }
            }
        }
    } catch (Exception e) {
        System.out.println("The Cause is " + e.getMessage() + ".");
        e.printStackTrace();
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : KMIPAttributes(com.ingrian.security.nae.KMIPAttributes) NAEException(com.ingrian.security.nae.NAEException) NAEKey(com.ingrian.security.nae.NAEKey) NAEPrivateKey(com.ingrian.security.nae.NAEPrivateKey) NAESecretKey(com.ingrian.security.nae.NAESecretKey) NAEPublicKey(com.ingrian.security.nae.NAEPublicKey) KMIPSecretData(com.ingrian.security.nae.KMIPSecretData) NAEClientCertificate(com.ingrian.security.nae.NAEClientCertificate) NAEException(com.ingrian.security.nae.NAEException) IngrianProvider(com.ingrian.security.nae.IngrianProvider) Provider(java.security.Provider) IngrianProvider(com.ingrian.security.nae.IngrianProvider) KMIPSession(com.ingrian.security.nae.KMIPSession) KMIPApplicationSpecificInformation(com.ingrian.security.nae.KMIPApplicationSpecificInformation) KMIPNameAttribute(com.ingrian.security.nae.KMIPNameAttribute)

Aggregations

NAEKey (com.ingrian.security.nae.NAEKey)32 IngrianProvider (com.ingrian.security.nae.IngrianProvider)25 NAESession (com.ingrian.security.nae.NAESession)20 Cipher (javax.crypto.Cipher)12 KMIPAttributes (com.ingrian.security.nae.KMIPAttributes)10 KMIPSession (com.ingrian.security.nae.KMIPSession)10 NAEClientCertificate (com.ingrian.security.nae.NAEClientCertificate)10 NAEParameterSpec (com.ingrian.security.nae.NAEParameterSpec)10 Provider (java.security.Provider)10 KeyGenerator (javax.crypto.KeyGenerator)9 NAEException (com.ingrian.security.nae.NAEException)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)8 NAEPrivateKey (com.ingrian.security.nae.NAEPrivateKey)7 NAEPublicKey (com.ingrian.security.nae.NAEPublicKey)7 NAESecretKey (com.ingrian.security.nae.NAESecretKey)7 SecretKey (javax.crypto.SecretKey)7 KMIPSecretData (com.ingrian.security.nae.KMIPSecretData)5 NAECipher (com.ingrian.security.nae.NAECipher)5 NAESecureRandom (com.ingrian.security.nae.NAESecureRandom)5 GCMParameterSpec (com.ingrian.security.nae.GCMParameterSpec)3