Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniRegisterX509PubKeyKmip.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 
13 
14  @BeforeClass
15  public static void oneTimeSetUp() {
16  // NOOP
17  System.out.println("@BeforeClass - oneTimeSetUp");
18  }
19 
20  @AfterClass
21  public static void oneTimeTearDown() {
22  // NOOP
23  System.out.println("@AfterClass - oneTimeTearDown");
24  }
25 
26  @Before
27  public void setUp() {
28  // NOOP
29  System.out.println("@Before - setUp");
30  }
31 
32  @After
33  public void tearDown() {
34  // NOOP
35  System.out.println("@After - tearDown");
36  }
37 
43  @Test
45  System.out.println("@Test - JNICall-RegisterX509PubKeyKMIP");
46 
47  // -> this parser is multi-thread safe by using JNI monitor locking
48  // -> use one parser object per server thread is recommended
49  P6KMIPServerLib sl = new P6KMIPServerLib();
50 
51  // -> KMIP 1.4 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
52  String testMessage = "<RequestMessage>\n" +
53  "<RequestHeader>\n" +
54  "<ProtocolVersion>\n" +
55  "<ProtocolVersionMajor type=\"Integer\" value=\"1\"/>\n" +
56  "<ProtocolVersionMinor type=\"Integer\" value=\"0\"/>\n" +
57  "</ProtocolVersion>\n" +
58  "<MaximumResponseSize type=\"Integer\" value=\"60000\"/>\n" +
59  "<BatchCount type=\"Integer\" value=\"1\"/>\n" +
60  "</RequestHeader>\n" +
61  "<BatchItem>\n" +
62  "<Operation type=\"Enumeration\" value=\"Register\"/>\n" +
63  "<RequestPayload>\n" +
64  "<ObjectType type=\"Enumeration\" value=\"PublicKey\"/>\n" +
65  "<TemplateAttribute>\n" +
66  "<Attribute>\n" +
67  "<AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/>\n" +
68  "<AttributeValue type=\"Integer\" value=\"Export\"/>\n" +
69  "</Attribute>\n" +
70  "<Attribute>\n" +
71  "<AttributeName type=\"TextString\" value=\"Link\"/>\n" +
72  "<AttributeValue>\n" +
73  "<LinkType type=\"Enumeration\" value=\"PrivateKeyLink\"/>\n" +
74  "<LinkedObjectIdentifier type=\"TextString\" value=\"AWIcPWsLVe9N9T_6Mvk5\"/>\n" +
75  "</AttributeValue>\n" +
76  "</Attribute>\n" +
77  "<Attribute>\n" +
78  "<AttributeName type=\"TextString\" value=\"x-ID\"/>\n" +
79  "<AttributeValue type=\"TextString\" value=\"TC-82-10-pubkey\"/>\n" +
80  "</Attribute>\n" +
81  "</TemplateAttribute>\n" +
82  "<PublicKey>\n" +
83  "<KeyBlock>\n" +
84  "<KeyFormatType type=\"Enumeration\" value=\"X_509\"/>\n" +
85  "<KeyValue>\n" +
86  "<KeyMaterial type=\"ByteString\" value=\"30819F300D06092A864886F70D010101050003818D0030818902818100A52763806B72B552CEEEF18416D0ED5C4732DC6C853661151E26CE01A90A18F7ADDF0AC13225957F5B4D4CB2A7069052CC1EF12F32029A567CA456E859A1E388DBF913FC90AD912CE73DCAF782917AB0733785240E0E2E39DF934E8A1B66FD19C6FCB5281BA9A58192E2D0756DA773FEE11F80C5DDB3ADFEAA2302E12B384B430203010001\"/>\n" +
87  "</KeyValue>\n" +
88  "<CryptographicAlgorithm type=\"Enumeration\" value=\"RSA\"/>\n" +
89  "<CryptographicLength type=\"Integer\" value=\"1024\"/>\n" +
90  "</KeyBlock>\n" +
91  "</PublicKey>\n" +
92  "</RequestPayload>\n" +
93  "</BatchItem>\n" +
94  "</RequestMessage>\n";
95 
96  try {
97  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
98 
99  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
100  // -> 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
101  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
102 
103  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
104  RequestHeader rh = sl.getRequestHeader();
105  assertEquals("1.0", rh.getProtocolVersion());
106 
107  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
108  for (int i = 0; i < rh.getBatchCount(); i++) {
109 
110  BatchItem bi = sl.getBatchItem(i + 1);
111  if (bi instanceof RegisterPublicKeyBatchItem) {
112  RegisterPublicKeyBatchItem ck = (RegisterPublicKeyBatchItem) bi;
113 
114  // -> batch id is not required if only one batch item is present
115  byte[] batchId = ck.getUniqueBatchId();
116  assertEquals(null, batchId);
117 
118  String[] attributes = ck.getTemplateAttributes();
119  assertEquals(3, attributes.length);
120  assertEquals("Cryptographic Usage Mask: 40", attributes[0]);
121  assertEquals("Link: AWIcPWsLVe9N9T_6Mvk5 - PrivateKey", attributes[1]);
122  assertEquals("x-ID: TC-82-10-pubkey", attributes[2]);
123 
124  int algorithm = ck.getCryptoAlgorithm();
125  assertEquals(KMIPConstants.ALG_RSA, algorithm);
126 
127  int keyLength = ck.getCryptolength();
128  assertEquals(1024,keyLength);
129 
130  int format = ck.getKeyFormatType();
131  assertEquals(KMIPConstants.KEYFORMAT_X509, format);
132  }
133  }
134  sl.freeLibrary();
135 
136  } catch (Exception e) {
137  // -> we shoud not get here
138  System.out.println(e.toString());
139  assertEquals(0, 1);
140  }
141  }
142 }
void JNICall_RegisterX509PubKeyKMIP()
Test: Verify parser can handle an XML formated Register Split Key operation.