Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniRegisterECKeyKmip.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 
7 import static org.junit.Assert.assertEquals;
8 
18 public class JniRegisterECKeyKmip {
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_RegisterECKeyKMIP() {
51  System.out.println("@Test - JNICall-RegisterEcKeyKMIP");
52 
53  byte[] expectedKeyBytes = { 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, (byte)0x91, 0x2a, 0x0e, 0x20, (byte)0x8f, 0x5d, (byte)0xd7, 0x34, (byte)0xb1, (byte)0x85, 0x18, 0x47,
54  (byte)0xe4, 0x65, (byte)0x9c, 0x45, (byte)0x88, (byte)0xb1, 0x6d, (byte)0x9f, 0x3b, (byte)0xc1, (byte)0xad, (byte)0xa5, 0x10, (byte)0xea, 0x2c, (byte)0x96, (byte)0x9d, (byte)0xaf, (byte)0x9e,
55  (byte)0xf4, (byte)0xa0, 0x0a, 0x06, 0x08, 0x2a, (byte)0x86, 0x48, (byte)0xce, 0x3d, 0x03, 0x01, 0x07, (byte)0xa1, 0x44, 0x03, 0x42, 0x00, 0x04,
56  (byte)0xb3, 0x44, 0x39, 0x2f, (byte)0xb7, 0x2a, (byte)0xa2, 0x67, (byte)0xf8, 0x6d, 0x7d, (byte)0x83, 0x7d, (byte)0xe1, (byte)0xd9, 0x6c, (byte)0xe9, 0x60, (byte)0xff,
57  (byte)0xc0, 0x5c, (byte)0xee, (byte)0xf3, (byte)0xf1, (byte)0xfa, 0x26, (byte)0xd8, 0x54, 0x6a, 0x64, 0x42, (byte)0xf3, (byte)0xc1, 0x39, 0x7e, (byte)0x92, (byte)0x9a, (byte)0xad,
58  (byte)0x8d, (byte)0xca, (byte)0xb2, 0x5b, 0x28, 0x2c, (byte)0xa8, (byte)0xa1, (byte)0xcf, 0x00, (byte)0xbe, (byte)0xf7, (byte)0xcb, 0x18, (byte)0xa0, (byte)0xf5, (byte)0xea, 0x44, 0x49,
59  0x3a, 0x30, (byte)0xa6, (byte)0xd2, 0x23, (byte)0xb2, 0x5b };
60 
61  // -> this parser is multi-thread safe by using JNI monitor locking
62  // -> use one parser object per server thread is recommended
63  P6KMIPServerLib sl = new P6KMIPServerLib();
64 
65  // -> KMIP 1.0 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
66  String testMessage
67  = "<RequestMessage>" +
68  "<RequestHeader>" +
69  "<ProtocolVersion>" +
70  "<ProtocolVersionMajor type=\"Integer\" value=\"1\" />" +
71  "<ProtocolVersionMinor type=\"Integer\" value=\"0\" />" +
72  "</ProtocolVersion>" +
73  "<BatchCount type=\"Integer\" value=\"1\" />" +
74  "</RequestHeader>" +
75  "<BatchItem>" +
76  "<Operation type=\"Enumeration\" value=\"Register\" />" +
77  "<RequestPayload>" +
78  "<ObjectType type=\"Enumeration\" value=\"PrivateKey\" />" +
79  "<TemplateAttribute>" +
80  "<Attribute>" +
81  "<AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\" />" +
82  "<AttributeValue type=\"Integer\" value=\"Sign\" />" +
83  "</Attribute>" +
84  "<Attribute>" +
85  "<AttributeName type=\"TextString\" value=\"x-ID\" />" +
86  "<AttributeValue type=\"TextString\" value=\"TC-ECC-1-10-prikey1\" />" +
87  "</Attribute>" +
88  "<Attribute><AttributeName type=\"TextString\" value=\"Name\"/><AttributeValue><NameValue type=\"TextString\" value=\"App-123-jdh-3432-X\"/><NameType type=\"Enumeration\" value=\"UninterpretedTextString\"/></AttributeValue></Attribute>" +
89  "</TemplateAttribute>" +
90  "<PrivateKey>" +
91  "<KeyBlock>" +
92  "<KeyFormatType type=\"Enumeration\" value=\"ECPrivateKey\" />" +
93  "<KeyValue>" +
94  "<KeyMaterial type=\"ByteString\" value=\"30770201010420912a0e208f5dd734b1851847e4659c4588b16d9f3bc1ada510ea2c969daf9ef4a00a06082a8648ce3d030107a14403420004b344392fb72aa267f86d7d837de1d96ce960ffc05ceef3f1fa26d8546a6442f3c1397e929aad8dcab25b282ca8a1cf00bef7cb18a0f5ea44493a30a6d223b25b\" />" +
95  "</KeyValue>" +
96  "<CryptographicAlgorithm type=\"Enumeration\" value=\"ECDSA\" />" +
97  "<CryptographicLength type=\"Integer\" value=\"256\" />" +
98  "</KeyBlock>" +
99  "</PrivateKey>" +
100  "</RequestPayload>" +
101  "</BatchItem>" +
102  "</RequestMessage>";
103 
104  try {
105  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
106 
107  String libVersion = sl.getLibraryVersion();
108  System.out.println(libVersion);
109 
110  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
111  // -> 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
112  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
113 
114  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
115  RequestHeader rh = sl.getRequestHeader();
116  assertEquals("1.0", rh.getProtocolVersion());
117 
118  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
119  for (int i = 0; i < rh.getBatchCount(); i++) {
120 
121  BatchItem bi = sl.getBatchItem(i + 1);
122  if (bi instanceof RegisterPrivateKeyBatchItem) {
123  RegisterPrivateKeyBatchItem rb = (RegisterPrivateKeyBatchItem) bi;
124  assertEquals(null, rb.getUniqueBatchId());
125  assertEquals(KMIPConstants.ALG_ECDSA, rb.getCryptoAlgorithm());
126  byte[] keyBytes = rb.getKeyMaterial();
127  assertEquals( 121, keyBytes.length );
128  for (int j = 0; j < keyBytes.length; j++) {
129  assertEquals(keyBytes[j], expectedKeyBytes[j]);
130  }
131 
132  String[] templateAttributes = rb.getTemplateAttributes();
133  assertEquals(3, templateAttributes.length);
134  assertEquals("Cryptographic Usage Mask: 1", templateAttributes[0]);
135  assertEquals("x-ID: TC-ECC-1-10-prikey1", templateAttributes[1]);
136  assertEquals("Name: App-123-jdh-3432-X - text_string", templateAttributes[2]); // The type of the Name attribute is append at the very send of the string
137 
138  String[] templateNames = rb.getTemplateNames();
139  if (null != templateNames) assertEquals(0,1);
140 
141  String[] keyAttributes = rb.getKeyValueAttributes();
142  if (null != templateNames) assertEquals(0,1);
143  }
144  }
145  sl.freeLibrary();
146 
147  } catch (Exception e) {
148  // -> we shoud not get here
149  System.out.println(e.toString());
150  assertEquals(0, 1);
151  }
152  }
153 }
void JNICall_RegisterECKeyKMIP()
Test: Verify parser can break message into multiple batch items.
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.