add TBS certificate support

pull/23/head
Jacob Barthelmeh 2019-03-06 16:36:59 -07:00
parent eafc4e7e0d
commit 0622f0a93d
4 changed files with 54 additions and 6 deletions

View File

@ -68,6 +68,45 @@ JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1get_1der
return (jbyteArray)wolfSSL_X509_get_der((WOLFSSL_X509*)x509, &sz);
}
JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1get_1tbs
(JNIEnv* jenv, jclass jcl, jlong x509)
{
jbyteArray ret;
int sz;
const unsigned char* tbs;
tbs = wolfSSL_X509_get_tbs((WOLFSSL_X509*)x509, &sz);
if (tbs == NULL) {
return NULL;
}
ret = (*jenv)->NewByteArray(jenv, sz);
if (!ret) {
(*jenv)->ThrowNew(jenv, jcl,
"Failed to create byte array in native X509_get_tbs");
return NULL;
}
jclass excClass = (*jenv)->FindClass(jenv,
"com/wolfssl/WolfSSLJNIException");
if ((*jenv)->ExceptionOccurred(jenv)) {
(*jenv)->ExceptionDescribe(jenv);
(*jenv)->ExceptionClear(jenv);
return NULL;
}
(*jenv)->SetByteArrayRegion(jenv, ret, 0, sz, (jbyte*)tbs);
if ((*jenv)->ExceptionOccurred(jenv)) {
(*jenv)->ExceptionDescribe(jenv);
(*jenv)->ExceptionClear(jenv);
(*jenv)->ThrowNew(jenv, excClass,
"Failed to set byte region in native X509_get_tbs");
return NULL;
}
return ret;
}
JNIEXPORT void JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1free
(JNIEnv* jenv, jclass jcl, jlong x509)
{
@ -156,7 +195,6 @@ JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1get_1sign
if (wolfSSL_X509_get_signature((WOLFSSL_X509*)x509, NULL, &sz) !=
WOLFSSL_SUCCESS) {
printf("first get signature call failed\n");
return NULL;
}
@ -164,20 +202,17 @@ JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1get_1sign
if (!ret) {
(*jenv)->ThrowNew(jenv, jcl,
"Failed to create byte array in native X509_get_signature");
printf("could not create new byte array\n");
return NULL;
}
buf = (unsigned char*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (buf == NULL) {
printf("malloc failed\n");
return NULL;
}
if (wolfSSL_X509_get_signature((WOLFSSL_X509*)x509, buf, &sz) !=
WOLFSSL_SUCCESS) {
XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
printf("get signature failed\n");
return NULL;
}
@ -186,7 +221,6 @@ JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1get_1sign
if ((*jenv)->ExceptionOccurred(jenv)) {
(*jenv)->ExceptionDescribe(jenv);
(*jenv)->ExceptionClear(jenv);
printf("set byte array region failed\n");
return NULL;
}

View File

@ -23,6 +23,14 @@ JNIEXPORT jlong JNICALL Java_com_wolfssl_WolfSSLCertificate_d2i_1X509
JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1get_1der
(JNIEnv *, jclass, jlong);
/*
* Class: com_wolfssl_WolfSSLCertificate
* Method: X509_get_tbs
* Signature: (J)[B
*/
JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1get_1tbs
(JNIEnv *, jclass, jlong);
/*
* Class: com_wolfssl_WolfSSLCertificate
* Method: X509_free

View File

@ -38,6 +38,7 @@ public class WolfSSLCertificate {
static native long d2i_X509(byte[] der, int len);
static native byte[] X509_get_der(long x509);
static native byte[] X509_get_tbs(long x509);
static native void X509_free(long x509);
static native int X509_get_serial_number(long x509, byte[] out);
static native String X509_notBefore(long x509);
@ -95,6 +96,11 @@ public class WolfSSLCertificate {
return X509_get_der(this.x509Ptr);
}
/* return the buffer that is To Be Signed */
public byte[] getTbs() {
return X509_get_tbs(this.x509Ptr);
}
public BigInteger getSerial() {
byte[] out = new byte[32];
int sz = X509_get_serial_number(this.x509Ptr, out);

View File

@ -134,7 +134,7 @@ public class WolfSSLX509 extends X509Certificate {
@Override
public byte[] getTBSCertificate() throws CertificateEncodingException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return this.cert.getTbs();
}
@Override