Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PK11RSAPrivateKey.getModulus() #7

Merged
merged 1 commit into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/jss.def
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,9 @@ Java_org_mozilla_jss_pkcs11_PK11Store_importEncryptedPrivateKeyInfo;
;+ local:
;+ *;
;+};
;+JSS_4.5 { # JSS 4.5 release
;+ global:
Java_org_mozilla_jss_pkcs11_PK11RSAPrivateKey_getModulusByteArray;
;+ local:
;+ *;
;+};
28 changes: 28 additions & 0 deletions org/mozilla/jss/pkcs11/PK11Exception.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.jss.pkcs11;

/**
* This is a generic PKCS #11 exception.
*/
public class PK11Exception extends RuntimeException {

private static final long serialVersionUID = 1L;

public PK11Exception() {
}

public PK11Exception(String mesg) {
super(mesg);
}

public PK11Exception(Throwable cause) {
super(cause);
}

public PK11Exception(String mesg, Throwable cause) {
super(mesg, cause);
}
}
64 changes: 64 additions & 0 deletions org/mozilla/jss/pkcs11/PK11PrivKey.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,67 @@ Java_org_mozilla_jss_pkcs11_PK11PrivKey_getDSAParamsNative

return pqgArray;
}

/**********************************************************************
* PK11RSAPrivateKey.getModulusByteArray
*/
JNIEXPORT jbyteArray JNICALL
Java_org_mozilla_jss_pkcs11_PK11RSAPrivateKey_getModulusByteArray
(JNIEnv *env, jobject this)
{
SECKEYPrivateKey *privateKey = NULL;
SECKEYPublicKey *publicKey = NULL;
PK11SlotInfo *slot = NULL;
int rv;
jbyte *value = NULL;
jint length;
jbyteArray array = NULL;

PR_ASSERT(env!=NULL && this!=NULL);

// get private key
rv = JSS_PK11_getPrivKeyPtr(env, this, &privateKey);

// check return code
if (rv != PR_SUCCESS) {
char *message = PR_smprintf("Unable to get RSA private key (rc: %d)", rv);
JSS_throwMsg(env, PK11_EXCEPTION, message);
PR_smprintf_free(message);
goto finish;
}

// get slot from private key
slot = PK11_GetSlotFromPrivateKey(privateKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR_ASSERT(slot!=NULL);

// login to token if necessary
PK11_Authenticate(slot, PR_TRUE, NULL);

// convert private key to public key
publicKey = SECKEY_ConvertToPublicKey(privateKey);

// get modulus from public key
value = (jbyte*)publicKey->u.rsa.modulus.data;
length = (jint)publicKey->u.rsa.modulus.len;

// create byte array
array = (*env)->NewByteArray(env, length);

// check byte array creation
if (array == NULL) {
JSS_throw(env, OUT_OF_MEMORY_ERROR);
goto finish;
}

// copy modulus into byte array
(*env)->SetByteArrayRegion(env, array, 0, length, value);

finish:
if (publicKey) {
SECKEY_DestroyPublicKey(publicKey);
}
if (slot) {
PK11_FreeSlot(slot);
}
return array;
}
12 changes: 9 additions & 3 deletions org/mozilla/jss/pkcs11/PK11RSAPrivateKey.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package org.mozilla.jss.pkcs11;

import org.mozilla.jss.crypto.PrivateKey;
import java.math.BigInteger;

import org.mozilla.jss.crypto.PrivateKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class PK11RSAPrivateKey
extends PK11PrivKey implements java.security.interfaces.RSAPrivateKey
{
public static Logger logger = LoggerFactory.getLogger(PK11RSAPrivateKey.class);

private static final long serialVersionUID = 1L;

Expand All @@ -20,10 +24,12 @@ public PrivateKey.Type getType() {
}

public BigInteger getModulus() {
// !!!
return null;
logger.debug("PK11RSAPrivateKey: getModulus()");
return new BigInteger(1, getModulusByteArray());
}

native byte[] getModulusByteArray();

public BigInteger getPrivateExponent() {
// !!!
return null;
Expand Down
2 changes: 2 additions & 0 deletions org/mozilla/jss/util/jss_exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ PR_BEGIN_EXTERN_C

#define OUT_OF_MEMORY_ERROR "java/lang/OutOfMemoryError"

#define PK11_EXCEPTION "org/mozilla/jss/pkcs11/PK11Exception"

#define PQG_PARAM_GEN_EXCEPTION "org/mozilla/jss/crypto/PQGParamGenException"

/* This is a RuntimeException */
Expand Down