Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniRekeyKeyPairKmip.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 import static org.junit.Assert.assertEquals;
7 
17 public class JniRekeyKeyPairKmip {
18 
19  @BeforeClass
20  public static void oneTimeSetUp() {
21  // NOOP
22  System.out.println("@BeforeClass - oneTimeSetUp");
23  }
24 
25  @AfterClass
26  public static void oneTimeTearDown() {
27  // NOOP
28  System.out.println("@AfterClass - oneTimeTearDown");
29  }
30 
31  @Before
32  public void setUp() {
33  // NOOP
34  System.out.println("@Before - setUp");
35  }
36 
37  @After
38  public void tearDown() {
39  // NOOP
40  System.out.println("@After - tearDown");
41  }
42 
48  @Test
49  public void JNICall_RekeyKeyPairKMIP() {
50  System.out.println("@Test - JNICall-RekeyKeyPairKMIP");
51 
52  // -> this parser is multi-thread safe by using JNI monitor locking
53  // -> use one parser object per server thread is recommended
54  P6KMIPServerLib sl = new P6KMIPServerLib();
55 
56  // -> KMIP 1.3 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
57  String testMessage1 =
58  "<RequestMessage><RequestHeader>" +
59  "<ProtocolVersion> " +
60  "<ProtocolVersionMajor type=\"Integer\" value=\"1\"/> " +
61  "<ProtocolVersionMinor type=\"Integer\" value=\"3\"/> " +
62  "</ProtocolVersion> " +
63  "<BatchCount type=\"Integer\" value=\"1\"/> " +
64  "</RequestHeader> " +
65  "<BatchItem> " +
66  "<Operation type=\"Enumeration\" value=\"ReKeyKeyPair\"/> " +
67  "<RequestPayload> " +
68  "<PrivateKeyUniqueIdentifier type=\"TextString\" value=\"AAaed7e7-2972-4fc8-9073-26a5e8f88b9e\"/> " +
69  "<PrivateKeyTemplateAttribute> " +
70  "<Attribute> " +
71  "<AttributeName type=\"TextString\" value=\"Name\"/> " +
72  "<AttributeValue> " +
73  "<NameValue type=\"TextString\" value=\"TC-81-13-privatekey1\"/> " +
74  "<NameType type=\"Enumeration\" value=\"UninterpretedTextString\"/> " +
75  "</AttributeValue> " +
76  "</Attribute> " +
77  "<Attribute> " +
78  "<AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/> " +
79  "<AttributeValue type=\"Integer\" value=\"0x00000001\"/> " +
80  "</Attribute> " +
81  "</PrivateKeyTemplateAttribute> " +
82  "<PublicKeyTemplateAttribute> " +
83  "<Attribute> " +
84  "<AttributeName type=\"TextString\" value=\"Name\"/> " +
85  "<AttributeValue> " +
86  "<NameValue type=\"TextString\" value=\"TC-81-13-publickey1\"/> " +
87  "<NameType type=\"Enumeration\" value=\"UninterpretedTextString\"/> " +
88  "</AttributeValue> " +
89  "</Attribute> " +
90  "<Attribute> " +
91  "<AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/> " +
92  "<AttributeValue type=\"Integer\" value=\"0x00000002\"/> " +
93  "</Attribute> " +
94  "</PublicKeyTemplateAttribute> " +
95  "</RequestPayload> " +
96  "</BatchItem></RequestMessage>";
97 
98  String testMessage2 =
99  "<RequestMessage><RequestHeader>" +
100  "<ProtocolVersion> " +
101  "<ProtocolVersionMajor type=\"Integer\" value=\"1\"/> " +
102  "<ProtocolVersionMinor type=\"Integer\" value=\"3\"/> " +
103  "</ProtocolVersion> " +
104  "<BatchCount type=\"Integer\" value=\"1\"/> " +
105  "</RequestHeader> " +
106  "<BatchItem> " +
107  "<Operation type=\"Enumeration\" value=\"ReKey\"/> " +
108  "<RequestPayload> " +
109  "<UniqueIdentifier type=\"TextString\" value=\"AAaed7e7-2972-4fc8-9073-26a5e8f88b9e\"/> " +
110  "<Offset type=\"Interval\" value=\"55\" />" +
111  "<TemplateAttribute> " +
112  "<Attribute> " +
113  "<AttributeName type=\"TextString\" value=\"Name\"/> " +
114  "<AttributeValue> " +
115  "<NameValue type=\"TextString\" value=\"TC-81-13-privatekey1\"/> " +
116  "<NameType type=\"Enumeration\" value=\"UninterpretedTextString\"/> " +
117  "</AttributeValue> " +
118  "</Attribute> " +
119  "<Attribute> " +
120  "<AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/> " +
121  "<AttributeValue type=\"Integer\" value=\"0x00000001\"/> " +
122  "</Attribute> " +
123  "</TemplateAttribute> " +
124  "</RequestPayload> " +
125  "</BatchItem></RequestMessage>";
126 
127  try {
128  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
129 
130  String libVersion = sl.getLibraryVersion();
131  System.out.println(libVersion);
132 
133  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
134  // -> 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
135  sl.setMessageBuffer(testMessage1.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
136 
137  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
138  RequestHeader rh = sl.getRequestHeader();
139  assertEquals("1.3", rh.getProtocolVersion());
140 
141  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
142  for (int i = 0; i < rh.getBatchCount(); i++) {
143 
144  BatchItem bi = sl.getBatchItem(i + 1);
145  if (bi instanceof RekeyKeyPairBatchItem) {
146  RekeyKeyPairBatchItem ck = (RekeyKeyPairBatchItem) bi;
147 
148  // -> batch id is not required if only one batch item is present
149  byte[] batchId = ck.getUniqueBatchId();
150  assertEquals(null, batchId);
151 
152  String uniqueIdentifier = ck.getUniqueId();
153  assertEquals("AAaed7e7-2972-4fc8-9073-26a5e8f88b9e", uniqueIdentifier);
154 
155  // when client does not set the return value should be a -1, to distinguish an offset of zero set by the client
156  int intervalOffset = ck.getOffset();
157  assertEquals(-1, intervalOffset);
158 
159  // test that attributes are as expected from message above
160  String[] commonAttribs = ck.getCommonAttributes();
161  assertEquals(null, commonAttribs);
162 
163  String[] commonNames = ck.getCommonNames();
164  assertEquals(null, commonNames);
165 
166  String[] privateAttribs = ck.getPrivateAttributes();
167  assertEquals(2, privateAttribs.length);
168  assertEquals("Name: TC-81-13-privatekey1 - text_string", privateAttribs[0]);
169  assertEquals("Cryptographic Usage Mask: 1", privateAttribs[1]);
170 
171  String[] publicAttribs = ck.getPublicAttributes();
172  assertEquals(2, publicAttribs.length);
173  assertEquals("Name: TC-81-13-publickey1 - text_string", publicAttribs[0]);
174  assertEquals("Cryptographic Usage Mask: 2", publicAttribs[1]);
175  }
176  }
177 
178 
179  // -> verify that the rekey also works
180  sl.setMessageBuffer(testMessage2.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
181 
182  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
183  rh = sl.getRequestHeader();
184  assertEquals("1.3", rh.getProtocolVersion());
185 
186  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
187  for (int i = 0; i < rh.getBatchCount(); i++) {
188 
189  BatchItem bi = sl.getBatchItem(i + 1);
190  if (bi instanceof RekeyBatchItem) {
191  RekeyBatchItem ck = (RekeyBatchItem) bi;
192 
193  // -> batch id is not required if only one batch item is present
194  byte[] batchId = ck.getUniqueBatchId();
195  assertEquals(null, batchId);
196 
197  String uniqueIdentifier = ck.getUniqueId();
198  assertEquals("AAaed7e7-2972-4fc8-9073-26a5e8f88b9e", uniqueIdentifier);
199 
200  // when client does not set the return value should be a -1, to distinguish an offset of zero set by the client
201  int intervalOffset = ck.getOffset();
202  assertEquals(55, intervalOffset);
203 
204  String[] privateAttribs = ck.getAttributes();
205  assertEquals(2, privateAttribs.length);
206  assertEquals("Name: TC-81-13-privatekey1 - text_string", privateAttribs[0]);
207  assertEquals("Cryptographic Usage Mask: 1", privateAttribs[1]);
208  }
209 
210  }
211  sl.freeLibrary();
212 
213  } catch (Exception e) {
214  // -> we shoud not get here
215  System.out.println(e.toString());
216  assertEquals(0, 1);
217  }
218  }
219 }
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
void JNICall_RekeyKeyPairKMIP()
Test: Verify parser can handle an XML formated Create Key Pair operation.