Add a test target and some classes and scripts.

Now we can test with "gmake test".


git-svn-id: svn://10.0.0.236/trunk@107077 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
nicolson%netscape.com 2001-11-02 06:33:21 +00:00
parent 86a7f7d224
commit 16c77d7089
7 changed files with 462 additions and 7 deletions

View File

@ -71,6 +71,6 @@ include $(CORE_DEPTH)/$(MODULE)/config/rules.mk
#######################################################################
# (7) Execute "local" rules. (OPTIONAL). #
#######################################################################
include ./rules.mk

View File

@ -0,0 +1,65 @@
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Netscape Security Services for Java.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 2001 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License Version 2 or later (the
* "GPL"), in which case the provisions of the GPL are applicable
* instead of those above. If you wish to allow use of your
* version of this file only under the terms of the GPL and not to
* allow others to use your version of this file under the MPL,
* indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by
* the GPL. If you do not delete the provisions above, a recipient
* may use your version of this file under either the MPL or the
* GPL.
*/
package org.mozilla.jss.tests;
import org.mozilla.jss.CryptoManager;
import org.mozilla.jss.crypto.*;
import org.mozilla.jss.util.*;
public class SetupDBs {
public static void main(String args[]) {
try {
if( args.length != 1 ) {
System.err.println("Invalid number of arguments");
System.exit(1);
}
String dbdir = args[0];
CryptoManager.initialize(dbdir);
CryptoManager cm = CryptoManager.getInstance();
CryptoToken tok = cm.getInternalKeyStorageToken();
tok.initPassword( new NullPasswordCallback(),
new Password( ("netscape").toCharArray() )
);
System.exit(0);
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}

View File

@ -0,0 +1,133 @@
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Netscape Security Services for Java.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998-2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License Version 2 or later (the
* "GPL"), in which case the provisions of the GPL are applicable
* instead of those above. If you wish to allow use of your
* version of this file only under the terms of the GPL and not to
* allow others to use your version of this file under the MPL,
* indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by
* the GPL. If you do not delete the provisions above, a recipient
* may use your version of this file under either the MPL or the
* GPL.
*/
/* This program demonstrates how to sign data with keys from JSS
* The token name can be either the name of a hardware token, or
* one of the internal tokens:
* Internal Crypto Services Token
* Internal Key Storage Token (keys stored in key3.db)
*/
package org.mozilla.jss.tests;
import org.mozilla.jss.crypto.*;
import org.mozilla.jss.crypto.Signature;
import org.mozilla.jss.crypto.KeyPairGenerator;
import java.security.*;
import java.security.cert.X509Certificate;
import java.io.*;
import java.lang.*;
import java.util.*;
import org.mozilla.jss.util.*;
import org.mozilla.jss.pkcs11.*;
import org.mozilla.jss.*;
public class SigTest {
public static void usage() {
System.out.println(
"Usage: java org.mozilla.jss.crypto.SigTest <dbdir> <tokenname>");
}
public static void main(String args[]) {
try {
CryptoToken token;
CryptoManager manager;
byte[] data = new byte[] {1,2,3,4,5,6,7,8,9};
byte[] signature;
Signature signer;
PublicKey pubk;
KeyPairGenerator kpgen;
KeyPair keyPair;
if(args.length != 2) {
usage();
System.exit(1);
}
String dbdir = args[0];
String tokenname = args[1];
CryptoManager.InitializationValues vals = new
CryptoManager.InitializationValues(args[0]);
CryptoManager.initialize(vals);
manager = CryptoManager.getInstance();
manager.setPasswordCallback(
new Password("netscape".toCharArray()));
/* Print out list of available tokens */
Enumeration en = manager.getAllTokens();
System.out.println("Available tokens:");
while (en.hasMoreElements()) {
PK11Token p = (PK11Token)en.nextElement();
System.out.println(" token : "+p.getName());
}
token = manager.getTokenByName(tokenname);
// Generate an RSA keypair
kpgen = token.getKeyPairGenerator(KeyPairAlgorithm.RSA);
kpgen.initialize(1024);
keyPair = kpgen.genKeyPair();
// RSA MD5
signer = token.getSignatureContext(
SignatureAlgorithm.RSASignatureWithMD5Digest);
System.out.println("Created a signing context");
signer.initSign(
(org.mozilla.jss.crypto.PrivateKey)keyPair.getPrivate());
System.out.println("initialized the signing operation");
signer.update(data);
System.out.println("updated signature with data");
signature = signer.sign();
System.out.println("Successfully signed!");
signer.initVerify(keyPair.getPublic());
System.out.println("initialized verification");
signer.update(data);
System.out.println("updated verification with data");
if( signer.verify(signature) ) {
System.out.println("Signature Verified Successfully!");
} else {
System.out.println("ERROR: Signature failed to verify.");
}
System.out.println("SigTest passed.");
System.exit(0);
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}

View File

@ -0,0 +1,171 @@
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Netscape Security Services for Java.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998-2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License Version 2 or later (the
* "GPL"), in which case the provisions of the GPL are applicable
* instead of those above. If you wish to allow use of your
* version of this file only under the terms of the GPL and not to
* allow others to use your version of this file under the MPL,
* indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by
* the GPL. If you do not delete the provisions above, a recipient
* may use your version of this file under either the MPL or the
* GPL.
*/
/* This file demonstrates the use of JSS api to generate RSA and
* DSA keys. The key pairs are stored in key3.db
*/
/**
* Note: when this program is run, it must have a key3.db WITH A PASSWORD
* SET in the directory specified by the argument. The first time the
* program is run, a key3.db file will be created, but it will not have
* a password. This will result in the error:
* Token error: org.mozilla.jss.crypto.TokenException: unable to login to token
*
* To create a database with a password, you can:
* use the modutil or keyutil tool,
* use the JSS API CryptoToken.changePassword() to set the password
* run the test 'TokenAccessTest'
* which will create db with the password 'netscape'.
*/
package org.mozilla.jss.tests;
import org.mozilla.jss.pkcs11.*;
import org.mozilla.jss.util.*;
import org.mozilla.jss.crypto.*;
import org.mozilla.jss.*;
import java.io.*;
import java.awt.*;
import java.security.cert.*;
import java.security.interfaces.*;
import java.math.BigInteger;
public class TestKeyGen {
public static void main(String[] args) {
try {
CryptoToken token;
CryptoManager manager;
KeyPairGenerator keyPairGenerator;
java.security.KeyPair keyPair;
Base64OutputStream base64;
if(args.length != 1) {
System.err.println(
"Usage: java org.mozilla.jss.pkcs11.TestKeyGen <dbdir>");
System.exit(1);
return;
}
CryptoManager.InitializationValues vals = new
CryptoManager.InitializationValues( args[0] );
CryptoManager.initialize(vals);
manager = CryptoManager.getInstance();
manager.setPasswordCallback(
new Password( "netscape".toCharArray() ));
java.util.Enumeration tokens =
manager.getTokensSupportingAlgorithm(KeyPairAlgorithm.RSA);
System.out.println("The following tokens support RSA keygen:");
while(tokens.hasMoreElements()) {
System.out.println("\t"+
((CryptoToken)tokens.nextElement()).getName() );
}
tokens = manager.getTokensSupportingAlgorithm(KeyPairAlgorithm.DSA);
System.out.println("The following tokens support DSA keygen:");
while(tokens.hasMoreElements()) {
System.out.println("\t"+
((CryptoToken)tokens.nextElement()).getName() );
}
RSAPublicKey rsaPubKey;
DSAPublicKey dsaPubKey;
DSAParams dsaParams;
RSAParameterSpec rsaParams;
token = manager.getInternalKeyStorageToken();
keyPairGenerator = token.getKeyPairGenerator(KeyPairAlgorithm.RSA);
// 512-bit RSA with default exponent
keyPairGenerator.initialize(512);
keyPair = keyPairGenerator.genKeyPair();
Assert.assert( keyPair.getPublic() instanceof RSAPublicKey);
rsaPubKey = (RSAPublicKey) keyPair.getPublic();
System.out.println("Generated 512-bit RSA KeyPair!");
System.out.println("Modulus: "+rsaPubKey.getModulus());
System.out.println("Exponent: "+rsaPubKey.getPublicExponent());
// 1024-bit RSA with default exponent
keyPairGenerator.initialize(1024);
keyPair = keyPairGenerator.genKeyPair();
Assert.assert( keyPair.getPublic() instanceof RSAPublicKey);
rsaPubKey = (RSAPublicKey) keyPair.getPublic();
System.out.println("Generated 1024-bit RSA KeyPair!");
System.out.println("Modulus: "+rsaPubKey.getModulus());
System.out.println("Exponent: "+rsaPubKey.getPublicExponent());
// 512-bit RSA with exponent = 3
rsaParams = new RSAParameterSpec(512, BigInteger.valueOf(3));
keyPairGenerator.initialize(rsaParams);
keyPair = keyPairGenerator.genKeyPair();
Assert.assert( keyPair.getPublic() instanceof RSAPublicKey);
rsaPubKey = (RSAPublicKey) keyPair.getPublic();
System.out.println("Generated 512-bit RSA KeyPair with public exponent=3!");
System.out.println("Modulus: "+rsaPubKey.getModulus());
System.out.println("Exponent: "+rsaPubKey.getPublicExponent());
// 512-bit DSA
keyPairGenerator = token.getKeyPairGenerator(KeyPairAlgorithm.DSA);
keyPairGenerator.initialize(512);
keyPair = keyPairGenerator.genKeyPair();
Assert.assert( keyPair.getPublic() instanceof DSAPublicKey);
dsaPubKey = (DSAPublicKey) keyPair.getPublic();
System.out.println("Generated 512-bit DSA KeyPair!");
dsaParams = dsaPubKey.getParams();
System.out.println("P: "+dsaParams.getP());
System.out.println("Q: "+dsaParams.getQ());
System.out.println("G: "+dsaParams.getG());
System.out.println("Y: "+dsaPubKey.getY());
// 1024-bit DSA
keyPairGenerator.initialize(1024);
keyPair = keyPairGenerator.genKeyPair();
Assert.assert( keyPair.getPublic() instanceof DSAPublicKey);
dsaPubKey = (DSAPublicKey) keyPair.getPublic();
System.out.println("Generated 1024-bit DSA KeyPair!");
dsaParams = dsaPubKey.getParams();
System.out.println("P: "+dsaParams.getP());
System.out.println("Q: "+dsaParams.getQ());
System.out.println("G: "+dsaParams.getG());
System.out.println("Y: "+dsaPubKey.getY());
System.out.println("TestKeyGen passed");
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}

View File

@ -0,0 +1,79 @@
#####
# TODO:
# test scripts should take passwd on command line & login manually.
# fix socket test: unknown issuer
# get rid of modutil, do it from java.
####
use strict;
my $passwd = "netscape";
my $passwdfile = "password";
my $dist = "$ARGV[0]";
my $java = "$ARGV[1]";
my $ssltesthost = "trading.etrade.com";
my $signingToken = "Internal Key Storage Token";
(-d $dist) or die "Directory '$dist' does not exist\n";
(-f $java) or die "'$java' does not exist\n";
my @env_vars = (
"LD_LIBRARY_PATH",
"CLASSPATH"
);
my $modutil = "$dist/bin/modutil";
print "*****ENVIRONMENT*****\n";
print "\$(DIST)=$dist\n";
print "\$(JAVA)=$java\n";
foreach my $var (@env_vars) {
print "$var=$ENV{$var}\n";
}
print "modutil is $modutil\n";
print "password is $passwd\n";
print "*********************\n";
#
# Make the test database directory
#
my $testdir = "testdir";
if( ! -d $testdir ) {
mkdir( $testdir, 0755 ) or die;
}
{
chdir "testdir" or die;
my @dbfiles =
("./cert7.db", "./key3.db", "./secmod.db", "./secmodule.db");
unlink @dbfiles;
(grep{ -f } @dbfiles) and die "Unable to delete old database files";
chdir ".." or die;
my $result = system("cp $dist/lib/libnssckbi.so testdir"); $result >>= 8;
$result and die "Failed to copy builtins library";
}
my $result;
#$result = system("$modutil -dbdir $testdir -create -force"); $result >>= 8;
#$result and die "modutil returned $result";
#system("echo $passwd > $testdir/$passwdfile");
#$result = system("$modutil -dbdir $testdir -force -changepw ".
# "\"NSS Certificate DB\" -pwfile $testdir/$passwdfile ".
# "-newpwfile $testdir/$passwdfile"); $result >>= 8;
#$result and die "modutil returned $result";
#$result = system("$modutil -force -dbdir $testdir -add builtins -libfile /u/nicolson/local/jss/mozilla/dist/SunOS5.8_DBG.OBJ/lib/libnssckbi.so");
#$result and die "modutil returned $result";
$result = system("$java org.mozilla.jss.tests.SetupDBs testdir"); $result >>=8;
$result and die "SetupDBs returned $result";
#
# test sockets
#
$result = system("$java socketTest $testdir $ssltesthost"); $result >>=8;
$result and die "socketTest returned $result";
# test key gen
#
$result = system("$java org.mozilla.jss.tests.TestKeyGen $testdir");$result >>=8;
$result and die "TestKeyGen returned $result";
# test signing
#
$result = system("$java org.mozilla.jss.tests.SigTest $testdir " .
"\"$signingToken\""); $result >>=8;
$result and die "SigTest returned $result";

View File

@ -49,6 +49,9 @@ PRIVATE_CLASSES = \
KeyWrapping \
ListCerts \
socketTest \
org.mozilla.jss.tests.SetupDBs \
org.mozilla.jss.tests.TestKeyGen \
org.mozilla.jss.tests.SigTest \
$(NULL)
JSRCS = \
@ -61,4 +64,7 @@ JSRCS = \
KeyWrapping.java \
ListCerts.java \
socketTest.java \
SetupDBs.java \
TestKeyGen.java \
SigTest.java \
$(NULL)

View File

@ -37,18 +37,19 @@ import org.mozilla.jss.*;
public class socketTest {
public static void main(String []args) {
try {
try {
// initialize CryptoManager.
CryptoManager.initialize(".");
//SSLSocket.setCipherPolicy(SSLSocket.CipherPolicy.DOMESTIC);
CryptoManager.initialize(args[0]);
SSLSocket sslSocket = new SSLSocket("www.amazon.com", 443);
SSLSocket sslSocket = new SSLSocket(args[1], 443);
sslSocket.forceHandshake();
System.out.println("Test, forced handshake");
} catch( Exception e ) {
System.exit(0);
} catch( Exception e ) {
e.printStackTrace();
}
System.exit(1);
}
}
}