Search in sources :

Example 6 with KMIPSecretData

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

the class KMIPCertLocateSample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 3) {
        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()));
        // import the certificate
        NAEParameterSpec spec = new NAEParameterSpec(args[2], 1024, (KMIPAttributes) null, session);
        byte[] c = Hex.decodeHex(certBytes.toCharArray());
        NAECertificate.importCertificate(c, null, spec);
        // This set holds the managed object unique identifiers (UIDs)
        Set<String> managedObjectIdentifiers;
        // Locate managed objects with ObjectType Certificate and crypto length = 2048
        // and Issuer Distinguished Name = "CN=KMIP,OU=OASIS,O=TEST,C=US"
        // by adding the KMIPAttribute name and the value to a KMIPAttributes
        // object
        KMIPAttributes queryAttributes = new KMIPAttributes();
        queryAttributes.add(KMIPAttribute.CryptographicLength, 2048);
        queryAttributes.add(KMIPAttribute.ObjectType, ObjectType.ObjectTypes.Certificate);
        // Have the session locate the keys matching the queryAttributes:
        managedObjectIdentifiers = session.locate(queryAttributes);
        System.out.println("Managed objects with attributes rsa, 2048:");
        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.
            Object managedObject = session.getManagedObject(uid);
            if (managedObject instanceof KMIPTemplate)
                break;
            if (managedObject instanceof NAEPublicKey)
                System.out.println(((NAEPublicKey) managedObject).getName());
            else if (managedObject instanceof NAEPrivateKey)
                System.out.println(((NAEPrivateKey) managedObject).getName());
            else if (managedObject instanceof NAESecretKey)
                System.out.println(((NAESecretKey) managedObject).getName());
            else if (managedObject instanceof KMIPSecretData) {
                System.out.println(((KMIPSecretData) managedObject).getName());
            } else if (managedObject instanceof NAECertificate) {
                System.out.println("Object is a certificate");
                System.out.println(((NAECertificate) managedObject).getName());
            }
        }
    } catch (Exception e) {
        System.out.println("The Cause is " + e.getMessage() + ".");
        e.printStackTrace();
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : NAEParameterSpec(com.ingrian.security.nae.NAEParameterSpec) KMIPAttributes(com.ingrian.security.nae.KMIPAttributes) NAEPrivateKey(com.ingrian.security.nae.NAEPrivateKey) 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) KMIPTemplate(com.ingrian.security.nae.KMIPTemplate)

Example 7 with KMIPSecretData

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

the class KMIPSecretDataGetCustomAttributeSample 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());
    String secretDataName = args[2];
    String custattrib = args[3];
    // create NAE Session: pass in Key Manager user name and password
    KMIPSession session = KMIPSession.getSession(new NAEClientCertificate(args[0], args[1].toCharArray()));
    KMIPAttributes getAttributes = new KMIPAttributes();
    if (custattrib.contains("#")) {
        String[] attrs = custattrib.split("#");
        for (String atr : attrs) {
            getAttributes.add(atr);
        }
    } else {
        getAttributes.add(custattrib);
    }
    try {
        // create the secret data object as a KMIP secret data Password type
        KMIPSecretData secretDataManagedObject = new KMIPSecretData(secretDataName, KMIPSecretData.SecretDataType.Password, session);
        KMIPAttributes returnedAttributes = secretDataManagedObject.getKMIPAttributes(getAttributes);
        printCustomAttribute(returnedAttributes);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : KMIPAttributes(com.ingrian.security.nae.KMIPAttributes) 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 8 with KMIPSecretData

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

the class KMIPSecretDataSample method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        usage();
    }
    String keyName = args.length == 3 ? args[2] : "KMIPSecretData";
    // add Ingrian provider to the list of JCE providers
    Security.addProvider(new IngrianProvider());
    KMIPSession session = KMIPSession.getSession(new NAEClientCertificate(args[0], args[1].toCharArray()));
    try {
        // generate the secret data (the bytes of a public key)
        // For IBM Java, change the provider from "SUN/SunRsaSign" to "IBMJCE"
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
        keyGen.initialize(1024, random);
        KeyPair keyPair = keyGen.generateKeyPair();
        PublicKey pub = keyPair.getPublic();
        byte[] data = pub.getEncoded();
        // create NAE Session: pass in Key Manager user name and password
        // KMIPSession session  = KMIPSession.getSession(new NAEClientCertificate( args[0],  args[1]));
        // create secret data managed object ParameterSpec
        KMIPAttributes initialAttributes = new KMIPAttributes();
        initialAttributes.add(KMIPAttribute.CryptographicUsageMask, (int) (UsageMask.Verify.getValue()));
        NAEParameterSpec spec = new NAEParameterSpec(keyName, 1024, (KMIPAttributes) initialAttributes, session);
        // create the secret data object as a KMIP secret data Password type
        KMIPSecretData secretDataManagedObject = new KMIPSecretData(keyName, KMIPSecretData.SecretDataType.Password, session);
        // register the secret data bytes
        secretDataManagedObject.register(data, spec);
        // now export() a copy of the secret data back from the Key Manager
        byte[] exportedSecretData = secretDataManagedObject.export();
        // compare the original and exported bytes
        if ((exportedSecretData != null) && Arrays.equals(exportedSecretData, data))
            System.out.println("Exported secret data equals original");
        else {
            System.out.println("Uh-oh!");
        }
        // print the bytes and close the session
        System.out.println("original: " + TTLVUtil.toHexString(data));
        System.out.println("exported: " + TTLVUtil.toHexString(exportedSecretData));
    } catch (Exception e) {
        System.out.println("The Cause is " + e.getMessage() + ".");
        e.printStackTrace();
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : KeyPair(java.security.KeyPair) KMIPAttributes(com.ingrian.security.nae.KMIPAttributes) NAEParameterSpec(com.ingrian.security.nae.NAEParameterSpec) PublicKey(java.security.PublicKey) SecureRandom(java.security.SecureRandom) KMIPSecretData(com.ingrian.security.nae.KMIPSecretData) KeyPairGenerator(java.security.KeyPairGenerator) NAEClientCertificate(com.ingrian.security.nae.NAEClientCertificate) IngrianProvider(com.ingrian.security.nae.IngrianProvider) KMIPSession(com.ingrian.security.nae.KMIPSession)

Aggregations

IngrianProvider (com.ingrian.security.nae.IngrianProvider)8 KMIPAttributes (com.ingrian.security.nae.KMIPAttributes)8 KMIPSecretData (com.ingrian.security.nae.KMIPSecretData)8 KMIPSession (com.ingrian.security.nae.KMIPSession)8 NAEClientCertificate (com.ingrian.security.nae.NAEClientCertificate)8 NAEPrivateKey (com.ingrian.security.nae.NAEPrivateKey)6 NAEPublicKey (com.ingrian.security.nae.NAEPublicKey)6 NAEKey (com.ingrian.security.nae.NAEKey)5 NAEException (com.ingrian.security.nae.NAEException)4 NAESecretKey (com.ingrian.security.nae.NAESecretKey)4 NAECertificate (com.ingrian.security.nae.NAECertificate)2 NAEParameterSpec (com.ingrian.security.nae.NAEParameterSpec)2 Attribute (com.ingrian.internal.kmip.api.Attribute)1 KMIPApplicationSpecificInformation (com.ingrian.security.nae.KMIPApplicationSpecificInformation)1 KMIPAttribute (com.ingrian.security.nae.KMIPAttributeNames.KMIPAttribute)1 KMIPNameAttribute (com.ingrian.security.nae.KMIPNameAttribute)1 KMIPTemplate (com.ingrian.security.nae.KMIPTemplate)1 KeyPair (java.security.KeyPair)1 KeyPairGenerator (java.security.KeyPairGenerator)1 Provider (java.security.Provider)1