diff options
Diffstat (limited to 'libjava/classpath/gnu/javax/crypto/keyring')
24 files changed, 821 insertions, 1148 deletions
diff --git a/libjava/classpath/gnu/javax/crypto/keyring/AuthenticatedEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/AuthenticatedEntry.java index 22b42b3ea0b..cd2e5868db9 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/AuthenticatedEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/AuthenticatedEntry.java @@ -38,46 +38,32 @@ exception statement from your version. */ package gnu.javax.crypto.keyring; +import gnu.java.security.Registry; +import gnu.javax.crypto.mac.IMac; +import gnu.javax.crypto.mac.MacFactory; +import gnu.javax.crypto.mac.MacOutputStream; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; - import java.security.InvalidKeyException; - import java.util.Arrays; -import java.util.Date; -import java.util.Iterator; import java.util.HashMap; -import java.util.List; - -import gnu.java.security.Registry; -import gnu.javax.crypto.mac.IMac; -import gnu.javax.crypto.mac.MacFactory; -import gnu.javax.crypto.mac.MacInputStream; -import gnu.javax.crypto.mac.MacOutputStream; +import java.util.Iterator; -public final class AuthenticatedEntry extends MaskableEnvelopeEntry implements - Registry +public final class AuthenticatedEntry + extends MaskableEnvelopeEntry + implements Registry { - - // Constants and fields. - // ------------------------------------------------------------------------ - public static final int TYPE = 2; - // Constructor. - // ------------------------------------------------------------------------ - public AuthenticatedEntry(String mac, int macLen, Properties properties) { super(TYPE, properties); - if (macLen <= 0) - { - throw new IllegalArgumentException("invalid mac length"); - } + throw new IllegalArgumentException("invalid mac length"); this.properties.put("mac", mac); this.properties.put("maclen", String.valueOf(macLen)); setMasked(false); @@ -89,32 +75,22 @@ public final class AuthenticatedEntry extends MaskableEnvelopeEntry implements setMasked(true); } - // Class methods. - // ------------------------------------------------------------------------ - public static AuthenticatedEntry decode(DataInputStream in) throws IOException { AuthenticatedEntry entry = new AuthenticatedEntry(); entry.properties.decode(in); - if (!entry.properties.containsKey("mac")) - { - throw new MalformedKeyringException("no mac specified"); - } - if (!entry.properties.containsKey("maclen")) - { - throw new MalformedKeyringException("no mac length specified"); - } + if (! entry.properties.containsKey("mac")) + throw new MalformedKeyringException("no mac specified"); + if (! entry.properties.containsKey("maclen")) + throw new MalformedKeyringException("no mac length specified"); return entry; } - // Instance methods. - // ------------------------------------------------------------------------ - /** * Computes the mac over this envelope's data. This method <b>must</b> be * called before this entry in encoded. - * + * * @param key The key to authenticate with. * @throws IOException If encoding fails. * @throws InvalidKeyException If the supplied key is bad. @@ -122,11 +98,8 @@ public final class AuthenticatedEntry extends MaskableEnvelopeEntry implements public void authenticate(byte[] key) throws IOException, InvalidKeyException { if (isMasked()) - { - throw new IllegalStateException("entry is masked"); - } + throw new IllegalStateException("entry is masked"); IMac m = getMac(key); - ByteArrayOutputStream bout = new ByteArrayOutputStream(1024); MacOutputStream macout = new MacOutputStream(bout, m); DataOutputStream out2 = new DataOutputStream(macout); @@ -140,36 +113,28 @@ public final class AuthenticatedEntry extends MaskableEnvelopeEntry implements } /** - * Verifies this entry's payload. This method will unmask this entry, - * thus it must be called before accessing its contents. - * + * Verifies this entry's payload. This method will unmask this entry, thus it + * must be called before accessing its contents. + * * @param key The key to use to authenticate. * @throws InvalidKeyException If the given key is improper. */ public void verify(byte[] key) throws InvalidKeyException { - if (!isMasked() || payload == null) - { - return; - } + if (! isMasked() || payload == null) + return; IMac m = getMac(key); - m.update(payload, 0, payload.length - m.macSize()); byte[] macValue = new byte[m.macSize()]; System.arraycopy(payload, payload.length - macValue.length, macValue, 0, macValue.length); - if (!Arrays.equals(macValue, m.digest())) - { - throw new IllegalArgumentException("MAC verification failed"); - } + if (! Arrays.equals(macValue, m.digest())) + throw new IllegalArgumentException("MAC verification failed"); try { - DataInputStream in = new DataInputStream( - new ByteArrayInputStream( - payload, - 0, - payload.length - - m.macSize())); + int len = payload.length - m.macSize(); + ByteArrayInputStream bais = new ByteArrayInputStream(payload, 0, len); + DataInputStream in = new DataInputStream(bais); decodeEnvelope(in); } catch (IOException ioe) @@ -183,27 +148,17 @@ public final class AuthenticatedEntry extends MaskableEnvelopeEntry implements protected void encodePayload() throws IOException { if (payload == null) - { - throw new IllegalStateException("not authenticated"); - } + throw new IllegalStateException("not authenticated"); } - // Own methods. - // ------------------------------------------------------------------------ - private IMac getMac(byte[] key) throws InvalidKeyException { IMac mac = MacFactory.getInstance(properties.get("mac")); if (mac == null) - { - throw new IllegalArgumentException("no such mac: " - + properties.get("mac")); - } + throw new IllegalArgumentException("no such mac: " + properties.get("mac")); int maclen = 0; - if (!properties.containsKey("maclen")) - { - throw new IllegalArgumentException("no MAC length"); - } + if (! properties.containsKey("maclen")) + throw new IllegalArgumentException("no MAC length"); try { maclen = Integer.parseInt(properties.get("maclen")); @@ -212,10 +167,9 @@ public final class AuthenticatedEntry extends MaskableEnvelopeEntry implements { throw new IllegalArgumentException("bad MAC length"); } - HashMap macAttr = new HashMap(); macAttr.put(IMac.MAC_KEY_MATERIAL, key); - macAttr.put(IMac.TRUNCATED_SIZE, new Integer(maclen)); + macAttr.put(IMac.TRUNCATED_SIZE, Integer.valueOf(maclen)); mac.init(macAttr); return mac; } diff --git a/libjava/classpath/gnu/javax/crypto/keyring/BaseKeyring.java b/libjava/classpath/gnu/javax/crypto/keyring/BaseKeyring.java index 5fe7dbf4deb..369507d4c73 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/BaseKeyring.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/BaseKeyring.java @@ -38,86 +38,58 @@ exception statement from your version. */ package gnu.javax.crypto.keyring; -import java.io.InputStream; +import gnu.java.security.Registry; + import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; - import java.util.Enumeration; import java.util.List; import java.util.Map; import java.util.StringTokenizer; -import gnu.java.security.Registry; - -public abstract class BaseKeyring implements IKeyring +public abstract class BaseKeyring + implements IKeyring { - - // Fields. - // ------------------------------------------------------------------------ - - /** - * The top-level keyring data. - */ + /** The top-level keyring data. */ protected PasswordAuthenticatedEntry keyring; - protected CompressedEntry keyring2; - // Constructors. - // ------------------------------------------------------------------------ - public BaseKeyring() { } - // Instance methods. - // ------------------------------------------------------------------------ - public void load(Map attributes) throws IOException { InputStream in = (InputStream) attributes.get(KEYRING_DATA_IN); if (in == null) - { - throw new IllegalArgumentException("no input stream"); - } + throw new IllegalArgumentException("no input stream"); char[] password = (char[]) attributes.get(KEYRING_PASSWORD); if (password == null) - { - password = new char[0]; - } + password = new char[0]; if (in.read() != Registry.GKR_MAGIC[0] || in.read() != Registry.GKR_MAGIC[1] || in.read() != Registry.GKR_MAGIC[2] || in.read() != Registry.GKR_MAGIC[3]) - { - throw new MalformedKeyringException("magic"); - } + throw new MalformedKeyringException("magic"); load(in, password); - List l = keyring.getEntries(); if (l.size() == 1 && (l.get(0) instanceof CompressedEntry)) - { - keyring2 = (CompressedEntry) l.get(0); - } + keyring2 = (CompressedEntry) l.get(0); } public void store(Map attributes) throws IOException { OutputStream out = (OutputStream) attributes.get(KEYRING_DATA_OUT); if (out == null) - { - throw new IllegalArgumentException("no output stream"); - } + throw new IllegalArgumentException("no output stream"); char[] password = (char[]) attributes.get(KEYRING_PASSWORD); if (password == null) - { - password = new char[0]; - } + password = new char[0]; if (keyring == null) - { - throw new IllegalStateException("empty keyring"); - } + throw new IllegalStateException("empty keyring"); out.write(Registry.GKR_MAGIC); store(out, password); @@ -131,45 +103,35 @@ public abstract class BaseKeyring implements IKeyring public int size() { if (keyring == null) - { - throw new IllegalStateException ("keyring not loaded"); - } + throw new IllegalStateException("keyring not loaded"); return ((StringTokenizer) aliases()).countTokens(); } public Enumeration aliases() { if (keyring == null) - { - throw new IllegalStateException ("keyring not loaded"); - } + throw new IllegalStateException("keyring not loaded"); return new StringTokenizer(keyring.getAliasList(), ";"); } public boolean containsAlias(String alias) { if (keyring == null) - { - throw new IllegalStateException("keyring not loaded"); - } + throw new IllegalStateException("keyring not loaded"); return keyring.containsAlias(alias); } public List get(String alias) { if (keyring == null) - { - throw new IllegalStateException("keyring not loaded"); - } + throw new IllegalStateException("keyring not loaded"); return keyring.get(alias); } public void add(Entry entry) { if (keyring == null) - { - throw new IllegalStateException("keyring not loaded"); - } + throw new IllegalStateException("keyring not loaded"); if (keyring2 != null) keyring2.add(entry); else @@ -179,9 +141,7 @@ public abstract class BaseKeyring implements IKeyring public void remove(String alias) { if (keyring == null) - { - throw new IllegalStateException("keyring not loaded"); - } + throw new IllegalStateException("keyring not loaded"); keyring.remove(alias); } diff --git a/libjava/classpath/gnu/javax/crypto/keyring/BinaryDataEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/BinaryDataEntry.java index 2dcd5454fb6..e694487e141 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/BinaryDataEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/BinaryDataEntry.java @@ -40,29 +40,22 @@ package gnu.javax.crypto.keyring; import java.io.DataInputStream; import java.io.IOException; - import java.util.Date; /** - * A binary data entry is a primitive entry that simply contains some amount - * of arbitrary binary data and an optional content type. + * A binary data entry is a primitive entry that simply contains some amount of + * arbitrary binary data and an optional content type. */ -public class BinaryDataEntry extends PrimitiveEntry +public class BinaryDataEntry + extends PrimitiveEntry { - - // Fields. - // ------------------------------------------------------------------------ - public static final int TYPE = 9; - // Constructors. - // ------------------------------------------------------------------------ - /** * Creates a new binary data entry. - * - * @param contentType The content type of this entry. This parameter can - * be <code>null</code> if no content type is needed. + * + * @param contentType The content type of this entry. This parameter can be + * <code>null</code> if no content type is needed. * @param data The data. * @param creationDate The creation date. * @param properties This entry's properties. @@ -72,14 +65,10 @@ public class BinaryDataEntry extends PrimitiveEntry { super(TYPE, creationDate, properties); if (data == null) - { - throw new IllegalArgumentException("no data"); - } + throw new IllegalArgumentException("no data"); payload = (byte[]) data.clone(); if (contentType != null) - { - this.properties.put("content-type", contentType); - } + this.properties.put("content-type", contentType); } private BinaryDataEntry() @@ -87,9 +76,6 @@ public class BinaryDataEntry extends PrimitiveEntry super(TYPE); } - // Class methods. - // ------------------------------------------------------------------------ - public static BinaryDataEntry decode(DataInputStream in) throws IOException { BinaryDataEntry entry = new BinaryDataEntry(); @@ -97,13 +83,10 @@ public class BinaryDataEntry extends PrimitiveEntry return entry; } - // Instance methods. - // ------------------------------------------------------------------------ - /** * Returns the content type of this entry, or <code>null</code> if this * property is not set. - * + * * @return The content type. */ public String getContentType() @@ -113,7 +96,7 @@ public class BinaryDataEntry extends PrimitiveEntry /** * Returns this object's data field. - * + * * @return The data. */ public byte[] getData() diff --git a/libjava/classpath/gnu/javax/crypto/keyring/CertPathEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/CertPathEntry.java index ef62347ec9d..32b86084f31 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/CertPathEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/CertPathEntry.java @@ -40,40 +40,28 @@ package gnu.javax.crypto.keyring; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; -import java.io.DataOutputStream; import java.io.IOException; - import java.security.cert.Certificate; import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; - import java.util.Date; /** * A primitive entry that contains a path of X.509 certificates. */ -public final class CertPathEntry extends PrimitiveEntry +public final class CertPathEntry + extends PrimitiveEntry { - - // Constants and fields. - // ------------------------------------------------------------------------ - public static final int TYPE = 8; - private Certificate[] path; - // Constructor. - // ------------------------------------------------------------------------ - public CertPathEntry(Certificate[] path, Date creationDate, Properties properties) { super(TYPE, creationDate, properties); if (path == null || path.length == 0) - { - throw new IllegalArgumentException("no certificate path"); - } + throw new IllegalArgumentException("no certificate path"); this.path = (Certificate[]) path.clone(); } @@ -82,9 +70,6 @@ public final class CertPathEntry extends PrimitiveEntry super(TYPE); } - // Class method. - // ------------------------------------------------------------------------ - public static CertPathEntry decode(DataInputStream in) throws IOException { CertPathEntry entry = new CertPathEntry(); @@ -95,8 +80,7 @@ public final class CertPathEntry extends PrimitiveEntry try { CertificateFactory fact = CertificateFactory.getInstance("X.509"); - entry.path = (Certificate[]) fact.generateCertificates(in2).toArray( - new Certificate[0]); + entry.path = (Certificate[]) fact.generateCertificates(in2).toArray(new Certificate[0]); } catch (CertificateException ce) { @@ -105,9 +89,6 @@ public final class CertPathEntry extends PrimitiveEntry return entry; } - // Instance methods. - // ------------------------------------------------------------------------ - public Certificate[] getCertPath() { return path; @@ -120,9 +101,7 @@ public final class CertPathEntry extends PrimitiveEntry try { for (int i = 0; i < path.length; i++) - { - bout.write(path[i].getEncoded()); - } + bout.write(path[i].getEncoded()); } catch (CertificateEncodingException cee) { diff --git a/libjava/classpath/gnu/javax/crypto/keyring/CertificateEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/CertificateEntry.java index 95a708ac53f..661758442cc 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/CertificateEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/CertificateEntry.java @@ -39,50 +39,38 @@ exception statement from your version. */ package gnu.javax.crypto.keyring; import java.io.DataInputStream; -import java.io.DataOutputStream; import java.io.IOException; import java.security.cert.Certificate; import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; -import java.security.cert.X509Certificate; import java.util.Date; /** - * <p>An immutable class representing a trusted certificate entry.</p> + * An immutable class representing a trusted certificate entry. */ -public final class CertificateEntry extends PrimitiveEntry +public final class CertificateEntry + extends PrimitiveEntry { - - // Constants and variables - // ------------------------------------------------------------------------- - public static final int TYPE = 5; - /** The certificate. */ private Certificate certificate; - // Constructor(s) - // ------------------------------------------------------------------------- - /** * Creates a new certificate entry. - * + * * @param certificate The certificate. * @param creationDate The creation date. * @param properties The alias. * @throws IllegalArgumentException If any argument is null, or if the alias - * is empty. + * is empty. */ public CertificateEntry(Certificate certificate, Date creationDate, Properties properties) { super(TYPE, creationDate, properties); - if (certificate == null) - { - throw new IllegalArgumentException("no certificate"); - } + throw new IllegalArgumentException("no certificate"); this.certificate = certificate; this.properties.put("type", certificate.getType()); } @@ -92,9 +80,6 @@ public final class CertificateEntry extends PrimitiveEntry super(TYPE); } - // Class methods - // ------------------------------------------------------------------------- - public static CertificateEntry decode(DataInputStream in) throws IOException { CertificateEntry entry = new CertificateEntry(); @@ -102,9 +87,7 @@ public final class CertificateEntry extends PrimitiveEntry entry.makeCreationDate(); String type = entry.properties.get("type"); if (type == null) - { - throw new MalformedKeyringException("no certificate type"); - } + throw new MalformedKeyringException("no certificate type"); int len = in.readInt(); MeteredInputStream in2 = new MeteredInputStream(in, len); try @@ -116,19 +99,14 @@ public final class CertificateEntry extends PrimitiveEntry { throw new MalformedKeyringException(ce.toString()); } - if (!in2.limitReached()) - { - throw new MalformedKeyringException("extra data at end of payload"); - } + if (! in2.limitReached()) + throw new MalformedKeyringException("extra data at end of payload"); return entry; } - // Instance methods - // ------------------------------------------------------------------------- - /** * Returns this entry's certificate. - * + * * @return The certificate. */ public Certificate getCertificate() diff --git a/libjava/classpath/gnu/javax/crypto/keyring/CompressedEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/CompressedEntry.java index cce930d739d..b24c6715ab7 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/CompressedEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/CompressedEntry.java @@ -42,22 +42,15 @@ import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; - import java.util.Iterator; import java.util.zip.DeflaterOutputStream; import java.util.zip.InflaterInputStream; -public class CompressedEntry extends EnvelopeEntry +public class CompressedEntry + extends EnvelopeEntry { - - // Constants and fields. - // ------------------------------------------------------------------------ - public static final int TYPE = 4; - // Constructor. - // ------------------------------------------------------------------------ - public CompressedEntry(Properties properties) { super(TYPE, properties); @@ -69,24 +62,16 @@ public class CompressedEntry extends EnvelopeEntry this(new Properties()); } - // Class methods. - // ------------------------------------------------------------------------ - public static CompressedEntry decode(DataInputStream in) throws IOException { CompressedEntry entry = new CompressedEntry(); entry.properties.decode(in); String alg = entry.properties.get("algorithm"); if (alg == null) - { - throw new MalformedKeyringException("no compression algorithm"); - } - if (!alg.equalsIgnoreCase("DEFLATE")) - { - throw new MalformedKeyringException( - "unsupported compression algorithm: " - + alg); - } + throw new MalformedKeyringException("no compression algorithm"); + if (! alg.equalsIgnoreCase("DEFLATE")) + throw new MalformedKeyringException("unsupported compression algorithm: " + + alg); int len = in.readInt(); MeteredInputStream min = new MeteredInputStream(in, len); InflaterInputStream infin = new InflaterInputStream(min); @@ -95,18 +80,13 @@ public class CompressedEntry extends EnvelopeEntry return entry; } - // Instance methods. - // ------------------------------------------------------------------------ - protected void encodePayload() throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(1024); DeflaterOutputStream dout = new DeflaterOutputStream(buf); DataOutputStream out2 = new DataOutputStream(dout); for (Iterator it = entries.iterator(); it.hasNext();) - { - ((Entry) it.next()).encode(out2); - } + ((Entry) it.next()).encode(out2); dout.finish(); payload = buf.toByteArray(); } diff --git a/libjava/classpath/gnu/javax/crypto/keyring/EncryptedEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/EncryptedEntry.java index fad5f54b236..f0693cc91d2 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/EncryptedEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/EncryptedEntry.java @@ -38,21 +38,6 @@ exception statement from your version. */ package gnu.javax.crypto.keyring; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import java.security.InvalidKeyException; - -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.Iterator; -import java.util.HashMap; -import java.util.List; - import gnu.java.security.Registry; import gnu.javax.crypto.cipher.CipherFactory; import gnu.javax.crypto.cipher.IBlockCipher; @@ -62,25 +47,24 @@ import gnu.javax.crypto.pad.IPad; import gnu.javax.crypto.pad.PadFactory; import gnu.javax.crypto.pad.WrongPaddingException; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.util.HashMap; +import java.util.Iterator; + public class EncryptedEntry extends MaskableEnvelopeEntry implements Registry { - - // Constants and fields. - // ------------------------------------------------------------------------ - public static final int TYPE = 0; - // Constructor. - // ------------------------------------------------------------------------ - public EncryptedEntry(String cipher, String mode, Properties properties) { super(TYPE, properties); if (cipher == null || mode == null) - { - throw new IllegalArgumentException( - "neither cipher nor mode can be null"); - } + throw new IllegalArgumentException("neither cipher nor mode can be null"); properties.put("cipher", cipher); properties.put("mode", mode); setMasked(false); @@ -92,34 +76,22 @@ public class EncryptedEntry extends MaskableEnvelopeEntry implements Registry setMasked(true); } - // Class methods. - // ------------------------------------------------------------------------ - public static EncryptedEntry decode(DataInputStream in) throws IOException { EncryptedEntry entry = new EncryptedEntry(); entry.defaultDecode(in); - if (!entry.properties.containsKey("cipher")) - { - throw new MalformedKeyringException("no cipher"); - } - if (!entry.properties.containsKey("cipher")) - { - throw new MalformedKeyringException("no cipher"); - } + if (! entry.properties.containsKey("cipher")) + throw new MalformedKeyringException("no cipher"); + if (! entry.properties.containsKey("cipher")) + throw new MalformedKeyringException("no cipher"); return entry; } - // Instance methods. - // ------------------------------------------------------------------------ - public void decrypt(byte[] key, byte[] iv) throws IllegalArgumentException, WrongPaddingException { - if (!isMasked() || payload == null) - { - return; - } + if (! isMasked() || payload == null) + return; IMode mode = getMode(key, iv, IMode.DECRYPTION); IPad padding = null; padding = PadFactory.getInstance("PKCS7"); @@ -132,12 +104,8 @@ public class EncryptedEntry extends MaskableEnvelopeEntry implements Registry count += mode.currentBlockSize(); } int padlen = padding.unpad(buf, 0, buf.length); - DataInputStream in = new DataInputStream( - new ByteArrayInputStream( - buf, - 0, - buf.length - - padlen)); + int len = buf.length - padlen; + DataInputStream in = new DataInputStream(new ByteArrayInputStream(buf, 0, len)); try { decodeEnvelope(in); @@ -181,22 +149,14 @@ public class EncryptedEntry extends MaskableEnvelopeEntry implements Registry public void encodePayload() throws IOException { if (payload == null) - { - throw new IOException("not encrypted"); - } + throw new IOException("not encrypted"); } - // Own methods. - // ------------------------------------------------------------------------ - private IMode getMode(byte[] key, byte[] iv, int state) { IBlockCipher cipher = CipherFactory.getInstance(properties.get("cipher")); if (cipher == null) - { - throw new IllegalArgumentException("no such cipher: " - + properties.get("cipher")); - } + throw new IllegalArgumentException("no such cipher: " + properties.get("cipher")); int blockSize = cipher.defaultBlockSize(); if (properties.containsKey("block-size")) { @@ -210,17 +170,13 @@ public class EncryptedEntry extends MaskableEnvelopeEntry implements Registry + nfe.getMessage()); } } - IMode mode = ModeFactory.getInstance(properties.get("mode"), cipher, - blockSize); + IMode mode = ModeFactory.getInstance(properties.get("mode"), cipher, blockSize); if (mode == null) - { - throw new IllegalArgumentException("no such mode: " - + properties.get("mode")); - } + throw new IllegalArgumentException("no such mode: " + properties.get("mode")); HashMap modeAttr = new HashMap(); modeAttr.put(IMode.KEY_MATERIAL, key); - modeAttr.put(IMode.STATE, new Integer(state)); + modeAttr.put(IMode.STATE, Integer.valueOf(state)); modeAttr.put(IMode.IV, iv); try { diff --git a/libjava/classpath/gnu/javax/crypto/keyring/Entry.java b/libjava/classpath/gnu/javax/crypto/keyring/Entry.java index fa7f496798b..ef225db95e6 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/Entry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/Entry.java @@ -38,49 +38,51 @@ exception statement from your version. */ package gnu.javax.crypto.keyring; +import gnu.java.security.Configuration; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.util.logging.Logger; /** * An immutable class representing a single entry in a keyring. */ public abstract class Entry { - - // Fields. - // ------------------------------------------------------------------------ - + private static final Logger log = Logger.getLogger(Entry.class.getName()); + private static final String[] TYPES = new String[] { + "Encrypted", + "PasswordEncrypted", + "Authenticated", + "PasswordAuthenticated", + "Compressed", + "Certificate", + "PublicKey", + "PrivateKey", + "CertPath", + "BinaryData" }; /** This entry's type identifier. */ protected int type; - /** This entry's property set. */ protected Properties properties; - /** This entry's payload. */ protected byte[] payload; - // Constructor. - // ------------------------------------------------------------------------ - /** * Creates a new Entry. - * + * * @param type This entry's type. * @param properties This entry's properties. - * @throws IllegalArgumentException If the properties argument is null, - * or if the type is out of range. + * @throws IllegalArgumentException If the properties argument is null, or if + * the type is out of range. */ protected Entry(int type, Properties properties) { if (type < 0 || type > 255) - { - throw new IllegalArgumentException("invalid packet type"); - } + throw new IllegalArgumentException("invalid packet type"); if (properties == null) - { - throw new IllegalArgumentException("no properties"); - } + throw new IllegalArgumentException("no properties"); this.type = type; this.properties = (Properties) properties.clone(); } @@ -91,20 +93,15 @@ public abstract class Entry protected Entry(final int type) { if (type < 0 || type > 255) - { - throw new IllegalArgumentException("invalid packet type"); - } + throw new IllegalArgumentException("invalid packet type"); this.type = type; properties = new Properties(); } - // Instance methods. - // ------------------------------------------------------------------------ - /** * Returns this entry's properties object. The properties are cloned before * being returned. - * + * * @return The properties. */ public Properties getProperties() @@ -123,28 +120,35 @@ public abstract class Entry } /** - * This method is called when this entry needs to be written to an - * output stream. - * + * This method is called when this entry needs to be written to an output + * stream. + * * @param out The stream to write to. * @throws IOException If an I/O exception occurs. */ public void encode(DataOutputStream out) throws IOException { if (payload == null) - { - encodePayload(); - } + encodePayload(); if (out == null) - { - return; - } + return; out.write(type); properties.encode(out); out.writeInt(payload.length); out.write(payload); } + public String toString() + { + return new StringBuilder("Entry{") + .append("type=").append(TYPES[type]) + .append(", properties=").append(properties) + .append(", payload=") + .append(payload == null ? "-" : "byte[" + payload.length + "]") + .append( "}") + .toString(); + } + /** * Generic decoding method, which simply decodes the properties field * and reads the payload field. @@ -158,16 +162,13 @@ public abstract class Entry properties.decode(in); int len = in.readInt(); if (len < 0) - { - throw new IOException("corrupt length"); - } + throw new IOException("corrupt length"); + if (Configuration.DEBUG) + log.fine("About to instantiate new payload byte array for " + this); payload = new byte[len]; in.readFully(payload); } - // Abstract methods. - // ------------------------------------------------------------------------ - /** * This method is called of subclasses when the payload data needs to be * created. diff --git a/libjava/classpath/gnu/javax/crypto/keyring/EnvelopeEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/EnvelopeEntry.java index 25b1dc2a04d..fe39091b05c 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/EnvelopeEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/EnvelopeEntry.java @@ -38,45 +38,38 @@ exception statement from your version. */ package gnu.javax.crypto.keyring; +import gnu.java.security.Configuration; + import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; - import java.util.ArrayList; -import java.util.Date; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.StringTokenizer; +import java.util.logging.Logger; /** - * An envelope entry is a generic container for some number of primitive - * and other envelope entries. + * An envelope entry is a generic container for some number of primitive and + * other envelope entries. */ -public abstract class EnvelopeEntry extends Entry +public abstract class EnvelopeEntry + extends Entry { - - // Fields. - // ------------------------------------------------------------------------ - + private static final Logger log = Logger.getLogger(EnvelopeEntry.class.getName()); /** The envelope that contains this one (if any). */ protected EnvelopeEntry containingEnvelope; - /** The contained entries. */ protected List entries; - // Constructor. - // ------------------------------------------------------------------------ - public EnvelopeEntry(int type, Properties properties) { super(type, properties); entries = new LinkedList(); if (this.properties.get("alias-list") != null) - { - this.properties.remove("alias-list"); - } + this.properties.remove("alias-list"); } protected EnvelopeEntry(int type) @@ -85,84 +78,85 @@ public abstract class EnvelopeEntry extends Entry entries = new LinkedList(); } - // Instance methods. - // ------------------------------------------------------------------------ - /** * Adds an entry to this envelope. - * + * * @param entry The entry to add. */ public void add(Entry entry) { - if (!containsEntry(entry)) + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "add", entry); + if (! containsEntry(entry)) { if (entry instanceof EnvelopeEntry) - { - ((EnvelopeEntry) entry).setContainingEnvelope(this); - } + ((EnvelopeEntry) entry).setContainingEnvelope(this); entries.add(entry); - payload = null; + if (Configuration.DEBUG) + log.fine("Payload is " + (payload == null ? "" : "not ") + "null"); makeAliasList(); } + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "add"); } /** - * Tests if this envelope contains a primitive entry with the - * given alias. - * + * Tests if this envelope contains a primitive entry with the given alias. + * * @param alias The alias to test. - * @return True if this envelope (or one of the contained envelopes) - * contains a primitive entry with the given alias. + * @return True if this envelope (or one of the contained envelopes) contains + * a primitive entry with the given alias. */ public boolean containsAlias(String alias) { + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "containsAlias", alias); String aliases = getAliasList(); - if (aliases == null) + if (Configuration.DEBUG) + log.fine("aliases = [" + aliases + "]"); + boolean result = false; + if (aliases != null) { - return false; + StringTokenizer tok = new StringTokenizer(aliases, ";"); + while (tok.hasMoreTokens()) + if (tok.nextToken().equals(alias)) + { + result = true; + break; + } } - StringTokenizer tok = new StringTokenizer(aliases, ";"); - while (tok.hasMoreTokens()) - { - if (tok.nextToken().equals(alias)) - { - return true; - } - } - return false; + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "containsAlias", + Boolean.valueOf(result)); + return result; } /** * Tests if this envelope contains the given entry. - * + * * @param entry The entry to test. * @return True if this envelope contains the given entry. */ public boolean containsEntry(Entry entry) { if (entry instanceof EnvelopeEntry) - { - return entries.contains(entry); - } - else if (entry instanceof PrimitiveEntry) - { - for (Iterator it = entries.iterator(); it.hasNext();) - { - Entry e = (Entry) it.next(); - if (e.equals(entry)) - return true; - if ((e instanceof EnvelopeEntry) - && ((EnvelopeEntry) e).containsEntry(entry)) - return true; - } - } + return entries.contains(entry); + if (entry instanceof PrimitiveEntry) + for (Iterator it = entries.iterator(); it.hasNext();) + { + Entry e = (Entry) it.next(); + if (e.equals(entry)) + return true; + if ((e instanceof EnvelopeEntry) + && ((EnvelopeEntry) e).containsEntry(entry)) + return true; + } return false; } /** * Returns a copy of all entries this envelope contains. - * + * * @return All contained entries. */ public List getEntries() @@ -171,73 +165,77 @@ public abstract class EnvelopeEntry extends Entry } /** - * Gets all primitive entries that have the given alias. If there - * are any masked entries that contain the given alias, they will - * be returned as well. - * + * Gets all primitive entries that have the given alias. If there are any + * masked entries that contain the given alias, they will be returned as well. + * * @param alias The alias of the entries to get. * @return A list of all primitive entries that have the given alias. */ public List get(String alias) { + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "get", alias); List result = new LinkedList(); for (Iterator it = entries.iterator(); it.hasNext();) { Entry e = (Entry) it.next(); if (e instanceof EnvelopeEntry) { - if (!((EnvelopeEntry) e).containsAlias(alias)) - { - continue; - } - if (e instanceof MaskableEnvelopeEntry) + EnvelopeEntry ee = (EnvelopeEntry) e; + if (! ee.containsAlias(alias)) + continue; + if (ee instanceof MaskableEnvelopeEntry) { - if (((MaskableEnvelopeEntry) e).isMasked()) + MaskableEnvelopeEntry mee = (MaskableEnvelopeEntry) ee; + if (mee.isMasked()) { - result.add(e); + if (Configuration.DEBUG) + log.fine("Processing masked entry: " + mee); + result.add(mee); continue; } } - result.addAll(((EnvelopeEntry) e).get(alias)); + if (Configuration.DEBUG) + log.fine("Processing unmasked entry: " + ee); + result.addAll(ee.get(alias)); } else if (e instanceof PrimitiveEntry) { - if (((PrimitiveEntry) e).getAlias().equals(alias)) - { - result.add(e); - } + PrimitiveEntry pe = (PrimitiveEntry) e; + if (pe.getAlias().equals(alias)) + result.add(e); } } + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "get", result); return result; } /** - * Returns the list of all aliases contained by this envelope, - * separated by a semicolon (';'). - * + * Returns the list of all aliases contained by this envelope, separated by a + * semicolon (';'). + * * @return The list of aliases. */ public String getAliasList() { String list = properties.get("alias-list"); if (list == null) - { - return ""; - } + return ""; else - { - return list; - } + return list; } /** * Removes the specified entry. - * + * * @param entry The entry. * @return True if an entry was removed. */ public boolean remove(Entry entry) { + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "remove", entry); boolean ret = false; for (Iterator it = entries.iterator(); it.hasNext();) { @@ -268,36 +266,71 @@ public abstract class EnvelopeEntry extends Entry } if (ret) { + if (Configuration.DEBUG) + log.fine("State before: " + this); payload = null; makeAliasList(); + if (Configuration.DEBUG) + log.fine("State after: " + this); } + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "remove", Boolean.valueOf(ret)); return ret; } /** * Removes all primitive entries that have the specified alias. - * + * * @param alias The alias of the entries to remove. + * @return <code>true</code> if <code>alias</code> was present and was + * successfully trmoved. Returns <code>false</code> if + * <code>alias</code> was not present in the list of aliases in this + * envelope. */ - public void remove(String alias) + public boolean remove(String alias) { + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "remove", alias); + boolean result = false; for (Iterator it = entries.iterator(); it.hasNext();) { Entry e = (Entry) it.next(); if (e instanceof EnvelopeEntry) { - ((EnvelopeEntry) e).remove(alias); + EnvelopeEntry ee = (EnvelopeEntry) e; + result = ee.remove(alias) || result; } else if (e instanceof PrimitiveEntry) { - if (((PrimitiveEntry) e).getAlias().equals(alias)) + PrimitiveEntry pe = (PrimitiveEntry) e; + if (pe.getAlias().equals(alias)) { it.remove(); + result = true; } } } - payload = null; - makeAliasList(); + if (result) + { + if (Configuration.DEBUG) + log.fine("State before: " + this); + payload = null; + makeAliasList(); + if (Configuration.DEBUG) + log.fine("State after: " + this); + } + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "remove", Boolean.valueOf(result)); + return result; + } + + public String toString() + { + return new StringBuilder("Envelope{") + .append(super.toString()) + .append(", entries=").append(entries) + .append("}") + .toString(); } // Protected methods. @@ -308,22 +341,19 @@ public abstract class EnvelopeEntry extends Entry ByteArrayOutputStream bout = new ByteArrayOutputStream(1024); DataOutputStream out = new DataOutputStream(bout); for (Iterator it = entries.iterator(); it.hasNext();) - { - ((Entry) it.next()).encode(out); - } + ((Entry) it.next()).encode(out); } protected void setContainingEnvelope(EnvelopeEntry e) { if (containingEnvelope != null) - { - throw new IllegalArgumentException("envelopes may not be shared"); - } + throw new IllegalArgumentException("envelopes may not be shared"); containingEnvelope = e; } protected void decodeEnvelope(DataInputStream in) throws IOException { + this.entries.clear(); while (true) { int type = in.read(); @@ -367,32 +397,43 @@ public abstract class EnvelopeEntry extends Entry } } - // Own methods. - // ------------------------------------------------------------------------ - private void makeAliasList() { - if (entries.isEmpty()) - return; - StringBuffer buf = new StringBuffer(); - for (Iterator it = entries.iterator(); it.hasNext();) + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "makeAliasList"); + if (! entries.isEmpty()) { - Entry entry = (Entry) it.next(); - if (entry instanceof EnvelopeEntry) - { - buf.append(((EnvelopeEntry) entry).getAliasList()); - } - else if (entry instanceof PrimitiveEntry) + StringBuilder buf = new StringBuilder(); + String aliasOrList; + for (Iterator it = entries.iterator(); it.hasNext();) { - buf.append(((PrimitiveEntry) entry).getAlias()); + Entry entry = (Entry) it.next(); + aliasOrList = null; + if (entry instanceof EnvelopeEntry) + aliasOrList = ((EnvelopeEntry) entry).getAliasList(); + else if (entry instanceof PrimitiveEntry) + aliasOrList = ((PrimitiveEntry) entry).getAlias(); + else if (Configuration.DEBUG) + log.fine("Entry with no Alias. Ignored: " + entry); + if (aliasOrList != null) + { + aliasOrList = aliasOrList.trim(); + if (aliasOrList.trim().length() > 0) + { + buf.append(aliasOrList); + if (it.hasNext()) + buf.append(';'); + } + } } - if (it.hasNext()) - buf.append(';'); - } - properties.put("alias-list", buf.toString()); - if (containingEnvelope != null) - { - containingEnvelope.makeAliasList(); + String aliasList = buf.toString(); + properties.put("alias-list", aliasList); + if (Configuration.DEBUG) + log.fine("alias-list=[" + aliasList + "]"); + if (containingEnvelope != null) + containingEnvelope.makeAliasList(); } + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "makeAliasList"); } } diff --git a/libjava/classpath/gnu/javax/crypto/keyring/GnuPrivateKeyring.java b/libjava/classpath/gnu/javax/crypto/keyring/GnuPrivateKeyring.java index c1fe30e677b..2ccdad6b3e3 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/GnuPrivateKeyring.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/GnuPrivateKeyring.java @@ -38,6 +38,7 @@ exception statement from your version. */ package gnu.javax.crypto.keyring; +import gnu.java.security.Configuration; import gnu.java.security.Registry; import java.io.DataInputStream; @@ -55,30 +56,21 @@ import java.util.logging.Level; import java.util.logging.Logger; /** - * <p>.</p> + * */ -public class GnuPrivateKeyring extends BaseKeyring implements IPrivateKeyring +public class GnuPrivateKeyring + extends BaseKeyring + implements IPrivateKeyring { - // Constants and variables - // ------------------------------------------------------------------------- - private static final Logger log = Logger.getLogger(GnuPrivateKeyring.class.getName()); public static final int USAGE = Registry.GKR_PRIVATE_KEYS | Registry.GKR_PUBLIC_CREDENTIALS; - protected String mac; - protected int maclen; - protected String cipher; - protected String mode; - protected int keylen; - // Constructor(s) - // ------------------------------------------------------------------------- - public GnuPrivateKeyring(String mac, int maclen, String cipher, String mode, int keylen) { @@ -97,16 +89,10 @@ public class GnuPrivateKeyring extends BaseKeyring implements IPrivateKeyring this("HMAC-SHA-1", 20, "AES", "OFB", 16); } - // Class methods - // ------------------------------------------------------------------------- - - // Instance methods - // ------------------------------------------------------------------------- - public boolean containsPrivateKey(String alias) { - log.entering(this.getClass().getName(), "containsPrivateKey", alias); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "containsPrivateKey", alias); boolean result = false; if (containsAlias(alias)) for (Iterator it = get(alias).iterator(); it.hasNext();) @@ -115,33 +101,34 @@ public class GnuPrivateKeyring extends BaseKeyring implements IPrivateKeyring result = true; break; } - - log.exiting(this.getClass().getName(), "containsPrivateKey", - Boolean.valueOf(result)); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "containsPrivateKey", + Boolean.valueOf(result)); return result; } public Key getPrivateKey(String alias, char[] password) throws UnrecoverableKeyException { - log.entering(this.getClass().getName(), "getPrivateKey", - new Object[] { alias, String.valueOf(password) }); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "getPrivateKey", alias); Key result = null; if (containsAlias(alias)) { PasswordAuthenticatedEntry e1 = null; - PasswordEncryptedEntry e2 = null; for (Iterator it = get(alias).iterator(); it.hasNext();) { Entry e = (Entry) it.next(); + if (Configuration.DEBUG) + log.finest("Entry: " + e); if (e instanceof PasswordAuthenticatedEntry) { e1 = (PasswordAuthenticatedEntry) e; break; } } - + if (Configuration.DEBUG) + log.fine("e1 = " + e1); if (e1 != null) { try @@ -150,9 +137,11 @@ public class GnuPrivateKeyring extends BaseKeyring implements IPrivateKeyring } catch (Exception e) { + if (Configuration.DEBUG) + log.throwing(this.getClass().getName(), "getPrivateKey", e); throw new UnrecoverableKeyException("authentication failed"); } - + PasswordEncryptedEntry e2 = null; for (Iterator it = e1.getEntries().iterator(); it.hasNext();) { Entry e = (Entry) it.next(); @@ -162,7 +151,6 @@ public class GnuPrivateKeyring extends BaseKeyring implements IPrivateKeyring break; } } - if (e2 != null) { try @@ -171,9 +159,9 @@ public class GnuPrivateKeyring extends BaseKeyring implements IPrivateKeyring } catch (Exception e) { + log.throwing(this.getClass().getName(), "getPrivateKey", e); throw new UnrecoverableKeyException("decryption failed"); } - for (Iterator it = e2.get(alias).iterator(); it.hasNext();) { Entry e = (Entry) it.next(); @@ -186,66 +174,67 @@ public class GnuPrivateKeyring extends BaseKeyring implements IPrivateKeyring } } } - - log.exiting(this.getClass().getName(), "getPrivateKey", result); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "getPrivateKey", + result == null ? "null" : result.getClass().getName()); return result; } public void putPrivateKey(String alias, Key key, char[] password) { - log.entering(this.getClass().getName(), "putPrivateKey", - new Object[] { alias, key, String.valueOf(password) }); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "putPrivateKey", + new Object[] { alias, key.getClass().getName() }); if (! containsPrivateKey(alias)) { alias = fixAlias(alias); Properties p = new Properties(); p.put("alias", alias); PrivateKeyEntry pke = new PrivateKeyEntry(key, new Date(), p); + if (Configuration.DEBUG) + log.fine("About to encrypt the key..."); PasswordEncryptedEntry enc; enc = new PasswordEncryptedEntry(cipher, mode, keylen, new Properties()); enc.add(pke); - - PasswordAuthenticatedEntry auth; - auth = new PasswordAuthenticatedEntry(mac, maclen, new Properties()); - auth.add(enc); - - log.finest("About to encrypt the key..."); try { enc.encode(null, password); } catch (IOException x) { - log.log(Level.FINER, "Exception while encrypting the key. " - + "Rethrow as IllegalArgumentException", x); + if (Configuration.DEBUG) + log.log(Level.FINE, "Exception while encrypting the key. " + + "Rethrow as IllegalArgumentException", x); throw new IllegalArgumentException(x.toString()); } - - log.finest("About to authenticate the encrypted key..."); + if (Configuration.DEBUG) + log.fine("About to authenticate the encrypted key..."); + PasswordAuthenticatedEntry auth; + auth = new PasswordAuthenticatedEntry(mac, maclen, new Properties()); + auth.add(enc); try { auth.encode(null, password); } catch (IOException x) { - log.log(Level.FINER, "Exception while authenticating the encrypted " - + "key. Rethrow as IllegalArgumentException", x); + if (Configuration.DEBUG) + log.log(Level.FINE, "Exception while authenticating the encrypted " + + "key. Rethrow as IllegalArgumentException", x); throw new IllegalArgumentException(x.toString()); } - keyring.add(auth); } - else - log.finer("Keyring already contains alias: " + alias); - - log.exiting(this.getClass().getName(), "putPrivateKey"); + else if (Configuration.DEBUG) + log.fine("Keyring already contains alias: " + alias); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "putPrivateKey"); } public boolean containsPublicKey(String alias) { - log.entering(this.getClass().getName(), "containsPublicKey", alias); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "containsPublicKey", alias); boolean result = false; if (containsAlias(alias)) for (Iterator it = get(alias).iterator(); it.hasNext();) @@ -254,16 +243,16 @@ public class GnuPrivateKeyring extends BaseKeyring implements IPrivateKeyring result = true; break; } - - log.exiting(this.getClass().getName(), "containsPublicKey", - Boolean.valueOf(result)); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "containsPublicKey", + Boolean.valueOf(result)); return result; } public PublicKey getPublicKey(String alias) { - log.entering(this.getClass().getName(), "getPublicKey", alias); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "getPublicKey", alias); PublicKey result = null; if (containsAlias(alias)) for (Iterator it = get(alias).iterator(); it.hasNext();) @@ -275,32 +264,33 @@ public class GnuPrivateKeyring extends BaseKeyring implements IPrivateKeyring break; } } - - log.exiting(this.getClass().getName(), "getPublicKey", result); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "getPublicKey", + result == null ? "null" : result.getClass().getName()); return result; } public void putPublicKey(String alias, PublicKey key) { - log.entering(this.getClass().getName(), "putPublicKey", - new Object[] { alias, key }); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "putPublicKey", + new Object[] { alias, key.getClass().getName() }); if (! containsPublicKey(alias)) { Properties p = new Properties(); p.put("alias", fixAlias(alias)); add(new PublicKeyEntry(key, new Date(), p)); } - else - log.finer("Keyring already contains alias: " + alias); - - log.exiting(this.getClass().getName(), "putPublicKey"); + else if (Configuration.DEBUG) + log.fine("Keyring already contains alias: " + alias); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "putPublicKey"); } public boolean containsCertPath(String alias) { - log.entering(this.getClass().getName(), "containsCertPath", alias); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "containsCertPath", alias); boolean result = false; if (containsAlias(alias)) for (Iterator it = get(alias).iterator(); it.hasNext();) @@ -309,16 +299,16 @@ public class GnuPrivateKeyring extends BaseKeyring implements IPrivateKeyring result = true; break; } - - log.exiting(this.getClass().getName(), "containsCertPath", - Boolean.valueOf(result)); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "containsCertPath", + Boolean.valueOf(result)); return result; } public Certificate[] getCertPath(String alias) { - log.entering(this.getClass().getName(), "getCertPath", alias); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "getCertPath", alias); Certificate[] result = null; if (containsAlias(alias)) for (Iterator it = get(alias).iterator(); it.hasNext();) @@ -330,52 +320,49 @@ public class GnuPrivateKeyring extends BaseKeyring implements IPrivateKeyring break; } } - - log.exiting(this.getClass().getName(), "getCertPath", result); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "getCertPath", result); return result; } public void putCertPath(String alias, Certificate[] path) { - log.entering(this.getClass().getName(), "putCertPath", - new Object[] { alias, path }); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "putCertPath", + new Object[] { alias, path }); if (! containsCertPath(alias)) { Properties p = new Properties(); p.put("alias", fixAlias(alias)); add(new CertPathEntry(path, new Date(), p)); } - else - log.finer("Keyring already contains alias: " + alias); - - log.exiting(this.getClass().getName(), "putCertPath"); + else if (Configuration.DEBUG) + log.fine("Keyring already contains alias: " + alias); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "putCertPath"); } protected void load(InputStream in, char[] password) throws IOException { - log.entering(this.getClass().getName(), "load", - new Object[] { in, String.valueOf(password) }); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "load"); if (in.read() != USAGE) throw new MalformedKeyringException("incompatible keyring usage"); - if (in.read() != PasswordAuthenticatedEntry.TYPE) - throw new MalformedKeyringException("expecting password-authenticated entry tag"); - + throw new MalformedKeyringException( + "expecting password-authenticated entry tag"); keyring = PasswordAuthenticatedEntry.decode(new DataInputStream(in), password); - - log.exiting(this.getClass().getName(), "load"); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "load"); } protected void store(OutputStream out, char[] password) throws IOException { - log.entering(this.getClass().getName(), "store", - new Object[] { out, String.valueOf(password) }); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "store"); out.write(USAGE); keyring.encode(new DataOutputStream(out), password); - - log.exiting(this.getClass().getName(), "store"); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "store"); } } diff --git a/libjava/classpath/gnu/javax/crypto/keyring/GnuPublicKeyring.java b/libjava/classpath/gnu/javax/crypto/keyring/GnuPublicKeyring.java index 490eb4458fa..5243919c3c9 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/GnuPublicKeyring.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/GnuPublicKeyring.java @@ -38,6 +38,7 @@ exception statement from your version. */ package gnu.javax.crypto.keyring; +import gnu.java.security.Configuration; import gnu.java.security.Registry; import java.io.DataInputStream; @@ -50,17 +51,13 @@ import java.util.Date; import java.util.Iterator; import java.util.logging.Logger; -public class GnuPublicKeyring extends BaseKeyring implements IPublicKeyring +public class GnuPublicKeyring + extends BaseKeyring + implements IPublicKeyring { - // Fields. - // ------------------------------------------------------------------------ - private static final Logger log = Logger.getLogger(GnuPublicKeyring.class.getName()); public static final int USAGE = Registry.GKR_CERTIFICATES; - // Constructors. - // ------------------------------------------------------------------------ - public GnuPublicKeyring(String mac, int macLen) { keyring = new PasswordAuthenticatedEntry(mac, macLen, new Properties()); @@ -72,13 +69,10 @@ public class GnuPublicKeyring extends BaseKeyring implements IPublicKeyring { } - // Instance methods. - // ------------------------------------------------------------------------ - public boolean containsCertificate(String alias) { - log.entering(this.getClass().getName(), "containsCertificate", alias); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "containsCertificate", alias); boolean result = false; if (containsAlias(alias)) for (Iterator it = get(alias).iterator(); it.hasNext();) @@ -87,16 +81,16 @@ public class GnuPublicKeyring extends BaseKeyring implements IPublicKeyring result = true; break; } - - log.exiting(this.getClass().getName(), "containsCertificate", - Boolean.valueOf(result)); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "containsCertificate", + Boolean.valueOf(result)); return result; } public Certificate getCertificate(String alias) { - log.entering(this.getClass().getName(), "getCertificate", alias); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "getCertificate", alias); Certificate result = null; if (containsAlias(alias)) for (Iterator it = get(alias).iterator(); it.hasNext();) @@ -108,53 +102,50 @@ public class GnuPublicKeyring extends BaseKeyring implements IPublicKeyring break; } } - - log.exiting(this.getClass().getName(), "getCertificate", result); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "getCertificate", result); return result; } public void putCertificate(String alias, Certificate cert) { - log.entering(this.getClass().getName(), "putCertificate", - new Object[] { alias, cert }); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "putCertificate", + new Object[] { alias, cert }); if (! containsCertificate(alias)) { Properties p = new Properties(); p.put("alias", fixAlias(alias)); add(new CertificateEntry(cert, new Date(), p)); } - else - log.finer("Keyring already contains alias: " + alias); - - log.exiting(this.getClass().getName(), "putCertificate"); + else if (Configuration.DEBUG) + log.fine("Keyring already contains alias: " + alias); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "putCertificate"); } protected void load(InputStream in, char[] password) throws IOException { - log.entering(this.getClass().getName(), "load", - new Object[] { in, String.valueOf(password) }); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "load"); if (in.read() != USAGE) throw new MalformedKeyringException("incompatible keyring usage"); - if (in.read() != PasswordAuthenticatedEntry.TYPE) - throw new MalformedKeyringException("expecting password-authenticated entry tag"); - + throw new MalformedKeyringException( + "expecting password-authenticated entry tag"); DataInputStream dis = new DataInputStream(in); keyring = PasswordAuthenticatedEntry.decode(dis, password); - - log.exiting(this.getClass().getName(), "load"); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "load"); } protected void store(OutputStream out, char[] password) throws IOException { - log.entering(this.getClass().getName(), "store", - new Object[] { out, String.valueOf(password) }); - + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "store"); out.write(USAGE); keyring.encode(new DataOutputStream(out), password); - - log.exiting(this.getClass().getName(), "store"); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "store"); } } diff --git a/libjava/classpath/gnu/javax/crypto/keyring/IKeyring.java b/libjava/classpath/gnu/javax/crypto/keyring/IKeyring.java index 56f467df26e..44d5377008a 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/IKeyring.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/IKeyring.java @@ -44,120 +44,118 @@ import java.util.List; import java.util.Map; /** - * <p>The top-level interface to a <i>keyring:</i> a file that is used to - * store and protect public and private cryptographic keys.</p> - * - * <p>A <i>keyring</i> is modelled as a mapping of one <i>alias</i> to one or - * more <i>entries</i> (optionally of different types).</p> - * - * <p>See also the sub-interfaces {@link IPublicKeyring} and - * {@link IPrivateKeyring} for special types of <i>keyrings</i> --the difference - * being in the type of entries they contain.</p> + * The top-level interface to a <i>keyring:</i> a file that is used to store + * and protect public and private cryptographic keys. + * <p> + * A <i>keyring</i> is modelled as a mapping of one <i>alias</i> to one or + * more <i>entries</i> (optionally of different types). + * <p> + * See also the sub-interfaces {@link IPublicKeyring} and + * {@link IPrivateKeyring} for special types of <i>keyrings</i> --the + * difference being in the type of entries they contain. */ public interface IKeyring { - /** - * <p>Property name for the source of data to load the keyring from. The - * value mapped must be a {@link java.io.InputStream}.</p> + * Property name for the source of data to load the keyring from. The value + * mapped must be a {@link java.io.InputStream}. */ public static final String KEYRING_DATA_IN = "gnu.crypto.keyring.data.in"; /** - * <p>Property name for the data sink to store the keyring to. The value - * mapped must be a {@link java.io.OutputStream}.</p> + * Property name for the data sink to store the keyring to. The value mapped + * must be a {@link java.io.OutputStream}. */ public static final String KEYRING_DATA_OUT = "gun.crypto.keyring.data.out"; /** - * <p>Property name for the keyring's top-level password, used to - * authenticate and/or transform the store itself. The mapped value must be a - * char array.</p> + * Property name for the keyring's top-level password, used to authenticate + * and/or transform the store itself. The mapped value must be a char array. */ public static final String KEYRING_PASSWORD = "gnu.crypto.keyring.password"; /** - * <p>Loads a keyring into memory.</p> - * - * <p>What happens to the current contents of this keyring? are the new ones - * merged with the current ones or do they simply replace them?</p> - * + * Loads a keyring into memory. + * <p> + * What happens to the current contents of this keyring? are the new ones + * merged with the current ones or do they simply replace them? + * * @param attributes The attributes that designate the source where the store - * is to be loaded from. What happens + * is to be loaded from. What happens * @throws IllegalArgumentException If the attributes are inappropriate. * @throws IOException If the keyring file cannot be read. * @throws SecurityException If the given password is incorrect, or if the - * top-level authentication or decryption fails. + * top-level authentication or decryption fails. */ void load(Map attributes) throws IOException; /** - * <p>Stores the contents of this keyring to persistent storage as specified - * by the designated <code>attributes</code>.</p> - * + * Stores the contents of this keyring to persistent storage as specified by + * the designated <code>attributes</code>. + * * @param attributes the attributes that define where the contents of this - * keyring will be stored. + * keyring will be stored. * @throws IOException if an exception occurs during the process. */ void store(Map attributes) throws IOException; /** - * <p>Resets this keyring, clearing all sensitive data. This method always - * suceeds.</p> + * Resets this keyring, clearing all sensitive data. This method always + * suceeds. */ void reset(); /** - * <p>Returns the number of entries in this keyring.</p> - * + * Returns the number of entries in this keyring. + * * @return The number of current entries in this keyring. */ int size(); /** - * <p>Returns an {@link Enumeration} of all aliases (instances of - * {@link String}) in this keyring.</p> - * - * @return The enumeration of {@link String}s each representing an - * <i>alias</i> found in this keyring. + * Returns an {@link Enumeration} of all aliases (instances of {@link String}) + * in this keyring. + * + * @return The enumeration of {@link String}s each representing an <i>alias</i> + * found in this keyring. */ Enumeration aliases(); /** * Tests whether or not this keyring contains the given alias. - * + * * @param alias The alias to check. * @return true if this keyring contains the alias. */ boolean containsAlias(String alias); /** - * <p>Returns a {@link List} of entries (instances of {@link Entry}) for the + * Returns a {@link List} of entries (instances of {@link Entry}) for the * given <code>alias</code>, or <code>null</code> if there no such entry - * exists.</p> - * + * exists. + * * @param alias The alias of the entry(ies) to return. * @return A list of all entries (instances of {@link Entry} that have the - * given <code>alias</code>, or <code>null</code> if no one {@link Entry} can - * be found with the designated <code>alias</code>. + * given <code>alias</code>, or <code>null</code> if no one + * {@link Entry} can be found with the designated <code>alias</code>. */ List get(String alias); /** - * <p>Adds a designated {@link Entry} to this keyring.</p> - * - * <p>What happens if there is already an entry with the same alias?</p> - * + * Adds a designated {@link Entry} to this keyring. + * <p> + * What happens if there is already an entry with the same alias? + * * @param entry The entry to put in this keyring. */ void add(Entry entry); /** - * <p>Removes an entry with the designated <code>alias</code> from this - * keyring. Does nothing if there was no such entry.</p> - * - * <p>What happens if there are more than one?</p> - * + * Removes an entry with the designated <code>alias</code> from this + * keyring. Does nothing if there was no such entry. + * <p> + * What happens if there are more than one? + * * @param alias The alias of the entry to remove. */ void remove(String alias); diff --git a/libjava/classpath/gnu/javax/crypto/keyring/IPrivateKeyring.java b/libjava/classpath/gnu/javax/crypto/keyring/IPrivateKeyring.java index 66bbd84f568..9e2ceab9b95 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/IPrivateKeyring.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/IPrivateKeyring.java @@ -44,41 +44,41 @@ import java.security.UnrecoverableKeyException; import java.security.cert.Certificate; /** - * <p>An interface to private, or "personal", keyrings, which contain private + * An interface to private, or "personal", keyrings, which contain private * credentials. The contract is that each such entry is known by a unique - * <i>alias</i>.</p> - * - * <p>What about public keys? and certificate-path?</p> + * <i>alias</i>. + * <p> + * What about public keys? and certificate-path? */ -public interface IPrivateKeyring extends IKeyring +public interface IPrivateKeyring + extends IKeyring { - /** - * <p>Tests if this keyring contains a private key entry with the given - * <code>alias</code>.</p> - * + * Tests if this keyring contains a private key entry with the given + * <code>alias</code>. + * * @param alias The alias to check. * @return <code>true</code> if this keyring contains a private key with the - * given <code>alias</code>; <code>false</code> otherwise.</p> + * given <code>alias</code>; <code>false</code> otherwise. */ boolean containsPrivateKey(String alias); /** - * <p>Returns the private key with the given <code>alias</code>.</p> - * + * Returns the private key with the given <code>alias</code>. + * * @param alias The alias of the private key to find. * @param password The password of the private key. * @return The private, or secret, key if one is found; <code>null</code> if - * none were found. + * none were found. * @throws UnrecoverableKeyException If the private key could not be - * recovered, possibly due to a bad password. + * recovered, possibly due to a bad password. */ Key getPrivateKey(String alias, char[] password) throws UnrecoverableKeyException; /** - * <p>Adds a private key to this keyring.</p> - * + * Adds a private key to this keyring. + * * @param alias The alias of the private key. * @param key The private key. * @param password The password used to protect this private key. @@ -86,55 +86,57 @@ public interface IPrivateKeyring extends IKeyring void putPrivateKey(String alias, Key key, char[] password); /** - * <p>Checks if this keyring contains a public key with the given - * <code>alias</code>.</p> - * + * Checks if this keyring contains a public key with the given + * <code>alias</code>. + * * @param alias The alias to test. - * @return <code>true</code> if this keyring contains a public key entry with - * the given <code>alias</code>; <code>false</code> otherwise. + * @return <code>true</code> if this keyring contains a public key entry + * with the given <code>alias</code>; <code>false</code> + * otherwise. */ boolean containsPublicKey(String alias); /** - * <p>Returns the public key with the given <code>alias</code>, or - * <code>null</code> if there is no such entry.</p> - * + * Returns the public key with the given <code>alias</code>, or + * <code>null</code> if there is no such entry. + * * @param alias The alias of the public key to find. * @return The public key; or <code>null</code> if none were found. */ PublicKey getPublicKey(String alias); /** - * <p>Sets a public key entry.</p> - * + * Sets a public key entry. + * * @param alias The alias for this public key. * @param key The public key. */ void putPublicKey(String alias, PublicKey key); /** - * <p>Checks if this keyring contains a certificate path with the given - * <code>alias</code>.</p> - * + * Checks if this keyring contains a certificate path with the given + * <code>alias</code>. + * * @param alias The alias to check. - * @return <code>true</code> if this keyring contains a certificate path with - * the given <code>alias</code>; <code>false</code> otherwise. + * @return <code>true</code> if this keyring contains a certificate path + * with the given <code>alias</code>; <code>false</code> + * otherwise. */ boolean containsCertPath(String alias); /** - * <p>Returns the certificate path with the given <code>alias</code>, or - * <code>null</code> if there is no such entry.</p> - * + * Returns the certificate path with the given <code>alias</code>, or + * <code>null</code> if there is no such entry. + * * @param alias The alias of the certificate path to find. * @return The certificate path for the designated <code>alias</code>; or - * <code>null</code> if none were found. + * <code>null</code> if none were found. */ Certificate[] getCertPath(String alias); /** - * <p>Sets a certificate path entry.</p> - * + * Sets a certificate path entry. + * * @param alias The alias for this certificate path. * @param path The certificate path. */ diff --git a/libjava/classpath/gnu/javax/crypto/keyring/IPublicKeyring.java b/libjava/classpath/gnu/javax/crypto/keyring/IPublicKeyring.java index ccf9ca73b55..efe568f185b 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/IPublicKeyring.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/IPublicKeyring.java @@ -41,39 +41,40 @@ package gnu.javax.crypto.keyring; import java.security.cert.Certificate; /** - * <p>An interface for keyrings that contain trusted (by the owner) public - * credentials (incl. certificates).</p> - * + * An interface for keyrings that contain trusted (by the owner) public + * credentials (incl. certificates). + * * @see IKeyring */ -public interface IPublicKeyring extends IKeyring +public interface IPublicKeyring + extends IKeyring { - /** - * <p>Tests if this keyring contains a certificate entry with the specified - * <code>alias</code>.</p> - * + * Tests if this keyring contains a certificate entry with the specified + * <code>alias</code>. + * * @param alias The alias of the certificate to check. * @return <code>true</code> if this keyring contains a certificate entry - * that has the given <code>alias</code>; <code>false</code> otherwise. + * that has the given <code>alias</code>; <code>false</code> + * otherwise. */ boolean containsCertificate(String alias); /** - * <p>Returns a certificate that has the given <code>alias</code>, or - * <code>null</code> if this keyring has no such entry.</p> - * + * Returns a certificate that has the given <code>alias</code>, or + * <code>null</code> if this keyring has no such entry. + * * @param alias The alias of the certificate to find. * @return The certificate with the designated <code>alias</code>, or - * <code>null</code> if none found. + * <code>null</code> if none found. */ Certificate getCertificate(String alias); /** - * <p>Adds a certificate in this keyring, with the given <code>alias</code>.</p> - * - * <p>What happens if there is already a certificate entry with this alias?</p> - * + * Adds a certificate in this keyring, with the given <code>alias</code>. + * <p> + * What happens if there is already a certificate entry with this alias? + * * @param alias The alias of this certificate entry. * @param cert The certificate. */ diff --git a/libjava/classpath/gnu/javax/crypto/keyring/MalformedKeyringException.java b/libjava/classpath/gnu/javax/crypto/keyring/MalformedKeyringException.java index 44c953946d4..0dab3a764c6 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/MalformedKeyringException.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/MalformedKeyringException.java @@ -40,12 +40,9 @@ package gnu.javax.crypto.keyring; import java.io.IOException; -public class MalformedKeyringException extends IOException +public class MalformedKeyringException + extends IOException { - - // Constructors. - // ------------------------------------------------------------------------ - public MalformedKeyringException() { super(); diff --git a/libjava/classpath/gnu/javax/crypto/keyring/MaskableEnvelopeEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/MaskableEnvelopeEntry.java index 7fed7c40c15..5206a5e071e 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/MaskableEnvelopeEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/MaskableEnvelopeEntry.java @@ -43,21 +43,15 @@ import java.util.List; /** * An envelope entry that can be "masked" -- placed in a state where the - * envelope's contents cannot be accessed, due to the envelope not being - * fully decoded, for example. + * envelope's contents cannot be accessed, due to the envelope not being fully + * decoded, for example. */ -public abstract class MaskableEnvelopeEntry extends EnvelopeEntry +public abstract class MaskableEnvelopeEntry + extends EnvelopeEntry { - - // Fields. - // ------------------------------------------------------------------------ - /** The masked state. */ protected boolean masked; - // Constructors. - // ------------------------------------------------------------------------ - public MaskableEnvelopeEntry(int type, Properties properties) { super(type, properties); @@ -68,12 +62,9 @@ public abstract class MaskableEnvelopeEntry extends EnvelopeEntry super(type); } - // Instance methods. - // ------------------------------------------------------------------------ - /** * Sets the masked state to the specified value. - * + * * @param masked The new masked state. */ protected final void setMasked(boolean masked) @@ -84,7 +75,7 @@ public abstract class MaskableEnvelopeEntry extends EnvelopeEntry /** * Gets the masked state of this object. Certain operations on this object * will fail if it is masked. - * + * * @return The current masked state. */ public boolean isMasked() @@ -95,54 +86,50 @@ public abstract class MaskableEnvelopeEntry extends EnvelopeEntry public void add(Entry entry) { if (isMasked()) - { - throw new IllegalStateException("masked envelope"); - } + throw new IllegalStateException("masked envelope"); super.add(entry); } public boolean containsEntry(Entry entry) { if (isMasked()) - { - throw new IllegalStateException("masked envelope"); - } + throw new IllegalStateException("masked envelope"); return super.containsEntry(entry); } public List getEntries() { if (isMasked()) - { - throw new IllegalStateException("masked envelope"); - } + throw new IllegalStateException("masked envelope"); return new ArrayList(entries); } public List get(String alias) { if (isMasked()) - { - throw new IllegalStateException("masked envelope"); - } + throw new IllegalStateException("masked envelope"); return super.get(alias); } public boolean remove(Entry entry) { if (isMasked()) - { - throw new IllegalStateException("masked envelope"); - } + throw new IllegalStateException("masked envelope"); return super.remove(entry); } - public void remove(String alias) + public boolean remove(String alias) { if (isMasked()) - { - throw new IllegalStateException("masked envelope"); - } - super.remove(alias); + throw new IllegalStateException("masked envelope"); + return super.remove(alias); + } + + public String toString() + { + return new StringBuilder("MaskableEnvelope{") + .append(super.toString()) + .append(", masked=").append(masked) + .append("}").toString(); } } diff --git a/libjava/classpath/gnu/javax/crypto/keyring/MeteredInputStream.java b/libjava/classpath/gnu/javax/crypto/keyring/MeteredInputStream.java index fcf2be746c9..330e4b20fb5 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/MeteredInputStream.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/MeteredInputStream.java @@ -39,22 +39,15 @@ exception statement from your version. */ package gnu.javax.crypto.keyring; import java.io.FilterInputStream; -import java.io.InputStream; import java.io.IOException; +import java.io.InputStream; -final class MeteredInputStream extends FilterInputStream +final class MeteredInputStream + extends FilterInputStream { - - // Fields. - // ------------------------------------------------------------------------ - private int count; - private final int limit; - // Constructor. - // ------------------------------------------------------------------------ - MeteredInputStream(InputStream in, int limit) { super(in); @@ -64,12 +57,9 @@ final class MeteredInputStream extends FilterInputStream count = 0; } - // Instance methods. - // ------------------------------------------------------------------------ - /** * Tests if the number of bytes read has reached the limit. - * + * * @return True if the limit has been reached. */ public boolean limitReached() diff --git a/libjava/classpath/gnu/javax/crypto/keyring/PasswordAuthenticatedEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/PasswordAuthenticatedEntry.java index 2e3a0d145c8..be10029c8bc 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/PasswordAuthenticatedEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/PasswordAuthenticatedEntry.java @@ -38,9 +38,11 @@ exception statement from your version. */ package gnu.javax.crypto.keyring; +import gnu.java.security.Configuration; import gnu.java.security.Registry; import gnu.java.security.prng.IRandom; import gnu.java.security.prng.LimitReachedException; +import gnu.java.security.util.PRNG; import gnu.java.security.util.Util; import gnu.javax.crypto.mac.IMac; import gnu.javax.crypto.mac.MacFactory; @@ -55,35 +57,27 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.security.InvalidKeyException; -import java.security.SecureRandom; import java.util.Arrays; -import java.util.Iterator; import java.util.HashMap; +import java.util.Iterator; +import java.util.logging.Logger; /** - * <p>An entry authenticated with a password-based MAC.</p> + * An entry authenticated with a password-based MAC. */ -public final class PasswordAuthenticatedEntry extends MaskableEnvelopeEntry +public final class PasswordAuthenticatedEntry + extends MaskableEnvelopeEntry implements PasswordProtectedEntry, Registry { - - // Constants and variables - // ------------------------------------------------------------------------- - + private static final Logger log = Logger.getLogger(PasswordAuthenticatedEntry.class.getName()); public static final int TYPE = 3; - // Constructor(s) - // ------------------------------------------------------------------------- - public PasswordAuthenticatedEntry(String mac, int maclen, Properties properties) { super(TYPE, properties); - if (mac == null || mac.length() == 0) - { - throw new IllegalArgumentException("no MAC specified"); - } + throw new IllegalArgumentException("no MAC specified"); this.properties.put("mac", mac); this.properties.put("maclen", String.valueOf(maclen)); setMasked(false); @@ -95,9 +89,6 @@ public final class PasswordAuthenticatedEntry extends MaskableEnvelopeEntry setMasked(true); } - // Class methods - // ------------------------------------------------------------------------- - public static PasswordAuthenticatedEntry decode(DataInputStream in, char[] password) throws IOException @@ -113,10 +104,8 @@ public final class PasswordAuthenticatedEntry extends MaskableEnvelopeEntry entry.decodeEnvelope(in2); byte[] macValue = new byte[mac.macSize()]; in.readFully(macValue); - if (!Arrays.equals(macValue, mac.digest())) - { - throw new MalformedKeyringException("MAC verification failed"); - } + if (! Arrays.equals(macValue, mac.digest())) + throw new MalformedKeyringException("MAC verification failed"); return entry; } @@ -125,74 +114,76 @@ public final class PasswordAuthenticatedEntry extends MaskableEnvelopeEntry { PasswordAuthenticatedEntry entry = new PasswordAuthenticatedEntry(); entry.defaultDecode(in); - if (!entry.properties.containsKey("mac")) - { - throw new MalformedKeyringException("no MAC"); - } - if (!entry.properties.containsKey("maclen")) - { - throw new MalformedKeyringException("no MAC length"); - } - if (!entry.properties.containsKey("salt")) - { - throw new MalformedKeyringException("no salt"); - } + if (! entry.properties.containsKey("mac")) + throw new MalformedKeyringException("no MAC"); + if (! entry.properties.containsKey("maclen")) + throw new MalformedKeyringException("no MAC length"); + if (! entry.properties.containsKey("salt")) + throw new MalformedKeyringException("no salt"); return entry; } - // Instance methods - // ------------------------------------------------------------------------- - public void verify(char[] password) { - if (!isMasked() || payload == null) - { - return; - } - IMac m = null; - try - { - m = getMac(password); - } - catch (Exception x) + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "verify"); + if (isMasked() && payload != null) { - throw new IllegalArgumentException(x.toString()); + if (Configuration.DEBUG) + log.fine("payload to verify: " + Util.dumpString(payload)); + long tt = -System.currentTimeMillis(); + IMac m = null; + try + { + m = getMac(password); + } + catch (Exception x) + { + throw new IllegalArgumentException(x.toString(), x); + } + int limit = payload.length - m.macSize(); + m.update(payload, 0, limit); + byte[] macValue = new byte[m.macSize()]; + System.arraycopy(payload, payload.length - macValue.length, macValue, + 0, macValue.length); + if (! Arrays.equals(macValue, m.digest())) + throw new IllegalArgumentException("MAC verification failed"); + setMasked(false); + ByteArrayInputStream bais; + try + { + bais = new ByteArrayInputStream(payload, 0, limit); + DataInputStream in = new DataInputStream(bais); + decodeEnvelope(in); + } + catch (IOException ioe) + { + throw new IllegalArgumentException("malformed keyring fragment"); + } + tt += System.currentTimeMillis(); + if (Configuration.DEBUG) + log.fine("Verified in " + tt + "ms."); } - - m.update(payload, 0, payload.length - m.macSize()); - byte[] macValue = new byte[m.macSize()]; - System.arraycopy(payload, payload.length - macValue.length, macValue, 0, - macValue.length); - if (!Arrays.equals(macValue, m.digest())) - { - throw new IllegalArgumentException("MAC verification failed"); - } - try - { - DataInputStream in = new DataInputStream( - new ByteArrayInputStream( - payload, - 0, - payload.length - - m.macSize())); - decodeEnvelope(in); - } - catch (IOException ioe) - { - throw new IllegalArgumentException("malformed keyring fragment"); - } - setMasked(false); - payload = null; + else if (Configuration.DEBUG) + log.fine("Skip verification; " + + (isMasked() ? "null payload" : "unmasked")); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "verify"); } public void authenticate(char[] password) throws IOException { + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "authenticate"); + long tt = -System.currentTimeMillis(); + long t1 = -System.currentTimeMillis(); if (isMasked()) - { - throw new IllegalStateException("entry is masked"); - } + throw new IllegalStateException("entry is masked"); byte[] salt = new byte[8]; - new SecureRandom ().nextBytes (salt); + PRNG.getInstance().nextBytes(salt); + t1 += System.currentTimeMillis(); + if (Configuration.DEBUG) + log.fine("-- Generated salt in " + t1 + "ms."); properties.put("salt", Util.toString(salt)); IMac m = getMac(password); ByteArrayOutputStream bout = new ByteArrayOutputStream(1024); @@ -201,10 +192,25 @@ public final class PasswordAuthenticatedEntry extends MaskableEnvelopeEntry for (Iterator it = entries.iterator(); it.hasNext();) { Entry entry = (Entry) it.next(); + if (Configuration.DEBUG) + log.fine("-- About to authenticate one " + entry); + t1 = -System.currentTimeMillis(); entry.encode(out2); + t1 += System.currentTimeMillis(); + if (Configuration.DEBUG) + log.fine("-- Authenticated an Entry in " + t1 + "ms."); } bout.write(m.digest()); payload = bout.toByteArray(); + if (Configuration.DEBUG) + log.fine("authenticated payload: " + Util.dumpString(payload)); + setMasked(true); + tt += System.currentTimeMillis(); + if (Configuration.DEBUG) + { + log.fine("Authenticated in " + tt + "ms."); + log.exiting(this.getClass().getName(), "authenticate"); + } } public void encode(DataOutputStream out, char[] password) throws IOException @@ -217,48 +223,42 @@ public final class PasswordAuthenticatedEntry extends MaskableEnvelopeEntry { if (payload == null) { + log.fine("Null payload: " + this); throw new IllegalStateException("mac not computed"); } } - // Own methods. - // ------------------------------------------------------------------------ - private IMac getMac(char[] password) throws MalformedKeyringException { - if (!properties.containsKey("salt")) - { - throw new MalformedKeyringException("no salt"); - } - byte[] salt = Util.toBytesFromString(properties.get("salt")); - IMac mac = MacFactory.getInstance(properties.get("mac")); + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "getMac"); + String saltString = properties.get("salt"); + if (saltString == null) + throw new MalformedKeyringException("no salt"); + byte[] salt = Util.toBytesFromString(saltString); + String macAlgorithm = properties.get("mac"); + IMac mac = MacFactory.getInstance(macAlgorithm); if (mac == null) - { - throw new MalformedKeyringException("no such mac: " - + properties.get("mac")); - } - int keylen = mac.macSize(); - int maclen = 0; - if (!properties.containsKey("maclen")) - { - throw new MalformedKeyringException("no MAC length"); - } + throw new MalformedKeyringException("no such mac: " + macAlgorithm); + String macLenString = properties.get("maclen"); + if (macLenString == null) + throw new MalformedKeyringException("no MAC length"); + int maclen; try { - maclen = Integer.parseInt(properties.get("maclen")); + maclen = Integer.parseInt(macLenString); } catch (NumberFormatException nfe) { throw new MalformedKeyringException("bad MAC length"); } - HashMap pbAttr = new HashMap(); pbAttr.put(IPBE.PASSWORD, password); pbAttr.put(IPBE.SALT, salt); pbAttr.put(IPBE.ITERATION_COUNT, ITERATION_COUNT); IRandom kdf = PRNGFactory.getInstance("PBKDF2-HMAC-SHA"); kdf.init(pbAttr); - + int keylen = mac.macSize(); byte[] dk = new byte[keylen]; try { @@ -268,10 +268,9 @@ public final class PasswordAuthenticatedEntry extends MaskableEnvelopeEntry { throw new Error(shouldNotHappen.toString()); } - HashMap macAttr = new HashMap(); macAttr.put(IMac.MAC_KEY_MATERIAL, dk); - macAttr.put(IMac.TRUNCATED_SIZE, new Integer(maclen)); + macAttr.put(IMac.TRUNCATED_SIZE, Integer.valueOf(maclen)); try { mac.init(macAttr); @@ -280,6 +279,8 @@ public final class PasswordAuthenticatedEntry extends MaskableEnvelopeEntry { throw new Error(shouldNotHappen.toString()); } + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "getMac"); return mac; } } diff --git a/libjava/classpath/gnu/javax/crypto/keyring/PasswordEncryptedEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/PasswordEncryptedEntry.java index 26b4032bdfb..00031cbf1bb 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/PasswordEncryptedEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/PasswordEncryptedEntry.java @@ -38,11 +38,12 @@ exception statement from your version. */ package gnu.javax.crypto.keyring; +import gnu.java.security.Configuration; import gnu.java.security.Registry; import gnu.java.security.prng.IRandom; import gnu.java.security.prng.LimitReachedException; +import gnu.java.security.util.PRNG; import gnu.java.security.util.Util; - import gnu.javax.crypto.cipher.CipherFactory; import gnu.javax.crypto.cipher.IBlockCipher; import gnu.javax.crypto.mode.IMode; @@ -58,41 +59,28 @@ import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; - import java.security.InvalidKeyException; -import java.security.SecureRandom; - -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.Iterator; import java.util.HashMap; -import java.util.List; +import java.util.Iterator; +import java.util.logging.Logger; /** * An envelope that is encrypted with a password-derived key. */ -public class PasswordEncryptedEntry extends MaskableEnvelopeEntry implements - PasswordProtectedEntry, Registry +public class PasswordEncryptedEntry + extends MaskableEnvelopeEntry + implements PasswordProtectedEntry, Registry { - - // Constants and fields. - // ------------------------------------------------------------------------ - + private static final Logger log = Logger.getLogger(PasswordEncryptedEntry.class.getName()); public static final int TYPE = 1; - // Constructors. - // ------------------------------------------------------------------------ - public PasswordEncryptedEntry(String cipher, String mode, int keylen, Properties properties) { super(TYPE, properties); if ((cipher == null || cipher.length() == 0) || (mode == null || mode.length() == 0)) - { - throw new IllegalArgumentException("cipher nor mode can be empty"); - } + throw new IllegalArgumentException("cipher nor mode can be empty"); this.properties.put("cipher", cipher); this.properties.put("mode", mode); this.properties.put("keylen", String.valueOf(keylen)); @@ -105,9 +93,6 @@ public class PasswordEncryptedEntry extends MaskableEnvelopeEntry implements setMasked(true); } - // Class methods. - // ------------------------------------------------------------------------ - public static PasswordEncryptedEntry decode(DataInputStream in, char[] password) throws IOException @@ -132,49 +117,57 @@ public class PasswordEncryptedEntry extends MaskableEnvelopeEntry implements return entry; } - // Instance methods. - // ------------------------------------------------------------------------ - public void decrypt(char[] password) throws IllegalArgumentException, WrongPaddingException { - if (!isMasked() || payload == null) + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "decrypt"); + if (isMasked() && payload != null) { - return; - } - IMode mode = getMode(password, IMode.DECRYPTION); - IPad padding = PadFactory.getInstance("PKCS7"); - padding.init(mode.currentBlockSize()); - byte[] buf = new byte[payload.length]; - int count = 0; - for (int i = 0; i < payload.length; i++) - { - mode.update(payload, count, buf, count); - count += mode.currentBlockSize(); - } - int padlen = padding.unpad(buf, 0, buf.length); - DataInputStream in = new DataInputStream( - new ByteArrayInputStream( - buf, - 0, - buf.length - - padlen)); - try - { - decodeEnvelope(in); - } - catch (IOException ioe) - { - throw new IllegalArgumentException("decryption failed"); + long tt = -System.currentTimeMillis(); + IMode mode = getMode(password, IMode.DECRYPTION); + IPad padding = PadFactory.getInstance("PKCS7"); + padding.init(mode.currentBlockSize()); + byte[] buf = new byte[payload.length]; + int count = 0; + while (count + mode.currentBlockSize() <= payload.length) + { + mode.update(payload, count, buf, count); + count += mode.currentBlockSize(); + } + int padlen = padding.unpad(buf, 0, buf.length); + setMasked(false); + int len = buf.length - padlen; + ByteArrayInputStream baos = new ByteArrayInputStream(buf, 0, len); + DataInputStream in = new DataInputStream(baos); + try + { + decodeEnvelope(in); + } + catch (IOException ioe) + { + throw new IllegalArgumentException("decryption failed"); + } + tt += System.currentTimeMillis(); + log.fine("Decrypted in " + tt + "ms."); } - setMasked(false); - payload = null; + else if (Configuration.DEBUG) + log.fine("Skip decryption; " + (isMasked() ? "null payload" : "unmasked")); + if (Configuration.DEBUG) + log.exiting(this.getClass().getName(), "decrypt"); } public void encrypt(char[] password) throws IOException { + if (Configuration.DEBUG) + log.entering(this.getClass().getName(), "encrypt", String.valueOf(password)); + long tt = -System.currentTimeMillis(); + long t1 = -System.currentTimeMillis(); byte[] salt = new byte[8]; - new SecureRandom ().nextBytes (salt); + PRNG.getInstance().nextBytes(salt); + t1 += System.currentTimeMillis(); + if (Configuration.DEBUG) + log.fine("-- Generated salt in " + t1 + "ms."); properties.put("salt", Util.toString(salt)); IMode mode = getMode(password, IMode.ENCRYPTION); IPad pad = PadFactory.getInstance("PKCS7"); @@ -184,7 +177,13 @@ public class PasswordEncryptedEntry extends MaskableEnvelopeEntry implements for (Iterator it = entries.iterator(); it.hasNext();) { Entry entry = (Entry) it.next(); + if (Configuration.DEBUG) + log.fine("-- About to encode one " + entry); + t1 = -System.currentTimeMillis(); entry.encode(out2); + t1 += System.currentTimeMillis(); + if (Configuration.DEBUG) + log.fine("-- Encoded an Entry in " + t1 + "ms."); } byte[] plaintext = bout.toByteArray(); byte[] padding = pad.pad(plaintext, 0, plaintext.length); @@ -200,6 +199,13 @@ public class PasswordEncryptedEntry extends MaskableEnvelopeEntry implements count += mode.currentBlockSize(); } mode.update(lastBlock, 0, payload, count); + setMasked(true); + tt += System.currentTimeMillis(); + if (Configuration.DEBUG) + { + log.fine("Encrypted in " + tt + "ms."); + log.exiting(this.getClass().getName(), "encrypt"); + } } public void encode(DataOutputStream out, char[] password) throws IOException @@ -212,60 +218,46 @@ public class PasswordEncryptedEntry extends MaskableEnvelopeEntry implements { if (payload == null) { + if (Configuration.DEBUG) + log.fine("Null payload: " + this); throw new IllegalStateException("not encrypted"); } } - // Own methods. - // ------------------------------------------------------------------------ - private IMode getMode(char[] password, int state) { String s = properties.get("salt"); if (s == null) - { - throw new IllegalArgumentException("no salt"); - } + throw new IllegalArgumentException("no salt"); byte[] salt = Util.toBytesFromString(s); IBlockCipher cipher = CipherFactory.getInstance(properties.get("cipher")); if (cipher == null) - { - throw new IllegalArgumentException("no such cipher: " - + properties.get("cipher")); - } + throw new IllegalArgumentException("no such cipher: " + + properties.get("cipher")); int blockSize = cipher.defaultBlockSize(); if (properties.containsKey("block-size")) - { - try - { - blockSize = Integer.parseInt(properties.get("block-size")); - } - catch (NumberFormatException nfe) - { - throw new IllegalArgumentException("bad block size: " - + nfe.getMessage()); - } - } - IMode mode = ModeFactory.getInstance(properties.get("mode"), cipher, - blockSize); + try + { + blockSize = Integer.parseInt(properties.get("block-size")); + } + catch (NumberFormatException nfe) + { + throw new IllegalArgumentException("bad block size: " + + nfe.getMessage()); + } + String modeName = properties.get("mode"); + IMode mode = ModeFactory.getInstance(modeName, cipher, blockSize); if (mode == null) - { - throw new IllegalArgumentException("no such mode: " - + properties.get("mode")); - } - + throw new IllegalArgumentException("no such mode: " + modeName); HashMap pbAttr = new HashMap(); pbAttr.put(IPBE.PASSWORD, password); pbAttr.put(IPBE.SALT, salt); pbAttr.put(IPBE.ITERATION_COUNT, ITERATION_COUNT); IRandom kdf = PRNGFactory.getInstance("PBKDF2-HMAC-SHA"); kdf.init(pbAttr); - int keylen = 0; - if (!properties.containsKey("keylen")) - { - throw new IllegalArgumentException("no key length"); - } + if (! properties.containsKey("keylen")) + throw new IllegalArgumentException("no key length"); try { keylen = Integer.parseInt(properties.get("keylen")); @@ -286,7 +278,7 @@ public class PasswordEncryptedEntry extends MaskableEnvelopeEntry implements } HashMap modeAttr = new HashMap(); modeAttr.put(IMode.KEY_MATERIAL, dk); - modeAttr.put(IMode.STATE, new Integer(state)); + modeAttr.put(IMode.STATE, Integer.valueOf(state)); modeAttr.put(IMode.IV, iv); try { diff --git a/libjava/classpath/gnu/javax/crypto/keyring/PasswordProtectedEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/PasswordProtectedEntry.java index 0dcf73eb8d2..5eff637927a 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/PasswordProtectedEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/PasswordProtectedEntry.java @@ -43,21 +43,12 @@ import java.io.IOException; public interface PasswordProtectedEntry { - - // Constant. - // ------------------------------------------------------------------------ - - /** - * The iteration count for password-based KDFs. - */ - Integer ITERATION_COUNT = new Integer(1000); - - // Method. - // ------------------------------------------------------------------------ + /** The iteration count for password-based KDFs. */ + Integer ITERATION_COUNT = Integer.valueOf(1000); /** * Encodes this entry, protected by a password. - * + * * @param out The output stream to encode to. * @param password The password. * @throws IOException If an I/O error occurs. diff --git a/libjava/classpath/gnu/javax/crypto/keyring/PrimitiveEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/PrimitiveEntry.java index 4c9ff0ff1d9..194fe9eeec1 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/PrimitiveEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/PrimitiveEntry.java @@ -43,36 +43,24 @@ import java.util.Date; /** * A primitive entry is an entry that contains a single cryptographic entity. */ -public abstract class PrimitiveEntry extends Entry +public abstract class PrimitiveEntry + extends Entry { - - // Fields. - // ------------------------------------------------------------------------ - /** The creation date. */ protected Date creationDate; - // Constructor. - // ------------------------------------------------------------------------ - protected PrimitiveEntry(int type, Date creationDate, Properties properties) { super(type, properties); if (creationDate == null) - { - this.creationDate = new Date(); - } + this.creationDate = new Date(); else - { - this.creationDate = (Date) creationDate.clone(); - } - if (!this.properties.containsKey("alias") + this.creationDate = (Date) creationDate.clone(); + if (! this.properties.containsKey("alias") || this.properties.get("alias").length() == 0) - { - throw new IllegalArgumentException( - "primitive entries MUST have an alias"); - } - this.properties.put("creation-date", String.valueOf(creationDate.getTime())); + throw new IllegalArgumentException("primitive entries MUST have an alias"); + this.properties.put("creation-date", + String.valueOf(this.creationDate.getTime())); } protected PrimitiveEntry(int type) @@ -80,12 +68,9 @@ public abstract class PrimitiveEntry extends Entry super(type); } - // Instance method. - // ------------------------------------------------------------------------ - /** * Returns the alias of this primitive entry. - * + * * @return The alias. */ public String getAlias() @@ -95,7 +80,7 @@ public abstract class PrimitiveEntry extends Entry /** * Returns the creation date of this primitive entry. - * + * * @return The creation date. */ public Date getCreationDate() @@ -105,7 +90,7 @@ public abstract class PrimitiveEntry extends Entry public boolean equals(Object object) { - if (!getClass().equals(object.getClass())) + if (! getClass().equals(object.getClass())) return false; return getAlias().equals(((PrimitiveEntry) object).getAlias()); } @@ -114,9 +99,7 @@ public abstract class PrimitiveEntry extends Entry { String s = properties.get("creation-date"); if (s == null) - { - throw new MalformedKeyringException("no creation date"); - } + throw new MalformedKeyringException("no creation date"); try { creationDate = new Date(Long.parseLong(s)); diff --git a/libjava/classpath/gnu/javax/crypto/keyring/PrivateKeyEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/PrivateKeyEntry.java index 88249563341..23244afee8d 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/PrivateKeyEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/PrivateKeyEntry.java @@ -42,12 +42,10 @@ import gnu.java.security.key.IKeyPairCodec; import gnu.java.security.key.KeyPairCodecFactory; import gnu.java.security.key.dss.DSSPrivateKey; import gnu.java.security.key.rsa.GnuRSAPrivateKey; - import gnu.javax.crypto.key.GnuSecretKey; import gnu.javax.crypto.key.dh.GnuDHPrivateKey; import java.io.DataInputStream; -import java.io.DataOutputStream; import java.io.IOException; import java.security.Key; import java.security.KeyFactory; @@ -56,25 +54,18 @@ import java.security.spec.PKCS8EncodedKeySpec; import java.util.Date; /** - * <p>An immutable class representing a private or secret key entry.</p> + * An immutable class representing a private or secret key entry. */ -public final class PrivateKeyEntry extends PrimitiveEntry +public final class PrivateKeyEntry + extends PrimitiveEntry { - - // Constants and variables - // ------------------------------------------------------------------------- - public static final int TYPE = 7; - /** The key. */ private Key key; - // Constructor(s) - // ------------------------------------------------------------------------- - /** - * <p>Creates a new key entry.</p> - * + * Creates a new key entry. + * * @param key The key. * @param creationDate The entry creation date. * @param properties The entry properties. @@ -83,15 +74,10 @@ public final class PrivateKeyEntry extends PrimitiveEntry public PrivateKeyEntry(Key key, Date creationDate, Properties properties) { super(TYPE, creationDate, properties); - if (key == null) - { - throw new IllegalArgumentException("no private key"); - } - if (!(key instanceof PrivateKey) && !(key instanceof GnuSecretKey)) - { - throw new IllegalArgumentException("not a private or secret key"); - } + throw new IllegalArgumentException("no private key"); + if (! (key instanceof PrivateKey) && ! (key instanceof GnuSecretKey)) + throw new IllegalArgumentException("not a private or secret key"); this.key = key; } @@ -100,18 +86,13 @@ public final class PrivateKeyEntry extends PrimitiveEntry super(TYPE); } - // Class methods - // ------------------------------------------------------------------------- - public static PrivateKeyEntry decode(DataInputStream in) throws IOException { PrivateKeyEntry entry = new PrivateKeyEntry(); entry.defaultDecode(in); String type = entry.properties.get("type"); if (type == null) - { - throw new MalformedKeyringException("no key type"); - } + throw new MalformedKeyringException("no key type"); if (type.equalsIgnoreCase("RAW-DSS")) { IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dss"); @@ -128,18 +109,16 @@ public final class PrivateKeyEntry extends PrimitiveEntry entry.key = coder.decodePrivateKey(entry.payload); } else if (type.equalsIgnoreCase("RAW")) - { - entry.key = new GnuSecretKey(entry.payload, null); - } + entry.key = new GnuSecretKey(entry.payload, null); else if (type.equalsIgnoreCase("PKCS8")) { try { KeyFactory kf = KeyFactory.getInstance("RSA"); - entry.key = kf.generatePrivate(new PKCS8EncodedKeySpec( - entry.payload)); + PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(entry.payload); + entry.key = kf.generatePrivate(ks); } - catch (Exception x) + catch (Exception ignored) { } if (entry.key == null) @@ -147,32 +126,24 @@ public final class PrivateKeyEntry extends PrimitiveEntry try { KeyFactory kf = KeyFactory.getInstance("DSA"); - entry.key = kf.generatePrivate(new PKCS8EncodedKeySpec( - entry.payload)); + PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(entry.payload); + entry.key = kf.generatePrivate(ks); } - catch (Exception x) + catch (Exception ignored) { } if (entry.key == null) - { - throw new MalformedKeyringException( - "could not decode PKCS#8 key"); - } + throw new MalformedKeyringException("could not decode PKCS#8 key"); } } else - { - throw new MalformedKeyringException("unsupported key type " + type); - } + throw new MalformedKeyringException("unsupported key type " + type); return entry; } - // Instance methods - // ------------------------------------------------------------------------- - /** - * <p>Returns this entry's key.</p> - * + * Returns this entry's key. + * * @return The key. */ public Key getKey() @@ -212,8 +183,12 @@ public final class PrivateKeyEntry extends PrimitiveEntry payload = key.getEncoded(); } else - { - throw new IllegalArgumentException("unsupported private key"); - } + throw new IllegalArgumentException("unsupported private key"); + } + + public String toString() + { + return "PrivateKeyEntry{key=" + + (key == null ? "-" : key.getClass().getName()) + "}"; } } diff --git a/libjava/classpath/gnu/javax/crypto/keyring/Properties.java b/libjava/classpath/gnu/javax/crypto/keyring/Properties.java index 646b5711df2..b833a74aeab 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/Properties.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/Properties.java @@ -42,27 +42,20 @@ import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; - import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * A set of <code>(name => value)</code> pairs used in keyring entries. - * Keys and values are simple strings, with the key never being empty and - * always treated case-insensitively. + * Keys and values are simple strings, with the key never being empty and always + * treated case-insensitively. */ -public class Properties implements Cloneable +public class Properties + implements Cloneable { - - // Field. - // ------------------------------------------------------------------------ - private HashMap props; - // Constructor. - // ------------------------------------------------------------------------ - /** * Creates a new properties object. */ @@ -71,9 +64,6 @@ public class Properties implements Cloneable props = new HashMap(); } - // Instance methods. - // ------------------------------------------------------------------------ - /** * Removes all properties from this object. */ @@ -84,7 +74,7 @@ public class Properties implements Cloneable /** * Creates a copy of this properties object. - * + * * @return The copy. */ public Object clone() @@ -96,86 +86,75 @@ public class Properties implements Cloneable /** * Tests if this object contains a given property name. - * + * * @param key The key to test. * @return True if this object contains the given key. */ public boolean containsKey(String key) { if (key == null || key.length() == 0) - { - return false; - } + return false; return props.containsKey(canonicalize(key)); } /** * Tests if this object contains a given property value. - * + * * @param value The value to test. * @return True if this object contains the given value. */ public boolean containsValue(String value) { if (value == null) - { - return false; - } + return false; return props.containsValue(value); } /** * Adds a new property to this object. - * + * * @param key The key, which can neither be null nor empty. * @param value The value, which cannot be null. * @return The old value mapped by the key, if any. - * @throws IllegalArgumentException If either the key or value parameter - - * is null, or if the key is empty. + * @throws IllegalArgumentException If either the key or value parameter is + * null, or if the key is empty. */ public String put(String key, String value) { if (key == null || value == null || key.length() == 0) - { - throw new IllegalArgumentException("key nor value can be null"); - } + throw new IllegalArgumentException("key nor value can be null"); return (String) props.put(canonicalize(key), value); } /** - * Returns the value mapped by the given key, or null if there is no - * such mapping. - * + * Returns the value mapped by the given key, or null if there is no such + * mapping. + * * @param key */ public String get(String key) { if (key == null || key.length() == 0) - { - return null; - } + return null; return (String) props.get(canonicalize(key)); } /** * Removes a key and its value from this object. - * + * * @param key The key of the property to remove. * @return The old value mapped by the key, if any. */ public String remove(String key) { if (key == null || key.length() == 0) - { - return null; - } + return null; return (String) props.remove(canonicalize(key)); } /** * Decodes a set of properties from the given input stream. - * + * * @param in The input stream. * @throws IOException If an I/O error occurs. */ @@ -184,7 +163,7 @@ public class Properties implements Cloneable int len = in.readInt(); MeteredInputStream min = new MeteredInputStream(in, len); DataInputStream in2 = new DataInputStream(min); - while (!min.limitReached()) + while (! min.limitReached()) { String name = in2.readUTF(); String value = in2.readUTF(); @@ -194,7 +173,7 @@ public class Properties implements Cloneable /** * Encodes this set of properties to the given output stream. - * + * * @param out The output stream to encode to. * @throws IOException If an I/O error occurs. */ @@ -217,9 +196,6 @@ public class Properties implements Cloneable return props.toString(); } - // Own methods. - // ------------------------------------------------------------------------ - private String canonicalize(String key) { return key.toLowerCase(); diff --git a/libjava/classpath/gnu/javax/crypto/keyring/PublicKeyEntry.java b/libjava/classpath/gnu/javax/crypto/keyring/PublicKeyEntry.java index 528e70cc648..3e7f54d3e20 100644 --- a/libjava/classpath/gnu/javax/crypto/keyring/PublicKeyEntry.java +++ b/libjava/classpath/gnu/javax/crypto/keyring/PublicKeyEntry.java @@ -38,43 +38,30 @@ exception statement from your version. */ package gnu.javax.crypto.keyring; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import java.security.PublicKey; -import java.security.KeyFactory; -import java.security.spec.X509EncodedKeySpec; - -import java.util.Date; - import gnu.java.security.key.IKeyPairCodec; import gnu.java.security.key.KeyPairCodecFactory; import gnu.java.security.key.dss.DSSPublicKey; import gnu.java.security.key.rsa.GnuRSAPublicKey; import gnu.javax.crypto.key.dh.GnuDHPublicKey; -public final class PublicKeyEntry extends PrimitiveEntry -{ - - // Constants and fields. - // ------------------------------------------------------------------------ +import java.io.DataInputStream; +import java.io.IOException; +import java.security.KeyFactory; +import java.security.PublicKey; +import java.security.spec.X509EncodedKeySpec; +import java.util.Date; +public final class PublicKeyEntry + extends PrimitiveEntry +{ public static final int TYPE = 6; - private PublicKey key; - // Constructor. - // ------------------------------------------------------------------------ - public PublicKeyEntry(PublicKey key, Date creationDate, Properties properties) { super(TYPE, creationDate, properties); - if (key == null) - { - throw new IllegalArgumentException("no key specified"); - } + throw new IllegalArgumentException("no key specified"); this.key = key; } @@ -83,18 +70,13 @@ public final class PublicKeyEntry extends PrimitiveEntry super(TYPE); } - // Class method. - // ------------------------------------------------------------------------ - public static PublicKeyEntry decode(DataInputStream in) throws IOException { PublicKeyEntry entry = new PublicKeyEntry(); entry.defaultDecode(in); String type = entry.properties.get("type"); if (type == null) - { - throw new MalformedKeyringException("no key type"); - } + throw new MalformedKeyringException("no key type"); if (type.equalsIgnoreCase("RAW-DSS")) { IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dss"); @@ -125,33 +107,23 @@ public final class PublicKeyEntry extends PrimitiveEntry try { KeyFactory kf = KeyFactory.getInstance("DSA"); - entry.key = kf.generatePublic(new X509EncodedKeySpec( - entry.payload)); + entry.key = kf.generatePublic(new X509EncodedKeySpec(entry.payload)); } catch (Exception x) { } if (entry.key == null) - { - throw new MalformedKeyringException( - "could not decode X.509 key"); - } + throw new MalformedKeyringException("could not decode X.509 key"); } } else - { - throw new MalformedKeyringException("unsupported public key type: " - + type); - } + throw new MalformedKeyringException("unsupported public key type: " + type); return entry; } - // Instance methods. - // ------------------------------------------------------------------------ - /** * Returns the public key. - * + * * @return The public key. */ public PublicKey getKey() @@ -185,8 +157,6 @@ public final class PublicKeyEntry extends PrimitiveEntry payload = key.getEncoded(); } else - { - throw new IllegalArgumentException("cannot encode public key"); - } + throw new IllegalArgumentException("cannot encode public key"); } } |