Skip to content

Commit

Permalink
Split CertificationRequest test into separate file
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 947c996 commit e7abe34
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 76 deletions.
76 changes: 0 additions & 76 deletions org/mozilla/jss/pkcs10/CertificationRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

package org.mozilla.jss.pkcs10;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -247,77 +244,4 @@ public ASN1Value decode(Tag implicitTag, InputStream istream)
);
}
}

public static void main(String argv[]) {

try {

if(argv.length > 2 || argv.length < 1) {
System.out.println("Usage: CertificationRequest <dbdir> [<certfile>]");
System.exit(0);
}

CryptoManager.initialize( argv[0] );
CryptoManager cm = CryptoManager.getInstance();

CertificationRequest cert;

// read in a cert
FileInputStream fis = new FileInputStream(argv[1]);
try (BufferedInputStream bis = new BufferedInputStream(fis)) {
cert = (CertificationRequest) CertificationRequest.getTemplate().decode(bis);
}

CertificationRequestInfo info = cert.getInfo();

info.print(System.out);

//X509CertificationRequest hardcore = cm.findCertByNickname("Hardcore");
//PublicKey key = hardcore.getPublicKey();

cert.verify();
System.out.println("verified");

FileOutputStream fos = new FileOutputStream("certinfo.der");
info.encode(fos);
fos.close();

// make a new public key
CryptoToken token = cm.getInternalKeyStorageToken();
KeyPairGenerator kpg = token.getKeyPairGenerator(KeyPairAlgorithm.RSA);
kpg.initialize(512);
System.out.println("Generating a new key pair...");
KeyPair kp = kpg.genKeyPair();
System.out.println("Generated key pair");

// set the CertificationRequest's public key
info.setSubjectPublicKeyInfo(kp.getPublic());

// make new Name
Name name = new Name();
name.addCommonName("asldkj");
name.addCountryName("US");
name.addOrganizationName("Some Corp");
name.addOrganizationalUnitName("Some Org Unit");
name.addLocalityName("Silicon Valley");
name.addStateOrProvinceName("California");
info.setSubject(name);

System.out.println("About to create a new cert request...");
// create a new cert requestfrom this certReqinfo
CertificationRequest genCert = new CertificationRequest(info, kp.getPrivate(),
SignatureAlgorithm.RSASignatureWithMD5Digest);
System.out.println("Created new cert request");

genCert.verify();
System.out.println("Cert verifies!");

fos = new FileOutputStream("gencert.der");
genCert.encode(fos);
fos.close();

} catch( Exception e ) {
e.printStackTrace();
}
}
}
33 changes: 33 additions & 0 deletions org/mozilla/jss/tests/TestCertificationRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.mozilla.jss.tests;

import java.io.*;
import java.security.KeyPair;

import org.mozilla.jss.*;
import org.mozilla.jss.asn1.*;
import org.mozilla.jss.crypto.*;
import org.mozilla.jss.pkcs10.*;
import org.mozilla.jss.pkix.primitive.*;

public class TestCertificationRequest {
public static void main(String[] argv) throws Exception {
if (argv.length > 2 || argv.length < 1) {
System.out.println("Usage: TestCertificationRequest <dbdir> [<certfile>]");
System.exit(0);
}

CryptoManager cm = CryptoManager.getInstance();

CertificationRequest cert;

// read in a cert
FileInputStream fis = new FileInputStream(argv[1]);
try (BufferedInputStream bis = new BufferedInputStream(fis)) {
cert = (CertificationRequest) CertificationRequest.getTemplate().decode(bis);
}

CertificationRequestInfo info = cert.getInfo();

info.print(System.out);
}
}

0 comments on commit e7abe34

Please sign in to comment.