Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniBasicKmip.java
Go to the documentation of this file.
1 package com.p6r.kmipserverlib;
2 
3 import org.junit.*;
4 
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.assertNotEquals;
7 
8 
18 public class JniBasicKmip {
19 
20  @BeforeClass
21  public static void oneTimeSetUp() {
22  // NOOP
23  System.out.println("@BeforeClass - oneTimeSetUp");
24  }
25 
26  @AfterClass
27  public static void oneTimeTearDown() {
28  // NOOP
29  System.out.println("@AfterClass - oneTimeTearDown");
30  }
31 
32  @Before
33  public void setUp() {
34  // NOOP
35  System.out.println("@Before - setUp");
36  }
37 
38  @After
39  public void tearDown() {
40  // NOOP
41  System.out.println("@After - tearDown");
42  }
43 
51  @Test
52  public void JNICall_BasicKMIP() {
53  System.out.println("@Test - JNICall-BasicKMIP");
54 
55  // -> this parser is multi-thread safe by using JNI monitor locking
56  // -> use one parser object per server thread is recommended
57  P6KMIPServerLib sl = new P6KMIPServerLib();
58 
59  // -> KMIP 1.3 TTLV message with 4 batch items generated by P6R's Secure KMIP Client (SKC)
60  byte[] testMessage = {
61  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,
62  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,
63  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,
64  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,
65  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,
66  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,
67  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,
68  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,
69  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,
70  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,
71  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,
72  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,
73  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,
74  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,
75  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,
76  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,
77  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,
78  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,
79  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,
80  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,
81  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,
82  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,
83  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,
84  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,
85  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,
86  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,
87  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,
88  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,
89  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};
90 
91  // 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
92  byte[] testMessageBroken = {
93  0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x00, (byte)0xc0, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x00, 0x78,
94  0x42, 0x00, 0x69, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x6a, 0x02, 0x00, 0x00, 0x00, 0x04,
95  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04,
96  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0c, 0x01, 0x00, 0x00, 0x00, 0x38,
97  0x42, 0x00, 0x23, 0x01, 0x00, 0x00, 0x00, 0x30, 0x42, 0x00, 0x24, 0x05, 0x00, 0x00, 0x00, 0x04,
98  (byte)0x80, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x25, 0x08, 0x00, 0x00, 0x00, 0x18,
99  0x54, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x10, 0x54, 0x00, 0x04, 0x07, 0x00, 0x00, 0x00, 0x03,
100  0x44, 0x42, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04,
101  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x00, 0x38,
102  0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00,
103  0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, (byte)0x94, 0x07, 0x00, 0x00, 0x00, 0x18,
104  0x41, 0x57, 0x4d, 0x6b, 0x73, 0x65, 0x70, 0x69, 0x4a, 0x54, 0x62, 0x5a, 0x49, 0x73, 0x6f, 0x5f,
105  0x30, 0x58, 0x6f, 0x72, 0x00, 0x00, 0x00, 0x00
106  };
107 
108  try {
109  sl.initializeLibrary(P6KMIPServerLib.FLAGS_ALLLOG);
110 
111  String libVersion = sl.getLibraryVersion();
112  System.out.println(libVersion);
113 
114  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
115  // -> 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
116  sl.setMessageBuffer(testMessage, KMIPConstants.FORMAT_TTLV);
117 
118  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
119  RequestHeader rh = sl.getRequestHeader();
120  assertEquals(true, rh.getAsynchIndicator());
121  assertEquals(500, rh.getMaxResponseSize());
122  assertEquals("1.3", rh.getProtocolVersion());
123  assertEquals(KMIPConstants.BATCHOPTION_UNDO, rh.getBatchErrorOption());
124  assertEquals(true, rh.getBatchOrderOption());
125  assertEquals(4, rh.getBatchCount());
126  String msgDate = rh.getTimeStamp().toString();
127  assertEquals("Mon Oct 09 18:50:30 PDT 2017", msgDate );
128 
129  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
130  for (int i = 0; i < rh.getBatchCount(); i++) {
131 
132  BatchItem bi = sl.getBatchItem(i + 1);
133  if (bi instanceof CreateBatchItem) {
134  CreateBatchItem cb = (CreateBatchItem) bi;
135  assertEquals(KMIPConstants.OBJECT_SYMMETRICKEY, cb.getObjectType());
136  assertEquals(i + 1, cb.getUniqueBatchId()[0]);
137 
138  String[] attributes = cb.getAttributes();
139  if (3 == attributes.length) {
140  assertEquals("Cryptographic Algorithm: 3", attributes[0]); // AES
141  assertEquals("Cryptographic Length: 128", attributes[1]); // 128 bits
142  assertEquals("Cryptographic Usage Mask: c", attributes[2]); // 0x0C is ENCRYPT and DECRYPT
143 
144  } else if (4 == attributes.length) {
145  assertEquals("Cryptographic Algorithm: 26", attributes[0]); // ECC
146  assertEquals("Cryptographic Length: 256", attributes[1]); // 256 bits
147  assertEquals("Cryptographic Usage Mask: 3", attributes[2]); // 0x03 is SIGN and VERIFY
148  assertEquals("Description: 1 2 3 4 5 6", attributes[3]); // attribute added to KMIP 1.3
149 
150  } else {
151  assertEquals(0, attributes.length);
152  }
153 
154  } else if (bi instanceof QueryBatchItem) {
155  QueryBatchItem qb = (QueryBatchItem) bi;
156  assertEquals(3, qb.getUniqueBatchId()[0]);
157 
158  int[] queryFunctions = qb.getQueryFunctions();
159  assertEquals(9, queryFunctions.length);
160  assertEquals(KMIPConstants.QUERY_OPERATIIONS, queryFunctions[0]);
161  assertEquals(KMIPConstants.QUERY_OBJECTS, queryFunctions[1]);
162  assertEquals(KMIPConstants.QUERY_SERVERINFORMATION, queryFunctions[2]);
163  assertEquals(KMIPConstants.QUERY_ATTENSTATIONTYPES, queryFunctions[3]);
164  assertEquals(KMIPConstants.QUERY_CAPABILITIES, queryFunctions[4]);
165  assertEquals(KMIPConstants.QUERY_PROFILES, queryFunctions[5]);
166  assertEquals(KMIPConstants.QUERY_VALIDATIONS, queryFunctions[6]);
167  assertEquals(KMIPConstants.QUERY_EXTENSIONLIST, queryFunctions[7]);
168  assertEquals(KMIPConstants.QUERY_REGMETHODS, queryFunctions[8]);
169 
170  } else if (bi instanceof VersionBatchItem) {
171  // -> in the above message the client sent no versions
172  VersionBatchItem vb = (VersionBatchItem) bi;
173  assertEquals(4, vb.getUniqueBatchId()[0]);
174  ProtocolVersion[] versions = vb.getProtocolVersions();
175  if (null != versions) assertEquals(0,1);
176  }
177  }
178  sl.freeLibrary();
179 
180  } catch (Exception e) {
181  // -> we shoud not get here
182  System.out.println(e.toString());
183  assertEquals(0, 1);
184  }
185  }
186 }
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
void JNICall_BasicKMIP()
Test: Verify parser can break message into multiple batch items.