Update of /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers
In directory sc8-pr-cvs1:/tmp/cvs-serv5157/src/java/org/neuclear/commons/crypto/signers
Modified Files:
JCESigner.java PublicKeySource.java Signer.java
SimpleSigner.java
Log Message:
Revamped a lot of exception handling throughout the framework, it has been simplified in most places:
- For most cases the main exception to worry about now is InvalidNamedObjectException.
- Most lowerlevel exception that cant be handled meaningful are now wrapped in the LowLevelException, a
runtime exception.
- Source and Store patterns each now have their own exceptions that generalizes the various physical
exceptions that can happen in that area.
Index: JCESigner.java
===================================================================
RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers/JCESigner.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** JCESigner.java 19 Dec 2003 00:31:15 -0000 1.13
--- JCESigner.java 19 Dec 2003 18:02:53 -0000 1.14
***************
*** 2,5 ****
--- 2,13 ----
* $Id$
* $Log$
+ * Revision 1.14 2003/12/19 18:02:53 pelle
+ * Revamped a lot of exception handling throughout the framework, it has been simplified in most places:
+ * - For most cases the main exception to worry about now is InvalidNamedObjectException.
+ * - Most lowerlevel exception that cant be handled meaningful are now wrapped in the LowLevelException, a
+ * runtime exception.
+ * - Source and Store patterns each now have their own exceptions that generalizes the various physical
+ * exceptions that can happen in that area.
+ *
* Revision 1.13 2003/12/19 00:31:15 pelle
* Lots of usability changes through out all the passphrase agents and end user tools.
***************
*** 311,327 ****
}
! /**
! * Signs the data with the privatekey of the given name
! *
! * @param name Alias of private key to be used within KeyStore
! * @param data Data to be signed
! * @return The signature
! * @throws org.neuclear.commons.crypto.signers.InvalidPassphraseException
! * if the passphrase doesn't match
! */
! public final byte[] sign(final String name, final byte[] data) throws CryptoException {
return sign(name,data,false);
}
! public final byte[] sign(final String name, final byte[] data,boolean incorrect) throws UserCancellationException, NonExistingSignerException,CryptoException {
try {
final char[] pass = getPassPhrase(name,incorrect);
--- 319,326 ----
}
! 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);
***************
*** 335,338 ****
--- 334,339 ----
// Could try to reload it here but I wont for now
throw new LowLevelException(e);
+ } catch (CryptoException e) {
+ throw new LowLevelException(e);
}
}
***************
*** 344,363 ****
}
! public final boolean canSignFor(final String name) throws CryptoException {
try {
return ks.containsAlias(name);
} catch (KeyStoreException e) {
! throw new CryptoException(e);
}
}
! /**
! * Checks the key type of the given alias
! *
! * @param name
! * @return KEY_NONE,KEY_RSA,KEY_DSA
! * @throws CryptoException
! */
! public final int getKeyType(final String name) throws CryptoException {
try {
if (ks.isKeyEntry(name)) {
--- 345,357 ----
}
! public final boolean canSignFor(final String name) {
try {
return ks.containsAlias(name);
} catch (KeyStoreException e) {
! throw new LowLevelException(e);
}
}
! public final int getKeyType(final String name) {
try {
if (ks.isKeyEntry(name)) {
***************
*** 370,374 ****
}
} catch (KeyStoreException e) {
! throw new CryptoException(e);
}
return KEY_NONE; //To change body of implemented methods use Options | File Templates.
--- 364,368 ----
}
} catch (KeyStoreException e) {
! throw new LowLevelException(e);
}
return KEY_NONE; //To change body of implemented methods use Options | File Templates.
***************
*** 376,380 ****
! public final PublicKey generateKey(final String alias) throws CryptoException {
try {
final KeyPair kp = kpg.generateKeyPair();
--- 370,374 ----
! public final PublicKey generateKey(final String alias) throws UserCancellationException {
try {
final KeyPair kp = kpg.generateKeyPair();
***************
*** 382,407 ****
return kp.getPublic();
} catch (KeyStoreException e) {
! throw new CryptoException(e);
} catch (SignatureException e) {
! throw new CryptoException(e);
} catch (InvalidKeyException e) {
! throw new CryptoException(e);
}
}
! public final PublicKey getPublicKey(final String name) throws CryptoException {
try {
return ks.getCertificate(name).getPublicKey();
} catch (KeyStoreException e) {
! throw new CryptoException(e);
}
}
! public void save() throws CryptoException {
! save(filename);
}
! public synchronized final void save(String filename) throws CryptoException{
if (Utility.isEmpty(filename))
! throw new CryptoException("We dont have a filename");
try {
File ksfile=new File(filename);
--- 376,405 ----
return kp.getPublic();
} catch (KeyStoreException e) {
! throw new LowLevelException(e);
} catch (SignatureException e) {
! throw new LowLevelException(e);
} catch (InvalidKeyException e) {
! throw new LowLevelException(e);
}
}
! public final PublicKey getPublicKey(final String name) {
try {
return ks.getCertificate(name).getPublicKey();
} catch (KeyStoreException e) {
! throw new LowLevelException(e);
}
}
! public void save() {
! try {
! save(filename);
! } catch (FileNotFoundException e) {
! throw new LowLevelException(e);
! }
}
! 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);
***************
*** 409,413 ****
ks.store(new FileOutputStream(ksfile),agent.getPassPhrase(filename));
} catch (Exception e) {
! throw new CryptoException(e);
}
}
--- 407,411 ----
ks.store(new FileOutputStream(ksfile),agent.getPassPhrase(filename));
} catch (Exception e) {
! throw new LowLevelException(e);
}
}
Index: PublicKeySource.java
===================================================================
RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers/PublicKeySource.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** PublicKeySource.java 11 Nov 2003 21:17:47 -0000 1.1
--- PublicKeySource.java 19 Dec 2003 18:02:53 -0000 1.2
***************
*** 25,28 ****
--- 25,36 ----
$Id$
$Log$
+ Revision 1.2 2003/12/19 18:02:53 pelle
+ Revamped a lot of exception handling throughout the framework, it has been simplified in most places:
+ - For most cases the main exception to worry about now is InvalidNamedObjectException.
+ - Most lowerlevel exception that cant be handled meaningful are now wrapped in the LowLevelException, a
+ runtime exception.
+ - Source and Store patterns each now have their own exceptions that generalizes the various physical
+ exceptions that can happen in that area.
+
Revision 1.1 2003/11/11 21:17:47 pelle
Further vital reshuffling.
***************
*** 46,49 ****
*/
public interface PublicKeySource {
! PublicKey getPublicKey(String name) throws CryptoException;
}
--- 54,57 ----
*/
public interface PublicKeySource {
! PublicKey getPublicKey(String name) ;
}
Index: Signer.java
===================================================================
RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers/Signer.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** Signer.java 18 Dec 2003 17:40:07 -0000 1.4
--- Signer.java 19 Dec 2003 18:02:53 -0000 1.5
***************
*** 2,5 ****
--- 2,13 ----
* $Id$
* $Log$
+ * Revision 1.5 2003/12/19 18:02:53 pelle
+ * Revamped a lot of exception handling throughout the framework, it has been simplified in most places:
+ * - For most cases the main exception to worry about now is InvalidNamedObjectException.
+ * - Most lowerlevel exception that cant be handled meaningful are now wrapped in the LowLevelException, a
+ * runtime exception.
+ * - Source and Store patterns each now have their own exceptions that generalizes the various physical
+ * exceptions that can happen in that area.
+ *
* Revision 1.4 2003/12/18 17:40:07 pelle
* You can now create keys that get stored with a X509 certificate in the keystore. These can be saved as well.
***************
*** 90,93 ****
--- 98,102 ----
import org.neuclear.commons.crypto.CryptoException;
+ import org.neuclear.commons.crypto.passphraseagents.UserCancellationException;
import java.security.PublicKey;
***************
*** 114,122 ****
* @param data Data to be signed
* @return The signature
- * @throws org.neuclear.commons.crypto.CryptoException
- *
*/
! public byte[] sign(String name, byte data[]) throws CryptoException;
// public void addKey(String name, char passphrase[], PrivateKey key) throws GeneralSecurityException, IOException ;
--- 123,129 ----
* @param data Data to be signed
* @return The signature
*/
! public byte[] sign(String name, byte data[])throws UserCancellationException, NonExistingSignerException ;
// public void addKey(String name, char passphrase[], PrivateKey key) throws GeneralSecurityException, IOException ;
***************
*** 127,133 ****
* @param name
* @return true if signer is contained
- * @throws CryptoException
*/
! public boolean canSignFor(String name) throws CryptoException;
--- 134,139 ----
* @param name
* @return true if signer is contained
*/
! public boolean canSignFor(String name);
***************
*** 137,143 ****
* @param name
* @return KEY_NONE,KEY_RSA,KEY_DSA
- * @throws CryptoException
*/
! public int getKeyType(String name) throws CryptoException;
/**
--- 143,148 ----
* @param name
* @return KEY_NONE,KEY_RSA,KEY_DSA
*/
! public int getKeyType(String name) ;
/**
***************
*** 147,153 ****
* @param alias
* @return Generated PublicKey
! * @throws CryptoException
*/
! public PublicKey generateKey(String alias) throws CryptoException;
final public static int KEY_NONE = 0;
--- 152,158 ----
* @param alias
* @return Generated PublicKey
! * @throws UserCancellationException
*/
! public PublicKey generateKey(String alias) throws UserCancellationException;
final public static int KEY_NONE = 0;
***************
*** 156,160 ****
final public static int KEY_OTHER = -1;
! void save() throws CryptoException;
}
--- 161,165 ----
final public static int KEY_OTHER = -1;
! void save() throws UserCancellationException;
}
Index: SimpleSigner.java
===================================================================
RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/signers/SimpleSigner.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** SimpleSigner.java 19 Dec 2003 00:31:15 -0000 1.8
--- SimpleSigner.java 19 Dec 2003 18:02:53 -0000 1.9
***************
*** 2,5 ****
--- 2,13 ----
* $Id$
* $Log$
+ * Revision 1.9 2003/12/19 18:02:53 pelle
+ * Revamped a lot of exception handling throughout the framework, it has been simplified in most places:
+ * - For most cases the main exception to worry about now is InvalidNamedObjectException.
+ * - Most lowerlevel exception that cant be handled meaningful are now wrapped in the LowLevelException, a
+ * runtime exception.
+ * - Source and Store patterns each now have their own exceptions that generalizes the various physical
+ * exceptions that can happen in that area.
+ *
* Revision 1.8 2003/12/19 00:31:15 pelle
* Lots of usability changes through out all the passphrase agents and end user tools.
***************
*** 120,126 ****
--- 128,137 ----
import org.neuclear.commons.NeuClearException;
+ import org.neuclear.commons.LowLevelException;
import org.neuclear.commons.crypto.CryptoException;
import org.neuclear.commons.crypto.CryptoTools;
import org.neuclear.commons.crypto.passphraseagents.PassPhraseAgent;
+ import org.neuclear.commons.crypto.passphraseagents.UserCancellationException;
+ import org.neuclear.commons.crypto.passphraseagents.InteractiveAgent;
import javax.crypto.Cipher;
***************
*** 142,151 ****
public final class SimpleSigner implements Signer {
! public SimpleSigner(final String file, final PassPhraseAgent agent) throws NeuClearException, GeneralSecurityException {
this.agent = agent;
try {
signerFile = new File(file);
if (signerFile.exists()) {
! System.out.println("NEUDIST: Loading KeyStore");
final FileInputStream in = new FileInputStream(signerFile);
final ObjectInputStream s = new ObjectInputStream(in);
--- 153,162 ----
public final class SimpleSigner implements Signer {
! public SimpleSigner(final String file, final PassPhraseAgent agent) {
this.agent = agent;
try {
signerFile = new File(file);
if (signerFile.exists()) {
! System.out.println("NeuClear: Loading KeyStore");
final FileInputStream in = new FileInputStream(signerFile);
final ObjectInputStream s = new ObjectInputStream(in);
***************
*** 153,173 ****
} else
ks = new HashMap();
!
! 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);
} catch (ClassNotFoundException e) {
! throw new NeuClearException(e);
}
}
! private PrivateKey getKey(final String name, final char[] passphrase) throws CryptoException, NonExistingSignerException {
System.out.println("NEUDIST: UnSealing key " + name + " ...");
final byte[] encrypted = (byte[]) ks.get(getDigestedName(name));
--- 164,181 ----
} else
ks = new HashMap();
! kf = KeyFactory.getInstance("RSA");
! kpg = KeyPairGenerator.getInstance("RSA");
! kpg.initialize(1024, SecureRandom.getInstance("SHA1PRNG"));
} catch (IOException e) {
! throw new LowLevelException(e);
} catch (ClassNotFoundException e) {
! throw new LowLevelException(e);
! } catch (NoSuchAlgorithmException e) {
! throw new LowLevelException(e);
}
}
! private PrivateKey getKey(final String name, final char[] passphrase) throws InvalidPassphraseException, NonExistingSignerException {
System.out.println("NEUDIST: UnSealing key " + name + " ...");
final byte[] encrypted = (byte[]) ks.get(getDigestedName(name));
***************
*** 196,200 ****
throw new InvalidPassphraseException(e.getLocalizedMessage());
} catch (IOException e) {
! throw new CryptoException(e);
}
}
--- 204,208 ----
throw new InvalidPassphraseException(e.getLocalizedMessage());
} catch (IOException e) {
! throw new LowLevelException(e);
}
}
***************
*** 208,212 ****
*/
! public final void addKey(final String name, final PrivateKey key) throws GeneralSecurityException, IOException,CryptoException {
addKey(name, agent.getPassPhrase(name), key);
}
--- 216,220 ----
*/
! public final void addKey(final String name, final PrivateKey key) throws UserCancellationException {
addKey(name, agent.getPassPhrase(name), key);
}
***************
*** 220,240 ****
*/
! public final void addKey(final String name, final char[] passphrase, final PrivateKey key) throws GeneralSecurityException, IOException {
! System.out.println("NEUDIST: Sealing key: " + name + " in format " + key.getFormat());
! final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
! DataOutputStream dOut = new DataOutputStream(bOut);
! final Cipher c = CryptoTools.makePBECipher(Cipher.ENCRYPT_MODE, passphrase);
! final CipherOutputStream cOut = new CipherOutputStream(dOut, c);
! dOut = new DataOutputStream(cOut);
! dOut.writeInt(11870);//This is just a quick check to see if the passphrase worked
! final byte[] keyBytes = key.getEncoded(); //I'm assuming this is PKCS8, If not tough dooda
! dOut.writeInt(keyBytes.length);
! dOut.write(keyBytes);
! dOut.close();
! final byte[] encrypted = bOut.toByteArray();
! ks.put(getDigestedName(name), encrypted);
}
! public final boolean canSignFor(final String name) throws CryptoException {
return ks.containsKey(getDigestedName(name));
}
--- 228,254 ----
*/
! public final void addKey(final String name, final char[] passphrase, final PrivateKey key) {
! System.out.println("NeuClear: Sealing key: " + name + " in format " + key.getFormat());
! try {
! final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
! DataOutputStream dOut = new DataOutputStream(bOut);
! final Cipher c = CryptoTools.makePBECipher(Cipher.ENCRYPT_MODE, passphrase);
! final CipherOutputStream cOut = new CipherOutputStream(dOut, c);
! dOut = new DataOutputStream(cOut);
! dOut.writeInt(11870);//This is just a quick check to see if the passphrase worked
! final byte[] keyBytes = key.getEncoded(); //I'm assuming this is PKCS8, If not tough dooda
! dOut.writeInt(keyBytes.length);
! dOut.write(keyBytes);
! dOut.close();
! final byte[] encrypted = bOut.toByteArray();
! ks.put(getDigestedName(name), encrypted);
! } catch (GeneralSecurityException e) {
! throw new LowLevelException(e);
! } catch (IOException e) {
! throw new LowLevelException(e);
! }
}
! public final boolean canSignFor(final String name) {
return ks.containsKey(getDigestedName(name));
}
***************
*** 245,251 ****
* @param name
* @return KEY_NONE,KEY_RSA,KEY_DSA
- * @throws CryptoException
*/
! public final int getKeyType(final String name) throws CryptoException {
return (canSignFor(name)) ? KEY_RSA : KEY_NONE; // We always use RSA here
}
--- 259,264 ----
* @param name
* @return KEY_NONE,KEY_RSA,KEY_DSA
*/
! public final int getKeyType(final String name) {
return (canSignFor(name)) ? KEY_RSA : KEY_NONE; // We always use RSA here
}
***************
*** 255,259 ****
}
! public final void save() throws CryptoException {
if (signerFile.getParent() != null)
signerFile.getParentFile().mkdirs();
--- 268,272 ----
}
! public final void save() {
if (signerFile.getParent() != null)
signerFile.getParentFile().mkdirs();
***************
*** 265,269 ****
s.flush();
} catch (IOException e) {
! throw new CryptoException(e);
}
--- 278,282 ----
s.flush();
} catch (IOException e) {
! throw new LowLevelException(e);
}
***************
*** 276,297 ****
* @param data Data to be signed
* @return The signature
! * @throws InvalidPassphraseException if the passphrase doesn't match
*/
!
! public final byte[] sign(final String name, final byte[] data) throws CryptoException {
!
! return CryptoTools.sign(getKey(name, agent.getPassPhrase(name)), data);
}
! public final PublicKey generateKey(final String alias) throws CryptoException {
try {
! final 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);
}
!
}
--- 289,308 ----
* @param data Data to be signed
* @return The signature
! * @throws UserCancellationException
*/
! public final byte[] sign(final String name, final byte[] data) throws UserCancellationException {
! return sign(name,data,false);
}
! private final byte[] sign(final String name, final byte[] data,boolean incorrect) throws UserCancellationException {
try {
! return CryptoTools.sign(getKey(name, agent.getPassPhrase(name,incorrect)), data);
! } catch (CryptoException e) {
! return sign(name,data,true);
}
! }
! public final PublicKey generateKey(final String alias) throws UserCancellationException {
! final KeyPair kp = kpg.generateKeyPair();
! addKey(alias, agent.getPassPhrase(alias), kp.getPrivate());
! return kp.getPublic();
}
|