Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Java Native Interface (JNI) Examples

KSL JNI KMIP Example 1 - Multi-Batch Request

This example demonstrates a request message that contains two Create operations, one Query, and one Discover Version KMIP operations.

JniBasicKmip.java

package com.p6r.kmipserverlib;
import org.junit.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class JniBasicKmip {
@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");
// -> this parser is multi-thread safe by using JNI monitor locking
// -> use one parser object per server thread is recommended
P6KMIPServerLib sl = new P6KMIPServerLib();
// -> KMIP 1.3 TTLV message with 4 batch items generated by P6R's Secure KMIP Client (SKC)
byte[] testMessage = {
0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x03, (byte) 0x98, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x00, (byte) 0x88, 0x42, 0x00, 0x69, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x6a, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x50, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x01, (byte) 0xf4, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x07, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x42, 0x00, 0x0e, 0x05, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x10, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x42, 0x00, (byte) 0x92, 0x09, 0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x00, 0x59, (byte) 0xdc, 0x27, 0x66, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x00, (byte) 0xe8,
0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0x93, 0x08, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x00, (byte) 0xc0, 0x42, 0x00, 0x57, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0x91, 0x01, 0x00, 0x00, 0x00, (byte) 0xa8,
0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x30, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x17, 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x20, 0x41, 0x6c,
0x67, 0x6f, 0x72, 0x69, 0x74, 0x68, 0x6d, 0x00, 0x42, 0x00, 0x0b, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x30,
0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x14, 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x20, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x0b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, (byte) 0x80, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x30, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x18,
0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x20, 0x55, 0x73, 0x61, 0x67, 0x65, 0x20, 0x4d, 0x61, 0x73, 0x6b, 0x42, 0x00, 0x0b, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x01, 0x20, 0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, (byte) 0x93, 0x08, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x00, (byte) 0xf8, 0x42, 0x00, 0x57, 0x05, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0x91, 0x01, 0x00, 0x00, 0x00, (byte) 0xe0, 0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x30, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x17,
0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x20, 0x41, 0x6c, 0x67, 0x6f, 0x72, 0x69, 0x74, 0x68, 0x6d, 0x00, 0x42, 0x00, 0x0b, 0x05, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x30, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x14, 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72,
0x61, 0x70, 0x68, 0x69, 0x63, 0x20, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x30, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x18, 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x20, 0x55, 0x73,
0x61, 0x67, 0x65, 0x20, 0x4d, 0x61, 0x73, 0x6b, 0x42, 0x00, 0x0b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x30,
0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x0b, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0b, 0x07, 0x00, 0x00, 0x00, 0x0b,
0x31, 0x20, 0x32, 0x20, 0x33, 0x20, 0x34, 0x20, 0x35, 0x20, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x00, (byte) 0xb8, 0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0x93, 0x08, 0x00, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x00, (byte) 0x90,
0x42, 0x00, 0x74, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x74, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x74, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x74, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x74, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x74, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x74, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x74, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x74, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x00, 0x28, 0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0x93, 0x08, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x00, 0x00};
// example of a broken TTLV message, the unique identifier encoding at the end of the message has the wrong length value, was taken from Db2
byte[] testMessageBroken = {
0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x00, (byte)0xc0, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x00, 0x78,
0x42, 0x00, 0x69, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x6a, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0c, 0x01, 0x00, 0x00, 0x00, 0x38,
0x42, 0x00, 0x23, 0x01, 0x00, 0x00, 0x00, 0x30, 0x42, 0x00, 0x24, 0x05, 0x00, 0x00, 0x00, 0x04,
(byte)0x80, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x25, 0x08, 0x00, 0x00, 0x00, 0x18,
0x54, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x10, 0x54, 0x00, 0x04, 0x07, 0x00, 0x00, 0x00, 0x03,
0x44, 0x42, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x00, 0x38,
0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, (byte)0x94, 0x07, 0x00, 0x00, 0x00, 0x18,
0x41, 0x57, 0x4d, 0x6b, 0x73, 0x65, 0x70, 0x69, 0x4a, 0x54, 0x62, 0x5a, 0x49, 0x73, 0x6f, 0x5f,
0x30, 0x58, 0x6f, 0x72, 0x00, 0x00, 0x00, 0x00
};
try {
sl.initializeLibrary(P6KMIPServerLib.FLAGS_ALLLOG);
String libVersion = sl.getLibraryVersion();
System.out.println(libVersion);
// -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
// -> the type of message: TTLC, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
sl.setMessageBuffer(testMessage, KMIPConstants.FORMAT_TTLV);
// -> now we can pull parts of the request message apart, this can be done over and over again if desired
RequestHeader rh = sl.getRequestHeader();
assertEquals(true, rh.getAsynchIndicator());
assertEquals(500, rh.getMaxResponseSize());
assertEquals("1.3", rh.getProtocolVersion());
assertEquals(KMIPConstants.BATCHOPTION_UNDO, rh.getBatchErrorOption());
assertEquals(true, rh.getBatchOrderOption());
assertEquals(4, rh.getBatchCount());
String msgDate = rh.getTimeStamp().toString();
assertEquals("Mon Oct 09 18:50:30 PDT 2017", msgDate );
// -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
for (int i = 0; i < rh.getBatchCount(); i++) {
BatchItem bi = sl.getBatchItem(i + 1);
if (bi instanceof CreateBatchItem) {
CreateBatchItem cb = (CreateBatchItem) bi;
assertEquals(KMIPConstants.OBJECT_SYMMETRICKEY, cb.getObjectType());
assertEquals(i + 1, cb.getUniqueBatchId()[0]);
String[] attributes = cb.getAttributes();
if (3 == attributes.length) {
assertEquals("Cryptographic Algorithm: 3", attributes[0]); // AES
assertEquals("Cryptographic Length: 128", attributes[1]); // 128 bits
assertEquals("Cryptographic Usage Mask: c", attributes[2]); // 0x0C is ENCRYPT and DECRYPT
} else if (4 == attributes.length) {
assertEquals("Cryptographic Algorithm: 26", attributes[0]); // ECC
assertEquals("Cryptographic Length: 256", attributes[1]); // 256 bits
assertEquals("Cryptographic Usage Mask: 3", attributes[2]); // 0x03 is SIGN and VERIFY
assertEquals("Description: 1 2 3 4 5 6", attributes[3]); // attribute added to KMIP 1.3
} else {
assertEquals(0, attributes.length);
}
} else if (bi instanceof QueryBatchItem) {
QueryBatchItem qb = (QueryBatchItem) bi;
assertEquals(3, qb.getUniqueBatchId()[0]);
int[] queryFunctions = qb.getQueryFunctions();
assertEquals(9, queryFunctions.length);
assertEquals(KMIPConstants.QUERY_OPERATIIONS, queryFunctions[0]);
assertEquals(KMIPConstants.QUERY_OBJECTS, queryFunctions[1]);
assertEquals(KMIPConstants.QUERY_SERVERINFORMATION, queryFunctions[2]);
assertEquals(KMIPConstants.QUERY_ATTENSTATIONTYPES, queryFunctions[3]);
assertEquals(KMIPConstants.QUERY_CAPABILITIES, queryFunctions[4]);
assertEquals(KMIPConstants.QUERY_PROFILES, queryFunctions[5]);
assertEquals(KMIPConstants.QUERY_VALIDATIONS, queryFunctions[6]);
assertEquals(KMIPConstants.QUERY_EXTENSIONLIST, queryFunctions[7]);
assertEquals(KMIPConstants.QUERY_REGMETHODS, queryFunctions[8]);
} else if (bi instanceof VersionBatchItem) {
// -> in the above message the client sent no versions
VersionBatchItem vb = (VersionBatchItem) bi;
assertEquals(4, vb.getUniqueBatchId()[0]);
ProtocolVersion[] versions = vb.getProtocolVersions();
if (null != versions) assertEquals(0,1);
}
}
sl.freeLibrary();
} catch (Exception e) {
// -> we shoud not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}

KSL JNI KMIP Example 10 - Get Responses

This example demonstrates a response with a Symmetric Key, a Certificate, and a Wrapped Symmetric Key with a detailed Key Wrapping Data KMIP structure as part of the response. The response is generated in TTLV, XML, and JSON.

JniResponse4Kmip.java

package com.p6r.kmipserverlib;
import org.junit.*;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import static org.junit.Assert.assertEquals;
public class JniResponse4Kmip {
@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_Response4KMIP() {
System.out.println("@Test - JNICall-Respnse4KMIP");
P6KMIPServerLib sl = new P6KMIPServerLib();
try {
sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
// [A] Generate a response message in TTLV
sl.startResponseMessage(5, KMIPConstants.FORMAT_TTLV, 2);
byte[] batchId1 = {0x01, 0x02, 0x03, 0x04};
byte[] batchId2 = {0x01, 0x02, 0x03, (byte) 0xF4};
byte[] batchId3 = {0x01, 0x77 };
byte[] batchId4 = {0x33, 0x77 };
byte[] batchId5 = {0x33, 0x00, 0x02, 0x77 };
byte[] fakeKeyMaterial = {0x7F, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
byte[] fakeECKeyBytes = { 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, (byte)0x91, 0x2a, 0x0e, 0x20, (byte)0x8f, 0x5d, (byte)0xd7, 0x34, (byte)0xb1, (byte)0x85, 0x18, 0x47,
(byte)0xe4, 0x65, (byte)0x9c, 0x45, (byte)0x88, (byte)0xb1, 0x6d, (byte)0x9f, 0x3b, (byte)0xc1, (byte)0xad, (byte)0xa5, 0x10, (byte)0xea, 0x2c, (byte)0x96, (byte)0x9d, (byte)0xaf, (byte)0x9e,
(byte)0xf4, (byte)0xa0, 0x0a, 0x06, 0x08, 0x2a, (byte)0x86, 0x48, (byte)0xce, 0x3d, 0x03, 0x01, 0x07, (byte)0xa1, 0x44, 0x03, 0x42, 0x00, 0x04,
(byte)0xb3, 0x44, 0x39, 0x2f, (byte)0xb7, 0x2a, (byte)0xa2, 0x67, (byte)0xf8, 0x6d, 0x7d, (byte)0x83, 0x7d, (byte)0xe1, (byte)0xd9, 0x6c, (byte)0xe9, 0x60, (byte)0xff,
(byte)0xc0, 0x5c, (byte)0xee, (byte)0xf3, (byte)0xf1, (byte)0xfa, 0x26, (byte)0xd8, 0x54, 0x6a, 0x64, 0x42, (byte)0xf3, (byte)0xc1, 0x39, 0x7e, (byte)0x92, (byte)0x9a, (byte)0xad,
(byte)0x8d, (byte)0xca, (byte)0xb2, 0x5b, 0x28, 0x2c, (byte)0xa8, (byte)0xa1, (byte)0xcf, 0x00, (byte)0xbe, (byte)0xf7, (byte)0xcb, 0x18, (byte)0xa0, (byte)0xf5, (byte)0xea, 0x44, 0x49,
0x3a, 0x30, (byte)0xa6, (byte)0xd2, 0x23, (byte)0xb2, 0x5b };
byte[] fakeCertBytes = {
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0x92, 0x09, 0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x00, 0x5a, 0x19, (byte) 0xac, (byte) 0xcc, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x03, 0x10,
0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0x93, 0x08, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
long timeStamp = System.currentTimeMillis();
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss Z");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Date today = new Date(timeStamp);
System.out.println("Unix Time stamp: " + timeStamp + ", date: " + df.format(today) + "\n");
Attribute state = new Attribute(KMIPConstants.ATTRIB_STATE, 0, KMIPConstants.STATE_ACTIVE);
Attribute cryptoMask = new Attribute(KMIPConstants.ATTRIB_CRYPTOUSAGEMASK, 0, new Integer((KMIPConstants.USAGE_MASK_ENCRYPT | KMIPConstants.USAGE_MASK_DECRYPT)));
Attribute initialDate = new Attribute(KMIPConstants.ATTRIB_INITIALDATE, 0, new Long(timeStamp));
CustomAttribute ca2 = new CustomAttribute("x-p6r-clientcomment", "by convention we place company name after x- to avoid collisions");
Attribute custom = new Attribute(0, ca2);
Attribute[] attributes = new Attribute[4];
attributes[0] = state;
attributes[1] = initialDate;
attributes[2] = cryptoMask;
attributes[3] = custom;
GetSymmetricKeyResponse sk = new GetSymmetricKeyResponse(batchId1, "1111-2222-3333-4444-5555", fakeKeyMaterial, KMIPConstants.ALG_AES, 128, attributes);
sl.addBatchItem(sk);
GetRawKeyResponse rk = new GetRawKeyResponse(batchId5, "1111-5555", fakeECKeyBytes, KMIPConstants.OBJECT_PRIVATEKEY, KMIPConstants.KEYFORMAT_ECPRIVKEY, KMIPConstants.ALG_EC, 128, attributes);
sl.addBatchItem(rk);
GetCertificateResponse cr = new GetCertificateResponse(batchId2, "aaa-bbb-cccc-ddd-eee", fakeCertBytes);
sl.addBatchItem(cr);
// -> the specification is complicated for wrapped keys, there are many options
byte[] fakeWrappedKey = {0x34, 0x01, 0x02, (byte)0xC3, 0x04, 0x05, 0x16, 0x22, 0x08, 0x45, 0x0A, 0x11, 0x0C, 0x0D, 0x0E, 0x0F, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
byte[] fakeSignature = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, (byte)0x80, (byte)0x90, (byte)0xA0, (byte)0xB0, (byte)0xC0};
// -> ** NOTE **, that setting fields in CryptographicParameters that are not supported by the KMIP protocol version chosen can result in the following
// exception: com.p6r.kmipserverlib.P6KMIPLibraryException: p6javakmipserverlib.endResponseMessage failed to generate KMIP message [ eInvalidVersion ],
// see comments in CryptographicParameters for details
CryptograhicParameters encryptParams = new CryptograhicParameters( KMIPConstants.MODE_ECB, KMIPConstants.PAD_PKCS5, -1, KMIPConstants.ROLE_KEK, -1, KMIPConstants.ALG_AES, false, -1, -1, -1, -1, -1, -1, -1, -1, -1, null, -1 );
CryptograhicParameters signParams = new CryptograhicParameters( -1, -1, KMIPConstants.HASH_SHA512, KMIPConstants.ROLE_MAC97974, -1, -1, false, -1, -1, -1, -1, -1, -1, -1, -1, -1, null, -1 );
KeyWrappingData wrapInfo = new KeyWrappingData(KMIPConstants.WRAP_ENCRYPTSIGN, "enc-1111", "sign-2222", fakeSignature, null, KMIPConstants.ENCODING_NO, encryptParams, signParams);
GetWrappedKeyResponse wr = new GetWrappedKeyResponse(batchId3, KMIPConstants.OBJECT_SYMMETRICKEY, "8383ururu-ododododo", fakeWrappedKey, wrapInfo);
sl.addBatchItem(wr);
KeyWrappingData wrapInfo2 = new KeyWrappingData(KMIPConstants.WRAP_ENCRYPT, "enc-1111", null, null, null, KMIPConstants.ENCODING_NO, encryptParams, null);
GetWrappedKeyResponse wr2 = new GetWrappedKeyResponse(batchId4, KMIPConstants.OBJECT_SYMMETRICKEY, "7777ururu-odododieieiei", fakeWrappedKey, KMIPConstants.KEYFORMAT_RAW, -1, KMIPConstants.ALG_AES, 128, wrapInfo2);
sl.addBatchItem(wr2);
byte[] responseBuffer = sl.endResponseMessage(0);
for (int j = 0; j < responseBuffer.length; j++) {
System.out.print(responseBuffer[j]);
}
System.out.println("\n");
// -> try a single get wrapped key response
sl.startResponseMessage(1, KMIPConstants.FORMAT_TTLV, 2);
CryptograhicParameters c1 = new CryptograhicParameters( KMIPConstants.MODE_NISTKEYWRAP, -1, -1, -1, -1, -1, true, 16, -1, -1, -1, -1, -1, -1, -1, -1, null, -1 );
KeyWrappingData k1 = new KeyWrappingData( KMIPConstants.WRAP_SIGN, null, "test2", fakeSignature, null, KMIPConstants.ENCODING_NO, null, c1 );
GetWrappedKeyResponse t1 = new GetWrappedKeyResponse(null, KMIPConstants.OBJECT_SYMMETRICKEY, "test", fakeWrappedKey, KMIPConstants.KEYFORMAT_RAW, -1, KMIPConstants.ALG_AES, 128, k1 );
sl.addBatchItem(t1);
responseBuffer = sl.endResponseMessage(0);
for (int j = 0; j < responseBuffer.length; j++) {
System.out.print(responseBuffer[j]);
}
System.out.println("\n");
// [B] Do the same thing in XML
sl.startResponseMessage(5, KMIPConstants.FORMAT_MSGXML, 2);
sl.addBatchItem(sk);
sl.addBatchItem(rk);
sl.addBatchItem(cr);
sl.addBatchItem(wr);
sl.addBatchItem(wr2);
byte[] responseBuffer2 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer2));
System.out.println("\n");
// [C] Do the same thing in JSON
sl.startResponseMessage(5, KMIPConstants.FORMAT_MSGJSON, 2);
sl.addBatchItem(sk);
sl.addBatchItem(rk);
sl.addBatchItem(cr);
sl.addBatchItem(wr);
sl.addBatchItem(wr2);
byte[] responseBuffer3 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer3));
System.out.println("\n");
// [D] Show how Secret Data and Opaque Data works
byte[] batchId20 = {0x01, 0x02, 0x03, 0x04};
byte[] batchId21 = {0x51, 0x52, 0x53, 0x54};
byte[] opaqueData = { 0x01, 0x02, 0x03, 0x04, 0x05 };
byte[] secretData = { 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F };
sl.startResponseMessage(2, KMIPConstants.FORMAT_MSGXML, 2);
GetBlobDataResponse db1 = new GetBlobDataResponse(batchId20, "3C3D6DD4-DB57-4AB7-A74E-D6F53F046BB7", KMIPConstants.OBJECT_OPAQUE, 0x80000001, opaqueData );
GetBlobDataResponse db2 = new GetBlobDataResponse(batchId21, "3C3D7DD4-DB57-4AC7-A74F-D6F53F146BB7", KMIPConstants.OBJECT_SECRETDATA, KMIPConstants.SECRET_SEED, secretData );
sl.addBatchItem(db1);
sl.addBatchItem(db2);
byte[] responseBuffer4 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer4));
System.out.println("\n");
// [E] Return a PGP key
byte[] pgpBytes = {
0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x42, 0x45, 0x47, 0x49, 0x4E, 0x20, 0x50, 0x47, 0x50, 0x20, 0x50, 0x55, 0x42, 0x4C, 0x49, 0x43, 0x20, 0x4B, 0x45, 0x59, 0x20, 0x42, 0x4C, 0x4F, 0x43, 0x4B, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x0A, 0x0A, 0x6D, 0x51, 0x45, 0x4E, 0x42, 0x46,
0x48, 0x41, 0x2F, 0x64, 0x51, 0x42, 0x43, 0x41, 0x44, 0x67, 0x65, 0x64, 0x39, 0x65, 0x36, 0x74, 0x69, 0x62, 0x38, 0x6B, 0x6C, 0x46, 0x68, 0x50, 0x70, 0x77, 0x53, 0x6E, 0x69, 0x31, 0x4A, 0x73, 0x65, 0x63, 0x6B, 0x71, 0x6A, 0x53, 0x4C, 0x35, 0x77, 0x54, 0x31, 0x51,
0x4B, 0x4B, 0x6D, 0x4A, 0x4D, 0x4D, 0x52, 0x79, 0x49, 0x31, 0x52, 0x75, 0x35, 0x52, 0x0A, 0x72, 0x4A, 0x53, 0x42, 0x63, 0x52, 0x77, 0x6A, 0x4F, 0x6F, 0x61, 0x61, 0x52, 0x64, 0x61, 0x76, 0x6E, 0x5A, 0x45, 0x4A, 0x31, 0x78, 0x75, 0x6A, 0x55, 0x33, 0x32, 0x5A, 0x6A,
0x2B, 0x32, 0x48, 0x32, 0x73, 0x71, 0x73, 0x36, 0x73, 0x53, 0x6E, 0x69, 0x7A, 0x43, 0x4E, 0x6E, 0x6B, 0x46, 0x35, 0x48, 0x43, 0x57, 0x4F, 0x32, 0x4A, 0x33, 0x62, 0x35, 0x4D, 0x35, 0x76, 0x50, 0x6B, 0x64, 0x47, 0x0A, 0x50, 0x59, 0x71, 0x72, 0x6C, 0x63, 0x76, 0x37,
0x6C, 0x76, 0x50, 0x38, 0x50, 0x30, 0x77, 0x6A, 0x63, 0x54, 0x4F, 0x75, 0x65, 0x44, 0x78, 0x6E, 0x53, 0x67, 0x78, 0x36, 0x49, 0x6A, 0x6E, 0x34, 0x51, 0x44, 0x6A, 0x64, 0x2B, 0x74, 0x4C, 0x6E, 0x54, 0x78, 0x54, 0x4B, 0x38, 0x55, 0x30, 0x59, 0x68, 0x7A, 0x73, 0x47,
0x5A, 0x43, 0x77, 0x75, 0x2B, 0x33, 0x53, 0x36, 0x6B, 0x77, 0x68, 0x67, 0x0A, 0x78, 0x6C, 0x57, 0x53, 0x7A, 0x2B, 0x33, 0x39, 0x6F, 0x50, 0x30, 0x6B, 0x54, 0x35, 0x38, 0x2B, 0x4A, 0x76, 0x6E, 0x6C, 0x78, 0x38, 0x76, 0x6F, 0x77, 0x74, 0x76, 0x71, 0x5A, 0x2B, 0x37,
0x6E, 0x70, 0x71, 0x72, 0x4D, 0x4F, 0x6F, 0x6A, 0x49, 0x56, 0x65, 0x64, 0x46, 0x53, 0x66, 0x6B, 0x2B, 0x7A, 0x32, 0x43, 0x6F, 0x31, 0x55, 0x4F, 0x62, 0x6A, 0x36, 0x6C, 0x2B, 0x78, 0x7A, 0x54, 0x74, 0x0A, 0x31, 0x6B, 0x71, 0x51, 0x5A, 0x66, 0x58, 0x43, 0x2F, 0x79,
0x79, 0x4D, 0x41, 0x46, 0x77, 0x54, 0x4B, 0x63, 0x50, 0x45, 0x79, 0x2F, 0x4B, 0x43, 0x77, 0x36, 0x75, 0x32, 0x62, 0x7A, 0x76, 0x6F, 0x75, 0x5A, 0x4D, 0x4D, 0x33, 0x64, 0x6B, 0x67, 0x66, 0x55, 0x2F, 0x41, 0x75, 0x71, 0x71, 0x70, 0x4B, 0x53, 0x42, 0x41, 0x42, 0x48,
0x30, 0x5A, 0x2F, 0x62, 0x6C, 0x6E, 0x43, 0x32, 0x54, 0x79, 0x0A, 0x51, 0x64, 0x53, 0x75, 0x4F, 0x4B, 0x78, 0x7A, 0x67, 0x63, 0x79, 0x49, 0x42, 0x63, 0x57, 0x35, 0x77, 0x52, 0x6E, 0x4B, 0x74, 0x6A, 0x73, 0x74, 0x57, 0x57, 0x39, 0x59, 0x43, 0x66, 0x2F, 0x6A, 0x49,
0x36, 0x45, 0x74, 0x41, 0x42, 0x45, 0x42, 0x41, 0x41, 0x47, 0x30, 0x54, 0x30, 0x74, 0x4E, 0x53, 0x56, 0x41, 0x67, 0x56, 0x56, 0x4E, 0x46, 0x55, 0x69, 0x42, 0x53, 0x55, 0x30, 0x45, 0x67, 0x0A, 0x4B, 0x45, 0x39, 0x42, 0x55, 0x30, 0x6C, 0x54, 0x49, 0x45, 0x74, 0x6C,
0x65, 0x53, 0x42, 0x4E, 0x59, 0x57, 0x35, 0x68, 0x5A, 0x32, 0x56, 0x74, 0x5A, 0x57, 0x35, 0x30, 0x49, 0x45, 0x6C, 0x75, 0x64, 0x47, 0x56, 0x79, 0x62, 0x33, 0x42, 0x6C, 0x63, 0x6D, 0x46, 0x69, 0x61, 0x57, 0x78, 0x70, 0x64, 0x48, 0x6B, 0x67, 0x55, 0x48, 0x4A, 0x76,
0x64, 0x47, 0x39, 0x6A, 0x62, 0x32, 0x77, 0x70, 0x0A, 0x49, 0x44, 0x78, 0x72, 0x62, 0x57, 0x6C, 0x77, 0x4D, 0x30, 0x42, 0x72, 0x62, 0x57, 0x6C, 0x77, 0x4C, 0x6D, 0x4E, 0x76, 0x62, 0x54, 0x36, 0x4A, 0x41, 0x54, 0x59, 0x45, 0x45, 0x77, 0x45, 0x43, 0x41, 0x43, 0x41,
0x46, 0x41, 0x6C, 0x48, 0x41, 0x2F, 0x64, 0x51, 0x43, 0x47, 0x79, 0x38, 0x47, 0x43, 0x77, 0x6B, 0x49, 0x42, 0x77, 0x4D, 0x43, 0x42, 0x42, 0x55, 0x43, 0x43, 0x41, 0x4D, 0x45, 0x0A, 0x46, 0x67, 0x49, 0x44, 0x41, 0x51, 0x49, 0x65, 0x41, 0x51, 0x49, 0x58, 0x67, 0x41,
0x41, 0x4B, 0x43, 0x52, 0x41, 0x6B, 0x44, 0x42, 0x65, 0x7A, 0x62, 0x4D, 0x6F, 0x4E, 0x4A, 0x6B, 0x64, 0x31, 0x42, 0x2F, 0x39, 0x50, 0x4C, 0x70, 0x68, 0x47, 0x62, 0x78, 0x50, 0x4F, 0x32, 0x37, 0x79, 0x65, 0x79, 0x58, 0x39, 0x43, 0x6E, 0x5A, 0x2F, 0x5A, 0x73, 0x52,
0x50, 0x62, 0x47, 0x56, 0x48, 0x57, 0x0A, 0x58, 0x6D, 0x4D, 0x52, 0x32, 0x72, 0x69, 0x58, 0x6F, 0x42, 0x58, 0x42, 0x31, 0x46, 0x52, 0x34, 0x64, 0x6C, 0x56, 0x2F, 0x58, 0x6E, 0x67, 0x72, 0x65, 0x76, 0x6B, 0x34, 0x53, 0x5A, 0x42, 0x58, 0x46, 0x49, 0x4B, 0x78, 0x32,
0x77, 0x30, 0x68, 0x70, 0x70, 0x53, 0x51, 0x56, 0x67, 0x72, 0x42, 0x4B, 0x59, 0x43, 0x6D, 0x73, 0x2F, 0x68, 0x46, 0x43, 0x4B, 0x53, 0x63, 0x56, 0x61, 0x56, 0x62, 0x0A, 0x63, 0x4E, 0x51, 0x79, 0x38, 0x4C, 0x30, 0x51, 0x35, 0x54, 0x4E, 0x6A, 0x46, 0x50, 0x7A, 0x78,
0x79, 0x55, 0x41, 0x50, 0x38, 0x4D, 0x4C, 0x55, 0x46, 0x75, 0x2B, 0x52, 0x72, 0x35, 0x4F, 0x49, 0x69, 0x50, 0x49, 0x59, 0x75, 0x47, 0x46, 0x4D, 0x32, 0x36, 0x51, 0x6E, 0x54, 0x42, 0x66, 0x44, 0x46, 0x4F, 0x68, 0x44, 0x68, 0x37, 0x4D, 0x76, 0x6A, 0x6A, 0x32, 0x77,
0x57, 0x55, 0x75, 0x5A, 0x0A, 0x62, 0x32, 0x6F, 0x56, 0x36, 0x69, 0x36, 0x67, 0x6A, 0x39, 0x37, 0x6D, 0x66, 0x79, 0x4E, 0x4A, 0x31, 0x6D, 0x4A, 0x48, 0x66, 0x72, 0x2B, 0x4D, 0x62, 0x4F, 0x36, 0x68, 0x42, 0x65, 0x62, 0x41, 0x50, 0x76, 0x6E, 0x73, 0x36, 0x6B, 0x59,
0x69, 0x76, 0x53, 0x44, 0x79, 0x72, 0x47, 0x72, 0x30, 0x36, 0x63, 0x70, 0x55, 0x61, 0x74, 0x6F, 0x65, 0x68, 0x30, 0x2B, 0x32, 0x59, 0x4E, 0x32, 0x35, 0x0A, 0x58, 0x64, 0x45, 0x6D, 0x44, 0x6B, 0x30, 0x62, 0x71, 0x79, 0x39, 0x6B, 0x70, 0x4A, 0x4D, 0x77, 0x48, 0x6A,
0x6A, 0x55, 0x4C, 0x6B, 0x77, 0x6E, 0x73, 0x46, 0x6A, 0x62, 0x76, 0x2B, 0x4B, 0x68, 0x58, 0x78, 0x6C, 0x32, 0x6A, 0x47, 0x36, 0x6D, 0x6C, 0x45, 0x71, 0x4E, 0x69, 0x41, 0x69, 0x55, 0x56, 0x50, 0x66, 0x74, 0x58, 0x59, 0x34, 0x79, 0x4B, 0x6D, 0x54, 0x66, 0x69, 0x42,
0x5A, 0x61, 0x0A, 0x34, 0x4A, 0x30, 0x6A, 0x30, 0x34, 0x6F, 0x6D, 0x6B, 0x55, 0x38, 0x4A, 0x6A, 0x34, 0x66, 0x76, 0x6C, 0x6B, 0x4F, 0x6F, 0x32, 0x6F, 0x36, 0x6B, 0x62, 0x73, 0x2B, 0x68, 0x74, 0x71, 0x44, 0x2B, 0x4A, 0x35, 0x57, 0x48, 0x78, 0x6D, 0x58, 0x31, 0x54,
0x6F, 0x73, 0x67, 0x72, 0x66, 0x7A, 0x75, 0x56, 0x38, 0x7A, 0x35, 0x4D, 0x74, 0x37, 0x55, 0x0A, 0x3D, 0x6E, 0x56, 0x4F, 0x79, 0x0A, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x45, 0x4E, 0x44, 0x20, 0x50, 0x47, 0x50, 0x20, 0x50, 0x55, 0x42, 0x4C, 0x49, 0x43, 0x20, 0x4B, 0x45,
0x59, 0x20, 0x42, 0x4C, 0x4F, 0x43, 0x4B, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x0A
};
sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 4);
GetPGPKeyResponse pgpKey = new GetPGPKeyResponse(null, "3C3DD4-DB57-4AC7-A74F-D6F5146BB7", pgpBytes, 4, KMIPConstants.ALG_RSA, 2048);
sl.addBatchItem(pgpKey);
byte[] responseBuffer5 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer5));
System.out.println("\n");
// [F] Return a PKCS12 encoded private key, PKCS12 encoding taken from test case TC-PKCS12-1-14 the last step
byte[] pkcs12Bytes = {
(byte)0x30, (byte)0x82, (byte)0x0A, (byte)0xE9, (byte)0x02, (byte)0x01, (byte)0x03, (byte)0x30, (byte)0x82, (byte)0x0A, (byte)0xAF, (byte)0x06, (byte)0x09, (byte)0x2A, (byte)0x86, (byte)0x48, (byte)0x86, (byte)0xF7, (byte)0x0D, (byte)0x01, (byte)0x07, (byte)0x01, (byte)0xA0, (byte)0x82, (byte)0x0A, (byte)0xA0, (byte)0x04, (byte)0x82, (byte)0x0A, (byte)0x9C, (byte)0x30, (byte)0x82, (byte)0x0A, (byte)0x98, (byte)0x30, (byte)0x82, (byte)0x07, (byte)0x97, (byte)0x06, (byte)0x09, (byte)0x2A, (byte)0x86, (byte)0x48, (byte)0x86, (byte)0xF7, (byte)0x0D,
(byte)0x01, (byte)0x07, (byte)0x06, (byte)0xA0, (byte)0x82, (byte)0x07, (byte)0x88, (byte)0x30, (byte)0x82, (byte)0x07, (byte)0x84, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0x30, (byte)0x82, (byte)0x07, (byte)0x7D, (byte)0x06, (byte)0x09, (byte)0x2A, (byte)0x86, (byte)0x48, (byte)0x86, (byte)0xF7, (byte)0x0D, (byte)0x01, (byte)0x07, (byte)0x01, (byte)0x30, (byte)0x1C, (byte)0x06, (byte)0x0A, (byte)0x2A, (byte)0x86, (byte)0x48, (byte)0x86, (byte)0xF7, (byte)0x0D, (byte)0x01, (byte)0x0C, (byte)0x01, (byte)0x03, (byte)0x30, (byte)0x0E, (byte)0x04,
(byte)0x08, (byte)0xA2, (byte)0x30, (byte)0x40, (byte)0x70, (byte)0x73, (byte)0x2C, (byte)0xD3, (byte)0x05, (byte)0x02, (byte)0x02, (byte)0x08, (byte)0x00, (byte)0x80, (byte)0x82, (byte)0x07, (byte)0x50, (byte)0xDA, (byte)0x87, (byte)0x01, (byte)0xBF, (byte)0x41, (byte)0xC8, (byte)0x0C, (byte)0xD6, (byte)0xF0, (byte)0xF5, (byte)0x19, (byte)0x77, (byte)0xE7, (byte)0x9F, (byte)0xDB, (byte)0x04, (byte)0x35, (byte)0xF9, (byte)0xAA, (byte)0x8E, (byte)0xCE, (byte)0x0E, (byte)0xAE, (byte)0x09, (byte)0xAE, (byte)0x08, (byte)0x3B, (byte)0x51, (byte)0xC9,
(byte)0x29, (byte)0x87, (byte)0x1B, (byte)0x3E, (byte)0x78, (byte)0x10, (byte)0xDA, (byte)0x04, (byte)0x30, (byte)0x88, (byte)0x3A, (byte)0xD8, (byte)0xFF, (byte)0xEE, (byte)0x4D, (byte)0x01, (byte)0x5B, (byte)0x8B, (byte)0xB5, (byte)0x30, (byte)0xC3, (byte)0xFC, (byte)0xD9, (byte)0x58, (byte)0x83, (byte)0xA4, (byte)0xA9, (byte)0x38, (byte)0xB2, (byte)0xD5, (byte)0x7C, (byte)0x81, (byte)0xB4, (byte)0x79, (byte)0x2C, (byte)0x9B, (byte)0x05, (byte)0xAF, (byte)0x5C, (byte)0xDB, (byte)0xD3, (byte)0x7A, (byte)0x66, (byte)0x22, (byte)0x33, (byte)0x9E,
(byte)0xF2, (byte)0x0B, (byte)0xBD, (byte)0xEA, (byte)0xC3, (byte)0xBC, (byte)0x38, (byte)0x29, (byte)0xE2, (byte)0x6A, (byte)0xCA, (byte)0xF1, (byte)0x24, (byte)0x55, (byte)0xF0, (byte)0x2C, (byte)0x2A, (byte)0x1D, (byte)0x8E, (byte)0x6F, (byte)0x1D, (byte)0x3E, (byte)0xE0, (byte)0x1F, (byte)0xB4, (byte)0xC4, (byte)0xFF, (byte)0x85, (byte)0x0E, (byte)0xE5, (byte)0x28, (byte)0x5D, (byte)0xE4, (byte)0x52, (byte)0xCE, (byte)0x33, (byte)0xDA, (byte)0x48, (byte)0x9B, (byte)0x50, (byte)0x55, (byte)0x69, (byte)0xC5, (byte)0x34, (byte)0x43, (byte)0x9E,
(byte)0x21, (byte)0x08, (byte)0x03, (byte)0x8B, (byte)0xA1, (byte)0x62, (byte)0x95, (byte)0xC1, (byte)0xBE, (byte)0x36, (byte)0x7A, (byte)0x93, (byte)0x07, (byte)0x94, (byte)0xAC, (byte)0x18, (byte)0x27, (byte)0x86, (byte)0x0D, (byte)0xE2, (byte)0x28, (byte)0x79, (byte)0x5D, (byte)0x96, (byte)0xF1, (byte)0x9D, (byte)0x2E, (byte)0xE6, (byte)0x0D, (byte)0xD3, (byte)0xA9, (byte)0x26, (byte)0x68, (byte)0xEE, (byte)0x57, (byte)0x17, (byte)0xE4, (byte)0x3A, (byte)0xE4, (byte)0x95, (byte)0x8A, (byte)0x9E, (byte)0xC9, (byte)0xAC, (byte)0x4D, (byte)0x23,
(byte)0xD0, (byte)0xF4, (byte)0x0D, (byte)0x8D, (byte)0x61, (byte)0xE8, (byte)0xCA, (byte)0xDD, (byte)0x5E, (byte)0x8D, (byte)0x02, (byte)0xBD, (byte)0x5F, (byte)0x35, (byte)0xF2, (byte)0x45, (byte)0xE4, (byte)0xDB, (byte)0x09, (byte)0xA1, (byte)0x8C, (byte)0x3F, (byte)0x9A, (byte)0x21, (byte)0x62, (byte)0x1A, (byte)0x95, (byte)0x62, (byte)0xE6, (byte)0xCA, (byte)0xBC, (byte)0x9E, (byte)0x1A, (byte)0xDF, (byte)0x47, (byte)0x5D, (byte)0xDC, (byte)0xFA, (byte)0xE9, (byte)0x6E, (byte)0xD7, (byte)0x12, (byte)0x5A, (byte)0xAC, (byte)0xFE, (byte)0xD6,
(byte)0xE5, (byte)0x9B, (byte)0x39, (byte)0x7E, (byte)0x1A, (byte)0x6E, (byte)0xC5, (byte)0x7B, (byte)0x74, (byte)0x9B, (byte)0xBA, (byte)0xFA, (byte)0x23, (byte)0xE8, (byte)0x18, (byte)0x7B, (byte)0x0A, (byte)0xD1, (byte)0x78, (byte)0xC5, (byte)0xF4, (byte)0x0B, (byte)0xC7, (byte)0xD2, (byte)0x12, (byte)0xFE, (byte)0x21, (byte)0x56, (byte)0xCD, (byte)0xEE, (byte)0x64, (byte)0xFE, (byte)0x21, (byte)0x05, (byte)0x29, (byte)0x22, (byte)0x7C, (byte)0x9A, (byte)0x50, (byte)0x5B, (byte)0xB7, (byte)0xCE, (byte)0xFC, (byte)0x1D, (byte)0xD8, (byte)0xE2,
(byte)0x85, (byte)0x07, (byte)0xAB, (byte)0x5B, (byte)0xE7, (byte)0xBC, (byte)0x5D, (byte)0xB1, (byte)0xB7, (byte)0xD1, (byte)0x44, (byte)0xFA, (byte)0x05, (byte)0x32, (byte)0x63, (byte)0x08, (byte)0x8E, (byte)0x09, (byte)0x76, (byte)0x5E, (byte)0x38, (byte)0x1F, (byte)0x16, (byte)0x9E, (byte)0x90, (byte)0xFD, (byte)0x5F, (byte)0x99, (byte)0x64, (byte)0x58, (byte)0xE9, (byte)0x9E, (byte)0xCD, (byte)0xDC, (byte)0x30, (byte)0x8F, (byte)0x28, (byte)0x58, (byte)0x2F, (byte)0xF0, (byte)0x04, (byte)0x77, (byte)0x45, (byte)0x95, (byte)0xE9, (byte)0x47,
(byte)0x4A, (byte)0x5A, (byte)0x75, (byte)0x00, (byte)0xE2, (byte)0xFF, (byte)0x7F, (byte)0xDE, (byte)0x04, (byte)0x9B, (byte)0x25, (byte)0xCE, (byte)0x96, (byte)0xE4, (byte)0x5D, (byte)0x04, (byte)0xF7, (byte)0xB2, (byte)0x2B, (byte)0xD5, (byte)0xBC, (byte)0xEC, (byte)0x5A, (byte)0x0E, (byte)0x6D, (byte)0x9E, (byte)0x87, (byte)0x2E, (byte)0x4A, (byte)0x82, (byte)0xF2, (byte)0x8C, (byte)0x48, (byte)0x90, (byte)0x59, (byte)0x0B, (byte)0xE7, (byte)0xB2, (byte)0x7F, (byte)0x2A, (byte)0xF2, (byte)0xC1, (byte)0x7E, (byte)0xC3, (byte)0x71, (byte)0x31,
(byte)0xBE, (byte)0x93, (byte)0xE5, (byte)0xFA, (byte)0x60, (byte)0xDB, (byte)0x77, (byte)0xCD, (byte)0xE9, (byte)0x65, (byte)0xB5, (byte)0xA3, (byte)0xBE, (byte)0xB7, (byte)0x65, (byte)0xAC, (byte)0xC6, (byte)0xF0, (byte)0x22, (byte)0x59, (byte)0x83, (byte)0x63, (byte)0x5D, (byte)0xEE, (byte)0x6C, (byte)0xD4, (byte)0x6D, (byte)0x26, (byte)0x0F, (byte)0x7F, (byte)0x35, (byte)0xF5, (byte)0x52, (byte)0x01, (byte)0xC4, (byte)0x73, (byte)0x4A, (byte)0x84, (byte)0x38, (byte)0x2F, (byte)0xBF, (byte)0x92, (byte)0x5E, (byte)0xA4, (byte)0x21, (byte)0x76,
(byte)0x00, (byte)0xA5, (byte)0x6A, (byte)0xA6, (byte)0x67, (byte)0x2B, (byte)0xA7, (byte)0xEF, (byte)0x29, (byte)0x60, (byte)0xF5, (byte)0x41, (byte)0x7A, (byte)0x1B, (byte)0x46, (byte)0xD5, (byte)0x63, (byte)0x9D, (byte)0xA2, (byte)0x03, (byte)0x36, (byte)0x83, (byte)0xA1, (byte)0x4D, (byte)0x94, (byte)0xFC, (byte)0xFD, (byte)0x4E, (byte)0x80, (byte)0x85, (byte)0x4E, (byte)0x18, (byte)0x0D, (byte)0x36, (byte)0x07, (byte)0x75, (byte)0x77, (byte)0xE6, (byte)0x8E, (byte)0xE5, (byte)0x23, (byte)0x31, (byte)0xE0, (byte)0xC7, (byte)0xDD, (byte)0x49,
(byte)0xB2, (byte)0x16, (byte)0xD8, (byte)0x67, (byte)0x72, (byte)0xB5, (byte)0xCF, (byte)0x63, (byte)0x37, (byte)0xA1, (byte)0x76, (byte)0xF4, (byte)0x24, (byte)0x9B, (byte)0xC4, (byte)0x22, (byte)0x2D, (byte)0x20, (byte)0x7B, (byte)0x09, (byte)0xB2, (byte)0xCF, (byte)0x92, (byte)0x0C, (byte)0xC2, (byte)0x5C, (byte)0xEC, (byte)0x6F, (byte)0x47, (byte)0x8D, (byte)0x7E, (byte)0x13, (byte)0x7A, (byte)0x0A, (byte)0x4C, (byte)0x4B, (byte)0x70, (byte)0x6E, (byte)0xB7, (byte)0x6D, (byte)0xFE, (byte)0xAC, (byte)0xEC, (byte)0xF4, (byte)0xE5, (byte)0x41,
(byte)0x78, (byte)0x35, (byte)0xB3, (byte)0xF8, (byte)0x05, (byte)0x60, (byte)0x4D, (byte)0x89, (byte)0xC7, (byte)0x3D, (byte)0x99, (byte)0x4A, (byte)0x77, (byte)0xD4, (byte)0x2C, (byte)0x57, (byte)0xFD, (byte)0x03, (byte)0x56, (byte)0x18, (byte)0x17, (byte)0x03, (byte)0x67, (byte)0x83, (byte)0x95, (byte)0x3F, (byte)0x16, (byte)0x79, (byte)0x07, (byte)0x62, (byte)0x3E, (byte)0xF4, (byte)0x8E, (byte)0x13, (byte)0xA8, (byte)0xBE, (byte)0xAF, (byte)0xB0, (byte)0x3D, (byte)0x36, (byte)0x67, (byte)0x04, (byte)0xD0, (byte)0xAA, (byte)0x33, (byte)0x73,
(byte)0x74, (byte)0x29, (byte)0xE2, (byte)0xC2, (byte)0x7D, (byte)0xCA, (byte)0xF9, (byte)0x89, (byte)0x24, (byte)0x3A, (byte)0xE0, (byte)0x68, (byte)0xEC, (byte)0x27, (byte)0xFA, (byte)0xF3, (byte)0xCF, (byte)0x80, (byte)0xC0, (byte)0xE3, (byte)0xB2, (byte)0x24, (byte)0x8B, (byte)0x97, (byte)0x9D, (byte)0x83, (byte)0xBC, (byte)0x32, (byte)0x43, (byte)0xD5, (byte)0x26, (byte)0x45, (byte)0x5E, (byte)0x0C, (byte)0x24, (byte)0x16, (byte)0xE0, (byte)0xA3, (byte)0xDA, (byte)0x60, (byte)0x46, (byte)0x3C, (byte)0xDB, (byte)0x52, (byte)0x86, (byte)0x8B,
(byte)0x64, (byte)0xAE, (byte)0x8F, (byte)0x80, (byte)0x79, (byte)0x6D, (byte)0x96, (byte)0xE0, (byte)0xB2, (byte)0x9A, (byte)0xF8, (byte)0xAA, (byte)0x3B, (byte)0xB8, (byte)0x45, (byte)0x28, (byte)0x27, (byte)0xBA, (byte)0x0B, (byte)0x63, (byte)0x70, (byte)0x8B, (byte)0x28, (byte)0x90, (byte)0xC0, (byte)0x0A, (byte)0x31, (byte)0x8F, (byte)0xE4, (byte)0x4B, (byte)0xC5, (byte)0x84, (byte)0x9A, (byte)0x3D, (byte)0x6A, (byte)0x5D, (byte)0x51, (byte)0x90, (byte)0x32, (byte)0xE9, (byte)0x92, (byte)0x26, (byte)0x41, (byte)0x19, (byte)0x55, (byte)0x9C,
(byte)0x05, (byte)0x55, (byte)0xC5, (byte)0x69, (byte)0xFA, (byte)0x69, (byte)0x84, (byte)0x64, (byte)0x93, (byte)0x93, (byte)0xB9, (byte)0xF8, (byte)0xE1, (byte)0x90, (byte)0xC6, (byte)0xA5, (byte)0xE2, (byte)0xC8, (byte)0xD3, (byte)0xA8, (byte)0xEB, (byte)0x2D, (byte)0x3F, (byte)0xFE, (byte)0x23, (byte)0xF1, (byte)0x48, (byte)0xC0, (byte)0xCA, (byte)0xA9, (byte)0x54, (byte)0xFF, (byte)0xEC, (byte)0x58, (byte)0x54, (byte)0xC9, (byte)0x8E, (byte)0xD5, (byte)0x02, (byte)0x21, (byte)0xA7, (byte)0x11, (byte)0xD5, (byte)0x93, (byte)0x7C, (byte)0x6F,
(byte)0x5D, (byte)0x05, (byte)0x02, (byte)0xC7, (byte)0xDA, (byte)0x1E, (byte)0xDB, (byte)0x13, (byte)0x99, (byte)0x8C, (byte)0xCD, (byte)0x51, (byte)0x69, (byte)0x4A, (byte)0x0F, (byte)0xD8, (byte)0x34, (byte)0x0C, (byte)0xF6, (byte)0xC3, (byte)0x9B, (byte)0x1E, (byte)0xDC, (byte)0x31, (byte)0xA6, (byte)0xAE, (byte)0xF2, (byte)0xD5, (byte)0x01, (byte)0xF1, (byte)0x1D, (byte)0x07, (byte)0x91, (byte)0x32, (byte)0x43, (byte)0x01, (byte)0x4D, (byte)0x56, (byte)0x8E, (byte)0x37, (byte)0x1B, (byte)0x5D, (byte)0x2F, (byte)0xFB, (byte)0x1D, (byte)0x43,
(byte)0x74, (byte)0xD3, (byte)0xE1, (byte)0x10, (byte)0x30, (byte)0x53, (byte)0x47, (byte)0xF3, (byte)0xBC, (byte)0x34, (byte)0x0A, (byte)0x4B, (byte)0x4D, (byte)0x8E, (byte)0xE5, (byte)0xF9, (byte)0x04, (byte)0x8D, (byte)0xDA, (byte)0x2C, (byte)0x36, (byte)0xB3, (byte)0xDF, (byte)0x8C, (byte)0x0A, (byte)0x69, (byte)0xD7, (byte)0x73, (byte)0xC2, (byte)0xE3, (byte)0x89, (byte)0x11, (byte)0xA3, (byte)0xCC, (byte)0x76, (byte)0x60, (byte)0x06, (byte)0x9D, (byte)0xE6, (byte)0x18, (byte)0xE3, (byte)0x8C, (byte)0x7C, (byte)0xC7, (byte)0x4E, (byte)0x56,
(byte)0x74, (byte)0x12, (byte)0xC2, (byte)0x72, (byte)0x6E, (byte)0xEB, (byte)0xA6, (byte)0xF8, (byte)0x2B, (byte)0xD9, (byte)0x73, (byte)0x14, (byte)0x7D, (byte)0x4B, (byte)0x47, (byte)0x0F, (byte)0xDA, (byte)0xC7, (byte)0xA3, (byte)0xA1, (byte)0x60, (byte)0x6C, (byte)0x82, (byte)0xD2, (byte)0x0F, (byte)0x1E, (byte)0xE0, (byte)0x43, (byte)0x0A, (byte)0x28, (byte)0x96, (byte)0x86, (byte)0xD8, (byte)0x9D, (byte)0xF8, (byte)0x0B, (byte)0x62, (byte)0xDC, (byte)0xBD, (byte)0x2A, (byte)0xC6, (byte)0x86, (byte)0x75, (byte)0x07, (byte)0xE8, (byte)0x6D,
(byte)0xBB, (byte)0xEF, (byte)0xDE, (byte)0x23, (byte)0x11, (byte)0x35, (byte)0x53, (byte)0x25, (byte)0x11, (byte)0x65, (byte)0xDA, (byte)0x10, (byte)0x5F, (byte)0xE6, (byte)0x7E, (byte)0x64, (byte)0xF0, (byte)0xA4, (byte)0xA5, (byte)0x49, (byte)0xDD, (byte)0x01, (byte)0xF9, (byte)0xD7, (byte)0x1C, (byte)0x2F, (byte)0xFA, (byte)0x89, (byte)0x5A, (byte)0x69, (byte)0xFB, (byte)0xBE, (byte)0xED, (byte)0xA3, (byte)0x4B, (byte)0x00, (byte)0xD1, (byte)0xDA, (byte)0xAD, (byte)0x76, (byte)0x00, (byte)0xC6, (byte)0xB3, (byte)0x2E, (byte)0x52, (byte)0x93,
(byte)0xEC, (byte)0xB2, (byte)0xBB, (byte)0x44, (byte)0x02, (byte)0x20, (byte)0x63, (byte)0x34, (byte)0x24, (byte)0x23, (byte)0xC4, (byte)0x0E, (byte)0x46, (byte)0x1C, (byte)0x22, (byte)0x97, (byte)0xF9, (byte)0xB0, (byte)0xB3, (byte)0xDE, (byte)0xC7, (byte)0xF1, (byte)0xFE, (byte)0xB8, (byte)0x7A, (byte)0x5F, (byte)0xCD, (byte)0x2D, (byte)0x49, (byte)0x38, (byte)0xB5, (byte)0x3F, (byte)0x5C, (byte)0x02, (byte)0x22, (byte)0x72, (byte)0x54, (byte)0x78, (byte)0x8F, (byte)0xAB, (byte)0xDE, (byte)0x0D, (byte)0xC7, (byte)0xF0, (byte)0x08, (byte)0xA1,
(byte)0x39, (byte)0x54, (byte)0xA9, (byte)0x9D, (byte)0x8F, (byte)0x70, (byte)0x81, (byte)0x35, (byte)0x8C, (byte)0xCA, (byte)0xF7, (byte)0x88, (byte)0x5C, (byte)0xBD, (byte)0x76, (byte)0x25, (byte)0xA7, (byte)0x6D, (byte)0x7B, (byte)0xB6, (byte)0xAE, (byte)0x8B, (byte)0xB1, (byte)0x25, (byte)0x69, (byte)0x28, (byte)0xA0, (byte)0xDB, (byte)0xF9, (byte)0x29, (byte)0x34, (byte)0x15, (byte)0xC8, (byte)0x72, (byte)0xD9, (byte)0x8B, (byte)0x94, (byte)0xFF, (byte)0xF2, (byte)0xBE, (byte)0xAA, (byte)0x98, (byte)0x5B, (byte)0xF7, (byte)0x14, (byte)0x52
};
sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 4);
GetRawKeyResponse pkcs12Key = new GetRawKeyResponse(null, "af310121-0ce4-4779-9a02-bacd5b1ac9f4", pkcs12Bytes, KMIPConstants.OBJECT_PRIVATEKEY, KMIPConstants.KEYFORMAT_PKCS12, -1, -1, null );
sl.addBatchItem(pkcs12Key);
byte[] responseBuffer6 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer6));
System.out.println("\n");
// [G] Return Split Key response
sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 4);
GetSplitKeyResponse split = new GetSplitKeyResponse(null, "af310121-0ce4-4779-9a02-bacd5b1AAAAAA", opaqueData, KMIPConstants.KEYFORMAT_RAW, 10, 2, 5, KMIPConstants.SPLITKEY_PRIME, secretData, KMIPConstants.ALG_AES, 256 );
sl.addBatchItem(split);
byte[] responseBuffer7 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer7));
System.out.println("\n");
sl.freeLibrary();
} catch (Exception e) {
// -> we should not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}

KSL JNI KMIP Example 2 - Get Key Request

This example demonstrates a request message with a Get key with a wrapping specification operation.

JniGetKmip.java

package com.p6r.kmipserverlib;
import org.junit.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class JniGetKmip {
@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_GetKMIP() {
System.out.println("@Test - JNICall-GetKMIP");
// -> this parser is multi-thread safe by using JNI monitor locking
// -> use one parser object per server thread is recommended
P6KMIPServerLib sl = new P6KMIPServerLib();
// -> KMIP 1.3 TTLV message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
byte[] testMessage = {
0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x01, 0x48, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x00, 0x68,
0x42, 0x00, 0x69, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x6a, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0e, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x10, 0x06, 0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x42, 0x00, (byte)0x92, 0x09, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x59, (byte)0xe0, (byte)0xf1, (byte)0xa1, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x00, (byte)0xd0, 0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, (byte)0x93, 0x08, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x00, (byte)0xa8, 0x42, 0x00, (byte)0x94, 0x07, 0x00, 0x00, 0x00, 0x0f,
0x31, 0x31, 0x31, 0x2d, 0x32, 0x32, 0x32, 0x2d, 0x33, 0x33, 0x33, 0x2d, 0x34, 0x34, 0x34, 0x00, 0x42, 0x00, 0x42, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x47, 0x01, 0x00, 0x00, 0x00, 0x78, 0x42, 0x00, (byte)0x9e, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x36, 0x01, 0x00, 0x00, 0x00, 0x50,
0x42, 0x00, (byte)0x94, 0x07, 0x00, 0x00, 0x00, 0x0c, 0x4b, 0x45, 0x4b, 0x2d, 0x33, 0x34, 0x35, 0x2d, 0x34, 0x34, 0x35, 0x36, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x2b, 0x01, 0x00, 0x00, 0x00, 0x30,
0x42, 0x00, 0x11, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x5f, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, (byte)0xd1, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x05, 0x53, 0x74, 0x61, 0x74, 0x65, 0x00, 0x00, 0x00 };
try {
sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
String libVersion = sl.getLibraryVersion();
System.out.println(libVersion);
// -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
// -> the type of message: TTLC, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
sl.setMessageBuffer(testMessage, KMIPConstants.FORMAT_TTLV);
// -> now we can pull parts of the request message apart, this can be done over and over again if desired
RequestHeader rh = sl.getRequestHeader();
assertEquals("1.3", rh.getProtocolVersion());
assertEquals(KMIPConstants.BATCHOPTION_UNDO, rh.getBatchErrorOption());
assertEquals(true, rh.getBatchOrderOption());
assertEquals(1, rh.getBatchCount());
assertEquals("Fri Oct 13 10:02:25 PDT 2017", rh.getTimeStamp().toString());
// -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
for (int i = 0; i < rh.getBatchCount(); i++) {
BatchItem bi = sl.getBatchItem(i + 1);
if (bi instanceof GetBatchItem) {
GetBatchItem cb = (GetBatchItem) bi;
assertEquals(1, cb.getUniqueBatchId()[0]);
assertEquals("111-222-333-444", cb.getUniqueId());
assertEquals(KMIPConstants.KEYFORMAT_PKCS8, cb.getKeyFormatType());
KeyWrappingSpecification kws = cb.getWrappingDetails();
assertEquals(KMIPConstants.WRAP_ENCRYPT, kws.getMethod());
String[] attributeNames = kws.getAttributeNames();
assertEquals("State", attributeNames[0]);
assertEquals("KEK-345-4456", kws.getEncryptKeyUID());
CryptograhicParameters cyp = kws.getEncryptParams();
assertEquals(2, cyp.getInitialCounterValue());
assertEquals(KMIPConstants.PAD_PKCS5, cyp.getPaddingMethod());
assertEquals(KMIPConstants.MODE_CTR, cyp.getBlockCipherMode());
}
}
sl.freeLibrary();
} catch (Exception e) {
// -> we shoud not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}

KSL JNI KMIP Example 3 - Register Key Request

This example demonstrates a request message with a Register Symmetric Key operation. This request also has two credentials passed in the body of the message: password and device types.

JniRegisterKmip.java

package com.p6r.kmipserverlib;
import org.junit.*;
import static org.junit.Assert.assertEquals;
public class JniRegisterKmip {
@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_RegisterKMIP() {
System.out.println("@Test - JNICall-RegisterKMIP");
// -> this parser is multi-thread safe by using JNI monitor locking
// -> use one parser object per server thread is recommended
P6KMIPServerLib sl = new P6KMIPServerLib();
// -> KMIP 1.3 TTLV message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
byte[] testMessage = {
0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x02, (byte) 0x98, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x01, 0x30, 0x42, 0x00, 0x69, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x6a, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0c, 0x01, 0x00, 0x00, 0x00, (byte) 0xe0,
0x42, 0x00, 0x23, 0x01, 0x00, 0x00, 0x00, 0x40, 0x42, 0x00, 0x24, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x25, 0x01, 0x00, 0x00, 0x00, 0x28,
0x42, 0x00, (byte) 0x99, 0x07, 0x00, 0x00, 0x00, 0x04, 0x46, 0x72, 0x65, 0x64, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0xa1, 0x07, 0x00, 0x00, 0x00, 0x09, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64,
0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x23, 0x01, 0x00, 0x00, 0x00, (byte) 0x90, 0x42, 0x00, 0x24, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x25, 0x01, 0x00, 0x00, 0x00, 0x78, 0x42, 0x00, (byte) 0xb0, 0x07, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x65, 0x72, 0x4e, 0x75, 0x6d, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, (byte) 0xa2, 0x07, 0x00, 0x00, 0x00, 0x09, 0x64, 0x65, 0x76, 0x49, 0x44, 0x32, 0x32, 0x33, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0xab, 0x07, 0x00, 0x00, 0x00, 0x09,
0x6e, 0x65, 0x74, 0x49, 0x44, 0x39, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0xa9, 0x07, 0x00, 0x00, 0x00, 0x0a, 0x6d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x49,
0x44, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0xaa, 0x07, 0x00, 0x00, 0x00, 0x0a, 0x6d, 0x65, 0x64, 0x69, 0x61, 0x49, 0x44, 0x33, 0x31, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, (byte) 0x92, 0x09, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x59, (byte) 0xe9, 0x77, (byte) 0xa7, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x01, 0x58, 0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0x93, 0x08, 0x00, 0x00, 0x00, 0x01,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x01, 0x30, 0x42, 0x00, 0x57, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, (byte) 0x91, 0x01, 0x00, 0x00, 0x00, 0x38, 0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x30, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x04, 0x78, 0x2d, 0x49, 0x44, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x0b, 0x07, 0x00, 0x00, 0x00, 0x11, 0x50, 0x36, 0x52, 0x2d, 0x4d, 0x45, 0x4d, 0x4f, 0x52, 0x59, 0x2d, 0x54, 0x45, 0x53, 0x54, 0x2d, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, (byte) 0x8f, 0x01, 0x00, 0x00, 0x00, (byte) 0xd8, 0x42, 0x00, 0x40, 0x01, 0x00, 0x00, 0x00, (byte) 0xd0, 0x42, 0x00, 0x42, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x45, 0x01, 0x00, 0x00, 0x00, (byte) 0x98, 0x42, 0x00, 0x43, 0x08, 0x00, 0x00, 0x00, 0x20, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0f, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x30, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x18,
0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x20, 0x55, 0x73, 0x61, 0x67, 0x65, 0x20, 0x4d, 0x61, 0x73, 0x6b, 0x42, 0x00, 0x0b, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x30, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x0b, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74,
0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0b, 0x07, 0x00, 0x00, 0x00, 0x0b, 0x31, 0x20, 0x32, 0x20, 0x33, 0x20, 0x34, 0x20, 0x35, 0x20, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x28, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
};
try {
sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
String libVersion = sl.getLibraryVersion();
System.out.println(libVersion);
// -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
// -> the type of message: TTLC, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
sl.setMessageBuffer(testMessage, KMIPConstants.FORMAT_TTLV);
// -> now we can pull parts of the request message apart, this can be done over and over again if desired
RequestHeader rh = sl.getRequestHeader();
assertEquals("1.3", rh.getProtocolVersion());
// -> check for message level credentials
Credential credOne = sl.getCredential(1);
if (credOne instanceof PasswordCredential) {
PasswordCredential pc = (PasswordCredential) credOne;
assertEquals("Fred", pc.getUserName());
assertEquals("password1", pc.getPassword());
} else assertEquals(0, 1);
Credential credTwo = sl.getCredential(2);
if (credTwo instanceof DeviceCredential) {
DeviceCredential dc = (DeviceCredential) credTwo;
assertEquals("serNum123456", dc.getSerialNumber());
assertEquals(null, dc.getPassword());
assertEquals("devID2233", dc.getDeviceIdentifier());
assertEquals("netID9000", dc.getNetworkIdentifer());
assertEquals("machineID1", dc.getMachineIdentifer());
assertEquals("mediaID313", dc.getMediaIdentifer());
} else assertEquals(1, 2);
Credential credThree = sl.getCredential(3);
assertEquals(null, credThree);
// -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
for (int i = 0; i < rh.getBatchCount(); i++) {
BatchItem bi = sl.getBatchItem(i + 1);
if (bi instanceof RegisterTransparentSymmetricKeyBatchItem) {
RegisterTransparentSymmetricKeyBatchItem rb = (RegisterTransparentSymmetricKeyBatchItem) bi;
assertEquals(1, rb.getUniqueBatchId()[0]);
assertEquals(KMIPConstants.ALG_AES, rb.getAlgorithm());
byte[] keyBytes = rb.getKeyMaterial();
assertEquals(32, keyBytes.length);
for (int j = 0; j < keyBytes.length; j++) {
if (j < 14)
assertEquals((byte) j, keyBytes[j]);
else assertEquals((byte)j+1, keyBytes[j]);
}
String[] templateAttributes = rb.getTemplateAttributes();
assertEquals(1, templateAttributes.length);
assertEquals("x-ID: P6R-MEMORY-TEST-7", templateAttributes[0]);
String[] templateNames = rb.getTemplateNames();
if (null != templateNames) assertEquals(0,1);
String[] keyAttributes = rb.getKeyValueAttributes();
assertEquals(2, keyAttributes.length);
assertEquals("Cryptographic Usage Mask: c", keyAttributes[0]);
assertEquals("Description: 1 2 3 4 5 6", keyAttributes[1]);
}
}
sl.freeLibrary();
} catch (Exception e) {
// -> we shoud not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}

KSL JNI KMIP Example 4 - Register a Wrapped Key Request

This example demonstrates a request message with a Register of a Symmetric key.

JniWrappedRegisterKmip.java

package com.p6r.kmipserverlib;
import org.junit.*;
import static org.junit.Assert.assertEquals;
public class JniWrappedRegisterKmip {
@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
System.out.println("@Test - JNICall-WrappedRegisterKMIP");
// -> this parser is multi-thread safe by using JNI monitor locking
// -> use one parser object per server thread is recommended
P6KMIPServerLib sl = new P6KMIPServerLib();
// -> KMIP 1.3 TTLV message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
byte[] testMessage = {
0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x02, 0x50, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x00, 0x48, 0x42, 0x00, 0x69, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x6a, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x92, 0x09, 0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x00, 0x59, (byte)0xee, 0x0d, (byte)0xe8, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x01, (byte)0xf8,
0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x93, 0x08, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x01, (byte)0xd0, 0x42, 0x00, 0x57, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x91, 0x01, 0x00, 0x00, 0x00, 0x38,
0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x30, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x04, 0x78, 0x2d, 0x49, 0x44, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0b, 0x07, 0x00, 0x00, 0x00, 0x11,
0x50, 0x36, 0x52, 0x2d, 0x4d, 0x45, 0x4d, 0x4f, 0x52, 0x59, 0x2d, 0x54, 0x45, 0x53, 0x54, 0x2d, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x8f, 0x01, 0x00, 0x00, 0x01, 0x78,
0x42, 0x00, 0x40, 0x01, 0x00, 0x00, 0x01, 0x70, 0x42, 0x00, 0x42, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x45, 0x01, 0x00, 0x00, 0x00, 0x30,
0x42, 0x00, 0x43, 0x08, 0x00, 0x00, 0x00, 0x28, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x42, 0x00, 0x28, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x46, 0x01, 0x00, 0x00, 0x01, 0x00, 0x42, 0x00, (byte)0x9e, 0x05, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x36, 0x01, 0x00, 0x00, 0x00, 0x78, 0x42, 0x00, (byte)0x94, 0x07, 0x00, 0x00, 0x00, 0x11, 0x31, 0x31, 0x31, 0x2d, 0x32, 0x32, 0x32, 0x2d,
0x64, 0x68, 0x64, 0x68, 0x64, 0x2d, 0x64, 0x64, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x2b, 0x01, 0x00, 0x00, 0x00, 0x50, 0x42, 0x00, 0x11, 0x05, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x5f, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x83, 0x05, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0xc5, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0xcd, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x4e, 0x01, 0x00, 0x00, 0x00, 0x38, 0x42, 0x00, (byte)0x94, 0x07, 0x00, 0x00, 0x00, 0x07, 0x34, 0x35, 0x2d, 0x33, 0x33, 0x34, 0x34, 0x00,
0x42, 0x00, 0x2b, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x38, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0xae, 0x05, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x4d, 0x08, 0x00, 0x00, 0x00, 0x08, (byte)0xab, (byte)0xbc, 0x34, (byte)0xff, (byte)0xf3, (byte)0xd3, 0x5a, 0x0c, 0x42, 0x00, 0x3d, 0x08, 0x00, 0x00, 0x00, 0x04,
0x11, 0x22, 0x33, 0x44, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0xa3, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
};
try {
sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
String libVersion = sl.getLibraryVersion();
System.out.println(libVersion);
// -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
// -> the type of message: TTLC, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
sl.setMessageBuffer(testMessage, KMIPConstants.FORMAT_TTLV);
// -> now we can pull parts of the request message apart, this can be done over and over again if desired
RequestHeader rh = sl.getRequestHeader();
assertEquals("1.3", rh.getProtocolVersion());
// -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
for (int i = 0; i < rh.getBatchCount(); i++) {
BatchItem bi = sl.getBatchItem(i + 1);
if (bi instanceof RegisterWrappedKeyBatchItem) {
RegisterWrappedKeyBatchItem rb = (RegisterWrappedKeyBatchItem) bi;
assertEquals(1, rb.getUniqueBatchId()[0]);
assertEquals(KMIPConstants.OBJECT_SYMMETRICKEY, rb.getObjectType());
assertEquals(KMIPConstants.ALG_AES, rb.getAlgorithm());
assertEquals(KMIPConstants.KEYFORMAT_TRANSYMKEY, rb.getKeyFromatType());
assertEquals(256, rb.getKeyLength());
byte[] keyBytes = rb.getWrappedBytes();
assertEquals(40, keyBytes.length);
for (int j = 0; j < keyBytes.length; j++) {
if (j < 14)
assertEquals((byte) j, keyBytes[j]);
else assertEquals((byte)j+1, keyBytes[j]);
}
// TBD get encrypt params
// TBD get sign params
String[] templateAttributes = rb.getTemplateAttributes();
assertEquals(1, templateAttributes.length);
assertEquals("x-ID: P6R-MEMORY-TEST-7", templateAttributes[0]);
String[] templateNames = rb.getTemplateNames();
if (null != templateNames) assertEquals(0,1);
}
}
sl.freeLibrary();
} catch (Exception e) {
// -> we shoud not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}

KSL JNI KMIP Example 5 - Register Certificate Request

This example demonstrates a request message with a Register Certificate operation. This request uses a X.509 Certificate.

JniRegisterCertKmip.java

package com.p6r.kmipserverlib;
import org.junit.*;
import javax.security.auth.x500.X500Principal;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import static org.junit.Assert.assertEquals;
public class JniRegisterCertKmip {
@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_RegisterCertKMIP() {
System.out.println("@Test - JNICall-RegisterCertKMIP");
// -> this parser is multi-thread safe by using JNI monitor locking
// -> use one parser object per server thread is recommended
P6KMIPServerLib sl = new P6KMIPServerLib();
X509Certificate x509Cert;
// -> KMIP 1.3 TTLV message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
byte[] testMessage = {
0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x03, 0x68, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x00, 0x48, 0x42, 0x00, 0x69, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x6a, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x92, 0x09, 0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x00, 0x5a, 0x19, (byte)0xac, (byte)0xcc, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x03, 0x10,
0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x93, 0x08, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x02, (byte)0xe8, 0x42, 0x00, 0x57, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x91, 0x01, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x13, 0x01, 0x00, 0x00, 0x02, (byte)0xc8, 0x42, 0x00, 0x1d, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x1e, 0x08, 0x00, 0x00, 0x02, (byte)0xaf,
0x30, (byte)0x82, 0x02, (byte)0xab, 0x30, (byte)0x82, 0x02, 0x14, (byte)0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01, 0x03, 0x30, 0x0d, 0x06, 0x09, 0x2a, (byte)0x86, 0x48, (byte)0x86, (byte)0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30,
0x62, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41, 0x31, 0x0e, 0x30, 0x0c, 0x06,
0x03, 0x55, 0x04, 0x07, 0x13, 0x05, 0x39, 0x35, 0x30, 0x33, 0x30, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x09, 0x50, 0x36, 0x52, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x31,
0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x08, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x06, 0x50, 0x36, 0x52,
0x20, 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x37, 0x31, 0x31, 0x32, 0x35, 0x31, 0x37, 0x34, 0x37, 0x35, 0x36, 0x5a, 0x17, 0x0d, 0x31, 0x38, 0x31, 0x31, 0x32, 0x35, 0x31, 0x37, 0x34, 0x37,
0x35, 0x36, 0x5a, 0x30, (byte)0x81, (byte)0xd4, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41,
0x31, 0x0e, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x05, 0x39, 0x35, 0x30, 0x33, 0x30, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x09, 0x50, 0x36, 0x52, 0x2c, 0x20,
0x49, 0x6e, 0x63, 0x2e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03,
0x55, 0x04, 0x0b, 0x13, 0x07, 0x4f, 0x70, 0x74, 0x69, 0x63, 0x73, 0x32, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0d, 0x4b, 0x4d, 0x49, 0x50, 0x20, 0x45, 0x6e, 0x67, 0x69,
0x6e, 0x65, 0x65, 0x72, 0x31, 0x1b, 0x30, 0x19, 0x06, 0x09, 0x2a, (byte)0x86, 0x48, (byte)0x86, (byte)0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x0c, 0x6d, 0x61, 0x72, 0x6b, 0x40, 0x70, 0x36, 0x72, 0x2e, 0x63, 0x6f,
0x6d, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x2a, 0x13, 0x04, 0x4d, 0x61, 0x72, 0x6b, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x04, 0x13, 0x06, 0x4a, 0x6f, 0x73, 0x65, 0x70,
0x68, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x29, 0x13, 0x0e, 0x4d, 0x61, 0x72, 0x6b, 0x20, 0x4b, 0x2e, 0x20, 0x4a, 0x6f, 0x73, 0x65, 0x70, 0x68, 0x30, (byte)0x81, (byte)0x9f, 0x30, 0x0d, 0x06,
0x09, 0x2a, (byte)0x86, 0x48, (byte)0x86, (byte)0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, (byte)0x81, (byte)0x8d, 0x00, 0x30, (byte)0x81, (byte)0x89, 0x02, (byte)0x81, (byte)0x81, 0x00, (byte)0xc1, (byte)0xa4, (byte)0xc5, (byte)0xd5, (byte)0xa1, (byte)0x93, (byte)0x95, 0x40, (byte)0x88,
0x4b, 0x58, 0x69, (byte)0xdc, 0x74, 0x13, 0x14, 0x63, 0x5b, (byte)0xbc, 0x0c, 0x5b, 0x53, (byte)0xca, (byte)0x96, (byte)0x87, 0x25, 0x44, 0x0c, 0x2e, (byte)0xc3, (byte)0xa4, (byte)0xb2, (byte)0xad, (byte)0xec, 0x45, 0x56, (byte)0x99, (byte)0xc3, 0x00, 0x72, 0x70,
(byte)0xb9, (byte)0x9f, (byte)0xff, (byte)0x96, (byte)0x97, 0x77, 0x33, (byte)0xd2, (byte)0xd2, 0x2b, (byte)0x91, 0x02, 0x28, 0x13, (byte)0xab, 0x0a, 0x51, (byte)0x98, (byte)0xe3, (byte)0xef, 0x36, 0x52, 0x50, 0x5f, 0x6c, (byte)0xe2, 0x60, 0x6d, 0x20, 0x41, (byte)0xf8, 0x16,
0x49, (byte)0x8b, (byte)0xac, 0x6b, (byte)0xc7, (byte)0xb5, 0x26, (byte)0xe1, 0x0e, (byte)0x82, 0x20, 0x6a, (byte)0xfc, (byte)0x8c, (byte)0xd9, (byte)0xa0, 0x0d, 0x1a, (byte)0xc3, (byte)0xd1, (byte)0xe1, (byte)0xff, 0x46, (byte)0xe9, 0x62, 0x70, 0x61, 0x4b, 0x7a, (byte)0xb1, 0x23, (byte)0xd1,
0x52, 0x60, (byte)0x98, 0x09, (byte)0xc9, 0x47, 0x63, (byte)0xd1, 0x00, 0x6c, (byte)0xdd, 0x58, 0x46, (byte)0xdf, (byte)0x93, (byte)0xfc, 0x1d, (byte)0xd5, (byte)0xed, (byte)0xef, (byte)0xfd, (byte)0xb2, (byte)0xa9, 0x02, 0x03, 0x01, 0x00, 0x01, 0x30, 0x0d, 0x06, 0x09,
0x2a, (byte)0x86, 0x48, (byte)0x86, (byte)0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, (byte)0x81, (byte)0x81, 0x00, (byte)0xb6, (byte)0xfe, (byte)0x93, 0x25, 0x4d, (byte)0x87, (byte)0xc8, 0x14, (byte)0xd3, (byte)0xf9, 0x6d, 0x32, 0x06, 0x48, (byte)0xb0, 0x27, (byte)0xfd,
(byte)0x8d, (byte)0x93, 0x52, 0x7e, 0x3e, (byte)0xd8, (byte)0xa7, 0x02, (byte)0xd5, (byte)0xbd, (byte)0xf1, (byte)0xfb, 0x2f, (byte)0x9d, 0x7d, 0x53, (byte)0xa2, 0x25, 0x77, 0x42, (byte)0x9e, 0x67, 0x1b, 0x14, 0x67, 0x36, (byte)0xac, 0x51, 0x26, 0x7a, 0x0c, 0x61,
0x6f, (byte)0xd1, 0x6c, 0x32, 0x41, (byte)0xad, 0x4b, (byte)0xc5, 0x15, (byte)0xe3, (byte)0xb4, 0x6f, 0x2f, (byte)0x89, (byte)0xa1, 0x55, 0x3c, 0x7a, 0x11, (byte)0xef, 0x78, 0x0b, (byte)0x81, 0x6f, (byte)0xde, (byte)0x97, 0x5a, (byte)0xe9, (byte)0xd8, (byte)0x8e, (byte)0xd7, (byte)0x93,
0x64, (byte)0xe3, 0x67, 0x78, (byte)0x8d, 0x0a, 0x1e, (byte)0xfc, (byte)0xfc, (byte)0xe6, (byte)0xa5, 0x00, (byte)0xeb, 0x52, 0x1e, 0x74, 0x58, 0x4c, (byte)0x95, (byte)0x93, 0x16, (byte)0xd1, (byte)0xee, 0x6c, 0x53, (byte)0xc6, (byte)0xcc, (byte)0xf2, 0x1f, (byte)0x95, (byte)0x9a, (byte)0x86,
0x46, (byte)0xa6, 0x57, 0x16, 0x40, 0x77, 0x10, (byte)0xbe, 0x34, 0x57, 0x36, (byte)0x90, 0x3a, 0x33, 0x59, 0x00
};
try {
sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
String libVersion = sl.getLibraryVersion();
System.out.println(libVersion);
// -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
// -> the type of message: TTLC, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
sl.setMessageBuffer(testMessage, KMIPConstants.FORMAT_TTLV);
// -> now we can pull parts of the request message apart, this can be done over and over again if desired
RequestHeader rh = sl.getRequestHeader();
assertEquals("1.3", rh.getProtocolVersion());
// -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
// -> register certificate request with out any attributes sent with the certificate
for (int i = 0; i < rh.getBatchCount(); i++) {
BatchItem bi = sl.getBatchItem(i + 1);
if (bi instanceof RegisterCertificateBatchItem) {
RegisterCertificateBatchItem rb = (RegisterCertificateBatchItem) bi;
assertEquals(1, rb.getUniqueBatchId()[0]);
assertEquals(KMIPConstants.CERT_X509, rb.getCertificateType());
byte[] certBytes = rb.getCertificate();
// -> convert the certificate bytes into a Java certificate object
InputStream in = new ByteArrayInputStream(certBytes);
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
x509Cert = (X509Certificate) certFactory.generateCertificate(in);
X500Principal subject = x509Cert.getSubjectX500Principal();
System.out.println( "X509 Certificate subject: " + subject.getName());
assertEquals("2.5.4.41=#130e4d61726b204b2e204a6f73657068,2.5.4.4=#13064a6f73657068,2.5.4.42=#13044d61726b,1.2.840.113549.1.9.1=#160c6d61726b407036722e636f6d,CN=KMIP Engineer,OU=Optics2,OU=Engineering,O=P6R\\, Inc.,L=95030,ST=CA,C=US", subject.getName());
}
}
sl.freeLibrary();
} catch (Exception e) {
// -> we shoud not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}

KSL JNI KMIP Example 6 - Delete Attribute Request

This example demonstrates a request message with a Delete Attribute operation. This request deletes the "Contact Informatio" attribute with the default index of zero.

JniDeleteAttributeKmip.java

package com.p6r.kmipserverlib;
import org.junit.*;
import javax.security.auth.x500.X500Principal;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import static org.junit.Assert.assertEquals;
public class JniDeleteAttributeKmip {
@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
System.out.println("@Test - JNICall-DeleteAttributeKMIP");
P6KMIPServerLib sl = new P6KMIPServerLib();
// -> KMIP 1.3 TTLV message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
byte[] testMessage = {
0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x00, (byte) 0xc0, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x00, 0x58, 0x42, 0x00, 0x69, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x6a, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x50, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x27, 0x10, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0x92, 0x09, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x29, (byte) 0xcf, (byte) 0xe7, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x00, 0x58, 0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00,
0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x00, 0x40, 0x42, 0x00, (byte) 0x94, 0x07, 0x00, 0x00, 0x00, 0x14, 0x41, 0x57, 0x41, 0x78, 0x77, 0x36, 0x47, 0x6b, 0x6f, 0x45, 0x67, 0x45, 0x36, 0x79, 0x65, 0x6b,
0x35, 0x61, 0x4c, 0x6a, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x13, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x63, 0x74, 0x20, 0x49, 0x6e, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00
};
// -> test the different forms of the KMIP 2.0 Delete Attribute operation
String testMessage20_1 = "<RequestMessage>\n" +
"<RequestHeader>\n" +
" <ProtocolVersion>\n" +
" <ProtocolVersionMajor type=\"Integer\" value=\"2\"/>\n" +
" <ProtocolVersionMinor type=\"Integer\" value=\"0\"/>\n" +
" </ProtocolVersion>\n" +
" <BatchCount type=\"Integer\" value=\"1\"/>\n" +
"</RequestHeader>\n" +
"<BatchItem>\n" +
" <Operation type=\"Enumeration\" value=\"DeleteAttribute\"/>\n" +
" <RequestPayload>\n" +
" <UniqueIdentifier type=\"TextString\" value=\"e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f\"/>\n" +
" <CurrentAttribute>\n" +
" <Name>\n" +
" <NameValue type=\"TextString\" value=\"Fake-Modify-test-23\"/>\n" +
" <NameType type=\"Enumeration\" value=\"UninterpretedTextString\"/>\n" +
" </Name>\n" +
" </CurrentAttribute>\n" +
" </RequestPayload>\n" +
"</BatchItem>\n" +
"</RequestMessage>";
String testMessage20_2 = "<RequestMessage>\n" +
"<RequestHeader>\n" +
" <ProtocolVersion>\n" +
" <ProtocolVersionMajor type=\"Integer\" value=\"2\"/>\n" +
" <ProtocolVersionMinor type=\"Integer\" value=\"0\"/>\n" +
" </ProtocolVersion>\n" +
" <BatchCount type=\"Integer\" value=\"1\"/>\n" +
"</RequestHeader>\n" +
"<BatchItem>\n" +
" <Operation type=\"Enumeration\" value=\"DeleteAttribute\"/>\n" +
" <RequestPayload>\n" +
" <UniqueIdentifier type=\"TextString\" value=\"e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f\"/>\n" +
" <AttributeReference type=\"Enumeration\" value=\"AlternativeName\"/>\n" +
" </RequestPayload>\n" +
"</BatchItem>\n" +
"</RequestMessage>";
String testMessage20_3 = "<RequestMessage>\n" +
"<RequestHeader>\n" +
" <ProtocolVersion>\n" +
" <ProtocolVersionMajor type=\"Integer\" value=\"2\"/>\n" +
" <ProtocolVersionMinor type=\"Integer\" value=\"0\"/>\n" +
" </ProtocolVersion>\n" +
" <BatchCount type=\"Integer\" value=\"1\"/>\n" +
"</RequestHeader>\n" +
"<BatchItem>\n" +
" <Operation type=\"Enumeration\" value=\"DeleteAttribute\"/>\n" +
" <RequestPayload>\n" +
" <UniqueIdentifier type=\"TextString\" value=\"e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f\"/>\n" +
" <AttributeReference><VendorIdentification type=\"TextString\" value=\"P6R\"/><AttributeName type=\"TextString\" value=\"SKC-client\" /></AttributeReference>\n" +
" </RequestPayload>\n" +
"</BatchItem>\n" +
"</RequestMessage>";
try {
sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
String libVersion = sl.getLibraryVersion();
System.out.println(libVersion);
// [A] Test delete attribute KMIP 1.x version
// -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
// -> the type of message: TTLC, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
sl.setMessageBuffer(testMessage, KMIPConstants.FORMAT_TTLV);
// -> now we can pull parts of the request message apart, this can be done over and over again if desired
RequestHeader rh = sl.getRequestHeader();
assertEquals("1.0", rh.getProtocolVersion());
// -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
// -> register certificate request with out any attributes sent with the certificate
for (int i = 0; i < rh.getBatchCount(); i++) {
BatchItem bi = sl.getBatchItem(i + 1);
if (bi instanceof DeleteAttributeBatchItem) {
DeleteAttributeBatchItem rb = (DeleteAttributeBatchItem) bi;
// -> there is no batch id required if there is only one batch item in a message
assertEquals(null, rb.getUniqueBatchId());
String uniqueId = rb.getUniqueId();
assertEquals("AWAxw6GkoEgE6yek5aLj", uniqueId);
String attribName = rb.getAttributeName();
assertEquals("Contact Information", attribName);
int index = rb.getIndex();
assertEquals(0, index);
}
}
// [B] Test KMIP 2.0 version of delete attribute operation, test various versions of the operation
// -> current attribute only
sl.setMessageBuffer(testMessage20_1.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
rh = sl.getRequestHeader();
assertEquals("2.0", rh.getProtocolVersion());
for (int i = 0; i < rh.getBatchCount(); i++) {
BatchItem bi = sl.getBatchItem(i + 1);
if (bi instanceof DeleteAttribute2BatchItem) {
DeleteAttribute2BatchItem rb = (DeleteAttribute2BatchItem) bi;
String uniqueId = rb.getUniqueId();
assertEquals("e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f", uniqueId);
String currentAttribute = rb.getCurrentAttribute();
assertEquals("Name: Fake-Modify-test-23 - text_string", currentAttribute);
String attributeReference = rb.getAttributeReference();
assertEquals(null, attributeReference);
}
}
// -> attribute reference as a text string
sl.setMessageBuffer(testMessage20_2.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
rh = sl.getRequestHeader();
assertEquals("2.0", rh.getProtocolVersion());
for (int i = 0; i < rh.getBatchCount(); i++) {
BatchItem bi = sl.getBatchItem(i + 1);
if (bi instanceof DeleteAttribute2BatchItem) {
DeleteAttribute2BatchItem rb = (DeleteAttribute2BatchItem) bi;
String uniqueId = rb.getUniqueId();
assertEquals("e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f", uniqueId);
String currentAttribute = rb.getCurrentAttribute();
assertEquals(null, currentAttribute);
String attributeReference = rb.getAttributeReference();
assertEquals("Alternative Name", attributeReference);
}
}
// -> attribute reference as a structure
sl.setMessageBuffer(testMessage20_3.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
rh = sl.getRequestHeader();
assertEquals("2.0", rh.getProtocolVersion());
for (int i = 0; i < rh.getBatchCount(); i++) {
BatchItem bi = sl.getBatchItem(i + 1);
if (bi instanceof DeleteAttribute2BatchItem) {
DeleteAttribute2BatchItem rb = (DeleteAttribute2BatchItem) bi;
String uniqueId = rb.getUniqueId();
assertEquals("e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f", uniqueId);
String currentAttribute = rb.getCurrentAttribute();
assertEquals(null, currentAttribute);
String attributeReference = rb.getAttributeReference();
assertEquals("P6R-SKC-client", attributeReference);
}
}
sl.freeLibrary();
} catch (Exception e) {
// -> we shoud not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}

KSL JNI KMIP Example 7 - Multi-batch item Response

This example demonstrates a response back to a KMIP client with two Create operations in it. This example also shows how to return an Error response back to a client. The responses are generated in TTLV, and XML.

JniResponse1Kmip.java

package com.p6r.kmipserverlib;
import org.junit.*;
import static org.junit.Assert.assertEquals;
public class JniResponse1Kmip {
@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_Response1KMIP() {
System.out.println("@Test - JNICall-Respnse1KMIP");
// -> this parser is multi-thread safe by using JNI monitor locking
// -> use one parser object per server thread is recommended
P6KMIPServerLib sl = new P6KMIPServerLib();
try {
sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
// [A] Generate a response message in TTLV
sl.startResponseMessage(2, KMIPConstants.FORMAT_TTLV, 4);
byte[] batchId1 = { 0x01, 0x02, 0x03, 0x04 };
CreateResponse cr1 = new CreateResponse(batchId1, KMIPConstants.OBJECT_SYMMETRICKEY, "1111-2222-3333-4444-5555", null, null);
sl.addBatchItem(cr1);
Attribute state = new Attribute( KMIPConstants.ATTRIB_STATE, 0, KMIPConstants.STATE_PREACTIVE );
Attribute[] attributes = new Attribute[1];
attributes[0] = state;
byte[] batchId2 = { 0x11, 0x22, 0x33 };
CreateResponse cr2 = new CreateResponse(batchId2, KMIPConstants.OBJECT_SECRETDATA, "AB11-2222-33ef-4444-555", attributes, null);
sl.addBatchItem(cr2);
byte[] responseBuffer = sl.endResponseMessage(0);
for( int j=0; j < responseBuffer.length; j++ ) {
System.out.print(responseBuffer[j]);
}
System.out.println( "\n" );
// [B] Do the same thing in XML
sl.startResponseMessage(2, KMIPConstants.FORMAT_MSGXML, 3);
sl.addBatchItem(cr1);
sl.addBatchItem(cr2);
byte[] responseBuffer2 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer2));
System.out.println("\n");
// [C] Error response
sl.startResponseMessage(2, KMIPConstants.FORMAT_MSGXML, 2);
ErrorResponse rp = new ErrorResponse(batchId2, KMIPConstants.OP_DECRYPT, KMIPConstants.RESULT_FAILED, KMIPConstants.REASON_OPREATION_NOTSUPPORTED, "This is for client logging");
sl.addBatchItem(cr1);
sl.addBatchItem(rp);
byte[] responseBuffer3 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer3));
System.out.println("\n");
// [D] An error response when a request cannot be parsed
sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 2);
ErrorResponse rp1 = new ErrorResponse(null, KMIPConstants.OP_NONE, KMIPConstants.RESULT_FAILED, KMIPConstants.REASON_INVALID_MESSAGE, "This is for client logging");
sl.addBatchItem(rp1);
byte[] errorResponse1 = sl.endResponseMessage(0);
System.out.println(new String(errorResponse1));
System.out.println("\n");
// [E] Lets do a single response with no batch Id assuming that the client request did not have one
sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 2);
CreateResponse cr = new CreateResponse(null, KMIPConstants.OBJECT_SYMMETRICKEY, "1111-2222-3333-4444-5555", null, null);
sl.addBatchItem(cr);
byte[] responseBuffer4 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer4));
System.out.println("\n");
// [F] Show a response with an attribute list and a name list as part of a template-attribute structure
sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 4);
NameAttribute nameOne = new NameAttribute(new String("Name One"), KMIPConstants.NAME_TEXTSTR);
NameAttribute nameTwo = new NameAttribute(new String("Name Two"), KMIPConstants.NAME_TEXTSTR);
NameAttribute[] names = new NameAttribute[2];
names[0] = nameOne;
names[1] = nameTwo;
CreateResponse crfull = new CreateResponse(null, KMIPConstants.OBJECT_SYMMETRICKEY, "AAAA-BBBB-3333-4444-5555", attributes, names);
sl.addBatchItem(crfull);
byte[] responseBuffer5 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer5));
System.out.println("\n");
// [G] Return an empty version response
sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 2);
VersionResponse vr1 = new VersionResponse(null, null);
sl.addBatchItem(vr1);
byte[] responseBuffer6 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer6));
System.out.println("\n");
// [H] Crypto operation responses
byte[] batchId11 = { 0x00, 0x00, 0x00, 0x00 };
byte[] batchId12 = { 0x00, 0x00, 0x00, 0x01 };
byte[] batchId13 = { 0x00, 0x00, 0x00, 0x02 };
byte[] batchId14 = { 0x00, 0x00, 0x00, 0x03 };
byte[] batchId15 = { 0x00, 0x00, 0x00, 0x04 };
byte[] batchId16 = { 0x00, 0x00, 0x00, 0x05 };
byte[] batchId17 = { 0x00, 0x00, 0x00, 0x06 };
byte[] plainText1 = { 0x02, (byte)0x87, (byte)0xab, (byte)0xcd, 0x45 };
byte[] cipherText1 = { 0x22, (byte)0x87, (byte)0xab, 0x00, (byte)0xcd, 0x45, 0x3B, 0x00, 0x4B, 0x5C };
byte[] cipherText2 = { 0x22, (byte)0x87, (byte)0xab, (byte)0xcd, 0x45, 0x3B, 0x4B, 0x5C };
byte[] IV = { 0x0A, 0x0A, 0x0A, 0x0A};
byte[] correlation = { 0x34, 0x35, 0x37, 0x35, 0x31, 0x35, 0x38, 0x36, 0x2D, 0x66, 0x33, 0x30, 0x63, 0x2D, 0x34, 0x61, 0x39, 0x33, 0x2D, 0x61, 0x34, 0x39, 0x66, 0x2D, 0x34, 0x62, 0x34, 0x37, 0x32, 0x66, 0x33, 0x66, 0x62, 0x61, 0x31, 0x32 };
byte[] tagData = { 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11 };
sl.startResponseMessage(7, KMIPConstants.FORMAT_MSGXML, 4);
// -> er3 and er4 are streaming encrypt responses
DecryptResponse dr = new DecryptResponse(batchId11, "123-9393-333", plainText1);
SignResponse sr = new SignResponse(batchId12, "123-9393-333", cipherText2);
EncryptResponse er1 = new EncryptResponse(batchId13, "abcd-383837-3938eie", cipherText1, null);
EncryptResponse er2 = new EncryptResponse(batchId14, "abcd-383837-3938eie", cipherText1, IV);
er2.setAuthenticatedEncryptionTag(tagData);
EncryptResponse er3 = new EncryptResponse(batchId15, "abcd-383837-3938eie", cipherText1, null, correlation);
EncryptResponse er4 = new EncryptResponse(batchId16, "abcd-383837-3938eie", null, null, correlation);
SignatureVerifyResponse sv = new SignatureVerifyResponse(batchId17, "abcd-383837-3938eie", null, KMIPConstants.VALIDITY_INVALID);
sl.addBatchItem(dr);
sl.addBatchItem(sr);
sl.addBatchItem(er1);
sl.addBatchItem(er2);
sl.addBatchItem(er3);
sl.addBatchItem(er4);
sl.addBatchItem(sv);
byte[] responseBuffer7 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer7));
System.out.println("\n");
// [C] MACs
sl.startResponseMessage(2, KMIPConstants.FORMAT_MSGXML, 2);
MACResponse macResponse = new MACResponse(null, "abcd-383837-3938eie", cipherText1 );
MACVerifyResponse verifyResponse = new MACVerifyResponse(null, "abcd-383837-3938eie", KMIPConstants.VALIDITY_VALID);
sl.addBatchItem(macResponse);
sl.addBatchItem(verifyResponse);
byte[] responseBuffer8 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer8));
System.out.println("\n");
sl.freeLibrary();
} catch (Exception e) {
// -> we should not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}

KSL JNI KMIP Example 8 - Many Attribute Response

This example demonstrates a response back to a KMIP client returning a larget list of different types of attributes. Specifically it shows a response to a Get Attributes Request message from a client. The response is generated in TTLV, XML, and JSON.

JniResponse2Kmip.java

package com.p6r.kmipserverlib;
import org.junit.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import static org.junit.Assert.assertEquals;
public class JniResponse2Kmip {
@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_Response2KMIP() {
System.out.println("@Test - JNICall-Respnse2KMIP");
// -> this parser is multi-thread safe by using JNI monitor locking
// -> use one parser object per server thread is recommended
P6KMIPServerLib sl = new P6KMIPServerLib();
try {
sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
// [A] Generate a response message in TTLV
sl.startResponseMessage(1, KMIPConstants.FORMAT_TTLV, 2);
byte[] batchId1 = {0x01, 0x02, 0x03, 0x04};
byte[] fingerPrint = {0x11, 0x22, 0x33, 0x44, 0x55};
long timeStamp = System.currentTimeMillis();
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss Z");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Date today = new Date(timeStamp);
System.out.println("Unix Time stamp: " + timeStamp + ", date: " + df.format(today) + "\n");
Attribute comment = new Attribute(KMIPConstants.ATTRIB_COMMENTS, 0, "attribute test case");
Attribute state = new Attribute(KMIPConstants.ATTRIB_STATE, 0, KMIPConstants.STATE_ACTIVE);
Attribute keyAlg = new Attribute(KMIPConstants.ATTRIB_CRYPTOALGORITHM, 0, KMIPConstants.ALG_AES);
Attribute fresh = new Attribute(KMIPConstants.ATTRIB_FRESH, 0, false);
Attribute cryptoLength = new Attribute(KMIPConstants.ATTRIB_CRYPTOLENGTH, 0, new Integer(128));
Attribute cryptoMask = new Attribute(KMIPConstants.ATTRIB_CRYPTOUSAGEMASK, 0, new Integer((KMIPConstants.USAGE_MASK_ENCRYPT | KMIPConstants.USAGE_MASK_DECRYPT)));
Attribute initialDate = new Attribute(KMIPConstants.ATTRIB_INITIALDATE, 0, new Long(timeStamp));
// -> ** NOTE **, that setting fields in CryptographicParameters that are not supported by the KMIP protocol version chosen can result in the following
// exception: com.p6r.kmipserverlib.P6KMIPLibraryException: p6javakmipserverlib.endResponseMessage failed to generate KMIP message [ eInvalidVersion ],
// see comments in CryptographicParameters for details
CryptograhicParameters keyParams = new CryptograhicParameters( KMIPConstants.MODE_ECB, KMIPConstants.PAD_PKCS5, -1, KMIPConstants.ROLE_KEK, -1, KMIPConstants.ALG_AES, false, -1, -1, -1, -1, -1, -1, -1, -1, -1, null, -1 );
Attribute keyCryptoParams = new Attribute(0, keyParams);
// -> can also be a "x-" custom attribute if a KMIP client placed such an attribute on a managed object
CustomAttribute ca1 = new CustomAttribute("y-ServerSpecific1", true);
Attribute custom1 = new Attribute(0, ca1);
CustomAttribute ca2 = new CustomAttribute("x-clientSpecific1", "a b c d e f");
Attribute custom2 = new Attribute(0, ca2);
CustomAttribute ca3 = new CustomAttribute("x-clientSpecific11", 55);
Attribute custom3 = new Attribute(0, ca3);
CustomAttribute ca4 = new CustomAttribute("y-fingerprint", fingerPrint);
Attribute custom4 = new Attribute(0, ca4);
NameAttribute name = new NameAttribute( "Client-Defined-ID: 456", KMIPConstants.NAME_TEXTSTR);
Attribute name1 = new Attribute(KMIPConstants.ATTRIB_NAME, 0, name);
NameAttribute altName = new NameAttribute("192.10.3.25", KMIPConstants.ALTNAME_IPADDRESS);
Attribute name2 = new Attribute(KMIPConstants.ATTRIB_ALTERNATIVENAME, 0, altName);
NameAttribute keyLoc = new NameAttribute("http://abc.hsm.com/12345", KMIPConstants.KVLT_URI);
Attribute name3 = new Attribute(KMIPConstants.ATTRIB_KEYVALUELOCATION, 0, keyLoc);
byte[] fakeDigest = { (byte)0xAA, (byte)0xBB, (byte)0xCC, (byte)0xDD, (byte)0xEE, (byte)0xFF, 0x00, (byte)0x99, (byte)0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11};
DigestAttribute da = new DigestAttribute(KMIPConstants.HASH_SHA512, fakeDigest, KMIPConstants.KEYFORMAT_TRANSYMKEY);
Attribute digest1 = new Attribute(0, da);
// -> old style certificate info
String[] alternativeNames = new String[3];
alternativeNames[0] = "frank@p6r.com";
alternativeNames[1] = "192.34.5.689";
alternativeNames[2] = "jni-example.p6r.com";
CertificateNameAttribute cn = new CertificateNameAttribute("CN=Sample Cert, OU=R&D, O=Company Ltd., L=Dublin 4, S=Dublin, C=IE", alternativeNames);
Attribute certName1 = new Attribute(KMIPConstants.ATTRIB_CERTIFICATEISSUER, cn);
// -> X.509 style certificate info, all info is fake here, just demonstrating how to pass it into the API
byte[] X509DN_fake = { 0x25, 0x00, 0x00, 0x00, 0x34 };
byte[] alt1bytes = { 0x01, 0x02, 0x03, 0x00, 0x04 };
byte[] alt2bytes = { 0x00, 0x71, 0x52, 0x23, 0x14, 0x0F };
ByteArrayObject alt1 = new ByteArrayObject( alt1bytes );
ByteArrayObject alt2 = new ByteArrayObject( alt2bytes );
ByteArrayObject[] X509alternativeNames = new ByteArrayObject[2];
X509alternativeNames[0] = alt1;
X509alternativeNames[1] = alt2;
X509CertificateNameAttribute x509CN = new X509CertificateNameAttribute(X509DN_fake, X509alternativeNames);
Attribute x509CertName1 = new Attribute(KMIPConstants.ATTRIB_X509CERTIFICATESUBJECT, x509CN);
CertificateIdentifierAttribute ci = new CertificateIdentifierAttribute("CN=Sample Cert, OU=R&D, O=Company Ltd., L=York, S=York, C=IE", "12345ABD9384");
Attribute certIdentifier = new Attribute(KMIPConstants.ATTRIB_CERTIFICATEIDENTIFIER, ci);
RevocationReasonAttribute rr = new RevocationReasonAttribute("Key exceeded its usage limits", KMIPConstants.REVOCATION_KEYCOMPROMISE);
Attribute revocation = new Attribute(rr);
NameAttribute link = new NameAttribute("UID-111-333-000", KMIPConstants.LINK_PRIVATEKEY);
Attribute privateLink = new Attribute(KMIPConstants.ATTRIB_LINK, 0, link);
UsageLimitsAttribute ul = new UsageLimitsAttribute(50000000, 2000000, KMIPConstants.USAGE_LIMITS_BYTE);
Attribute usageLimits = new Attribute(ul);
ApplicationSpecificInfoAttribute as = new ApplicationSpecificInfoAttribute("P6R", "#12345678990000#");
Attribute appInfo = new Attribute(1,as);
Attribute digitalSigAlg = new Attribute(KMIPConstants.ATTRIB_DIGITALSIGALG, 0, KMIPConstants.SIG_ECDSASHA1);
Attribute certSubjectCN = new Attribute(KMIPConstants.ATTRIB_CERTSUBJECT_CN, 0, "www.example.com"); // -> server can return parts of a certificate so that simple clients do not have to parse the X.509 encoding
Attribute[] attributes = new Attribute[25];
attributes[0] = comment;
attributes[1] = state;
attributes[2] = fresh;
attributes[3] = cryptoLength;
attributes[4] = initialDate;
attributes[5] = keyAlg;
attributes[6] = cryptoMask;
attributes[7] = custom1;
attributes[8] = custom2;
attributes[9] = custom3;
attributes[10] = custom4;
attributes[11] = keyCryptoParams;
attributes[12] = name1;
attributes[13] = name2;
attributes[14] = name3;
attributes[15] = digest1;
attributes[16] = certName1;
attributes[17] = x509CertName1;
attributes[18] = certIdentifier;
attributes[19] = revocation;
attributes[20] = privateLink;
attributes[21] = usageLimits;
attributes[22] = appInfo;
attributes[23] = digitalSigAlg;
attributes[24] = certSubjectCN;
GetAttributesResponse ga = new GetAttributesResponse(batchId1, "1111-2222-3333-4444-5555", attributes);
sl.addBatchItem(ga);
byte[] responseBuffer = sl.endResponseMessage(0);
for (int j = 0; j < responseBuffer.length; j++) {
System.out.print(responseBuffer[j]);
}
System.out.println("\n");
// [B] Do the same thing in XML
sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 2);
sl.addBatchItem(ga);
byte[] responseBuffer2 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer2));
System.out.println("\n");
// [C] Do the same thing in JSON
sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGJSON, 2);
sl.addBatchItem(ga);
byte[] responseBuffer3 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer3));
System.out.println("\n");
// [D] Add Attribute just takes a single attribute
sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 1);
AddAttributeResponse aa = new AddAttributeResponse(batchId1, "UID-383837jjejej", state);
sl.addBatchItem(aa);
byte[] responseBuffer4 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer4));
System.out.println("\n");
// [E] Return a template response
sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 2);
GetTemplateResponse tr = new GetTemplateResponse(null, "1111-2222-3333-4444-5555", attributes);
sl.addBatchItem(tr);
byte[] responseBuffer5 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer5));
System.out.println("\n");
sl.freeLibrary();
} catch (Exception e) {
// -> we should not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}

KSL JNI KMIP Example 9 - Locate Response

This example demonstrates a the following response messages: Locate (with and without unique identifiers), Check, Get Attribute List, and Discover Versions. The response are generated in TTLV, XML, and JSON.

JniResponse3Kmip.java

package com.p6r.kmipserverlib;
import org.junit.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import static org.junit.Assert.assertEquals;
public class JniResponse3Kmip {
@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_Response3KMIP() {
System.out.println("@Test - JNICall-Respnse3KMIP");
// -> this parser is multi-thread safe by using JNI monitor locking
// -> use one parser object per server thread is recommended
P6KMIPServerLib sl = new P6KMIPServerLib();
try {
sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
// [A] Generate a response message in TTLV
sl.startResponseMessage(6, KMIPConstants.FORMAT_TTLV, 1);
byte[] batchId1 = {0x01, 0x02, 0x03, 0x04};
byte[] batchId2 = {(byte) 0xFF, (byte) 0xFB, (byte) 0xFC, (byte) 0xFD};
byte[] batchId3 = { 0x07, 0x09 };
byte[] batchId4 = { 0x15 };
byte[] batchId5 = { 0x00, 0x00, 0x00, 0x20 };
byte[] batchId6 = { 0x11, 0x07, 0x00, 0x20 };
String[] attributeNames = new String[5];
attributeNames[0] = new String("Cryptographic Usage Mask");
attributeNames[1] = new String("Cryptographic Algorithm");
attributeNames[2] = new String("Cryptographic Length");
attributeNames[3] = new String("y-ServerSpecific1");
attributeNames[4] = new String("Digest");
GetAttributeListResponse gal = new GetAttributeListResponse(batchId1, "1111-2222-3333-4444-5555", attributeNames);
sl.addBatchItem(gal);
String[] locatedObjects = new String[3];
locatedObjects[0] = new String("aaa-bbb-ccc-ddd-eee-fff");
locatedObjects[1] = new String("1111-222-7777-3333-9696969");
locatedObjects[2] = new String("aklfakjakieiei-oioioioioi");
LocateResponse lr = new LocateResponse(batchId2, locatedObjects);
sl.addBatchItem(lr);
// -> found no objects to return
LocateResponse lr2 = new LocateResponse(batchId6, null);
sl.addBatchItem(lr2);
String[] versions = new String[3];
versions[0] = "1.2";
versions[1] = "1.1";
versions[2] = "1.0";
VersionResponse vr = new VersionResponse(batchId3, versions);
sl.addBatchItem(vr);
CheckResponse cr = new CheckResponse(batchId4, "UID-3939393939", (KMIPConstants.USAGE_MASK_DECRYPT | KMIPConstants.USAGE_MASK_ENCRYPT), new Integer(300));
sl.addBatchItem(cr);
int[] operations = new int[5];
operations[0] = KMIPConstants.OP_CREATE;
operations[1] = KMIPConstants.OP_REGISTER;
operations[2] = KMIPConstants.OP_REKEY;
operations[3] = KMIPConstants.OP_LOCATE;
operations[4] = KMIPConstants.OP_GET;
int[] objectTypes = new int[5];
objectTypes[0] = KMIPConstants.OBJECT_CERTIFICATE;
objectTypes[1] = KMIPConstants.OBJECT_SYMMETRICKEY;
objectTypes[2] = KMIPConstants.OBJECT_SECRETDATA;
objectTypes[3] = KMIPConstants.OBJECT_SPLITKEY;
objectTypes[4] = KMIPConstants.OBJECT_OPAQUE;
RNGParameters[] RNGs = new RNGParameters[2];
RNGs[0] = new RNGParameters(KMIPConstants.RNGALG_FIPS186_2, KMIPConstants.ALG_AES, 256, KMIPConstants.HASH_SHA256, -1, -1, KMIPConstants.FIPS186_GPXCHANGENOTICE, 1 );
RNGs[1] = new RNGParameters(KMIPConstants.RNGALG_UNSPECIFIED, -1, -1, -1, -1, -1, -1, -1 );
CapabilityInformation[] Caps = new CapabilityInformation[2];
Caps[0] = new CapabilityInformation(-1, 1, 2, 1, 1, KMIPConstants.UNWRAPMODE_PROCESSED, KMIPConstants.ONDESTROY_METADATA_SHREDDED, KMIPConstants.SHREDALG_UNSUPPORTED, KMIPConstants.RNGMODE_SHARED);
Caps[1] = new CapabilityInformation(1, 1, 1, KMIPConstants.UNWRAPMODE_PROCESSED, KMIPConstants.ONDESTROY_METADATA_SHREDDED, KMIPConstants.SHREDALG_CRYPTOGRAPHIC, KMIPConstants.RNGMODE_SHARED);
ProfileInformation[] profiles = new ProfileInformation[4];
profiles[0] = new ProfileInformation(ProfileConstants.PN_28_SERVER_V12 );
profiles[1] = new ProfileInformation(ProfileConstants.PN_5_SERVER_V12, "/kmip/abc", 6423);
profiles[2] = new ProfileInformation(ProfileConstants.PN_10_SERVER_V10, null, 7834);
profiles[3] = new ProfileInformation(ProfileConstants.PN_40_SERVER_V10, "/kmip2", -1);
String[] profileNames = new String[2];
profileNames[0] = new String("Profile1");
profileNames[1] = new String("Profile2");
ValidationInformation[] validations = new ValidationInformation[2];
validations[0] = new ValidationInformation(KMIPConstants.VALAUTH_NISTCMVP, 2, KMIPConstants.VALTYPE_HYBRID, 3 );
validations[1] = new ValidationInformation(KMIPConstants.VALAUTH_UNSPECIFIED, "US", "http://us.export.gov/v", 2, 0, KMIPConstants.VALTYPE_SOFTWARE, 1, "ABCD13874", "http://us.export.gov/certs", "https://www.p6r.com/val1", profileNames );
ExtensionInformation[] extensions = new ExtensionInformation[4];
extensions[0] = new ExtensionInformation("Extension1");
extensions[1] = new ExtensionInformation("Extension2", 0x80000010, KMIPConstants.TYPE_BIGINTEGER );
extensions[2] = new ExtensionInformation("Extension3", 0x80000001, -1);
extensions[3] = new ExtensionInformation("Extension4", -1, KMIPConstants.TYPE_ENUMERATION);
String[] namespaces = new String[2];
namespaces[0] = new String("Namespace 1");
namespaces[1] = new String("Namespace 2");
int[] attestationTypes = new int[2];
attestationTypes[0] = KMIPConstants.ATTEST_SAML_ASSERTION;
attestationTypes[1] = KMIPConstants.ATTEST_TPM_QUOTE;
// -> KMIP spec makes Server Information a server defined structure
// Our message generator handles this by allowing an application to pass in a TTLV encoded byte string (without the enclosing KMIP Structure)
// Our library places the enclosing KMIP structure around the passed in value. The TTLV passed in MUST be a multiple of 8 bytes in length.
// Here is an example of a TTLV encoded bytes string:
// A 3 byte extension tag value, followed by a one byte "byte string" type indicator, a length field indicating a byte string of 4 bytes padded with zeros to make it a multiple of 8
byte[] fakeServerInfo = { (byte)0x80, 0x01, 0x02, 0x08, 0x00, 0x00, 0x00, 0x04, (byte)0xFF, (byte)0xFE, (byte)0xFD, (byte)0xFE, 0x00, 0x00, 0x00, 0x00 };
QueryResponse qr = new QueryResponse(batchId5, operations, objectTypes, "Project6Research, Inc", fakeServerInfo, null );
qr.setRNGParameters(RNGs);
qr.setCapabilities(Caps);
qr.setProfiles(profiles);
qr.setValidations(validations);
qr.setExtensions(extensions);
qr.setAppNamespace(namespaces);
qr.setAttestation(attestationTypes);
sl.addBatchItem(qr);
byte[] responseBuffer = sl.endResponseMessage(0);
for (int j = 0; j < responseBuffer.length; j++) {
System.out.print(responseBuffer[j]);
}
System.out.println("\n");
// [B] Do the same thing in XML
sl.startResponseMessage(6, KMIPConstants.FORMAT_MSGXML, 4);
sl.addBatchItem(gal);
sl.addBatchItem(lr);
sl.addBatchItem(lr2);
sl.addBatchItem(vr);
sl.addBatchItem(cr);
sl.addBatchItem(qr);
byte[] responseBuffer2 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer2));
System.out.println("\n");
// [C] Do the same thing in JSON
sl.startResponseMessage(6, KMIPConstants.FORMAT_MSGJSON, 4);
sl.addBatchItem(gal);
sl.addBatchItem(lr);
sl.addBatchItem(lr2);
sl.addBatchItem(vr);
sl.addBatchItem(cr);
sl.addBatchItem(qr);
byte[] responseBuffer3 = sl.endResponseMessage(0);
System.out.println(new String(responseBuffer3));
System.out.println("\n");
sl.freeLibrary();
} catch (Exception e) {
// -> we should not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}