/uClinux-dist/lib/classpath/gnu/javax/net/ssl/provider/InputSecurityParameters.java

https://bitbucket.org/__wp__/mb-linux-msli · Java · 334 lines · 234 code · 27 blank · 73 comment · 48 complexity · 6eccd7cb7cf11a6365405744b8e60f40 MD5 · raw file

  1. /* SecurityParameters.java -- SSL security parameters.
  2. Copyright (C) 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.net.ssl.provider;
  32. import gnu.classpath.debug.Component;
  33. import gnu.classpath.debug.SystemLogger;
  34. import gnu.java.security.util.ByteArray;
  35. import gnu.java.security.util.ByteBufferOutputStream;
  36. import java.nio.BufferOverflowException;
  37. import java.nio.ByteBuffer;
  38. import java.util.Arrays;
  39. import java.util.zip.DataFormatException;
  40. import java.util.zip.Inflater;
  41. import javax.crypto.Cipher;
  42. import javax.crypto.IllegalBlockSizeException;
  43. import javax.crypto.Mac;
  44. import javax.crypto.ShortBufferException;
  45. import javax.net.ssl.SSLException;
  46. public class InputSecurityParameters
  47. {
  48. private static final SystemLogger logger = SystemLogger.SYSTEM;
  49. private final Cipher cipher;
  50. private final Mac mac;
  51. private final Inflater inflater;
  52. private SessionImpl session;
  53. private final CipherSuite suite;
  54. private long sequence;
  55. public InputSecurityParameters (final Cipher cipher, final Mac mac,
  56. final Inflater inflater,
  57. final SessionImpl session,
  58. final CipherSuite suite)
  59. {
  60. this.cipher = cipher;
  61. this.mac = mac;
  62. this.inflater = inflater;
  63. this.session = session;
  64. this.suite = suite;
  65. sequence = 0;
  66. }
  67. /**
  68. * Decrypt a record, storing the decrypted fragment into the given array
  69. * of byte buffers.
  70. *
  71. * @param record The input record.
  72. * @param output The output buffers.
  73. * @param offset The offset of the first buffer to use.
  74. * @param length The number of buffers to use.
  75. * @return The number of bytes put in the output buffers.
  76. * @throws DataFormatException If decompression fails.
  77. * @throws IllegalBlockSizeException If the current cipher is a block cipher,
  78. * and the input fragment is not a multiple of the block size.
  79. * @throws MacException If verifying the MAC fails.
  80. * @throws SSLException ???
  81. * @throws ShortBufferException
  82. */
  83. public int decrypt(Record record, ByteBuffer[] output, int offset, int length)
  84. throws DataFormatException, IllegalBlockSizeException,
  85. MacException, SSLException, ShortBufferException
  86. {
  87. return decrypt(record, output, offset, length, null);
  88. }
  89. /**
  90. * Decrypt a record, storing the decrypted fragment into the given growable
  91. * buffer.
  92. *
  93. * @param record The input record.
  94. * @param outputStream The output buffer.
  95. * @return The number of bytes put into the output buffer.
  96. * @throws DataFormatException
  97. * @throws IllegalBlockSizeException
  98. * @throws MacException
  99. * @throws SSLException
  100. * @throws ShortBufferException
  101. */
  102. public int decrypt(Record record, ByteBufferOutputStream outputStream)
  103. throws DataFormatException, IllegalBlockSizeException,
  104. MacException, SSLException, ShortBufferException
  105. {
  106. return decrypt(record, null, 0, 0, outputStream);
  107. }
  108. private int decrypt(Record record, ByteBuffer[] output, int offset, int length,
  109. ByteBufferOutputStream outputStream)
  110. throws DataFormatException, IllegalBlockSizeException,
  111. MacException, SSLException, ShortBufferException
  112. {
  113. boolean badPadding = false;
  114. ByteBuffer fragment;
  115. if (cipher != null)
  116. {
  117. ByteBuffer input = record.fragment();
  118. fragment = ByteBuffer.allocate(input.remaining());
  119. cipher.update(input, fragment);
  120. }
  121. else
  122. fragment = record.fragment();
  123. if (Debug.DEBUG_DECRYPTION)
  124. logger.logv(Component.SSL_RECORD_LAYER, "decrypted fragment:\n{0}",
  125. Util.hexDump((ByteBuffer) fragment.duplicate().position(0), " >> "));
  126. int fragmentLength = record.length();
  127. int maclen = 0;
  128. if (mac != null)
  129. maclen = mac.getMacLength();
  130. fragmentLength -= maclen;
  131. int padlen = 0;
  132. int padRemoveLen = 0;
  133. if (!suite.isStreamCipher ())
  134. {
  135. padlen = fragment.get(record.length() - 1) & 0xFF;
  136. padRemoveLen = padlen + 1;
  137. if (Debug.DEBUG)
  138. logger.logv(Component.SSL_RECORD_LAYER, "padlen:{0}", padlen);
  139. if (record.version() == ProtocolVersion.SSL_3)
  140. {
  141. // In SSLv3, the padding length must not be larger than
  142. // the cipher's block size.
  143. if (padlen > cipher.getBlockSize ())
  144. badPadding = true;
  145. }
  146. else if (record.version().compareTo(ProtocolVersion.TLS_1) >= 0)
  147. {
  148. // In TLSv1 and later, the padding must be `padlen' copies of the
  149. // value `padlen'.
  150. byte[] pad = new byte[padlen];
  151. ((ByteBuffer) fragment.duplicate().position(record.length() - padlen - 1)).get(pad);
  152. for (int i = 0; i < pad.length; i++)
  153. if ((pad[i] & 0xFF) != padlen)
  154. badPadding = true;
  155. if (Debug.DEBUG)
  156. logger.logv(Component.SSL_RECORD_LAYER, "TLSv1.x padding\n{0}",
  157. new ByteArray(pad));
  158. }
  159. if (Debug.DEBUG)
  160. logger.logv(Component.SSL_RECORD_LAYER, "padding bad? {0}",
  161. badPadding);
  162. if (!badPadding)
  163. fragmentLength = fragmentLength - padRemoveLen;
  164. }
  165. int ivlen = 0;
  166. if (session.version.compareTo(ProtocolVersion.TLS_1_1) >= 0
  167. && !suite.isStreamCipher())
  168. ivlen = cipher.getBlockSize();
  169. // Compute and check the MAC.
  170. if (mac != null)
  171. {
  172. mac.update((byte) (sequence >>> 56));
  173. mac.update((byte) (sequence >>> 48));
  174. mac.update((byte) (sequence >>> 40));
  175. mac.update((byte) (sequence >>> 32));
  176. mac.update((byte) (sequence >>> 24));
  177. mac.update((byte) (sequence >>> 16));
  178. mac.update((byte) (sequence >>> 8));
  179. mac.update((byte) sequence);
  180. mac.update((byte) record.getContentType().getValue());
  181. ProtocolVersion version = record.version();
  182. if (version != ProtocolVersion.SSL_3)
  183. {
  184. mac.update((byte) version.major());
  185. mac.update((byte) version.minor());
  186. }
  187. mac.update((byte) ((fragmentLength - ivlen) >>> 8));
  188. mac.update((byte) (fragmentLength - ivlen));
  189. ByteBuffer content =
  190. (ByteBuffer) fragment.duplicate().position(ivlen).limit(fragmentLength);
  191. mac.update(content);
  192. byte[] mac1 = mac.doFinal ();
  193. byte[] mac2 = new byte[maclen];
  194. mac.reset();
  195. ((ByteBuffer) fragment.duplicate().position(fragmentLength)).get(mac2);
  196. if (Debug.DEBUG)
  197. logger.logv(Component.SSL_RECORD_LAYER, "mac1:{0} mac2:{1}",
  198. Util.toHexString(mac1, ':'), Util.toHexString(mac2, ':'));
  199. if (!Arrays.equals (mac1, mac2))
  200. badPadding = true;
  201. }
  202. // We always say "bad MAC" and not "bad padding," because saying
  203. // the latter will leak information to an attacker.
  204. if (badPadding)
  205. throw new MacException ();
  206. // Inflate the compressed bytes.
  207. int produced = 0;
  208. if (inflater != null)
  209. {
  210. ByteBufferOutputStream out = new ByteBufferOutputStream(fragmentLength);
  211. byte[] inbuffer = new byte[1024];
  212. byte[] outbuffer = new byte[1024];
  213. boolean done = false;
  214. if (record.version().compareTo(ProtocolVersion.TLS_1_1) >= 0
  215. && !suite.isStreamCipher())
  216. fragment.position (cipher.getBlockSize());
  217. else
  218. fragment.position(0);
  219. fragment.limit(fragmentLength);
  220. while (!done)
  221. {
  222. int l;
  223. if (inflater.needsInput())
  224. {
  225. l = Math.min(inbuffer.length, fragment.remaining());
  226. fragment.get(inbuffer, 0, l);
  227. inflater.setInput(inbuffer);
  228. }
  229. l = inflater.inflate(outbuffer);
  230. out.write(outbuffer, 0, l);
  231. done = !fragment.hasRemaining() && inflater.finished();
  232. }
  233. ByteBuffer outbuf = out.buffer();
  234. if (outputStream != null)
  235. {
  236. byte[] buf = new byte[1024];
  237. while (outbuf.hasRemaining())
  238. {
  239. int l = Math.min(outbuf.remaining(), buf.length);
  240. outbuf.get(buf, 0, l);
  241. outputStream.write(buf, 0, l);
  242. produced += l;
  243. }
  244. }
  245. else
  246. {
  247. int i = offset;
  248. while (outbuf.hasRemaining() && i < offset + length)
  249. {
  250. int l = Math.min(output[i].remaining(), outbuf.remaining());
  251. ByteBuffer b = (ByteBuffer)
  252. outbuf.duplicate().limit(outbuf.position() + l);
  253. output[i++].put(b);
  254. outbuf.position(outbuf.position() + l);
  255. produced += l;
  256. }
  257. if (outbuf.hasRemaining())
  258. throw new BufferOverflowException();
  259. }
  260. }
  261. else
  262. {
  263. ByteBuffer outbuf = (ByteBuffer)
  264. fragment.duplicate().position(0).limit(record.length() - maclen - padRemoveLen);
  265. if (record.version().compareTo(ProtocolVersion.TLS_1_1) >= 0
  266. && !suite.isStreamCipher())
  267. outbuf.position(cipher.getBlockSize());
  268. if (outputStream != null)
  269. {
  270. byte[] buf = new byte[1024];
  271. while (outbuf.hasRemaining())
  272. {
  273. int l = Math.min(outbuf.remaining(), buf.length);
  274. outbuf.get(buf, 0, l);
  275. outputStream.write(buf, 0, l);
  276. produced += l;
  277. }
  278. }
  279. else
  280. {
  281. int i = offset;
  282. while (outbuf.hasRemaining() && i < offset + length)
  283. {
  284. int l = Math.min(output[i].remaining(), outbuf.remaining());
  285. ByteBuffer b = (ByteBuffer) outbuf.duplicate().limit(outbuf.position() + l);
  286. output[i++].put(b);
  287. outbuf.position(outbuf.position() + l);
  288. produced += l;
  289. }
  290. if (outbuf.hasRemaining())
  291. throw new BufferOverflowException();
  292. }
  293. }
  294. sequence++;
  295. return produced;
  296. }
  297. CipherSuite cipherSuite ()
  298. {
  299. return suite;
  300. }
  301. }