Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniSignKmip.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 import static org.junit.Assert.assertNotEquals;
8 
18 public class JniSignKmip {
19 
20  @BeforeClass
21  public static void oneTimeSetUp() {
22  // NOOP
23  System.out.println("@BeforeClass - oneTimeSetUp");
24  }
25 
26  @AfterClass
27  public static void oneTimeTearDown() {
28  // NOOP
29  System.out.println("@AfterClass - oneTimeTearDown");
30  }
31 
32  @Before
33  public void setUp() {
34  // NOOP
35  System.out.println("@Before - setUp");
36  }
37 
38  @After
39  public void tearDown() {
40  // NOOP
41  System.out.println("@After - tearDown");
42  }
43 
49  @Test
50  public void JNICall_SignKMIP() {
51  System.out.println("@Test - JNICall-SignKMIP");
52 
53  byte[] expectedData = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
54 
55  byte[] signatureExpected = { 0x28, (byte)0xAC, (byte)0x86, (byte)0xFD, (byte)0xAC, 0x40, (byte)0xCE, (byte)0xFB, 0x7D, (byte)0xA8, (byte)0xE0, (byte)0xB1, (byte)0x88, 0x18, 0x6A, 0x2F, (byte)0x93, (byte)0xC0, 0x1E, 0x32, 0x25, 0x0A, 0x32, 0x70, 0x66, 0x40, (byte)0x89, 0x1E, 0x19, (byte)0xE1, 0x7A, 0x4D,
56  0x4D, 0x15, 0x64, (byte)0xD7, 0x0F, 0x3A, 0x09, (byte)0xD2, 0x41, 0x62, (byte)0xA8, (byte)0x98, 0x25, 0x40, 0x04, (byte)0xB0, 0x6D, (byte)0x8A, 0x1A, (byte)0xAB, (byte)0xBF, 0x0F, 0x7E, 0x14, (byte)0x9D, (byte)0xC8, (byte)0xA0, 0x0B, (byte)0xC6, 0x64, 0x4D, (byte)0x92,
57  0x22, (byte)0xA7, (byte)0xEE, (byte)0xE7, 0x5A, 0x1F, (byte)0xB1, (byte)0xE5, 0x26, 0x28, 0x64, (byte)0x8E, (byte)0x94, (byte)0xDF, 0x0C, (byte)0xC8, (byte)0x8D, (byte)0xE7, 0x07, (byte)0xCF, 0x65, 0x6C, 0x25, (byte)0xED, (byte)0xC6, (byte)0xFF, (byte)0xB1, (byte)0xBB, 0x1C, 0x35, (byte)0xB2, 0x1A,
58  (byte)0xFF, (byte)0xC3, 0x09, 0x13, (byte)0xA6, (byte)0xB3, (byte)0xC4, (byte)0xD3, (byte)0xD4, (byte)0x9C, (byte)0xAB, 0x46, (byte)0x9F, (byte)0x9B, 0x18, 0x22, (byte)0xB1, (byte)0xFD, 0x0E, (byte)0xFB, (byte)0xC2, 0x31, 0x43, (byte)0x87, 0x13, (byte)0xBC, 0x71, 0x39, 0x1C, (byte)0x99, (byte)0x8B, (byte)0xBD,
59  0x42, 0x5F, 0x38, 0x13, (byte)0xAE, (byte)0xEF, 0x04, (byte)0x9F, 0x71, 0x29, 0x59, 0x47, (byte)0xDF, 0x36, 0x43, (byte)0xE0, 0x68, 0x55, (byte)0xDA, 0x7B, 0x11, (byte)0xEE, 0x73, (byte)0xD5, 0x63, (byte)0xB1, (byte)0x9D, 0x66, (byte)0x8F, 0x41, (byte)0xE4, 0x43,
60  (byte)0xA2, (byte)0xE6, (byte)0xA3, 0x5A, (byte)0xCB, 0x59, (byte)0xF7, (byte)0xCB, 0x71, (byte)0xBC, 0x4B, (byte)0x88, 0x00, (byte)0xF5, 0x5E, 0x63, 0x47, 0x72, 0x68, (byte)0x91, 0x49, (byte)0xC3, 0x70, 0x6D, (byte)0xE4, (byte)0x9B, 0x74, (byte)0xC9, (byte)0xEF, 0x29, (byte)0xD2, (byte)0x9E,
61  0x3B, 0x1F, 0x73, 0x5E, 0x47, (byte)0xE1, (byte)0xAA, (byte)0x85, (byte)0xE1, 0x7D, (byte)0xD4, (byte)0xA5, (byte)0x8A, 0x43, (byte)0xEE, (byte)0x91, (byte)0xAB, (byte)0x9B, (byte)0x90, (byte)0xFC, 0x59, 0x06, (byte)0xC8, (byte)0x93, (byte)0xB2, (byte)0xB7, (byte)0xC7, 0x77, 0x07, 0x40, 0x1D, (byte)0xC7,
62  0x51, 0x61, (byte)0xB4, (byte)0xF9, 0x7C, (byte)0xC4, 0x79, 0x06, 0x0A, 0x58, (byte)0xE0, (byte)0xF9, 0x43, (byte)0xD4, (byte)0x97, 0x27, (byte)0x8A, 0x4A, (byte)0xBA, (byte)0xB7, 0x73, (byte)0x9E, 0x55, (byte)0x98, 0x6E, (byte)0x88, (byte)0xF3, (byte)0x9B, (byte)0xB8, (byte)0xFC, (byte)0x85, 0x41 };
63 
64  // -> this parser is multi-thread safe by using JNI monitor locking
65  // -> use one parser object per server thread is recommended
66  P6KMIPServerLib sl = new P6KMIPServerLib();
67 
68  // -> KMIP 1.3 JSON message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
69  String testMessage1 = "{\"tag\":\"RequestMessage\", \"value\":[{\"tag\":\"RequestHeader\", \"value\":[{\"tag\":\"ProtocolVersion\", \"value\":[{\"tag\":\"ProtocolVersionMajor\", \"type\":\"Integer\", \"value\":1}, {\"tag\":\"ProtocolVersionMinor\", \"type\":\"Integer\", \"value\":3}]}, {\"tag\":\"BatchCount\", \"type\":\"Integer\", \"value\":1}]}, {\"tag\":\"BatchItem\", \"value\":[{\"tag\":\"Operation\", \"type\":\"Enumeration\", \"value\":\"Sign\"}, {\"tag\":\"RequestPayload\", \"value\":[{\"tag\":\"UniqueIdentifier\", \"type\":\"TextString\", \"value\":\"ad273a0f-6560-48d0-97fc-53f2aa42edb5\"}, {\"tag\":\"Data\", \"type\":\"ByteString\", \"value\":\"01020304050607080910111213141516\"}]}]}]}";
70 
71  String testMessage2 = "{\"tag\":\"RequestMessage\", \"value\":[{\"tag\":\"RequestHeader\", \"value\":[{\"tag\":\"ProtocolVersion\", \"value\":[{\"tag\":\"ProtocolVersionMajor\", \"type\":\"Integer\", \"value\":1}, {\"tag\":\"ProtocolVersionMinor\", \"type\":\"Integer\", \"value\":3}]}, {\"tag\":\"BatchCount\", \"type\":\"Integer\", \"value\":1}]}, {\"tag\":\"BatchItem\", \"value\":[{\"tag\":\"Operation\", \"type\":\"Enumeration\", \"value\":\"SignatureVerify\"}, {\"tag\":\"RequestPayload\", \"value\":[{\"tag\":\"UniqueIdentifier\", \"type\":\"TextString\", \"value\":\"ad273a0f-6560-48d0-97fc-53f2aa42edb5\"}, {\"tag\":\"Data\", \"type\":\"ByteString\", \"value\":\"01020304050607080910111213141516\"}, {\"tag\":\"SignatureData\", \"type\":\"ByteString\", \"value\":\"28AC86FDAC40CEFB7DA8E0B188186A2F93C01E32250A32706640891E19E17A4D4D1564D70F3A09D24162A898254004B06D8A1AABBF0F7E149DC8A00BC6644D9222A7EEE75A1FB1E52628648E94DF0CC88DE707CF656C25EDC6FFB1BB1C35B21AFFC30913A6B3C4D3D49CAB469F9B1822B1FD0EFBC231438713BC71391C998BBD425F3813AEEF049F71295947DF3643E06855DA7B11EE73D563B19D668F41E443A2E6A35ACB59F7CB71BC4B8800F55E634772689149C3706DE49B74C9EF29D29E3B1F735E47E1AA85E17DD4A58A43EE91AB9B90FC5906C893B2B7C77707401DC75161B4F97CC479060A58E0F943D497278A4ABAB7739E55986E88F39BB8FC8541\"}]}]}]}";
72 
73 
74  try {
75  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
76 
77  String libVersion = sl.getLibraryVersion();
78  System.out.println(libVersion);
79 
80  // [A] Here we test non-streaming sign request
81  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
82  // -> 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
83  sl.setMessageBuffer(testMessage1.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGJSON);
84 
85  RequestHeader rh = sl.getRequestHeader();
86  assertEquals("1.3", rh.getProtocolVersion());
87 
88  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
89  for (int i = 0; i < rh.getBatchCount(); i++) {
90 
91  BatchItem bi = sl.getBatchItem(i + 1);
92  if (bi instanceof SignBatchItem) {
93  SignBatchItem ck = (SignBatchItem) bi;
94 
95  String uniqueId = ck.getUniqueId();
96  assertEquals("ad273a0f-6560-48d0-97fc-53f2aa42edb5", uniqueId);
97 
98  // -> object is null since none in message
99  CryptograhicParameters params = ck.getParams();
100  assertEquals(null, params);
101 
102  byte[] dataToSign = ck.getData();
103  assertEquals(16, dataToSign.length);
104  for( int j=0; j < dataToSign.length; j++) {
105  assertEquals( expectedData[j], dataToSign[j]);
106  }
107  }
108  }
109 
110  // [B] Here we test a similar signature verify request
111  sl.setMessageBuffer(testMessage2.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGJSON);
112 
113  rh = sl.getRequestHeader();
114  assertEquals("1.3", rh.getProtocolVersion());
115 
116  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
117  for (int i = 0; i < rh.getBatchCount(); i++) {
118 
119  BatchItem bi = sl.getBatchItem(i + 1);
120  if (bi instanceof SignatureVerifyBatchItem) {
121  SignatureVerifyBatchItem ck = (SignatureVerifyBatchItem) bi;
122 
123  String uniqueId = ck.getUniqueId();
124  assertEquals("ad273a0f-6560-48d0-97fc-53f2aa42edb5", uniqueId);
125 
126  // -> object is null since none in message
127  CryptograhicParameters params = ck.getParams();
128  assertEquals(null, params);
129 
130  byte[] dataToSign = ck.getData();
131  assertEquals(16, dataToSign.length);
132  for( int j=0; j < dataToSign.length; j++) {
133  assertEquals( expectedData[j], dataToSign[j]);
134  }
135 
136  byte[] signature = ck.getSignatureData();
137  assertEquals(256, signature.length);
138  for( int k=0; k < signature.length; k++) {
139  assertEquals( signatureExpected[k], signature[k]);
140  }
141  }
142  }
143  sl.freeLibrary();
144 
145  } catch (Exception e) {
146  // -> we shoud not get here
147  System.out.println(e.toString());
148  assertEquals(0, 1);
149  }
150  }
151 }
152 
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
void JNICall_SignKMIP()
Test: Verify parser can handle an JSON formated Sign operation.