Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniRegisterKmip.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 
16 public class JniRegisterKmip {
17 
18  @BeforeClass
19  public static void oneTimeSetUp() {
20  // NOOP
21  System.out.println("@BeforeClass - oneTimeSetUp");
22  }
23 
24  @AfterClass
25  public static void oneTimeTearDown() {
26  // NOOP
27  System.out.println("@AfterClass - oneTimeTearDown");
28  }
29 
30  @Before
31  public void setUp() {
32  // NOOP
33  System.out.println("@Before - setUp");
34  }
35 
36  @After
37  public void tearDown() {
38  // NOOP
39  System.out.println("@After - tearDown");
40  }
41 
47  @Test
48  public void JNICall_RegisterKMIP() {
49  System.out.println("@Test - JNICall-RegisterKMIP");
50 
51  // -> this parser is multi-thread safe by using JNI monitor locking
52  // -> use one parser object per server thread is recommended
53  P6KMIPServerLib sl = new P6KMIPServerLib();
54 
55  // -> KMIP 1.3 TTLV message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
56  byte[] testMessage = {
57  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,
58  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,
59  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,
60  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,
61  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,
62  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,
63  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,
64  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,
65  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,
66  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,
67  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,
68  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,
69  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,
70  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,
71  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,
72  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,
73  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,
74  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,
75  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,
76  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,
77  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
78  };
79 
80  try {
81  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
82 
83  String libVersion = sl.getLibraryVersion();
84  System.out.println(libVersion);
85 
86  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
87  // -> 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
88  sl.setMessageBuffer(testMessage, KMIPConstants.FORMAT_TTLV);
89 
90  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
91  RequestHeader rh = sl.getRequestHeader();
92  assertEquals("1.3", rh.getProtocolVersion());
93 
94 
95  // -> check for message level credentials
96  Credential credOne = sl.getCredential(1);
97  if (credOne instanceof PasswordCredential) {
98  PasswordCredential pc = (PasswordCredential) credOne;
99  assertEquals("Fred", pc.getUserName());
100  assertEquals("password1", pc.getPassword());
101  } else assertEquals(0, 1);
102 
103  Credential credTwo = sl.getCredential(2);
104  if (credTwo instanceof DeviceCredential) {
105  DeviceCredential dc = (DeviceCredential) credTwo;
106  assertEquals("serNum123456", dc.getSerialNumber());
107  assertEquals(null, dc.getPassword());
108  assertEquals("devID2233", dc.getDeviceIdentifier());
109  assertEquals("netID9000", dc.getNetworkIdentifer());
110  assertEquals("machineID1", dc.getMachineIdentifer());
111  assertEquals("mediaID313", dc.getMediaIdentifer());
112  } else assertEquals(1, 2);
113 
114  Credential credThree = sl.getCredential(3);
115  assertEquals(null, credThree);
116 
117 
118  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
119  for (int i = 0; i < rh.getBatchCount(); i++) {
120 
121  BatchItem bi = sl.getBatchItem(i + 1);
122  if (bi instanceof RegisterTransparentSymmetricKeyBatchItem) {
123  RegisterTransparentSymmetricKeyBatchItem rb = (RegisterTransparentSymmetricKeyBatchItem) bi;
124  assertEquals(1, rb.getUniqueBatchId()[0]);
125  assertEquals(KMIPConstants.ALG_AES, rb.getAlgorithm());
126  byte[] keyBytes = rb.getKeyMaterial();
127  assertEquals(32, keyBytes.length);
128  for (int j = 0; j < keyBytes.length; j++) {
129  if (j < 14)
130  assertEquals((byte) j, keyBytes[j]);
131  else assertEquals((byte)j+1, keyBytes[j]);
132  }
133 
134  String[] templateAttributes = rb.getTemplateAttributes();
135  assertEquals(1, templateAttributes.length);
136  assertEquals("x-ID: P6R-MEMORY-TEST-7", templateAttributes[0]);
137 
138  String[] templateNames = rb.getTemplateNames();
139  if (null != templateNames) assertEquals(0,1);
140 
141  String[] keyAttributes = rb.getKeyValueAttributes();
142  assertEquals(2, keyAttributes.length);
143  assertEquals("Cryptographic Usage Mask: c", keyAttributes[0]);
144  assertEquals("Description: 1 2 3 4 5 6", keyAttributes[1]);
145  }
146  }
147  sl.freeLibrary();
148 
149  } catch (Exception e) {
150  // -> we shoud not get here
151  System.out.println(e.toString());
152  assertEquals(0, 1);
153  }
154  }
155 }
void JNICall_RegisterKMIP()
Test: Verify parser can break message into multiple batch items.
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.