Skip to content

Commit

Permalink
Add PKCS10Test based off of netscape.security
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Scheel <[email protected]>
  • Loading branch information
cipherboy committed May 18, 2020
1 parent e7abe34 commit b8c1665
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
9 changes: 7 additions & 2 deletions cmake/JSSTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ macro(jss_tests)
COMMAND "pk12util" "-o" "${RESULTS_NSSDB_OUTPUT_DIR}/dss.pfx" "-n" "CA_DSS" "-d" "${RESULTS_NSSDB_OUTPUT_DIR}" "-K" "${DB_PWD}" "-W" "${DB_PWD}"
DEPENDS "Generate_known_DSS_cert_pair"
)
jss_test_java(
NAME "Netscape_Security_PKCS10"
COMMAND "org.mozilla.jss.tests.PKCS10Test"
DEPENDS "Setup_DBs"
)
jss_test_java(
NAME "List_CA_certs"
COMMAND "org.mozilla.jss.tests.ListCACerts" "${RESULTS_NSSDB_OUTPUT_DIR}" "Verbose"
Expand Down Expand Up @@ -264,12 +269,12 @@ macro(jss_tests)
jss_test_java(
NAME "KeyStoreTest"
COMMAND "org.mozilla.jss.tests.KeyStoreTest" "${RESULTS_NSSDB_OUTPUT_DIR}" "${PASSWORD_FILE}" getAliases
DEPENDS "List_CA_certs" "X509CertTest" "Secret_Key_Generation" "Symmetric_Key_Deriving" "SSLClientAuth"
DEPENDS "List_CA_certs" "X509CertTest" "Secret_Key_Generation" "Symmetric_Key_Deriving" "SSLClientAuth" "Netscape_Security_PKCS10"
)
jss_test_java(
NAME "JSSProvider"
COMMAND "org.mozilla.jss.tests.JSSProvider" "${RESULTS_NSSDB_OUTPUT_DIR}" "${PASSWORD_FILE}"
DEPENDS "List_CA_certs" "X509CertTest" "Secret_Key_Generation" "Symmetric_Key_Deriving" "SSLClientAuth"
DEPENDS "List_CA_certs" "X509CertTest" "Secret_Key_Generation" "Symmetric_Key_Deriving" "SSLClientAuth" "Netscape_Security_PKCS10"
)
jss_test_java(
NAME "SSLEngine_RSA"
Expand Down
11 changes: 4 additions & 7 deletions org/mozilla/jss/netscape/security/pkcs/PKCS10.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,10 @@ public PKCS10(byte data[], boolean sigver)
//
// Inner sequence: version, name, key, attributes
//
@SuppressWarnings("unused")
BigInt serial = seq[0].data.getInteger(); // consume serial

/*
if (serial.toInt () != 0)
throw new IllegalArgumentException ("not PKCS #10 v1");
*/
BigInt version = seq[0].data.getInteger(); // consume version number
if (version.toInt() != 0) {
throw new IllegalArgumentException ("unknown version: not PKCS #10 v1: " + version);
}

subject = new X500Name(seq[0].data);
msg = "Request Subject: " + subject + ": ";
Expand Down
9 changes: 9 additions & 0 deletions org/mozilla/jss/netscape/security/provider/RSAPublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.IOException;
import java.io.Serializable;
import java.math.BigInteger;
import java.security.InvalidKeyException;

import org.mozilla.jss.netscape.security.util.BigInt;
Expand Down Expand Up @@ -64,6 +65,14 @@ in bits (redundant!)
public RSAPublicKey() {
}

/*
* Make a RSA public key out of a public exponent and modulus
* in the standard classes (BigInteger).
*/
public RSAPublicKey(BigInteger modulus, BigInteger exponent) throws InvalidKeyException {
this(new BigInt(modulus), new BigInt(exponent));
}

/**
* Make a RSA public key out of a public exponent and modulus
*/
Expand Down
6 changes: 6 additions & 0 deletions org/mozilla/jss/netscape/security/x509/CertAndKeyGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ public void generate(int keyBits)
if (publicKey instanceof X509Key) {
this.publicKey = (X509Key) publicKey;

} else if (publicKey instanceof java.security.interfaces.RSAPublicKey) {
java.security.interfaces.RSAPublicKey rsa = (java.security.interfaces.RSAPublicKey) publicKey;
this.publicKey = new org.mozilla.jss.netscape.security.provider.RSAPublicKey(
rsa.getModulus(),
rsa.getPublicExponent()
);
} else {
throw new InvalidKeyException("public key " + publicKey +
" not an X509Key.");
Expand Down
20 changes: 20 additions & 0 deletions org/mozilla/jss/tests/PKCS10Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.mozilla.jss.tests;

import java.security.PublicKey;
import java.security.KeyPair;
import java.security.interfaces.*;

import org.mozilla.jss.*;
import org.mozilla.jss.crypto.*;
import org.mozilla.jss.netscape.security.pkcs.*;
import org.mozilla.jss.netscape.security.x509.*;

public class PKCS10Test {
public static void main(String[] args) throws Exception {
CryptoManager cm = CryptoManager.getInstance();

CertAndKeyGen ckg = new CertAndKeyGen("RSA", "SHA256withRSA");
ckg.generate(4096);
PKCS10 csr = ckg.getCertRequest(new X500Name("CN=localhost"));
}
}

0 comments on commit b8c1665

Please sign in to comment.