Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniRegisterTranECPubKeyKmip.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-RegisterTransparentECPrublicKeyKMIP");
54 
55  byte[] expectedBytes = { 0x04, 0x4D, (byte)0xAD, (byte)0x9B, 0x5C, (byte)0x8E, 0x4F, 0x1C, 0x55, 0x3A, (byte)0xFF, 0x3C, 0x0E, 0x17, (byte)0x83, (byte)0x94, (byte)0xC4, 0x64, (byte)0xC2, 0x18, (byte)0xC3, 0x2D,
56  (byte)0xEA, (byte)0xAC, (byte)0x8A, 0x7B, 0x2A, (byte)0xF5, 0x05, (byte)0xA3, 0x10, 0x3C, (byte)0x8F, 0x03, (byte)0xBC, (byte)0xAD, (byte)0xD4, 0x78, 0x6A, (byte)0xDB, 0x1A, (byte)0x88, (byte)0xBA, 0x7B,
57  0x5A, 0x48, 0x2E, 0x49, (byte)0xAC, 0x7B, 0x07, (byte)0xC7, 0x6B, 0x4C, 0x19, (byte)0xCA, 0x1E, (byte)0xB1, (byte)0xBE, (byte)0xD1, (byte)0x8B, (byte)0xBB, 0x3A, (byte)0xC3, (byte)0xF5 };
58 
59  // -> this parser is multi-thread safe by using JNI monitor locking
60  // -> use one parser object per server thread is recommended
61  P6KMIPServerLib sl = new P6KMIPServerLib();
62 
63  // -> KMIP 1.4 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
64  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=\"PublicKey\"/><TemplateAttribute><Attribute><AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/><AttributeValue type=\"Integer\" value=\"0x00000002\"/></Attribute><Attribute><AttributeName type=\"TextString\" value=\"x-ID\"/><AttributeValue type=\"TextString\" value=\"TC-ECC-1-14-pubkey\"/></Attribute></TemplateAttribute><PublicKey><KeyBlock><KeyFormatType type=\"Enumeration\" value=\"TransparentECPublicKey\"/><KeyValue><KeyMaterial><RecommendedCurve type=\"Enumeration\" value=\"SECP256K1\"/><QString type=\"BigInteger\" value=\"044DAD9B5C8E4F1C553AFF3C0E178394C464C218C32DEAAC8A7B2AF505A3103C8F03BCADD4786ADB1A88BA7B5A482E49AC7B07C76B4C19CA1EB1BED18BBB3AC3F5\"/></KeyMaterial></KeyValue><CryptographicAlgorithm type=\"Enumeration\" value=\"EC\"/><CryptographicLength type=\"Integer\" value=\"256\"/></KeyBlock></PublicKey></RequestPayload></BatchItem></RequestMessage>";
65 
66  try {
67  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
68 
69  String libVersion = sl.getLibraryVersion();
70  System.out.println(libVersion);
71 
72  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
73  // -> 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
74  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
75 
76  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
77  RequestHeader rh = sl.getRequestHeader();
78  assertEquals("1.4", rh.getProtocolVersion());
79 
80  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
81  for (int i = 0; i < rh.getBatchCount(); i++) {
82 
83  BatchItem bi = sl.getBatchItem(i + 1);
84  if (bi instanceof RegisterTransparentECPublicKeyBatchItem) {
85  RegisterTransparentECPublicKeyBatchItem ck = (RegisterTransparentECPublicKeyBatchItem) bi;
86 
87  // -> batch id is not required if only one batch item is present
88  byte[] batchId = ck.getUniqueBatchId();
89  assertEquals(null, batchId);
90 
91  int recommendedCurve = ck.getRecommendedCurve();
92  assertEquals(KMIPConstants.CURVE_SECP256K1, recommendedCurve);
93 
94  byte[] QBytes = ck.getQ();
95  assertEquals(65, QBytes.length);
96  for( int j=0; j < QBytes.length; j++ ) {
97  assertEquals(expectedBytes[j], QBytes[j]);
98  }
99 
100  String[] attributes = ck.getTemplateAttributes();
101  assertEquals(2, attributes.length);
102  assertEquals("Cryptographic Usage Mask: 2", attributes[0]);
103  assertEquals("x-ID: TC-ECC-1-14-pubkey", attributes[1]);
104  }
105  }
106  sl.freeLibrary();
107 
108  } catch (Exception e) {
109  // -> we shoud not get here
110  System.out.println(e.toString());
111  assertEquals(0, 1);
112  }
113  }
114 
115 }
void JNICall_RegisterTransparentECPublicKeyKMIP()
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.