Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniAttributes2Kmip.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 JniAttributes2Kmip {
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 JniAttributes2Kmip() {
51  System.out.println("@Test - JNICall-RegisterKMIP");
52 
53  // -> this parser is multi-thread safe by using JNI monitor locking
54  // -> use one parser object per server thread is recommended
55  P6KMIPServerLib sl = new P6KMIPServerLib();
56 
57  // -> KMIP 2.0 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
58  String testMessage = "<RequestMessage>\n" +
59  "<RequestHeader>\n" +
60  "<ProtocolVersion>\n" +
61  "<ProtocolVersionMajor type=\"Integer\" value=\"2\"/>\n" +
62  "<ProtocolVersionMinor type=\"Integer\" value=\"0\"/>\n" +
63  "</ProtocolVersion>\n" +
64  "<ClientCorrelationValue type=\"TextString\" value=\"SKFF-M-2-20 step=0\"/>\n" +
65  "<BatchCount type=\"Integer\" value=\"1\"/>\n" +
66  "</RequestHeader>\n" +
67  "<BatchItem>\n" +
68  "<Operation type=\"Enumeration\" value=\"Create\"/>\n" +
69  "<RequestPayload>\n" +
70  "<ObjectType type=\"Enumeration\" value=\"SymmetricKey\"/>\n" +
71  "<Attributes>\n" +
72  "<CryptographicAlgorithm type=\"Enumeration\" value=\"AES\"/>\n" +
73  "<CryptographicLength type=\"Integer\" value=\"192\"/>\n" +
74  "<CryptographicUsageMask type=\"Integer\" value=\"0x0000000C\"/>\n" +
75  "<ShortUniqueIdentifier type=\"ByteString\" value=\"37b9e6d7ba994e87512247c1d8894c047fcfbe572f415832ac2295c679c576ee\"/>\n" +
76  "<NISTKeyType type=\"Enumeration\" value=\"PrivateSignatureKey\"/>\n" +
77  "<Attribute><VendorIdentification type=\"TextString\" value=\"P6R\"/><AttributeName type=\"TextString\" value=\"client-version\" /><AttributeValue type=\"TextString\" value=\"2018.1\"/></Attribute>\n" +
78  "<Name>\n" +
79  "<NameValue type=\"TextString\" value=\"SKFF-M-2-20\"/>\n" +
80  "<NameType type=\"Enumeration\" value=\"UninterpretedTextString\"/>\n" +
81  "</Name>\n" +
82  "<X_509CertificateIdentifier><IssuerDistinguishedName type=\"ByteString\" value=\"3031310b300906035504061302415531123010060355040a13094b6d69702054657374310e300c06035504031305496e746572\"/><CertificateSerialNumber type=\"ByteString\" value=\"02083132333435363738\"/></X_509CertificateIdentifier>\n" +
83  "<X_509CertificateIssuer><IssuerDistinguishedName type=\"ByteString\" value=\"3031310b300906035504061302415531123010060355040a13094b6d69702054657374310e300c06035504031305496e746572\"/></X_509CertificateIssuer>\n" +
84  "<X_509CertificateSubject><SubjectDistinguishedName type=\"ByteString\" value=\"302e310b300906035504061302415531123010060355040a13094b6d69702054657374310b3009060355040313024545\"/></X_509CertificateSubject>\n" +
85  "<Digest><HashingAlgorithm type=\"Enumeration\" value=\"SHA_256\"/><DigestValue type=\"ByteString\" value=\"c2afb2c05ca8690d7d9cf753c3cceb758030c29538b730bb23b77dc5ff0bbb36\"/><KeyFormatType type=\"Enumeration\" value=\"Raw\"/></Digest>\n" +
86  "</Attributes>\n" +
87  "</RequestPayload>\n" +
88  "</BatchItem>\n" +
89  "</RequestMessage>";
90 
91  try {
92  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
93 
94  String libVersion = sl.getLibraryVersion();
95  System.out.println(libVersion);
96 
97  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
98  // -> the type of message: TTLC, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
99  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
100 
101  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
102  RequestHeader rh = sl.getRequestHeader();
103  assertEquals("2.0", rh.getProtocolVersion());
104 
105  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
106  for (int i = 0; i < rh.getBatchCount(); i++) {
107 
108  BatchItem bi = sl.getBatchItem(i + 1);
109  if (bi instanceof CreateBatchItem) {
110  CreateBatchItem rb = (CreateBatchItem) bi;
111 
112  int objectType = rb.getObjectType();
113  assertEquals(KMIPConstants.OBJECT_SYMMETRICKEY, objectType);
114 
115  // -> handling KMIP 2.0 attributes which use a different message format
116  String[] attributes = rb.getAttributes();
117  assertEquals(11, attributes.length);
118  assertEquals("Cryptographic Algorithm: 3", attributes[0]);
119  assertEquals("Cryptographic Length: 192", attributes[1]);
120  assertEquals("Cryptographic Usage Mask: c", attributes[2]);
121  assertEquals("Short Unique Identifier: 37B9E6D7BA994E87512247C1D8894C047FCFBE572F415832AC2295C679C576EE", attributes[3]);
122  assertEquals("NIST Key Type: 1", attributes[4]);
123  assertEquals("VENDORID: P6R, client-version: 2018.1", attributes[5]);
124  assertEquals("Name: SKFF-M-2-20 - text_string", attributes[6]);
125 
126  // -> the following attributes where just added to test that they are properly parsed, but they would not appear in a Create Operation
127  assertEquals("X509 Certificate Identifier: 3031310B300906035504061302415531123010060355040A13094B6D69702054657374310E300C06035504031305496E746572 - 02083132333435363738", attributes[7]);
128  assertEquals("X509 Certificate Issuer: 3031310B300906035504061302415531123010060355040A13094B6D69702054657374310E300C06035504031305496E746572" , attributes[8]);
129  assertEquals("X509 Certificate Subject: 302E310B300906035504061302415531123010060355040A13094B6D69702054657374310B3009060355040313024545", attributes[9]);
130  assertEquals("Digest: 6 - 1 - C2AFB2C05CA8690D7D9CF753C3CCEB758030C29538B730BB23B77DC5FF0BBB36", attributes[10]);
131  }
132  }
133  sl.freeLibrary();
134 
135  } catch (Exception e) {
136  // -> we shoud not get here
137  System.out.println(e.toString());
138  assertEquals(0, 1);
139  }
140  }
141 }
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
void JniAttributes2Kmip()
Test: KMIP 2.0 Attributes are properly parsed.