Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniGetKmip.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 JniGetKmip {
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 
49  @Test
50  public void JNICall_GetKMIP() {
51  System.out.println("@Test - JNICall-GetKMIP");
52 
53  // -> this parser is multi-thread safe by using JNI monitor locking
54  // -> use one parser object per server thread is recommended
55  P6KMIPServerLib sl = new P6KMIPServerLib();
56 
57  // -> KMIP 1.3 TTLV message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
58  byte[] testMessage = {
59  0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x01, 0x48, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x00, 0x68,
60  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,
61  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,
62  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,
63  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,
64  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,
65  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,
66  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,
67  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,
68  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,
69  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 };
70 
71  try {
72  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
73 
74  String libVersion = sl.getLibraryVersion();
75  System.out.println(libVersion);
76 
77  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
78  // -> 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
79  sl.setMessageBuffer(testMessage, KMIPConstants.FORMAT_TTLV);
80 
81  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
82  RequestHeader rh = sl.getRequestHeader();
83  assertEquals("1.3", rh.getProtocolVersion());
84  assertEquals(KMIPConstants.BATCHOPTION_UNDO, rh.getBatchErrorOption());
85  assertEquals(true, rh.getBatchOrderOption());
86  assertEquals(1, rh.getBatchCount());
87  assertEquals("Fri Oct 13 10:02:25 PDT 2017", rh.getTimeStamp().toString());
88 
89  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
90  for (int i = 0; i < rh.getBatchCount(); i++) {
91 
92  BatchItem bi = sl.getBatchItem(i + 1);
93  if (bi instanceof GetBatchItem) {
94  GetBatchItem cb = (GetBatchItem) bi;
95  assertEquals(1, cb.getUniqueBatchId()[0]);
96  assertEquals("111-222-333-444", cb.getUniqueId());
97  assertEquals(KMIPConstants.KEYFORMAT_PKCS8, cb.getKeyFormatType());
98 
99  KeyWrappingSpecification kws = cb.getWrappingDetails();
100  assertEquals(KMIPConstants.WRAP_ENCRYPT, kws.getMethod());
101  String[] attributeNames = kws.getAttributeNames();
102  assertEquals("State", attributeNames[0]);
103  assertEquals("KEK-345-4456", kws.getEncryptKeyUID());
104 
105  CryptograhicParameters cyp = kws.getEncryptParams();
106  assertEquals(2, cyp.getInitialCounterValue());
107  assertEquals(KMIPConstants.PAD_PKCS5, cyp.getPaddingMethod());
108  assertEquals(KMIPConstants.MODE_CTR, cyp.getBlockCipherMode());
109  }
110  }
111  sl.freeLibrary();
112 
113  } catch (Exception e) {
114  // -> we shoud not get here
115  System.out.println(e.toString());
116  assertEquals(0, 1);
117  }
118  }
119 }
void JNICall_GetKMIP()
Test: Verify parser can break message into multiple batch items.
Definition: JniGetKmip.java:50
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
Definition: JniGetKmip.java:18