Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniRegisterTranECPrivKeyKmip.java
Go to the documentation of this file.
1 package com.p6r.kmipserverlib;
2 
3 import org.junit.*;
4 
5 import java.math.BigInteger;
6 import java.nio.charset.Charset;
7 
8 import static org.junit.Assert.assertEquals;
9 import static org.junit.Assert.assertNotEquals;
10 
21 
22  @BeforeClass
23  public static void oneTimeSetUp() {
24  // NOOP
25  System.out.println("@BeforeClass - oneTimeSetUp");
26  }
27 
28  @AfterClass
29  public static void oneTimeTearDown() {
30  // NOOP
31  System.out.println("@AfterClass - oneTimeTearDown");
32  }
33 
34  @Before
35  public void setUp() {
36  // NOOP
37  System.out.println("@Before - setUp");
38  }
39 
40  @After
41  public void tearDown() {
42  // NOOP
43  System.out.println("@After - tearDown");
44  }
45 
51  @Test
53  System.out.println("@Test - JNICall-RegisterTransparentECPrivateKeyKMIP");
54 
55  byte[] expectedBytes = { (byte)0xCB, (byte)0xE5, (byte)0xA4, (byte)0xFD, 0x53, (byte)0xE6, (byte)0xC3, 0x02, 0x7D, 0x66, (byte)0xC2, (byte)0xFC, 0x42, 0x40, 0x60, (byte)0x99, 0x41, 0x42, 0x21, 0x04, 0x6A, 0x01, 0x38, (byte)0xD7, 0x21, (byte)0x9D, (byte)0xDB, (byte)0xEE, (byte)0xF4, 0x6A, 0x58, 0x23 };
56 
57  // -> this parser is multi-thread safe by using JNI monitor locking
58  // -> use one parser object per server thread is recommended
59  P6KMIPServerLib sl = new P6KMIPServerLib();
60 
61  // -> KMIP 1.4 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
62  String testMessage = "<RequestMessage><RequestHeader><ProtocolVersion><ProtocolVersionMajor type=\"Integer\" value=\"1\"/><ProtocolVersionMinor type=\"Integer\" value=\"4\"/></ProtocolVersion><BatchCount type=\"Integer\" value=\"1\"/></RequestHeader><BatchItem><Operation type=\"Enumeration\" value=\"Register\"/><RequestPayload><ObjectType type=\"Enumeration\" value=\"PrivateKey\"/><TemplateAttribute><Attribute><AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/><AttributeValue type=\"Integer\" value=\"0x00000001\"/></Attribute><Attribute><AttributeName type=\"TextString\" value=\"x-ID\"/><AttributeValue type=\"TextString\" value=\"TC-ECC-1-14-privkey1\"/></Attribute></TemplateAttribute><PrivateKey><KeyBlock><KeyFormatType type=\"Enumeration\" value=\"TransparentECPrivateKey\"/><KeyValue><KeyMaterial><RecommendedCurve type=\"Enumeration\" value=\"SECP256K1\"/><D type=\"BigInteger\" value=\"CBE5A4FD53E6C3027D66C2FC42406099414221046A0138D7219DDBEEF46A5823\"/></KeyMaterial></KeyValue><CryptographicAlgorithm type=\"Enumeration\" value=\"EC\"/><CryptographicLength type=\"Integer\" value=\"256\"/></KeyBlock></PrivateKey></RequestPayload></BatchItem></RequestMessage>";
63 
64  // -> this uses ECPrivateKey encoding which makes the key material an ASN.1 blob
65  String testMessage2 ="<RequestMessage>\n" +
66  " <RequestHeader>\n" +
67  " <ProtocolVersion>\n" +
68  " <ProtocolVersionMajor type=\"Integer\" value=\"1\"/>\n" +
69  " <ProtocolVersionMinor type=\"Integer\" value=\"4\"/>\n" +
70  " </ProtocolVersion>\n" +
71  " <BatchCount type=\"Integer\" value=\"1\"/>\n" +
72  " </RequestHeader>\n" +
73  " <BatchItem>\n" +
74  " <Operation type=\"Enumeration\" value=\"Register\"/>\n" +
75  " <RequestPayload>\n" +
76  " <ObjectType type=\"Enumeration\" value=\"PrivateKey\"/>\n" +
77  " <TemplateAttribute>\n" +
78  " <Attribute>\n" +
79  " <AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/>\n" +
80  " <AttributeValue type=\"Integer\" value=\"0x00000001\"/>\n" +
81  " </Attribute>\n" +
82  " <Attribute>\n" +
83  " <AttributeName type=\"TextString\" value=\"x-ID\"/>\n" +
84  " <AttributeValue type=\"TextString\" value=\"TC-ECC-1-13-pubkey1_C\"/>\n" +
85  " </Attribute>\n" +
86  " </TemplateAttribute>\n" +
87  " <PrivateKey>\n" +
88  " <KeyBlock>\n" +
89  " <KeyFormatType type=\"Enumeration\" value=\"ECPrivateKey\"/>\n" +
90  " <KeyValue>\n" +
91  " <KeyMaterial type=\"ByteString\" value=\"3074020101042044A9071D7C9981FF0337FB542325727DA766B4FC045C8FD6063AAD1EAE29CEAFA00706052B8104000AA1440342000460E215293DC0EF43BA410F099F64E52847EBCED098476AC1CF94B9C6F723779E1FC092EDC2645EAEF86F2FA1187E8EE81EEA7D91E2C5EDDD329EC6A89A5ECCCF\"/>\n" +
92  " </KeyValue>\n" +
93  " <CryptographicAlgorithm type=\"Enumeration\" value=\"ECDSA\"/>\n" +
94  " <CryptographicLength type=\"Integer\" value=\"256\"/>\n" +
95  " </KeyBlock>\n" +
96  " </PrivateKey>\n" +
97  " </RequestPayload>\n" +
98  " </BatchItem>\n" +
99  "</RequestMessage>\n";
100 
101  // -> uses PKCS#8 key format which we still map into the Java Transparent Private key object
102  String testMessage3 = "<RequestMessage>\n" +
103  " <RequestHeader>\n" +
104  " <ProtocolVersion>\n" +
105  " <ProtocolVersionMajor type=\"Integer\" value=\"1\"/>\n" +
106  " <ProtocolVersionMinor type=\"Integer\" value=\"4\"/>\n" +
107  " </ProtocolVersion>\n" +
108  " <BatchCount type=\"Integer\" value=\"1\"/>\n" +
109  " </RequestHeader>\n" +
110  " <BatchItem>\n" +
111  " <Operation type=\"Enumeration\" value=\"Register\"/>\n" +
112  " <RequestPayload>\n" +
113  " <ObjectType type=\"Enumeration\" value=\"PrivateKey\"/>\n" +
114  " <TemplateAttribute>\n" +
115  " <Attribute>\n" +
116  " <AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/>\n" +
117  " <AttributeValue type=\"Integer\" value=\"0x00000001\"/>\n" +
118  " </Attribute>\n" +
119  " <Attribute>\n" +
120  " <AttributeName type=\"TextString\" value=\"x-ID\"/>\n" +
121  " <AttributeValue type=\"TextString\" value=\"TC-ECC-2-14-prikey1\"/>\n" +
122  " </Attribute>\n" +
123  " </TemplateAttribute>\n" +
124  " <PrivateKey>\n" +
125  " <KeyBlock>\n" +
126  " <KeyFormatType type=\"Enumeration\" value=\"PKCS_8\"/>\n" +
127  " <KeyValue>\n" +
128  " <KeyMaterial type=\"ByteString\" value=\"308184020100301006072A8648CE3D020106052B8104000A046D306B0201010420EB2F5F5E14EAE9C3A130078B53D2895EE3538F0DBA9223687E49FF5F7E77638BA14403420004425F86FD82B7DC90A00C6711327B926F5D6C1CDB06834FB69D409E8049CE5A7907A091E79A9B1ACBE6A4E9EF84C425860CAA7BB427033E3393678CC7694EF936\"/>\n" +
129  " </KeyValue>\n" +
130  " <CryptographicAlgorithm type=\"Enumeration\" value=\"EC\"/>\n" +
131  " <CryptographicLength type=\"Integer\" value=\"256\"/>\n" +
132  " </KeyBlock>\n" +
133  " </PrivateKey>\n" +
134  " </RequestPayload>\n" +
135  " </BatchItem>\n" +
136  "</RequestMessage>";
137 
138  try {
139  sl.initializeLibrary(P6KMIPServerLib.FLAGS_ALLLOG);
140 
141  String libVersion = sl.getLibraryVersion();
142  System.out.println(libVersion);
143 
144  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
145  // -> 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
146  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
147  //sl.setMessageBuffer(testMessage2.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
148  //sl.setMessageBuffer(testMessage3.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
149 
150  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
151  RequestHeader rh = sl.getRequestHeader();
152  assertEquals("1.4", rh.getProtocolVersion());
153 
154  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
155  for (int i = 0; i < rh.getBatchCount(); i++) {
156 
157  BatchItem bi = sl.getBatchItem(i + 1);
158  if (bi instanceof RegisterTransparentECPrivateKeyBatchItem) {
159  RegisterTransparentECPrivateKeyBatchItem ck = (RegisterTransparentECPrivateKeyBatchItem) bi;
160 
161  // -> batch id is not required if only one batch item is present
162  byte[] batchId = ck.getUniqueBatchId();
163  assertEquals(null, batchId);
164 
165  int recommendedCurve = ck.getRecommendedCurve();
166  assertEquals(KMIPConstants.CURVE_SECP256K1, recommendedCurve);
167 
168  BigInteger D = ck.getD();
169  assertNotEquals(null, D);
170  byte[] DBytes = D.toByteArray();
171  assertEquals(32, DBytes.length);
172  for( int j=0; j < DBytes.length; j++ ) {
173  assertEquals(expectedBytes[j], DBytes[j]);
174  }
175 
176  String[] attributes = ck.getTemplateAttributes();
177  assertEquals(2, attributes.length);
178  assertEquals("Cryptographic Usage Mask: 1", attributes[0]);
179  assertEquals("x-ID: TC-ECC-1-14-privkey1", attributes[1]);
180  }
181  }
182  sl.freeLibrary();
183 
184  } catch (Exception e) {
185  // -> we shoud not get here
186  System.out.println(e.toString());
187  assertEquals(0, 1);
188  }
189  }
190 }
void JNICall_RegisterTransparentECPrivateKeyKMIP()
Test: Verify parser can handle an XML formated Register Split Key operation.
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.