Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniECKeyKmip.java
package com.p6r.kmip;
import org.junit.*;
import javax.crypto.spec.SecretKeySpec;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.math.BigInteger;
import java.security.spec.*;
public class JniECKeyKmip {
private static final String _hostName = "kmiptest01.p6r.com";
@BeforeClass
public static void oneTimeSetUp() {
// NOOP
System.out.println("@BeforeClass - oneTimeSetUp");
}
@AfterClass
public static void oneTimeTearDown() {
// NOOP
System.out.println("@AfterClass - oneTimeTearDown");
}
@Before
public void setUp() {
// NOOP
System.out.println("@Before - setUp");
}
@After
public void tearDown() {
// NOOP
System.out.println("@After - tearDown");
}
@Test
public void JNICall_ECKeyKMIP() {
System.out.println("@Test - JNICall-ECKeyKMIP");
String uid = null;
String[] uidList = null;
String[] attribute = null;
KeyPairIds pi = null;
TransparentKey tk = null;
TransparentECPublicKey ecPubKey = null;
TransparentECPrivateKey ecPrvKey = null;
X509Key x509key = null; // if KMIP version is 1.2 or less
P6KMIPClient kc = new P6KMIPClient();
try {
kc.initializeLibrary(P6KMIPClient.FLAGS_NONE);
kc.open(_hostName, null);
// Test EC public/private keys
// note that not all KMIP servers will provide support for EC keys so an exception may be thrown here
DomainParams dpp = new DomainParams();
dpp.setQlength(256);
dpp.setRecommendedCurve(KMIPConstants.CURVE_SECP256K1);
pi = kc.createKeyPairWithParams(KMIPConstants.ALG_EC, dpp, (KMIPConstants.USAGE_MASK_DERIVEKEY | KMIPConstants.USAGE_MASK_VERIFY), (KMIPConstants.USAGE_MASK_DERIVEKEY | KMIPConstants.USAGE_MASK_SIGN));
System.out.println("\nUID of new EC public key with params: " + pi.getPublicKeyUID());
System.out.println("UID of new EC private key with params: " + pi.getPrivateKeyUID());
attribute = kc.getAttribute(pi.getPublicKeyUID(), "Cryptographic Algorithm");
assertEquals(attribute[0], "Elliptic Curve");
assertEquals(attribute.length, 1);
try {
tk = kc.getPublicKey(pi.getPublicKeyUID());
if (tk instanceof TransparentECPublicKey) {
// -> when JNI can use KMIP 1.3 and greater
ecPubKey = (TransparentECPublicKey) tk;
System.out.println("\nEC public key, Q bytes, curveId: " + ecPubKey.getCurveId());
byte[] q = ecPubKey.getQ_Bytes();
for (int i = 0; i < q.length; i++) {
System.out.print(q[i] + ", ");
}
System.out.print("\n");
} else if (tk instanceof X509Key) {
// -> when JNI has to use KMIP 1.2 and eailer
x509key = (X509Key) tk;
System.out.println("\nEC public key in X509 format, key algorithm: " + x509key.getAlgorithm());
byte[] q = x509key.getBytes();
for (int i = 0; i < q.length; i++) {
System.out.print(q[i] + ", ");
}
System.out.print("\n");
} else assertEquals(0, 1);
tk = kc.getPrivateKey(pi.getPrivateKeyUID());
if (tk instanceof TransparentECPrivateKey) {
ecPrvKey = (TransparentECPrivateKey) tk;
BigInteger D = ecPrvKey.getD();
System.out.println("\nEC private key, D: " + D.toString(16) + "\ncurveId: " + ecPrvKey.getCurveId() + "\n");
} else assertEquals(0, 1);
} catch (Exception e) {
// Not a supported Key format?
System.out.println(e.toString());
}
kc.destroy(pi.getPublicKeyUID());
kc.destroy(pi.getPrivateKeyUID());
kc.close();
kc.freeLibrary();
} catch (Exception e) {
// -> we shoud not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}