Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniDeriveKeyKmip.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 JniDeriveKeyKmip {
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_DeriveKeyKMIP() {
50  System.out.println("@Test - JNICall-DeriveKeyKMIP");
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 testMessage = "<RequestMessage><RequestHeader><ProtocolVersion><ProtocolVersionMajor type=\"Integer\" value=\"1\"/><ProtocolVersionMinor type=\"Integer\" value=\"3\"/></ProtocolVersion><BatchCount type=\"Integer\" value=\"1\"/></RequestHeader><BatchItem><Operation type=\"Enumeration\" value=\"DeriveKey\"/><RequestPayload><ObjectType type=\"Enumeration\" value=\"SymmetricKey\"/><UniqueIdentifier type=\"TextString\" value=\"5daf8487-c50b-43ce-a02f-f1784b8cbc16\"/><UniqueIdentifier type=\"TextString\" value=\"CDFf8487-c50b-43ce-a02f-f1784b8cbc16\"/><DerivationMethod type=\"Enumeration\" value=\"PBKDF2\"/><DerivationParameters><CryptographicParameters><HashingAlgorithm type=\"Enumeration\" value=\"SHA_256\"/></CryptographicParameters><Salt type=\"ByteString\" value=\"73616C74\"/><IterationCount type=\"Integer\" value=\"4096\"/></DerivationParameters><TemplateAttribute><Attribute><AttributeName type=\"TextString\" value=\"Cryptographic Algorithm\"/><AttributeValue type=\"Enumeration\" value=\"AES\"/></Attribute><Attribute><AttributeName type=\"TextString\" value=\"Cryptographic Length\"/><AttributeValue type=\"Integer\" value=\"256\"/></Attribute><Attribute><AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/><AttributeValue type=\"Integer\" value=\"0x0000000C\"/></Attribute></TemplateAttribute></RequestPayload></BatchItem></RequestMessage>";
58 
59  // OR make sure this parses too
60  String testMessage1 ="<RequestMessage><RequestHeader><ProtocolVersion><ProtocolVersionMajor type=\"Integer\" value=\"1\"/><ProtocolVersionMinor type=\"Integer\" value=\"3\"/></ProtocolVersion><BatchCount type=\"Integer\" value=\"1\"/></RequestHeader>" +
61  "<BatchItem>" +
62  "<Operation type=\"Enumeration\" value=\"DeriveKey\"/>\n" +
63  "<RequestPayload>\n" +
64  "<ObjectType type=\"Enumeration\" value=\"SymmetricKey\"/>\n" +
65  "<UniqueIdentifier type=\"TextString\" value=\"5daf8487-c50b-43ce-a02f-f1784b8cbc16\"/>\n" +
66  "<DerivationMethod type=\"Enumeration\" value=\"HASH\"/>\n" +
67  "<DerivationParameters>\n" +
68  "<CryptographicParameters>\n" +
69  "<HashingAlgorithm type=\"Enumeration\" value=\"SHA_256\"/>\n" +
70  "</CryptographicParameters>\n" +
71  "</DerivationParameters>\n" +
72  "<TemplateAttribute>\n" +
73  "<Attribute>\n" +
74  "<AttributeName type=\"TextString\" value=\"Cryptographic Algorithm\"/>\n" +
75  "<AttributeValue type=\"Enumeration\" value=\"AES\"/>\n" +
76  "</Attribute>\n" +
77  "<Attribute>\n" +
78  "<AttributeName type=\"TextString\" value=\"Cryptographic Length\"/>\n" +
79  "<AttributeValue type=\"Integer\" value=\"128\"/>\n" +
80  "</Attribute>\n" +
81  "<Attribute>\n" +
82  "<AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/>\n" +
83  "<AttributeValue type=\"Integer\" value=\"Decrypt Encrypt\"/>\n" +
84  "</Attribute>\n" +
85  "</TemplateAttribute>\n" +
86  "</RequestPayload>\n" +
87  "</BatchItem>\n" +
88  "</RequestMessage>";
89 
90  try {
91  sl.initializeLibrary(P6KMIPServerLib.FLAGS_ALLLOG);
92 
93  String libVersion = sl.getLibraryVersion();
94  System.out.println(libVersion);
95 
96  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
97  // -> 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
98  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
99 
100  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
101  RequestHeader rh = sl.getRequestHeader();
102  assertEquals("1.3", rh.getProtocolVersion());
103 
104  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
105  for (int i = 0; i < rh.getBatchCount(); i++) {
106 
107  BatchItem bi = sl.getBatchItem(i + 1);
108  if (bi instanceof DeriveKeyBatchItem) {
109  DeriveKeyBatchItem ck = (DeriveKeyBatchItem) bi;
110 
111  // -> batch id is not required if only one batch item is present
112  byte[] batchId = ck.getUniqueBatchId();
113  assertEquals(null, batchId);
114 
115  int objectType = ck.getObjectType();
116  assertEquals(KMIPConstants.OBJECT_SYMMETRICKEY, objectType);
117 
118  String[] UIDs = ck.getUniqueIds();
119  assertEquals(2, UIDs.length);
120  assertEquals("5daf8487-c50b-43ce-a02f-f1784b8cbc16", UIDs[0]);
121  assertEquals("CDFf8487-c50b-43ce-a02f-f1784b8cbc16", UIDs[1]);
122 
123  String[] attribNames = ck.getTemplateNames();
124  assertEquals(null, attribNames);
125 
126  String[] attributes = ck.getAttributes();
127  assertEquals(3, attributes.length);
128  assertEquals("Cryptographic Algorithm: 3", attributes[0]);
129  assertEquals("Cryptographic Length: 256", attributes[1]);
130  assertEquals("Cryptographic Usage Mask: c", attributes[2]);
131 
132  int derivationMethod = ck.getDerivationMethod();
133  assertEquals(KMIPConstants.DERIVE_PBKDF2, derivationMethod);
134 
135  DerivationParameters params = ck.getParams();
136  assertEquals(4096, params.getIterationCount());
137  byte[] salt = params.getSalt();
138  assertEquals(0x73, salt[0]);
139  assertEquals(0x61, salt[1]);
140  assertEquals(0x6C, salt[2]);
141  assertEquals(0x74, salt[3]);
142 
143  CryptograhicParameters cp = params.getCryptoParams();
144  assertEquals(KMIPConstants.HASH_SHA256, cp.getHashAlgorithm());
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 }
156 
void JNICall_DeriveKeyKMIP()
Test: Verify parser can handle an XML formated Derive Key operation.
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.