|
From: Pelle B. <pe...@us...> - 2004-03-22 20:19:25
|
Update of /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11773/src/java/org/neuclear/commons/crypto/signers Modified Files: JCESigner.java KeyCache.java TestCaseSigner.java Log Message: Added simple ledger for unit testing and in memory use Index: KeyCache.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers/KeyCache.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** KeyCache.java 21 Nov 2003 04:43:41 -0000 1.2 --- KeyCache.java 22 Mar 2004 20:09:05 -0000 1.3 *************** *** 27,30 **** --- 27,33 ---- $Id$ $Log$ + Revision 1.3 2004/03/22 20:09:05 pelle + Added simple ledger for unit testing and in memory use + Revision 1.2 2003/11/21 04:43:41 pelle EncryptedFileStore now works. It uses the PBECipher with DES3 afair. *************** *** 34,38 **** Revision 1.1 2003/11/18 00:01:02 pelle ! The sample signing web application for logging in and out is now working. There had been an issue in the canonicalizer when dealing with the embedded object of the SignatureRequest object. --- 37,41 ---- Revision 1.1 2003/11/18 00:01:02 pelle ! The simple signing web application for logging in and out is now working. There had been an issue in the canonicalizer when dealing with the embedded object of the SignatureRequest object. Index: JCESigner.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers/JCESigner.java,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** JCESigner.java 20 Jan 2004 17:38:58 -0000 1.17 --- JCESigner.java 22 Mar 2004 20:09:05 -0000 1.18 *************** *** 2,5 **** --- 2,8 ---- * $Id$ * $Log$ + * Revision 1.18 2004/03/22 20:09:05 pelle + * Added simple ledger for unit testing and in memory use + * * Revision 1.17 2004/01/20 17:38:58 pelle * Further updates to unit tests *************** *** 72,76 **** * * Revision 1.4 2003/11/18 00:01:02 pelle ! * The sample signing web application for logging in and out is now working. * There had been an issue in the canonicalizer when dealing with the embedded object of the SignatureRequest object. * --- 75,79 ---- * * Revision 1.4 2003/11/18 00:01:02 pelle ! * The simple signing web application for logging in and out is now working. * There had been an issue in the canonicalizer when dealing with the embedded object of the SignatureRequest object. * *************** *** 176,179 **** --- 179,183 ---- /** * Constructs a JCESigner with the agent providing the keystore passphrase. + * * @param filename * @param type *************** *** 181,192 **** * @param agent * @throws InvalidPassphraseException If the given passphrase is incorrect ! * @throws UserCancellationException If the user choses to cancel the process in the passphrase agent, this should cancel the loading process */ ! public JCESigner(final String filename, final String type, final String provider, final PassPhraseAgent agent) throws UserCancellationException,InvalidPassphraseException { this(filename, createInputStream(filename), type, provider, agent); ! this.filename=filename; } /** * Constructs a JCESigner providing a initial passphrase in the parameters. * @param filename * @param type --- 185,198 ---- * @param agent * @throws InvalidPassphraseException If the given passphrase is incorrect ! * @throws UserCancellationException If the user choses to cancel the process in the passphrase agent, this should cancel the loading process */ ! public JCESigner(final String filename, final String type, final String provider, final PassPhraseAgent agent) throws UserCancellationException, InvalidPassphraseException { this(filename, createInputStream(filename), type, provider, agent); ! this.filename = filename; } + /** * Constructs a JCESigner providing a initial passphrase in the parameters. + * * @param filename * @param type *************** *** 196,212 **** * @throws InvalidPassphraseException If the given passphrase is incorrect */ ! public JCESigner(final String filename, final String type, final String provider, final PassPhraseAgent agent,final char[] initialpassphrase) throws InvalidPassphraseException ! { ! this(filename, createInputStream(filename), type, provider, agent,initialpassphrase); ! this.filename=filename; } /** * The purpose of this method is to either return an InputStream or Null. The reason being that the Keystore accepts null * to create a new KeyStore in memory. * @param filename * @return */ ! private static InputStream createInputStream(final String filename) { if (Utility.isEmpty(filename)) return null; --- 202,219 ---- * @throws InvalidPassphraseException If the given passphrase is incorrect */ ! public JCESigner(final String filename, final String type, final String provider, final PassPhraseAgent agent, final char[] initialpassphrase) throws InvalidPassphraseException { ! this(filename, createInputStream(filename), type, provider, agent, initialpassphrase); ! this.filename = filename; } + /** * The purpose of this method is to either return an InputStream or Null. The reason being that the Keystore accepts null * to create a new KeyStore in memory. + * * @param filename * @return */ ! private static InputStream createInputStream(final String filename) { if (Utility.isEmpty(filename)) return null; *************** *** 224,227 **** --- 231,235 ---- /** * Constructs a JCESigner using the agent to provide the initial passphrase + * * @param name * @param in *************** *** 230,240 **** * @param agent * @throws InvalidPassphraseException If the given passphrase is incorrect ! * @throws UserCancellationException If the user choses to cancel the process in the passphrase agent, this should cancel the loading process */ protected JCESigner(final String name, final InputStream in, final String type, final String provider, final PassPhraseAgent agent) throws UserCancellationException, InvalidPassphraseException { this(loadKeyStore(provider, type, in, agent, name), agent); } /** * Constructs a JCESigner using the provided Initial passphrase to load the keystore * @param name * @param in --- 238,250 ---- * @param agent * @throws InvalidPassphraseException If the given passphrase is incorrect ! * @throws UserCancellationException If the user choses to cancel the process in the passphrase agent, this should cancel the loading process */ protected JCESigner(final String name, final InputStream in, final String type, final String provider, final PassPhraseAgent agent) throws UserCancellationException, InvalidPassphraseException { this(loadKeyStore(provider, type, in, agent, name), agent); } + /** * Constructs a JCESigner using the provided Initial passphrase to load the keystore + * * @param name * @param in *************** *** 245,253 **** * @throws InvalidPassphraseException If the given passphrase is incorrect */ ! protected JCESigner(final String name, final InputStream in, final String type, final String provider, final PassPhraseAgent agent, final char[] initpassphrase) throws InvalidPassphraseException { this(loadKeyStore(provider, type, in, initpassphrase), agent); } /** - * * @param provider * @param type --- 255,263 ---- * @throws InvalidPassphraseException If the given passphrase is incorrect */ ! protected JCESigner(final String name, final InputStream in, final String type, final String provider, final PassPhraseAgent agent, final char[] initpassphrase) throws InvalidPassphraseException { this(loadKeyStore(provider, type, in, initpassphrase), agent); } + /** * @param provider * @param type *************** *** 259,268 **** * @throws UserCancellationException */ ! private static KeyStore loadKeyStore(final String provider, final String type, final InputStream in, final PassPhraseAgent agent, final String name) throws InvalidPassphraseException,UserCancellationException { // System.out.println("Loading JCESigner: "+name); ! return loadKeyStore(provider,type,in,agent.getPassPhrase("Keystore password for: "+name)); } /** - * * @param provider * @param type --- 269,278 ---- * @throws UserCancellationException */ ! private static KeyStore loadKeyStore(final String provider, final String type, final InputStream in, final PassPhraseAgent agent, final String name) throws InvalidPassphraseException, UserCancellationException { // System.out.println("Loading JCESigner: "+name); ! return loadKeyStore(provider, type, in, agent.getPassPhrase("Keystore password for: " + name)); } + /** * @param provider * @param type *************** *** 298,305 **** /** * Creates a signer based on a fully loaded keystore * @param ks * @param agent */ ! public JCESigner(final KeyStore ks, final PassPhraseAgent agent) { this.agent = agent; this.ks = ks; --- 308,316 ---- /** * Creates a signer based on a fully loaded keystore + * * @param ks * @param agent */ ! public JCESigner(final KeyStore ks, final PassPhraseAgent agent) { this.agent = agent; this.ks = ks; *************** *** 327,339 **** public final byte[] sign(final String name, final byte[] data) throws NonExistingSignerException, UserCancellationException { ! return sign(name,data,false); } ! public final byte[] sign(final String name, final byte[] data,boolean incorrect) throws UserCancellationException, NonExistingSignerException { try { ! final char[] pass = getPassPhrase(name,incorrect); return CryptoTools.sign(getKey(name, pass), data); } catch (UnrecoverableKeyException e) { ! System.err.println("Incorrect Passphrase Attemt on: "+name); ! return sign(name,data,true); } catch (NoSuchAlgorithmException e) { throw new LowLevelException(e); --- 338,351 ---- public final byte[] sign(final String name, final byte[] data) throws NonExistingSignerException, UserCancellationException { ! return sign(name, data, false); } ! ! public final byte[] sign(final String name, final byte[] data, boolean incorrect) throws UserCancellationException, NonExistingSignerException { try { ! final char[] pass = getPassPhrase(name, incorrect); return CryptoTools.sign(getKey(name, pass), data); } catch (UnrecoverableKeyException e) { ! System.err.println("Incorrect Passphrase Attemt on: " + name); ! return sign(name, data, true); } catch (NoSuchAlgorithmException e) { throw new LowLevelException(e); *************** *** 346,356 **** } ! private char[] getPassPhrase(final String name,boolean incorrect) throws UserCancellationException { ! if (incorrect&&(agent instanceof InteractiveAgent)) ! ((InteractiveAgent)agent).getPassPhrase(name,true); return agent.getPassPhrase(name); } ! public final boolean canSignFor(final String name) { try { return ks.containsAlias(name); --- 358,368 ---- } ! private char[] getPassPhrase(final String name, boolean incorrect) throws UserCancellationException { ! if (incorrect && (agent instanceof InteractiveAgent)) ! ((InteractiveAgent) agent).getPassPhrase(name, true); return agent.getPassPhrase(name); } ! public final boolean canSignFor(final String name) { try { return ks.containsAlias(name); *************** *** 360,364 **** } ! public final int getKeyType(final String name) { try { if (ks.isKeyEntry(name)) { --- 372,376 ---- } ! public final int getKeyType(final String name) { try { if (ks.isKeyEntry(name)) { *************** *** 382,386 **** try { final KeyPair kp = kpg.generateKeyPair(); ! ks.setKeyEntry(alias, kp.getPrivate(), agent.getPassPhrase(alias), new Certificate[]{CryptoTools.createCertificate(alias,kp)}); return kp.getPublic(); } catch (KeyStoreException e) { --- 394,398 ---- try { final KeyPair kp = kpg.generateKeyPair(); ! ks.setKeyEntry(alias, kp.getPrivate(), agent.getPassPhrase(alias), new Certificate[]{CryptoTools.createCertificate(alias, kp)}); return kp.getPublic(); } catch (KeyStoreException e) { *************** *** 396,404 **** try { final Certificate certificate = ks.getCertificate(name); ! if (certificate==null) throw new NonExistingSignerException(name); ! PublicKey pub= certificate.getPublicKey(); ! if (pub==null) throw new NonExistingSignerException(name); return pub; --- 408,416 ---- try { final Certificate certificate = ks.getCertificate(name); ! if (certificate == null) throw new NonExistingSignerException(name); ! PublicKey pub = certificate.getPublicKey(); ! if (pub == null) throw new NonExistingSignerException(name); return pub; *************** *** 416,430 **** } } ! public synchronized final void save(String filename) throws FileNotFoundException{ if (Utility.isEmpty(filename)) throw new FileNotFoundException("no keystore filename"); try { ! File ksfile=new File(filename); ksfile.getParentFile().mkdirs(); ! ks.store(new FileOutputStream(ksfile),agent.getPassPhrase(filename)); } catch (Exception e) { throw new LowLevelException(e); } } private final KeyStore ks; private final KeyCache cache; --- 428,444 ---- } } ! ! public synchronized final void save(String filename) throws FileNotFoundException { if (Utility.isEmpty(filename)) throw new FileNotFoundException("no keystore filename"); try { ! File ksfile = new File(filename); ksfile.getParentFile().mkdirs(); ! ks.store(new FileOutputStream(ksfile), agent.getPassPhrase(filename)); } catch (Exception e) { throw new LowLevelException(e); } } + private final KeyStore ks; private final KeyCache cache; Index: TestCaseSigner.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers/TestCaseSigner.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** TestCaseSigner.java 19 Jan 2004 17:53:14 -0000 1.10 --- TestCaseSigner.java 22 Mar 2004 20:09:05 -0000 1.11 *************** *** 27,30 **** --- 27,33 ---- $Id$ $Log$ + Revision 1.11 2004/03/22 20:09:05 pelle + Added simple ledger for unit testing and in memory use + Revision 1.10 2004/01/19 17:53:14 pelle Various clean ups *************** *** 62,66 **** Revision 1.3 2003/11/18 00:01:02 pelle ! The sample signing web application for logging in and out is now working. There had been an issue in the canonicalizer when dealing with the embedded object of the SignatureRequest object. --- 65,69 ---- Revision 1.3 2003/11/18 00:01:02 pelle ! The simple signing web application for logging in and out is now working. There had been an issue in the canonicalizer when dealing with the embedded object of the SignatureRequest object. *************** *** 89,93 **** */ public TestCaseSigner() throws InvalidPassphraseException { ! this(KEYSTORE, getKeyStore(), "neuclear"); } --- 92,96 ---- */ public TestCaseSigner() throws InvalidPassphraseException { ! this(KEYSTORE, getKeyStore(), "neuclear"); } *************** *** 100,109 **** * be a SUN JKS format file and the passphrase for the keystore and all * keys must be the same. ! * * @param in InputStream * @param passphrase The passphrase to use * @throws InvalidPassphraseException */ ! public TestCaseSigner(final String name, final InputStream in, final String passphrase) throws InvalidPassphraseException { this(name, in, new AlwaysTheSamePassphraseAgent(passphrase)); } --- 103,112 ---- * be a SUN JKS format file and the passphrase for the keystore and all * keys must be the same. ! * * @param in InputStream * @param passphrase The passphrase to use * @throws InvalidPassphraseException */ ! public TestCaseSigner(final String name, final InputStream in, final String passphrase) throws InvalidPassphraseException { this(name, in, new AlwaysTheSamePassphraseAgent(passphrase)); } *************** *** 114,119 **** "jks", "SUN", agent, ! "neuclear".toCharArray() ! ); } --- 117,121 ---- "jks", "SUN", agent, ! "neuclear".toCharArray()); } |