EncryptedEntry.java 6.5 KB

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