Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniResponse3Kmip.java
Go to the documentation of this file.
1 package com.p6r.kmipserverlib;
2 
3 import org.junit.*;
4 
5 import java.text.DateFormat;
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
8 import java.util.TimeZone;
9 
10 import static org.junit.Assert.assertEquals;
11 
21 public class JniResponse3Kmip {
22 
23  @BeforeClass
24  public static void oneTimeSetUp() {
25  // NOOP
26  System.out.println("@BeforeClass - oneTimeSetUp");
27  }
28 
29  @AfterClass
30  public static void oneTimeTearDown() {
31  // NOOP
32  System.out.println("@AfterClass - oneTimeTearDown");
33  }
34 
35  @Before
36  public void setUp() {
37  // NOOP
38  System.out.println("@Before - setUp");
39  }
40 
41  @After
42  public void tearDown() {
43  // NOOP
44  System.out.println("@After - tearDown");
45  }
46 
52  @Test
53  public void JNICall_Response3KMIP() {
54  System.out.println("@Test - JNICall-Respnse3KMIP");
55 
56  // -> this parser is multi-thread safe by using JNI monitor locking
57  // -> use one parser object per server thread is recommended
58  P6KMIPServerLib sl = new P6KMIPServerLib();
59 
60  try {
61  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
62 
63  // [A] Generate a response message in TTLV
64  sl.startResponseMessage(6, KMIPConstants.FORMAT_TTLV, 1);
65 
66  byte[] batchId1 = {0x01, 0x02, 0x03, 0x04};
67  byte[] batchId2 = {(byte) 0xFF, (byte) 0xFB, (byte) 0xFC, (byte) 0xFD};
68  byte[] batchId3 = { 0x07, 0x09 };
69  byte[] batchId4 = { 0x15 };
70  byte[] batchId5 = { 0x00, 0x00, 0x00, 0x20 };
71  byte[] batchId6 = { 0x11, 0x07, 0x00, 0x20 };
72 
73  String[] attributeNames = new String[5];
74  attributeNames[0] = new String("Cryptographic Usage Mask");
75  attributeNames[1] = new String("Cryptographic Algorithm");
76  attributeNames[2] = new String("Cryptographic Length");
77  attributeNames[3] = new String("y-ServerSpecific1");
78  attributeNames[4] = new String("Digest");
79 
80  GetAttributeListResponse gal = new GetAttributeListResponse(batchId1, "1111-2222-3333-4444-5555", attributeNames);
81  sl.addBatchItem(gal);
82 
83  String[] locatedObjects = new String[3];
84  locatedObjects[0] = new String("aaa-bbb-ccc-ddd-eee-fff");
85  locatedObjects[1] = new String("1111-222-7777-3333-9696969");
86  locatedObjects[2] = new String("aklfakjakieiei-oioioioioi");
87  LocateResponse lr = new LocateResponse(batchId2, locatedObjects);
88  sl.addBatchItem(lr);
89 
90  // -> found no objects to return
91  LocateResponse lr2 = new LocateResponse(batchId6, null);
92  sl.addBatchItem(lr2);
93 
94  String[] versions = new String[3];
95  versions[0] = "1.2";
96  versions[1] = "1.1";
97  versions[2] = "1.0";
98  VersionResponse vr = new VersionResponse(batchId3, versions);
99  sl.addBatchItem(vr);
100 
101  CheckResponse cr = new CheckResponse(batchId4, "UID-3939393939", (KMIPConstants.USAGE_MASK_DECRYPT | KMIPConstants.USAGE_MASK_ENCRYPT), new Integer(300));
102  sl.addBatchItem(cr);
103 
104  int[] operations = new int[5];
105  operations[0] = KMIPConstants.OP_CREATE;
106  operations[1] = KMIPConstants.OP_REGISTER;
107  operations[2] = KMIPConstants.OP_REKEY;
108  operations[3] = KMIPConstants.OP_LOCATE;
109  operations[4] = KMIPConstants.OP_GET;
110 
111  int[] objectTypes = new int[5];
112  objectTypes[0] = KMIPConstants.OBJECT_CERTIFICATE;
113  objectTypes[1] = KMIPConstants.OBJECT_SYMMETRICKEY;
114  objectTypes[2] = KMIPConstants.OBJECT_SECRETDATA;
115  objectTypes[3] = KMIPConstants.OBJECT_SPLITKEY;
116  objectTypes[4] = KMIPConstants.OBJECT_OPAQUE;
117 
118  RNGParameters[] RNGs = new RNGParameters[2];
119  RNGs[0] = new RNGParameters(KMIPConstants.RNGALG_FIPS186_2, KMIPConstants.ALG_AES, 256, KMIPConstants.HASH_SHA256, -1, -1, KMIPConstants.FIPS186_GPXCHANGENOTICE, 1 );
120  RNGs[1] = new RNGParameters(KMIPConstants.RNGALG_UNSPECIFIED, -1, -1, -1, -1, -1, -1, -1 );
121 
122  CapabilityInformation[] Caps = new CapabilityInformation[2];
123  Caps[0] = new CapabilityInformation(-1, 1, 2, 1, 1, KMIPConstants.UNWRAPMODE_PROCESSED, KMIPConstants.ONDESTROY_METADATA_SHREDDED, KMIPConstants.SHREDALG_UNSUPPORTED, KMIPConstants.RNGMODE_SHARED);
124  Caps[1] = new CapabilityInformation(1, 1, 1, KMIPConstants.UNWRAPMODE_PROCESSED, KMIPConstants.ONDESTROY_METADATA_SHREDDED, KMIPConstants.SHREDALG_CRYPTOGRAPHIC, KMIPConstants.RNGMODE_SHARED);
125 
126  ProfileInformation[] profiles = new ProfileInformation[4];
127  profiles[0] = new ProfileInformation(ProfileConstants.PN_28_SERVER_V12 );
128  profiles[1] = new ProfileInformation(ProfileConstants.PN_5_SERVER_V12, "/kmip/abc", 6423);
129  profiles[2] = new ProfileInformation(ProfileConstants.PN_10_SERVER_V10, null, 7834);
130  profiles[3] = new ProfileInformation(ProfileConstants.PN_40_SERVER_V10, "/kmip2", -1);
131 
132  String[] profileNames = new String[2];
133  profileNames[0] = new String("Profile1");
134  profileNames[1] = new String("Profile2");
135  ValidationInformation[] validations = new ValidationInformation[2];
136  validations[0] = new ValidationInformation(KMIPConstants.VALAUTH_NISTCMVP, 2, KMIPConstants.VALTYPE_HYBRID, 3 );
137  validations[1] = new ValidationInformation(KMIPConstants.VALAUTH_UNSPECIFIED, "US", "http://us.export.gov/v", 2, 0, KMIPConstants.VALTYPE_SOFTWARE, 1, "ABCD13874", "http://us.export.gov/certs", "https://www.p6r.com/val1", profileNames );
138 
139  ExtensionInformation[] extensions = new ExtensionInformation[4];
140  extensions[0] = new ExtensionInformation("Extension1");
141  extensions[1] = new ExtensionInformation("Extension2", 0x80000010, KMIPConstants.TYPE_BIGINTEGER );
142  extensions[2] = new ExtensionInformation("Extension3", 0x80000001, -1);
143  extensions[3] = new ExtensionInformation("Extension4", -1, KMIPConstants.TYPE_ENUMERATION);
144 
145  String[] namespaces = new String[2];
146  namespaces[0] = new String("Namespace 1");
147  namespaces[1] = new String("Namespace 2");
148 
149  int[] attestationTypes = new int[2];
150  attestationTypes[0] = KMIPConstants.ATTEST_SAML_ASSERTION;
151  attestationTypes[1] = KMIPConstants.ATTEST_TPM_QUOTE;
152 
153  // -> KMIP spec makes Server Information a server defined structure
154  // Our message generator handles this by allowing an application to pass in a TTLV encoded byte string (without the enclosing KMIP Structure)
155  // Our library places the enclosing KMIP structure around the passed in value. The TTLV passed in MUST be a multiple of 8 bytes in length.
156  // Here is an example of a TTLV encoded bytes string:
157  // A 3 byte extension tag value, followed by a one byte "byte string" type indicator, a length field indicating a byte string of 4 bytes padded with zeros to make it a multiple of 8
158  byte[] fakeServerInfo = { (byte)0x80, 0x01, 0x02, 0x08, 0x00, 0x00, 0x00, 0x04, (byte)0xFF, (byte)0xFE, (byte)0xFD, (byte)0xFE, 0x00, 0x00, 0x00, 0x00 };
159  QueryResponse qr = new QueryResponse(batchId5, operations, objectTypes, "Project6Research, Inc", fakeServerInfo, null );
160  qr.setRNGParameters(RNGs);
161  qr.setCapabilities(Caps);
162  qr.setProfiles(profiles);
163  qr.setValidations(validations);
164  qr.setExtensions(extensions);
165  qr.setAppNamespace(namespaces);
166  qr.setAttestation(attestationTypes);
167  sl.addBatchItem(qr);
168 
169  byte[] responseBuffer = sl.endResponseMessage(0);
170  for (int j = 0; j < responseBuffer.length; j++) {
171  System.out.print(responseBuffer[j]);
172  }
173  System.out.println("\n");
174 
175 
176  // [B] Do the same thing in XML
177  sl.startResponseMessage(6, KMIPConstants.FORMAT_MSGXML, 4);
178 
179  sl.addBatchItem(gal);
180  sl.addBatchItem(lr);
181  sl.addBatchItem(lr2);
182  sl.addBatchItem(vr);
183  sl.addBatchItem(cr);
184  sl.addBatchItem(qr);
185 
186  byte[] responseBuffer2 = sl.endResponseMessage(0);
187  System.out.println(new String(responseBuffer2));
188  System.out.println("\n");
189 
190 
191  // [C] Do the same thing in JSON
192  sl.startResponseMessage(6, KMIPConstants.FORMAT_MSGJSON, 4);
193 
194  sl.addBatchItem(gal);
195  sl.addBatchItem(lr);
196  sl.addBatchItem(lr2);
197  sl.addBatchItem(vr);
198  sl.addBatchItem(cr);
199  sl.addBatchItem(qr);
200 
201  byte[] responseBuffer3 = sl.endResponseMessage(0);
202  System.out.println(new String(responseBuffer3));
203  System.out.println("\n");
204 
205 
206  sl.freeLibrary();
207 
208  } catch (Exception e) {
209  // -> we should not get here
210  System.out.println(e.toString());
211  assertEquals(0, 1);
212  }
213  }
214 }
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
void JNICall_Response3KMIP()
Test: Generate a message with multiple batch items.