Source for gnu.javax.crypto.keyring.EncryptedEntry

   1: /* EncryptedEntry.java --
   2:    Copyright (C) 2003, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.javax.crypto.keyring;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.javax.crypto.cipher.CipherFactory;
  43: import gnu.javax.crypto.cipher.IBlockCipher;
  44: import gnu.javax.crypto.mode.IMode;
  45: import gnu.javax.crypto.mode.ModeFactory;
  46: import gnu.javax.crypto.pad.IPad;
  47: import gnu.javax.crypto.pad.PadFactory;
  48: import gnu.javax.crypto.pad.WrongPaddingException;
  49: 
  50: import java.io.ByteArrayInputStream;
  51: import java.io.ByteArrayOutputStream;
  52: import java.io.DataInputStream;
  53: import java.io.DataOutputStream;
  54: import java.io.IOException;
  55: import java.security.InvalidKeyException;
  56: import java.util.HashMap;
  57: import java.util.Iterator;
  58: 
  59: public class EncryptedEntry extends MaskableEnvelopeEntry implements Registry
  60: {
  61:   public static final int TYPE = 0;
  62: 
  63:   public EncryptedEntry(String cipher, String mode, Properties properties)
  64:   {
  65:     super(TYPE, properties);
  66:     if (cipher == null || mode == null)
  67:       throw new IllegalArgumentException("neither cipher nor mode can be null");
  68:     properties.put("cipher", cipher);
  69:     properties.put("mode", mode);
  70:     setMasked(false);
  71:   }
  72: 
  73:   private EncryptedEntry()
  74:   {
  75:     super(TYPE, new Properties());
  76:     setMasked(true);
  77:   }
  78: 
  79:   public static EncryptedEntry decode(DataInputStream in) throws IOException
  80:   {
  81:     EncryptedEntry entry = new EncryptedEntry();
  82:     entry.defaultDecode(in);
  83:     if (! entry.properties.containsKey("cipher"))
  84:       throw new MalformedKeyringException("no cipher");
  85:     if (! entry.properties.containsKey("cipher"))
  86:       throw new MalformedKeyringException("no cipher");
  87:     return entry;
  88:   }
  89: 
  90:   public void decrypt(byte[] key, byte[] iv) throws IllegalArgumentException,
  91:       WrongPaddingException
  92:   {
  93:     if (! isMasked() || payload == null)
  94:       return;
  95:     IMode mode = getMode(key, iv, IMode.DECRYPTION);
  96:     IPad padding = null;
  97:     padding = PadFactory.getInstance("PKCS7");
  98:     padding.init(mode.currentBlockSize());
  99:     byte[] buf = new byte[payload.length];
 100:     int count = 0;
 101:     for (int i = 0; i < payload.length; i++)
 102:       {
 103:         mode.update(payload, count, buf, count);
 104:         count += mode.currentBlockSize();
 105:       }
 106:     int padlen = padding.unpad(buf, 0, buf.length);
 107:     int len = buf.length - padlen;
 108:     DataInputStream in = new DataInputStream(new ByteArrayInputStream(buf, 0, len));
 109:     try
 110:       {
 111:         decodeEnvelope(in);
 112:       }
 113:     catch (IOException ioe)
 114:       {
 115:         throw new IllegalArgumentException("decryption failed");
 116:       }
 117:     setMasked(false);
 118:     payload = null;
 119:   }
 120: 
 121:   public void encrypt(byte[] key, byte[] iv) throws IOException
 122:   {
 123:     IMode mode = getMode(key, iv, IMode.ENCRYPTION);
 124:     IPad pad = PadFactory.getInstance("PKCS7");
 125:     pad.init(mode.currentBlockSize());
 126:     ByteArrayOutputStream bout = new ByteArrayOutputStream(1024);
 127:     DataOutputStream out2 = new DataOutputStream(bout);
 128:     for (Iterator it = entries.iterator(); it.hasNext();)
 129:       {
 130:         Entry entry = (Entry) it.next();
 131:         entry.encode(out2);
 132:       }
 133:     byte[] plaintext = bout.toByteArray();
 134:     byte[] padding = pad.pad(plaintext, 0, plaintext.length);
 135:     payload = new byte[plaintext.length + padding.length];
 136:     byte[] lastBlock = new byte[mode.currentBlockSize()];
 137:     int l = mode.currentBlockSize() - padding.length;
 138:     System.arraycopy(plaintext, plaintext.length - l, lastBlock, 0, l);
 139:     System.arraycopy(padding, 0, lastBlock, l, padding.length);
 140:     int count = 0;
 141:     while (count + mode.currentBlockSize() < plaintext.length)
 142:       {
 143:         mode.update(plaintext, count, payload, count);
 144:         count += mode.currentBlockSize();
 145:       }
 146:     mode.update(lastBlock, 0, payload, count);
 147:   }
 148: 
 149:   public void encodePayload() throws IOException
 150:   {
 151:     if (payload == null)
 152:       throw new IOException("not encrypted");
 153:   }
 154: 
 155:   private IMode getMode(byte[] key, byte[] iv, int state)
 156:   {
 157:     IBlockCipher cipher = CipherFactory.getInstance(properties.get("cipher"));
 158:     if (cipher == null)
 159:       throw new IllegalArgumentException("no such cipher: " + properties.get("cipher"));
 160:     int blockSize = cipher.defaultBlockSize();
 161:     if (properties.containsKey("block-size"))
 162:       {
 163:         try
 164:           {
 165:             blockSize = Integer.parseInt(properties.get("block-size"));
 166:           }
 167:         catch (NumberFormatException nfe)
 168:           {
 169:             throw new IllegalArgumentException("bad block size: "
 170:                                                + nfe.getMessage());
 171:           }
 172:       }
 173:     IMode mode = ModeFactory.getInstance(properties.get("mode"), cipher, blockSize);
 174:     if (mode == null)
 175:       throw new IllegalArgumentException("no such mode: " + properties.get("mode"));
 176: 
 177:     HashMap modeAttr = new HashMap();
 178:     modeAttr.put(IMode.KEY_MATERIAL, key);
 179:     modeAttr.put(IMode.STATE, Integer.valueOf(state));
 180:     modeAttr.put(IMode.IV, iv);
 181:     try
 182:       {
 183:         mode.init(modeAttr);
 184:       }
 185:     catch (InvalidKeyException ike)
 186:       {
 187:         throw new IllegalArgumentException(ike.toString());
 188:       }
 189:     return mode;
 190:   }
 191: }