|
From: <pe...@us...> - 2003-11-19 23:33:34
|
Update of /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers In directory sc8-pr-cvs1:/tmp/cvs-serv12633/src/java/org/neuclear/commons/crypto/signers Modified Files: JCESigner.java Signer.java SimpleSigner.java Log Message: Signers now can generatekeys via the generateKey() method. Refactored the relationship between SignedNamedObject and NamedObjectBuilder a bit. SignedNamedObject now contains the full xml which is returned with getEncoded() This means that it is now possible to further send on or process a SignedNamedObject, leaving NamedObjectBuilder for its original purposes of purely generating new Contracts. NamedObjectBuilder.sign() now returns a SignedNamedObject which is the prefered way of processing it. Updated all major interfaces that used the old model to use the new model. Index: JCESigner.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers/JCESigner.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** JCESigner.java 18 Nov 2003 15:07:18 -0000 1.5 --- JCESigner.java 19 Nov 2003 23:32:50 -0000 1.6 *************** *** 2,5 **** --- 2,14 ---- * $Id$ * $Log$ + * Revision 1.6 2003/11/19 23:32:50 pelle + * Signers now can generatekeys via the generateKey() method. + * Refactored the relationship between SignedNamedObject and NamedObjectBuilder a bit. + * SignedNamedObject now contains the full xml which is returned with getEncoded() + * This means that it is now possible to further send on or process a SignedNamedObject, leaving + * NamedObjectBuilder for its original purposes of purely generating new Contracts. + * NamedObjectBuilder.sign() now returns a SignedNamedObject which is the prefered way of processing it. + * Updated all major interfaces that used the old model to use the new model. + * * Revision 1.5 2003/11/18 15:07:18 pelle * Changes to JCE Implementation *************** *** 92,99 **** --- 101,110 ---- import org.neuclear.commons.crypto.CryptoException; import org.neuclear.commons.crypto.CryptoTools; + import org.neuclear.commons.crypto.RawCertificate; import org.neuclear.commons.crypto.passphraseagents.PassPhraseAgent; import java.io.*; import java.security.*; + import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.interfaces.DSAPublicKey; *************** *** 110,119 **** protected JCESigner(String name, InputStream in, String type, String provider, PassPhraseAgent agent) throws NeuClearException { ! this(loadKeyStore(provider, type, in, agent, name),agent); } private static KeyStore loadKeyStore(String provider, String type, InputStream in, PassPhraseAgent agent, String name) throws NeuClearException { try { ! KeyStore ki=null; if (provider == null) ki = KeyStore.getInstance(type); --- 121,130 ---- protected JCESigner(String name, InputStream in, String type, String provider, PassPhraseAgent agent) throws NeuClearException { ! this(loadKeyStore(provider, type, in, agent, name), agent); } private static KeyStore loadKeyStore(String provider, String type, InputStream in, PassPhraseAgent agent, String name) throws NeuClearException { try { ! KeyStore ki = null; if (provider == null) ki = KeyStore.getInstance(type); *************** *** 136,144 **** } ! public JCESigner(KeyStore ks,PassPhraseAgent agent) { this.agent = agent; this.ks = ks; cache = new KeyCache(ks); } private PrivateKey getKey(String name, char passphrase[]) throws InvalidPassphraseException, NonExistingSignerException, IOException { try { --- 147,163 ---- } ! public JCESigner(KeyStore ks, PassPhraseAgent agent) throws CryptoException { this.agent = agent; this.ks = ks; cache = new KeyCache(ks); + try { + kpg = KeyPairGenerator.getInstance("RSA"); + kpg.initialize(1024, SecureRandom.getInstance("SHA1PRNG")); + } catch (NoSuchAlgorithmException e) { + throw new CryptoException(e); + } + } + private PrivateKey getKey(String name, char passphrase[]) throws InvalidPassphraseException, NonExistingSignerException, IOException { try { *************** *** 204,207 **** --- 223,245 ---- } + /** + * Creates a new KeyPair, stores the PrivateKey using the given alias + * and returns the PublicKey. + * + * @param alias + * @return Generated PublicKey + * @throws org.neuclear.commons.crypto.CryptoException + * + */ + public PublicKey generateKey(String alias) throws CryptoException { + try { + KeyPair kp = kpg.generateKeyPair(); + ks.setKeyEntry(alias, kp.getPrivate(), agent.getPassPhrase(alias), new Certificate[]{new RawCertificate(kp.getPublic())}); + return kp.getPublic(); + } catch (KeyStoreException e) { + throw new CryptoException(e); + } + } + public PublicKey getPublicKey(String name) throws CryptoException { try { *************** *** 216,218 **** --- 254,257 ---- private final PassPhraseAgent agent; + private final KeyPairGenerator kpg; } Index: Signer.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers/Signer.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Signer.java 11 Nov 2003 21:17:47 -0000 1.1 --- Signer.java 19 Nov 2003 23:32:50 -0000 1.2 *************** *** 2,5 **** --- 2,14 ---- * $Id$ * $Log$ + * Revision 1.2 2003/11/19 23:32:50 pelle + * Signers now can generatekeys via the generateKey() method. + * Refactored the relationship between SignedNamedObject and NamedObjectBuilder a bit. + * SignedNamedObject now contains the full xml which is returned with getEncoded() + * This means that it is now possible to further send on or process a SignedNamedObject, leaving + * NamedObjectBuilder for its original purposes of purely generating new Contracts. + * NamedObjectBuilder.sign() now returns a SignedNamedObject which is the prefered way of processing it. + * Updated all major interfaces that used the old model to use the new model. + * * Revision 1.1 2003/11/11 21:17:47 pelle * Further vital reshuffling. *************** *** 70,73 **** --- 79,84 ---- import org.neuclear.commons.crypto.CryptoException; + import java.security.PublicKey; + /** *************** *** 91,94 **** --- 102,106 ---- * @return The signature * @throws org.neuclear.commons.crypto.CryptoException + * */ *************** *** 109,122 **** /** * Checks the key type of the given alias ! * @param name * @return KEY_NONE,KEY_RSA,KEY_DSA ! * @throws CryptoException */ public int getKeyType(String name) throws CryptoException; ! final public static int KEY_NONE=0; ! final public static int KEY_RSA=1; ! final public static int KEY_DSA=2; ! final public static int KEY_OTHER=-1; } --- 121,145 ---- /** * Checks the key type of the given alias ! * ! * @param name * @return KEY_NONE,KEY_RSA,KEY_DSA ! * @throws CryptoException */ public int getKeyType(String name) throws CryptoException; ! /** ! * Creates a new KeyPair, stores the PrivateKey using the given alias ! * and returns the PublicKey. ! * ! * @param alias ! * @return Generated PublicKey ! * @throws CryptoException ! */ ! public PublicKey generateKey(String alias) throws CryptoException; ! ! final public static int KEY_NONE = 0; ! final public static int KEY_RSA = 1; ! final public static int KEY_DSA = 2; ! final public static int KEY_OTHER = -1; } Index: SimpleSigner.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers/SimpleSigner.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** SimpleSigner.java 12 Nov 2003 18:54:42 -0000 1.2 --- SimpleSigner.java 19 Nov 2003 23:32:50 -0000 1.3 *************** *** 2,5 **** --- 2,14 ---- * $Id$ * $Log$ + * Revision 1.3 2003/11/19 23:32:50 pelle + * Signers now can generatekeys via the generateKey() method. + * Refactored the relationship between SignedNamedObject and NamedObjectBuilder a bit. + * SignedNamedObject now contains the full xml which is returned with getEncoded() + * This means that it is now possible to further send on or process a SignedNamedObject, leaving + * NamedObjectBuilder for its original purposes of purely generating new Contracts. + * NamedObjectBuilder.sign() now returns a SignedNamedObject which is the prefered way of processing it. + * Updated all major interfaces that used the old model to use the new model. + * * Revision 1.2 2003/11/12 18:54:42 pelle * Updated SimpleSignerStoreTest to use a StoredPassPhraseAgent eliminating the popup during testing. *************** *** 90,96 **** import javax.crypto.CipherOutputStream; import java.io.*; ! import java.security.GeneralSecurityException; ! import java.security.KeyFactory; ! import java.security.PrivateKey; import java.security.spec.KeySpec; import java.security.spec.PKCS8EncodedKeySpec; --- 99,103 ---- import javax.crypto.CipherOutputStream; import java.io.*; ! import java.security.*; import java.security.spec.KeySpec; import java.security.spec.PKCS8EncodedKeySpec; *************** *** 117,120 **** --- 124,134 ---- kf = KeyFactory.getInstance("RSA", "BC"); + try { + kpg = KeyPairGenerator.getInstance("RSA"); + kpg.initialize(1024, SecureRandom.getInstance("SHA1PRNG")); + } catch (NoSuchAlgorithmException e) { + throw new CryptoException(e); + } + } catch (IOException e) { throw new NeuClearException(e); *************** *** 235,238 **** --- 249,274 ---- } + /** + * Creates a new KeyPair, stores the PrivateKey using the given alias + * and returns the PublicKey. + * + * @param alias + * @return Generated PublicKey + * @throws org.neuclear.commons.crypto.CryptoException + * + */ + public PublicKey generateKey(String alias) throws CryptoException { + try { + KeyPair kp = kpg.generateKeyPair(); + addKey(alias, agent.getPassPhrase(alias), kp.getPrivate()); + return kp.getPublic(); + } catch (GeneralSecurityException e) { + throw new CryptoException(e); + } catch (IOException e) { + throw new CryptoException(e); + } + + } + private KeyFactory kf; private Map ks; *************** *** 240,242 **** --- 276,279 ---- private final File signerFile; private final PassPhraseAgent agent; + private final KeyPairGenerator kpg; } |