AuthenticatedEntry.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* AuthenticatedEntry.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.mac.IMac;
  34. import gnu.javax.crypto.mac.MacFactory;
  35. import gnu.javax.crypto.mac.MacOutputStream;
  36. import java.io.ByteArrayInputStream;
  37. import java.io.ByteArrayOutputStream;
  38. import java.io.DataInputStream;
  39. import java.io.DataOutputStream;
  40. import java.io.IOException;
  41. import java.security.InvalidKeyException;
  42. import java.util.Arrays;
  43. import java.util.HashMap;
  44. import java.util.Iterator;
  45. public final class AuthenticatedEntry
  46. extends MaskableEnvelopeEntry
  47. implements Registry
  48. {
  49. public static final int TYPE = 2;
  50. public AuthenticatedEntry(String mac, int macLen, Properties properties)
  51. {
  52. super(TYPE, properties);
  53. if (macLen <= 0)
  54. throw new IllegalArgumentException("invalid mac length");
  55. this.properties.put("mac", mac);
  56. this.properties.put("maclen", String.valueOf(macLen));
  57. setMasked(false);
  58. }
  59. private AuthenticatedEntry()
  60. {
  61. super(TYPE);
  62. setMasked(true);
  63. }
  64. public static AuthenticatedEntry decode(DataInputStream in)
  65. throws IOException
  66. {
  67. AuthenticatedEntry entry = new AuthenticatedEntry();
  68. entry.properties.decode(in);
  69. if (! entry.properties.containsKey("mac"))
  70. throw new MalformedKeyringException("no mac specified");
  71. if (! entry.properties.containsKey("maclen"))
  72. throw new MalformedKeyringException("no mac length specified");
  73. return entry;
  74. }
  75. /**
  76. * Computes the mac over this envelope's data. This method <b>must</b> be
  77. * called before this entry in encoded.
  78. *
  79. * @param key The key to authenticate with.
  80. * @throws IOException If encoding fails.
  81. * @throws InvalidKeyException If the supplied key is bad.
  82. */
  83. public void authenticate(byte[] key) throws IOException, InvalidKeyException
  84. {
  85. if (isMasked())
  86. throw new IllegalStateException("entry is masked");
  87. IMac m = getMac(key);
  88. ByteArrayOutputStream bout = new ByteArrayOutputStream(1024);
  89. MacOutputStream macout = new MacOutputStream(bout, m);
  90. DataOutputStream out2 = new DataOutputStream(macout);
  91. for (Iterator it = entries.iterator(); it.hasNext();)
  92. {
  93. Entry entry = (Entry) it.next();
  94. entry.encode(out2);
  95. }
  96. bout.write(m.digest());
  97. payload = bout.toByteArray();
  98. }
  99. /**
  100. * Verifies this entry's payload. This method will unmask this entry, thus it
  101. * must be called before accessing its contents.
  102. *
  103. * @param key The key to use to authenticate.
  104. * @throws InvalidKeyException If the given key is improper.
  105. */
  106. public void verify(byte[] key) throws InvalidKeyException
  107. {
  108. if (! isMasked() || payload == null)
  109. return;
  110. IMac m = getMac(key);
  111. m.update(payload, 0, payload.length - m.macSize());
  112. byte[] macValue = new byte[m.macSize()];
  113. System.arraycopy(payload, payload.length - macValue.length, macValue, 0,
  114. macValue.length);
  115. if (! Arrays.equals(macValue, m.digest()))
  116. throw new IllegalArgumentException("MAC verification failed");
  117. try
  118. {
  119. int len = payload.length - m.macSize();
  120. ByteArrayInputStream bais = new ByteArrayInputStream(payload, 0, len);
  121. DataInputStream in = new DataInputStream(bais);
  122. decodeEnvelope(in);
  123. }
  124. catch (IOException ioe)
  125. {
  126. throw new IllegalArgumentException("malformed keyring fragment");
  127. }
  128. setMasked(false);
  129. payload = null;
  130. }
  131. protected void encodePayload() throws IOException
  132. {
  133. if (payload == null)
  134. throw new IllegalStateException("not authenticated");
  135. }
  136. private IMac getMac(byte[] key) throws InvalidKeyException
  137. {
  138. IMac mac = MacFactory.getInstance(properties.get("mac"));
  139. if (mac == null)
  140. throw new IllegalArgumentException("no such mac: " + properties.get("mac"));
  141. int maclen = 0;
  142. if (! properties.containsKey("maclen"))
  143. throw new IllegalArgumentException("no MAC length");
  144. try
  145. {
  146. maclen = Integer.parseInt(properties.get("maclen"));
  147. }
  148. catch (NumberFormatException nfe)
  149. {
  150. throw new IllegalArgumentException("bad MAC length");
  151. }
  152. HashMap macAttr = new HashMap();
  153. macAttr.put(IMac.MAC_KEY_MATERIAL, key);
  154. macAttr.put(IMac.TRUNCATED_SIZE, Integer.valueOf(maclen));
  155. mac.init(macAttr);
  156. return mac;
  157. }
  158. }