Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniStructAttributesKmip.java
package com.p6r.kmip;
import org.junit.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.security.*;
import java.security.spec.*;
public class JniStructAttributesKmip {
private static final String _hostName = "kmiptest01.p6r.com";
@BeforeClass
public static void oneTimeSetUp() {
// NOOP
System.out.println("@BeforeClass - oneTimeSetUp");
}
@AfterClass
public static void oneTimeTearDown() {
// NOOP
System.out.println("@AfterClass - oneTimeTearDown");
}
@Before
public void setUp() {
// NOOP
System.out.println("@Before - setUp");
}
@After
public void tearDown() {
// NOOP
System.out.println("@After - tearDown");
}
@Test
public void JNICall_structAttributesKMIP() {
System.out.println("@Test - JNICall-structAttributesKMIP");
String uid = null;
String prvUid = null;
String pubUid = null;
StructAttribute complex;
String[] attribute = null;
P6KMIPClient kc = new P6KMIPClient();
try {
kc.initializeLibrary(P6KMIPClient.FLAGS_NONE);
kc.open(_hostName, null);
// [A] Create a symmetric key and use it to test out attributes
uid = kc.createSymmetricKey(KMIPConstants.ALG_AES, 128, (KMIPConstants.USAGE_MASK_ENCRYPT | KMIPConstants.USAGE_MASK_DECRYPT));
System.out.println("UID of new AES key: " + uid);
complex = new StructAttribute();
complex.setAttributeName("Alternative Name");
complex.setEnumeration(KMIPConstants.ALTNAME_IPADDRESS);
complex.setAttributeValue("192.168.72.1");
kc.addStructAttribute(uid, complex);
complex.setEnumeration(KMIPConstants.ALTNAME_URI);
complex.setAttributeValue("https://www.p6r.com/software/skc.html");
kc.addStructAttribute(uid, complex);
complex.setEnumeration(KMIPConstants.ALTNAME_SERIAL_NUMBER);
complex.setAttributeValue("240F17B368CA0D26");
kc.addStructAttribute(uid, complex);
System.out.println("Alternative Name attribute supports multiple instances");
attribute = kc.getAttribute(uid, "Alternative Name");
assertEquals(attribute.length, 3);
for( int i=0; i < attribute.length; i++ ) {
System.out.println( "attribute " + i + "> " + attribute[i]);
}
// -> lets try to change the URL on the 2nd instance of the alternative name attribute
complex.setEnumeration(KMIPConstants.ALTNAME_URI);
complex.setAttributeValue("https://www.p6r.com");
kc.modifyStructAttribute(uid, complex, 1);
System.out.println("\nAlternative Name attribute supports multiple instances, modified");
attribute = kc.getAttribute(uid, "Alternative Name");
assertEquals(attribute.length, 3);
for( int i=0; i < attribute.length; i++ ) {
System.out.println( "attribute " + i + "> " + attribute[i]);
}
kc.destroy(uid);
// [B] Use the link attribute to associate to registered public / private keys
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair RSAkeyPair = kpg.genKeyPair();
PrivateKey prvKey = RSAkeyPair.getPrivate();
PublicKey pubKey = RSAkeyPair.getPublic();
byte[] privateKeyBytes = prvKey.getEncoded();
byte[] publicKeyBytes = pubKey.getEncoded();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
prvUid = kc.registerPrivateKey(KMIPConstants.ALG_RSA, 1024, KMIPConstants.USAGE_MASK_SIGN, null, privateKeySpec.getEncoded());
System.out.println("\nUID of registered RSA private key: " + prvUid);
assertNotEquals(prvUid, null);
pubUid = kc.registerPublicKey(KMIPConstants.ALG_RSA, 1024, KMIPConstants.USAGE_MASK_VERIFY, null, publicKeySpec.getEncoded());
System.out.println("UID of registered RSA public key: " + pubUid);
assertNotEquals(pubUid, null);
// -> place a Link attribute on the public key which points to its private key counter part
complex = new StructAttribute();
complex.setAttributeName("Link");
complex.setEnumeration(KMIPConstants.LINK_PRIVATEKEY);
complex.setAttributeValue(prvUid);
kc.addStructAttribute(pubUid, complex);
System.out.println("Show that the link was set on the public key");
attribute = kc.getAttribute(pubUid, "Link");
assertEquals(attribute.length, 1);
for( int i=0; i < attribute.length; i++ ) {
System.out.println( "attribute " + i + "> " + attribute[i]);
}
kc.addTextAttribute(pubUid, "x-ID-5", "Test-Attributes-5");
System.out.println("\nShow ALL attributes associated with the public key");
attribute = kc.getAllAttributes(pubUid);
assertNotEquals(attribute, null);
for( int i=0; i < attribute.length; i++ ) {
System.out.println( "attribute " + i + "> " + attribute[i]);
}
kc.destroy(prvUid);
kc.destroy(pubUid);
kc.close();
kc.freeLibrary();
} catch (Exception e) {
// -> we shoud not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}