Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniDecryptKmip.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 JniDecryptKmip {
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_DecryptKMIP() {
50  System.out.println("@Test - JNICall-DecryptKMIP");
51 
52  byte[] expectedData = { (byte)0xEC, (byte)0x82, 0x0D, (byte)0xDF, (byte)0xA9, 0x62, (byte)0xDE, 0x6C, 0x49, 0x13, 0x6B, (byte)0xAD, 0x1A, (byte)0xE2, 0x71,
53  0x69, 0x77, (byte)0xF1, (byte)0xE1, (byte)0xEA, (byte)0xE6, 0x75, (byte)0x98, 0x51, (byte)0x94, 0x36, 0x5F, (byte)0x81, 0x3C, 0x20,
54  0x41, 0x5F, 0x29, (byte)0xAD, (byte)0xE1, 0x57, (byte)0xD7, (byte)0xA9, (byte)0xFC, (byte)0xA7, 0x03, 0x23, (byte)0x85, 0x3A, 0x54,
55  0x55, (byte)0xFC, 0x45 };
56 
57  byte[] expectedIV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
58 
59  // -> this parser is multi-thread safe by using JNI monitor locking
60  // -> use one parser object per server thread is recommended
61  P6KMIPServerLib sl = new P6KMIPServerLib();
62 
63  // -> KMIP 1.2 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
64  String testMessage = "{\"tag\":\"RequestMessage\", \"value\":[{\"tag\":\"RequestHeader\", \"value\":[{\"tag\":\"ProtocolVersion\", \"value\":[{\"tag\":\"ProtocolVersionMajor\", \"type\":\"Integer\", \"value\":1}, {\"tag\":\"ProtocolVersionMinor\", \"type\":\"Integer\", \"value\":2}]}, {\"tag\":\"BatchCount\", \"type\":\"Integer\", \"value\":1}]}, {\"tag\":\"BatchItem\", \"value\":[{\"tag\":\"Operation\", \"type\":\"Enumeration\", \"value\":\"Decrypt\"}, {\"tag\":\"RequestPayload\", \"value\":[{\"tag\":\"UniqueIdentifier\", \"type\":\"TextString\", \"value\":\"36af82da-d120-44ef-8dd1-9dd218ab9d4a\"}, {\"tag\":\"CryptographicParameters\", \"value\":[{\"tag\":\"BlockCipherMode\", \"type\":\"Enumeration\", \"value\":\"CBC\"}, {\"tag\":\"PaddingMethod\", \"type\":\"Enumeration\", \"value\":\"PKCS5\"}]}, {\"tag\":\"Data\", \"type\":\"ByteString\", \"value\":\"EC820DDFA962DE6C49136BAD1AE2716977F1E1EAE675985194365F813C20415F29ADE157D7A9FCA70323853A5455FC45\"}, {\"tag\":\"IVCounterNonce\", \"type\":\"ByteString\", \"value\":\"01020304050607080910111213141516\"}]}]}]}";
65 
66  try {
67  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
68 
69  String libVersion = sl.getLibraryVersion();
70  System.out.println(libVersion);
71 
72  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
73  // -> 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
74  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGJSON);
75 
76  // -> here we test non-streaming encryption request
77  RequestHeader rh = sl.getRequestHeader();
78  assertEquals("1.2", rh.getProtocolVersion());
79 
80  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
81  for (int i = 0; i < rh.getBatchCount(); i++) {
82 
83  BatchItem bi = sl.getBatchItem(i + 1);
84  if (bi instanceof DecryptBatchItem) {
85  DecryptBatchItem ck = (DecryptBatchItem) bi;
86 
87  String uniqueId = ck.getUniqueId();
88  assertEquals("36af82da-d120-44ef-8dd1-9dd218ab9d4a", uniqueId);
89 
90  CryptograhicParameters params = ck.getParams();
91  int mode = params.getBlockCipherMode();
92  assertEquals(mode, KMIPConstants.MODE_CBC);
93  int pad = params.getPaddingMethod();
94  assertEquals(pad, KMIPConstants.PAD_PKCS5);
95 
96  byte[] dataToEncrypt = ck.getData();
97  assertEquals(48, dataToEncrypt.length);
98  for( int j=0; j < dataToEncrypt.length; j++) {
99  assertEquals( expectedData[j], dataToEncrypt[j]);
100  }
101 
102  byte[] IV = ck.getIVCounterNonce();
103  assertEquals(16, IV.length);
104  for( int k=0; k < IV.length; k++) {
105  assertEquals( expectedIV[k], IV[k]);
106  }
107  }
108  }
109  sl.freeLibrary();
110 
111  } catch (Exception e) {
112  // -> we shoud not get here
113  System.out.println(e.toString());
114  assertEquals(0, 1);
115  }
116  }
117 }
118 
void JNICall_DecryptKMIP()
Test: Verify parser can handle an JSON formated Encrypt operation.
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.