Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniRegisterCertKmip.java
Go to the documentation of this file.
1 package com.p6r.kmipserverlib;
2 
3 import org.junit.*;
4 
5 import javax.security.auth.x500.X500Principal;
6 import java.io.ByteArrayInputStream;
7 import java.io.InputStream;
8 import java.security.cert.CertificateFactory;
9 import java.security.cert.X509Certificate;
10 
11 import static org.junit.Assert.assertEquals;
12 
22 public class JniRegisterCertKmip {
23 
24  @BeforeClass
25  public static void oneTimeSetUp() {
26  // NOOP
27  System.out.println("@BeforeClass - oneTimeSetUp");
28  }
29 
30  @AfterClass
31  public static void oneTimeTearDown() {
32  // NOOP
33  System.out.println("@AfterClass - oneTimeTearDown");
34  }
35 
36  @Before
37  public void setUp() {
38  // NOOP
39  System.out.println("@Before - setUp");
40  }
41 
42  @After
43  public void tearDown() {
44  // NOOP
45  System.out.println("@After - tearDown");
46  }
47 
53  @Test
54  public void JNICall_RegisterCertKMIP() {
55  System.out.println("@Test - JNICall-RegisterCertKMIP");
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  X509Certificate x509Cert;
61 
62  // -> KMIP 1.3 TTLV message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
63  byte[] testMessage = {
64  0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x03, 0x68, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x00, 0x48, 0x42, 0x00, 0x69, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x6a, 0x02, 0x00, 0x00, 0x00, 0x04,
65  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x92, 0x09, 0x00, 0x00, 0x00, 0x08,
66  0x00, 0x00, 0x00, 0x00, 0x5a, 0x19, (byte)0xac, (byte)0xcc, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x03, 0x10,
67  0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x93, 0x08, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68  0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x02, (byte)0xe8, 0x42, 0x00, 0x57, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x91, 0x01, 0x00, 0x00, 0x00, 0x00,
69  0x42, 0x00, 0x13, 0x01, 0x00, 0x00, 0x02, (byte)0xc8, 0x42, 0x00, 0x1d, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x1e, 0x08, 0x00, 0x00, 0x02, (byte)0xaf,
70  0x30, (byte)0x82, 0x02, (byte)0xab, 0x30, (byte)0x82, 0x02, 0x14, (byte)0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01, 0x03, 0x30, 0x0d, 0x06, 0x09, 0x2a, (byte)0x86, 0x48, (byte)0x86, (byte)0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30,
71  0x62, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41, 0x31, 0x0e, 0x30, 0x0c, 0x06,
72  0x03, 0x55, 0x04, 0x07, 0x13, 0x05, 0x39, 0x35, 0x30, 0x33, 0x30, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x09, 0x50, 0x36, 0x52, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x31,
73  0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x08, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x06, 0x50, 0x36, 0x52,
74  0x20, 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x37, 0x31, 0x31, 0x32, 0x35, 0x31, 0x37, 0x34, 0x37, 0x35, 0x36, 0x5a, 0x17, 0x0d, 0x31, 0x38, 0x31, 0x31, 0x32, 0x35, 0x31, 0x37, 0x34, 0x37,
75  0x35, 0x36, 0x5a, 0x30, (byte)0x81, (byte)0xd4, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41,
76  0x31, 0x0e, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x05, 0x39, 0x35, 0x30, 0x33, 0x30, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x09, 0x50, 0x36, 0x52, 0x2c, 0x20,
77  0x49, 0x6e, 0x63, 0x2e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03,
78  0x55, 0x04, 0x0b, 0x13, 0x07, 0x4f, 0x70, 0x74, 0x69, 0x63, 0x73, 0x32, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0d, 0x4b, 0x4d, 0x49, 0x50, 0x20, 0x45, 0x6e, 0x67, 0x69,
79  0x6e, 0x65, 0x65, 0x72, 0x31, 0x1b, 0x30, 0x19, 0x06, 0x09, 0x2a, (byte)0x86, 0x48, (byte)0x86, (byte)0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x0c, 0x6d, 0x61, 0x72, 0x6b, 0x40, 0x70, 0x36, 0x72, 0x2e, 0x63, 0x6f,
80  0x6d, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x2a, 0x13, 0x04, 0x4d, 0x61, 0x72, 0x6b, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x04, 0x13, 0x06, 0x4a, 0x6f, 0x73, 0x65, 0x70,
81  0x68, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x29, 0x13, 0x0e, 0x4d, 0x61, 0x72, 0x6b, 0x20, 0x4b, 0x2e, 0x20, 0x4a, 0x6f, 0x73, 0x65, 0x70, 0x68, 0x30, (byte)0x81, (byte)0x9f, 0x30, 0x0d, 0x06,
82  0x09, 0x2a, (byte)0x86, 0x48, (byte)0x86, (byte)0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, (byte)0x81, (byte)0x8d, 0x00, 0x30, (byte)0x81, (byte)0x89, 0x02, (byte)0x81, (byte)0x81, 0x00, (byte)0xc1, (byte)0xa4, (byte)0xc5, (byte)0xd5, (byte)0xa1, (byte)0x93, (byte)0x95, 0x40, (byte)0x88,
83  0x4b, 0x58, 0x69, (byte)0xdc, 0x74, 0x13, 0x14, 0x63, 0x5b, (byte)0xbc, 0x0c, 0x5b, 0x53, (byte)0xca, (byte)0x96, (byte)0x87, 0x25, 0x44, 0x0c, 0x2e, (byte)0xc3, (byte)0xa4, (byte)0xb2, (byte)0xad, (byte)0xec, 0x45, 0x56, (byte)0x99, (byte)0xc3, 0x00, 0x72, 0x70,
84  (byte)0xb9, (byte)0x9f, (byte)0xff, (byte)0x96, (byte)0x97, 0x77, 0x33, (byte)0xd2, (byte)0xd2, 0x2b, (byte)0x91, 0x02, 0x28, 0x13, (byte)0xab, 0x0a, 0x51, (byte)0x98, (byte)0xe3, (byte)0xef, 0x36, 0x52, 0x50, 0x5f, 0x6c, (byte)0xe2, 0x60, 0x6d, 0x20, 0x41, (byte)0xf8, 0x16,
85  0x49, (byte)0x8b, (byte)0xac, 0x6b, (byte)0xc7, (byte)0xb5, 0x26, (byte)0xe1, 0x0e, (byte)0x82, 0x20, 0x6a, (byte)0xfc, (byte)0x8c, (byte)0xd9, (byte)0xa0, 0x0d, 0x1a, (byte)0xc3, (byte)0xd1, (byte)0xe1, (byte)0xff, 0x46, (byte)0xe9, 0x62, 0x70, 0x61, 0x4b, 0x7a, (byte)0xb1, 0x23, (byte)0xd1,
86  0x52, 0x60, (byte)0x98, 0x09, (byte)0xc9, 0x47, 0x63, (byte)0xd1, 0x00, 0x6c, (byte)0xdd, 0x58, 0x46, (byte)0xdf, (byte)0x93, (byte)0xfc, 0x1d, (byte)0xd5, (byte)0xed, (byte)0xef, (byte)0xfd, (byte)0xb2, (byte)0xa9, 0x02, 0x03, 0x01, 0x00, 0x01, 0x30, 0x0d, 0x06, 0x09,
87  0x2a, (byte)0x86, 0x48, (byte)0x86, (byte)0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, (byte)0x81, (byte)0x81, 0x00, (byte)0xb6, (byte)0xfe, (byte)0x93, 0x25, 0x4d, (byte)0x87, (byte)0xc8, 0x14, (byte)0xd3, (byte)0xf9, 0x6d, 0x32, 0x06, 0x48, (byte)0xb0, 0x27, (byte)0xfd,
88  (byte)0x8d, (byte)0x93, 0x52, 0x7e, 0x3e, (byte)0xd8, (byte)0xa7, 0x02, (byte)0xd5, (byte)0xbd, (byte)0xf1, (byte)0xfb, 0x2f, (byte)0x9d, 0x7d, 0x53, (byte)0xa2, 0x25, 0x77, 0x42, (byte)0x9e, 0x67, 0x1b, 0x14, 0x67, 0x36, (byte)0xac, 0x51, 0x26, 0x7a, 0x0c, 0x61,
89  0x6f, (byte)0xd1, 0x6c, 0x32, 0x41, (byte)0xad, 0x4b, (byte)0xc5, 0x15, (byte)0xe3, (byte)0xb4, 0x6f, 0x2f, (byte)0x89, (byte)0xa1, 0x55, 0x3c, 0x7a, 0x11, (byte)0xef, 0x78, 0x0b, (byte)0x81, 0x6f, (byte)0xde, (byte)0x97, 0x5a, (byte)0xe9, (byte)0xd8, (byte)0x8e, (byte)0xd7, (byte)0x93,
90  0x64, (byte)0xe3, 0x67, 0x78, (byte)0x8d, 0x0a, 0x1e, (byte)0xfc, (byte)0xfc, (byte)0xe6, (byte)0xa5, 0x00, (byte)0xeb, 0x52, 0x1e, 0x74, 0x58, 0x4c, (byte)0x95, (byte)0x93, 0x16, (byte)0xd1, (byte)0xee, 0x6c, 0x53, (byte)0xc6, (byte)0xcc, (byte)0xf2, 0x1f, (byte)0x95, (byte)0x9a, (byte)0x86,
91  0x46, (byte)0xa6, 0x57, 0x16, 0x40, 0x77, 0x10, (byte)0xbe, 0x34, 0x57, 0x36, (byte)0x90, 0x3a, 0x33, 0x59, 0x00
92  };
93 
94  try {
95  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
96 
97  String libVersion = sl.getLibraryVersion();
98  System.out.println(libVersion);
99 
100  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
101  // -> 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
102  sl.setMessageBuffer(testMessage, KMIPConstants.FORMAT_TTLV);
103 
104  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
105  RequestHeader rh = sl.getRequestHeader();
106  assertEquals("1.3", rh.getProtocolVersion());
107 
108  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
109  // -> register certificate request with out any attributes sent with the certificate
110  for (int i = 0; i < rh.getBatchCount(); i++) {
111 
112  BatchItem bi = sl.getBatchItem(i + 1);
113  if (bi instanceof RegisterCertificateBatchItem) {
114  RegisterCertificateBatchItem rb = (RegisterCertificateBatchItem) bi;
115  assertEquals(1, rb.getUniqueBatchId()[0]);
116  assertEquals(KMIPConstants.CERT_X509, rb.getCertificateType());
117  byte[] certBytes = rb.getCertificate();
118 
119  // -> convert the certificate bytes into a Java certificate object
120  InputStream in = new ByteArrayInputStream(certBytes);
121  CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
122  x509Cert = (X509Certificate) certFactory.generateCertificate(in);
123 
124  X500Principal subject = x509Cert.getSubjectX500Principal();
125  System.out.println( "X509 Certificate subject: " + subject.getName());
126  assertEquals("2.5.4.41=#130e4d61726b204b2e204a6f73657068,2.5.4.4=#13064a6f73657068,2.5.4.42=#13044d61726b,1.2.840.113549.1.9.1=#160c6d61726b407036722e636f6d,CN=KMIP Engineer,OU=Optics2,OU=Engineering,O=P6R\\, Inc.,L=95030,ST=CA,C=US", subject.getName());
127  }
128  }
129  sl.freeLibrary();
130 
131  } catch (Exception e) {
132  // -> we shoud not get here
133  System.out.println(e.toString());
134  assertEquals(0, 1);
135  }
136  }
137 }
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
void JNICall_RegisterCertKMIP()
Test: Verify parser can parse an incoming register operation with a certificate object.