Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniEncryptKmip.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 JniEncryptKmip {
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_EncryptKMIP() {
50  System.out.println("@Test - JNICall-EncryptKMIP");
51 
52  byte[] expectedData = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
53 
54  // -> this parser is multi-thread safe by using JNI monitor locking
55  // -> use one parser object per server thread is recommended
56  P6KMIPServerLib sl = new P6KMIPServerLib();
57 
58  // -> KMIP 1.2 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
59  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\":\"Encrypt\"}, {\"tag\":\"RequestPayload\", \"value\":[{\"tag\":\"UniqueIdentifier\", \"type\":\"TextString\", \"value\":\"c031edb5-fc17-4f1d-a0a7-35ff29fbf130\"}, {\"tag\":\"CryptographicParameters\", \"value\":[{\"tag\":\"BlockCipherMode\", \"type\":\"Enumeration\", \"value\":\"ECB\"}]}, {\"tag\":\"Data\", \"type\":\"ByteString\", \"value\":\"01020304050607080910111213141516\"}]}]}]}";
60 
61  try {
62  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
63 
64  String libVersion = sl.getLibraryVersion();
65  System.out.println(libVersion);
66 
67  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
68  // -> 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
69  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGJSON);
70 
71  // -> here we test non-streaming encryption request
72  RequestHeader rh = sl.getRequestHeader();
73  assertEquals("1.2", rh.getProtocolVersion());
74 
75  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
76  for (int i = 0; i < rh.getBatchCount(); i++) {
77 
78  BatchItem bi = sl.getBatchItem(i + 1);
79  if (bi instanceof EncryptBatchItem) {
80  EncryptBatchItem ck = (EncryptBatchItem) bi;
81 
82  String uniqueId = ck.getUniqueId();
83  assertEquals("c031edb5-fc17-4f1d-a0a7-35ff29fbf130", uniqueId);
84 
85  CryptograhicParameters params = ck.getParams();
86  int mode = params.getBlockCipherMode();
87  assertEquals(mode, KMIPConstants.MODE_ECB);
88 
89  byte[] dataToEncrypt = ck.getData();
90  assertEquals(16, dataToEncrypt.length);
91  for( int j=0; j < dataToEncrypt.length; j++) {
92  assertEquals( expectedData[j], dataToEncrypt[j]);
93  }
94 
95  byte[] IV = ck.getIVCounterNonce();
96  assertEquals(null, IV);
97  }
98  }
99  sl.freeLibrary();
100 
101  } catch (Exception e) {
102  // -> we shoud not get here
103  System.out.println(e.toString());
104  assertEquals(0, 1);
105  }
106  }
107 }
108 
void JNICall_EncryptKMIP()
Test: Verify parser can handle an JSON formated Encrypt operation.
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.