Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniBasicKmip.java
package com.p6r.kmip;
import org.junit.*;
import javax.crypto.spec.SecretKeySpec;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.math.BigInteger;
import java.security.spec.*;
public class JniBasicKmip {
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_basicKMIP() {
System.out.println("@Test - JNICall-basicKMIP");
String uid = null;
String[] uidList = null;
String[] attribute = null;
KeyPairIds pi = null;
TransparentKey tk = null;
TransparentSymmetricKey symKey = null;
TransparentRSAPublicKey rsaPubKey = null;
TransparentRSAPrivateKey rsaPrvKey = null;
P6KMIPClient kc = new P6KMIPClient();
try {
kc.initializeLibrary(P6KMIPClient.FLAGS_NONE);
kc.open(_hostName, null);
// [A] Cipher name (AES) from KMIP 1.2 specification, 9.1.3.2.13 Cryptographic Algorithm Enumeration
// -> Cryptographic Usage Mask from KMIP 1.2 specification, 9.1.3.3.1 Cryptographic Usage Mask
uid = kc.createSymmetricKey(KMIPConstants.ALG_AES, 128, (KMIPConstants.USAGE_MASK_ENCRYPT | KMIPConstants.USAGE_MASK_DECRYPT));
System.out.println("\nUID of new AES key: " + uid);
tk = kc.getSymmetricKey(uid);
if (tk instanceof TransparentSymmetricKey) {
symKey = (TransparentSymmetricKey) tk;
SecretKeySpec sks = symKey.getSecretKeySpec();
System.out.println("Secret key, algorithm: " + sks.getAlgorithm() + ", format: " + sks.getFormat() + ", encoded: " + sks.getEncoded() + "\n\n");
assertEquals(sks.getAlgorithm(), "AES");
} else assertEquals(0, 1);
// -> add attributes to the symmetric key we just added so that we can then search for the key by them
kc.addTextAttribute(uid, "Name", "HenryTheMonster12-D");
kc.addTextAttribute(uid, "x-ID", "Java-JNI-basic-test12");
// -> verify that the attributes have been set
attribute = kc.getAttribute(uid, "Name");
assertEquals("HenryTheMonster12-D", attribute[0]);
assertEquals(attribute.length, 1);
attribute = kc.getAttribute(uid, "x-ID");
assertEquals("Java-JNI-basic-test12", attribute[0]);
assertEquals(attribute.length, 1);
// -> show how we can search for a key by its attributes
uidList = kc.locateByTextAttribute("Name", "HenryTheMonster12-D");
assertEquals(uidList[0], uid);
assertEquals(uidList.length, 1);
System.out.println("locateByTextAttribute using Name attribute found uid: " + uidList[0]);
uidList = null;
uidList = kc.locateByTextAttribute("x-ID", "Java-JNI-basic-test12");
assertEquals(uidList[0], uid);
assertEquals(uidList.length, 1);
System.out.println("locateByTextAttribute using Custom attribute found uid: " + uidList[0]);
uidList = null;
// -> no match should be found so return nothing, returns an array of length 0
uidList = kc.locateByTextAttribute("x-ID", "nomatch");
assertEquals(uidList.length, 0);
kc.destroy(uid);
// [B] Here show that we can use an extension enumeration value with this API
// -> some servers may not support this
try {
String uid2 = kc.createSymmetricKey(0x80000004, 128, 0x000C);
assertNotEquals(uid2, null);
System.out.println("Created a symmetric key with a vendor extension algorithm: " + uid2);
kc.destroy(uid2);
} catch (Exception e) {
System.out.println(e.toString());
}
// [C] Test RSA public/private keys
pi = kc.createKeyPair(KMIPConstants.ALG_RSA, 1024, KMIPConstants.USAGE_MASK_VERIFY, KMIPConstants.USAGE_MASK_SIGN);
System.out.println("\nUID of new RSA public key: " + pi.getPublicKeyUID());
System.out.println("UID of new RSA private key: " + pi.getPrivateKeyUID());
attribute = kc.getAttribute(pi.getPublicKeyUID(), "Cryptographic Algorithm");
assertEquals(attribute[0], "RSA");
assertEquals(attribute.length, 1);
tk = kc.getPublicKey(pi.getPublicKeyUID());
if (tk instanceof TransparentRSAPublicKey) {
rsaPubKey = (TransparentRSAPublicKey) tk;
RSAPublicKeySpec rsaSpec = rsaPubKey.getRSAPublicKeySpec();
System.out.println("RSA public key, modulus: " + rsaSpec.getModulus() + "\npublic exponent: " + rsaSpec.getPublicExponent() + "\n");
} else assertEquals(0, 1);
tk = kc.getPrivateKey(pi.getPrivateKeyUID());
if (tk instanceof TransparentRSAPrivateKey) {
rsaPrvKey = (TransparentRSAPrivateKey) tk;
RSAPrivateKeySpec rsaSpec = rsaPrvKey.getRSAPrivateKeySpec();
System.out.println("RSA private key, modulus: " + rsaSpec.getModulus() + "\nprivate exponent: " + rsaSpec.getPrivateExponent() + "\n\n");
} else assertEquals(0, 1);
kc.destroy(pi.getPublicKeyUID());
kc.destroy(pi.getPrivateKeyUID());
kc.close();
kc.freeLibrary();
} catch (Exception e) {
// -> we shoud not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}