1 package com.p6r.kmipserverlib;
5 import java.text.DateFormat;
6 import java.text.SimpleDateFormat;
8 import java.util.TimeZone;
10 import static org.junit.Assert.assertEquals;
26 System.out.println(
"@BeforeClass - oneTimeSetUp");
32 System.out.println(
"@AfterClass - oneTimeTearDown");
38 System.out.println(
"@Before - setUp");
44 System.out.println(
"@After - tearDown");
54 System.out.println(
"@Test - JNICall-Respnse2KMIP");
58 P6KMIPServerLib sl =
new P6KMIPServerLib();
61 sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
64 sl.startResponseMessage(1, KMIPConstants.FORMAT_TTLV, 2);
66 byte[] batchId1 = {0x01, 0x02, 0x03, 0x04};
67 byte[] fingerPrint = {0x11, 0x22, 0x33, 0x44, 0x55};
69 long timeStamp = System.currentTimeMillis();
70 DateFormat df =
new SimpleDateFormat(
"MM/dd/yyyy HH:mm:ss Z");
71 df.setTimeZone(TimeZone.getTimeZone(
"GMT"));
73 Date today =
new Date(timeStamp);
74 System.out.println(
"Unix Time stamp: " + timeStamp +
", date: " + df.format(today) +
"\n");
76 Attribute comment =
new Attribute(KMIPConstants.ATTRIB_COMMENTS, 0,
"attribute test case");
77 Attribute state =
new Attribute(KMIPConstants.ATTRIB_STATE, 0, KMIPConstants.STATE_ACTIVE);
78 Attribute keyAlg =
new Attribute(KMIPConstants.ATTRIB_CRYPTOALGORITHM, 0, KMIPConstants.ALG_AES);
79 Attribute fresh =
new Attribute(KMIPConstants.ATTRIB_FRESH, 0,
false);
80 Attribute cryptoLength =
new Attribute(KMIPConstants.ATTRIB_CRYPTOLENGTH, 0,
new Integer(128));
81 Attribute cryptoMask =
new Attribute(KMIPConstants.ATTRIB_CRYPTOUSAGEMASK, 0,
new Integer((KMIPConstants.USAGE_MASK_ENCRYPT | KMIPConstants.USAGE_MASK_DECRYPT)));
82 Attribute initialDate =
new Attribute(KMIPConstants.ATTRIB_INITIALDATE, 0,
new Long(timeStamp));
87 CryptograhicParameters keyParams =
new CryptograhicParameters( KMIPConstants.MODE_ECB, KMIPConstants.PAD_PKCS5, -1, KMIPConstants.ROLE_KEK, -1, KMIPConstants.ALG_AES,
false, -1, -1, -1, -1, -1, -1, -1, -1, -1, null, -1 );
88 Attribute keyCryptoParams =
new Attribute(0, keyParams);
91 CustomAttribute ca1 =
new CustomAttribute(
"y-ServerSpecific1",
true);
92 Attribute custom1 =
new Attribute(0, ca1);
93 CustomAttribute ca2 =
new CustomAttribute(
"x-clientSpecific1",
"a b c d e f");
94 Attribute custom2 =
new Attribute(0, ca2);
95 CustomAttribute ca3 =
new CustomAttribute(
"x-clientSpecific11", 55);
96 Attribute custom3 =
new Attribute(0, ca3);
97 CustomAttribute ca4 =
new CustomAttribute(
"y-fingerprint", fingerPrint);
98 Attribute custom4 =
new Attribute(0, ca4);
100 NameAttribute name =
new NameAttribute(
"Client-Defined-ID: 456", KMIPConstants.NAME_TEXTSTR);
101 Attribute name1 =
new Attribute(KMIPConstants.ATTRIB_NAME, 0, name);
102 NameAttribute altName =
new NameAttribute(
"192.10.3.25", KMIPConstants.ALTNAME_IPADDRESS);
103 Attribute name2 =
new Attribute(KMIPConstants.ATTRIB_ALTERNATIVENAME, 0, altName);
104 NameAttribute keyLoc =
new NameAttribute(
"http://abc.hsm.com/12345", KMIPConstants.KVLT_URI);
105 Attribute name3 =
new Attribute(KMIPConstants.ATTRIB_KEYVALUELOCATION, 0, keyLoc);
107 byte[] fakeDigest = { (byte)0xAA, (byte)0xBB, (byte)0xCC, (byte)0xDD, (byte)0xEE, (byte)0xFF, 0x00, (byte)0x99, (byte)0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11};
108 DigestAttribute da =
new DigestAttribute(KMIPConstants.HASH_SHA512, fakeDigest, KMIPConstants.KEYFORMAT_TRANSYMKEY);
109 Attribute digest1 =
new Attribute(0, da);
112 String[] alternativeNames =
new String[3];
113 alternativeNames[0] =
"frank@p6r.com";
114 alternativeNames[1] =
"192.34.5.689";
115 alternativeNames[2] =
"jni-example.p6r.com";
116 CertificateNameAttribute cn =
new CertificateNameAttribute(
"CN=Sample Cert, OU=R&D, O=Company Ltd., L=Dublin 4, S=Dublin, C=IE", alternativeNames);
117 Attribute certName1 =
new Attribute(KMIPConstants.ATTRIB_CERTIFICATEISSUER, cn);
120 byte[] X509DN_fake = { 0x25, 0x00, 0x00, 0x00, 0x34 };
121 byte[] alt1bytes = { 0x01, 0x02, 0x03, 0x00, 0x04 };
122 byte[] alt2bytes = { 0x00, 0x71, 0x52, 0x23, 0x14, 0x0F };
123 ByteArrayObject alt1 =
new ByteArrayObject( alt1bytes );
124 ByteArrayObject alt2 =
new ByteArrayObject( alt2bytes );
125 ByteArrayObject[] X509alternativeNames =
new ByteArrayObject[2];
126 X509alternativeNames[0] = alt1;
127 X509alternativeNames[1] = alt2;
128 X509CertificateNameAttribute x509CN =
new X509CertificateNameAttribute(X509DN_fake, X509alternativeNames);
129 Attribute x509CertName1 =
new Attribute(KMIPConstants.ATTRIB_X509CERTIFICATESUBJECT, x509CN);
131 CertificateIdentifierAttribute ci =
new CertificateIdentifierAttribute(
"CN=Sample Cert, OU=R&D, O=Company Ltd., L=York, S=York, C=IE",
"12345ABD9384");
132 Attribute certIdentifier =
new Attribute(KMIPConstants.ATTRIB_CERTIFICATEIDENTIFIER, ci);
134 RevocationReasonAttribute rr =
new RevocationReasonAttribute(
"Key exceeded its usage limits", KMIPConstants.REVOCATION_KEYCOMPROMISE);
135 Attribute revocation =
new Attribute(rr);
137 NameAttribute link =
new NameAttribute(
"UID-111-333-000", KMIPConstants.LINK_PRIVATEKEY);
138 Attribute privateLink =
new Attribute(KMIPConstants.ATTRIB_LINK, 0, link);
140 UsageLimitsAttribute ul =
new UsageLimitsAttribute(50000000, 2000000, KMIPConstants.USAGE_LIMITS_BYTE);
141 Attribute usageLimits =
new Attribute(ul);
143 ApplicationSpecificInfoAttribute as =
new ApplicationSpecificInfoAttribute(
"P6R",
"#12345678990000#");
144 Attribute appInfo =
new Attribute(1,as);
146 Attribute digitalSigAlg =
new Attribute(KMIPConstants.ATTRIB_DIGITALSIGALG, 0, KMIPConstants.SIG_ECDSASHA1);
147 Attribute certSubjectCN =
new Attribute(KMIPConstants.ATTRIB_CERTSUBJECT_CN, 0,
"www.example.com");
149 Attribute[] attributes =
new Attribute[25];
150 attributes[0] = comment;
151 attributes[1] = state;
152 attributes[2] = fresh;
153 attributes[3] = cryptoLength;
154 attributes[4] = initialDate;
155 attributes[5] = keyAlg;
156 attributes[6] = cryptoMask;
157 attributes[7] = custom1;
158 attributes[8] = custom2;
159 attributes[9] = custom3;
160 attributes[10] = custom4;
161 attributes[11] = keyCryptoParams;
162 attributes[12] = name1;
163 attributes[13] = name2;
164 attributes[14] = name3;
165 attributes[15] = digest1;
166 attributes[16] = certName1;
167 attributes[17] = x509CertName1;
168 attributes[18] = certIdentifier;
169 attributes[19] = revocation;
170 attributes[20] = privateLink;
171 attributes[21] = usageLimits;
172 attributes[22] = appInfo;
173 attributes[23] = digitalSigAlg;
174 attributes[24] = certSubjectCN;
176 GetAttributesResponse ga =
new GetAttributesResponse(batchId1,
"1111-2222-3333-4444-5555", attributes);
179 byte[] responseBuffer = sl.endResponseMessage(0);
180 for (
int j = 0; j < responseBuffer.length; j++) {
181 System.out.print(responseBuffer[j]);
183 System.out.println(
"\n");
187 sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 2);
191 byte[] responseBuffer2 = sl.endResponseMessage(0);
192 System.out.println(
new String(responseBuffer2));
193 System.out.println(
"\n");
197 sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGJSON, 2);
201 byte[] responseBuffer3 = sl.endResponseMessage(0);
202 System.out.println(
new String(responseBuffer3));
203 System.out.println(
"\n");
207 sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 1);
209 AddAttributeResponse aa =
new AddAttributeResponse(batchId1,
"UID-383837jjejej", state);
212 byte[] responseBuffer4 = sl.endResponseMessage(0);
213 System.out.println(
new String(responseBuffer4));
214 System.out.println(
"\n");
218 sl.startResponseMessage(1, KMIPConstants.FORMAT_MSGXML, 2);
220 GetTemplateResponse tr =
new GetTemplateResponse(null,
"1111-2222-3333-4444-5555", attributes);
223 byte[] responseBuffer5 = sl.endResponseMessage(0);
224 System.out.println(
new String(responseBuffer5));
225 System.out.println(
"\n");
229 }
catch (Exception e) {
231 System.out.println(e.toString());
static void oneTimeSetUp()
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
void JNICall_Response2KMIP()
Test: Generate a message with multiple batch items.
static void oneTimeTearDown()