Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniMsgExtensionKmip.java
Go to the documentation of this file.
1 package com.p6r.kmipserverlib;
2 
3 import org.junit.*;
4 
5 import java.nio.charset.Charset;
6 
7 import static org.junit.Assert.assertEquals;
8 
18 public class JniMsgExtensionKmip {
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_MsgExtenisonKMIP() {
51  System.out.println("@Test - JNICall-MsgExtensionKMIP");
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 XML message containing a Message Extension, taken from TC-71-12 -
58  String testMessage1 =
59  "<RequestMessage><RequestHeader>\n" +
60  " <ProtocolVersion>\n" +
61  " <ProtocolVersionMajor type=\"Integer\" value=\"1\"/>\n" +
62  " <ProtocolVersionMinor type=\"Integer\" value=\"2\"/>\n" +
63  " </ProtocolVersion>\n" +
64  " <BatchCount type=\"Integer\" value=\"1\"/>\n" +
65  " </RequestHeader>\n" +
66  " <BatchItem>\n" +
67  " <Operation type=\"Enumeration\" value=\"Create\"/>\n" +
68  " <RequestPayload>\n" +
69  " <ObjectType type=\"Enumeration\" value=\"SymmetricKey\"/>\n" +
70  " <TemplateAttribute>\n" +
71  " <Attribute>\n" +
72  " <AttributeName type=\"TextString\" value=\"Cryptographic Length\"/>\n" +
73  " <AttributeValue type=\"Integer\" value=\"128\"/>\n" +
74  " </Attribute>\n" +
75  " <Attribute>\n" +
76  " <AttributeName type=\"TextString\" value=\"Cryptographic Algorithm\"/>\n" +
77  " <AttributeValue type=\"Enumeration\" value=\"AES\"/>\n" +
78  " </Attribute>\n" +
79  " <Attribute>\n" +
80  " <AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/>\n" +
81  " <AttributeValue type=\"Integer\" value=\"Decrypt Encrypt\"/>\n" +
82  " </Attribute>\n" +
83  " <Attribute>\n" +
84  " <AttributeName type=\"TextString\" value=\"x-ID\"/>\n" +
85  " <AttributeValue type=\"TextString\" value=\"TC-71-12\"/>\n" +
86  " </Attribute>\n" +
87  " </TemplateAttribute>\n" +
88  " </RequestPayload>\n" +
89  " <MessageExtension>\n" +
90  " <VendorIdentification type=\"TextString\" value=\"Acme\"/>\n" +
91  " <CriticalityIndicator type=\"Boolean\" value=\"true\"/>\n" +
92  " <VendorExtension>\n" +
93  " <TTLV tag=\"0x540001\" type=\"TextString\" value=\"na\"/>\n" +
94  " </VendorExtension>\n" +
95  " </MessageExtension>\n" +
96  " </BatchItem>\n" +
97  "</RequestMessage>";
98 
99 
100  // -> similar message as the XML see if vendor extension has a value
101  byte[] testMessage2 = {
102  0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x01, (byte)0x88, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x00, 0x38, 0x42, 0x00, 0x69, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x6a, 0x02, 0x00, 0x00, 0x00, 0x04,
103  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04,
104  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x01, 0x40, 0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
105  0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x00, (byte)0xe8, 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)0xd0,
106  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,
107  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,
108  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,
109  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, 0x18,
110  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,
111  0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x04, 0x78, 0x2d, 0x49, 0x44, 0x00, 0x00, 0x00, 0x00,
112  0x42, 0x00, 0x0b, 0x07, 0x00, 0x00, 0x00, 0x08, 0x54, 0x43, 0x2d, 0x37, 0x31, 0x2d, 0x31, 0x32, 0x42, 0x00, 0x51, 0x01, 0x00, 0x00, 0x00, 0x38, 0x42, 0x00, (byte)0x9d, 0x07, 0x00, 0x00, 0x00, 0x04,
113  0x41, 0x63, 0x6d, 0x65, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x26, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x9c, 0x01, 0x00, 0x00, 0x00, 0x10,
114  0x54, 0x00, 0x01, 0x07, 0x00, 0x00, 0x00, 0x02, 0x6e, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
115 
116  byte expectedExtension[] = { 84, 0, 1, 7, 0, 0, 0, 2, 110, 97, 0, 0, 0, 0, 0, 0 };
117 
118  try {
119  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
120 
121  String libVersion = sl.getLibraryVersion();
122  System.out.println(libVersion);
123 
124  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
125  // -> the type of message: TTLV, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
126  sl.setMessageBuffer(testMessage1.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
127 
128  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
129  RequestHeader rh = sl.getRequestHeader();
130  assertEquals("1.2", rh.getProtocolVersion());
131 
132  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
133  for (int i = 0; i < rh.getBatchCount(); i++) {
134 
135  BatchItem bi = sl.getBatchItem(i + 1);
136  if (bi instanceof CreateBatchItem) {
137  CreateBatchItem ck = (CreateBatchItem) bi;
138 
139  // -> batch id is not required if only one batch item is present
140  byte[] batchId = ck.getUniqueBatchId();
141  assertEquals(null, batchId);
142 
143  // -> Note, in TTLV encoded messages we can return the VendorExtension back as a byte array,
144  // but in XML and JSON we cannot do that instead we have to build into the API specific supported structures
145  MessageExtension me = ck.getExtension();
146  assertEquals("Acme", me.getVendorIdentification());
147  assertEquals(true, me.getCriticalityIndicator()); // made this true in the XML to show we can get both true/false values
148 
149  String[] attributes = ck.getAttributes();
150  assertEquals("Cryptographic Length: 128", attributes[0]); // 128 bits
151  assertEquals("Cryptographic Algorithm: 3", attributes[1]); // AES
152  assertEquals("Cryptographic Usage Mask: c", attributes[2]); // 0x0C is ENCRYPT and DECRYPT
153  assertEquals("x-ID: TC-71-12", attributes[3]);
154  }
155  }
156 
157 
158  // -> Similar message in TTLV show how we can now get to the Vendor Extension field in the Message Extension in a general way
159  sl.setMessageBuffer(testMessage2, KMIPConstants.FORMAT_TTLV );
160 
161  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
162  rh = sl.getRequestHeader();
163  assertEquals("1.2", rh.getProtocolVersion());
164 
165  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
166  for (int i = 0; i < rh.getBatchCount(); i++) {
167 
168  BatchItem bi = sl.getBatchItem(i + 1);
169  if (bi instanceof CreateBatchItem) {
170  CreateBatchItem ck = (CreateBatchItem) bi;
171 
172  // -> batch id is not required if only one batch item is present
173  byte[] batchId = ck.getUniqueBatchId();
174  assertEquals(null, batchId);
175 
176  // -> in TTLV messages we can pull out the Message Extension, Vendor Extension field as an array of bytes for the application to parse
177  MessageExtension me = ck.getExtension();
178  assertEquals("Acme", me.getVendorIdentification());
179  assertEquals(false, me.getCriticalityIndicator());
180  byte[] vendorExtension = me.getVendorExtension();
181  assertEquals(16, vendorExtension.length);
182  for( int q=0; q < 16; q++ ) {
183  assertEquals(expectedExtension[q], vendorExtension[q]);
184  }
185 
186  String[] attributes = ck.getAttributes();
187  assertEquals("Cryptographic Length: 128", attributes[0]); // 128 bits
188  assertEquals("Cryptographic Algorithm: 3", attributes[1]); // AES
189  assertEquals("Cryptographic Usage Mask: c", attributes[2]); // 0x0C is ENCRYPT and DECRYPT
190  assertEquals("x-ID: TC-71-12", attributes[3]);
191  }
192  }
193 
194  sl.freeLibrary();
195 
196  } catch (Exception e) {
197  // -> we shoud not get here
198  System.out.println(e.toString());
199  assertEquals(0, 1);
200  }
201  }
202 }
void JNICall_MsgExtenisonKMIP()
Test: Verify parser can handle an XML formated Create Key Pair operation.
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.