J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.security.jgss.krb5; |
| 27 | |
| 28 | import org.ietf.jgss.*; |
| 29 | import java.io.InputStream; |
| 30 | import java.io.OutputStream; |
| 31 | import java.io.IOException; |
| 32 | import java.io.ByteArrayInputStream; |
| 33 | import java.io.ByteArrayOutputStream; |
| 34 | |
| 35 | /** |
| 36 | * This class represents the new format of GSS MIC tokens, as specified |
| 37 | * in draft-ietf-krb-wg-gssapi-cfx-07.txt |
| 38 | * |
| 39 | * MIC tokens = { 16-byte token-header | HMAC } |
| 40 | * where HMAC is on { plaintext | 16-byte token-header } |
| 41 | * |
| 42 | * @author Seema Malkani |
| 43 | */ |
| 44 | |
| 45 | class MicToken_v2 extends MessageToken_v2 { |
| 46 | |
| 47 | public MicToken_v2(Krb5Context context, |
| 48 | byte[] tokenBytes, int tokenOffset, int tokenLen, |
| 49 | MessageProp prop) throws GSSException { |
| 50 | super(Krb5Token.MIC_ID_v2, context, |
| 51 | tokenBytes, tokenOffset, tokenLen, prop); |
| 52 | } |
| 53 | |
| 54 | public MicToken_v2(Krb5Context context, |
| 55 | InputStream is, MessageProp prop) |
| 56 | throws GSSException { |
| 57 | super(Krb5Token.MIC_ID_v2, context, is, prop); |
| 58 | } |
| 59 | |
| 60 | public void verify(byte[] data, int offset, int len) throws GSSException { |
| 61 | if (!verifySign(data, offset, len)) |
| 62 | throw new GSSException(GSSException.BAD_MIC, -1, |
| 63 | "Corrupt checksum or sequence number in MIC token"); |
| 64 | } |
| 65 | |
| 66 | public void verify(InputStream data) throws GSSException { |
| 67 | |
| 68 | byte[] dataBytes = null; |
| 69 | try { |
| 70 | dataBytes = new byte[data.available()]; |
| 71 | data.read(dataBytes); |
| 72 | } catch (IOException e) { |
| 73 | // Error reading application data |
| 74 | throw new GSSException(GSSException.BAD_MIC, -1, |
| 75 | "Corrupt checksum or sequence number in MIC token"); |
| 76 | } |
| 77 | verify(dataBytes, 0, dataBytes.length); |
| 78 | } |
| 79 | |
| 80 | public MicToken_v2(Krb5Context context, MessageProp prop, |
| 81 | byte[] data, int pos, int len) |
| 82 | throws GSSException { |
| 83 | super(Krb5Token.MIC_ID_v2, context); |
| 84 | |
| 85 | // debug("Application data to MicToken verify is [" + |
| 86 | // getHexBytes(data, pos, len) + "]\n"); |
| 87 | if (prop == null) prop = new MessageProp(0, false); |
| 88 | genSignAndSeqNumber(prop, data, pos, len); |
| 89 | } |
| 90 | |
| 91 | public MicToken_v2(Krb5Context context, MessageProp prop, InputStream data) |
| 92 | throws GSSException, IOException { |
| 93 | |
| 94 | super(Krb5Token.MIC_ID_v2, context); |
| 95 | byte[] dataBytes = new byte[data.available()]; |
| 96 | data.read(dataBytes); |
| 97 | |
| 98 | // debug("Application data to MicToken cons is [" + |
| 99 | // getHexBytes(dataBytes) + "]\n"); |
| 100 | if (prop == null) prop = new MessageProp(0, false); |
| 101 | genSignAndSeqNumber(prop, dataBytes, 0, dataBytes.length); |
| 102 | } |
| 103 | |
| 104 | public int encode(byte[] outToken, int offset) |
| 105 | throws IOException, GSSException { |
| 106 | |
| 107 | // Token is small |
| 108 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 109 | super.encode(bos); |
| 110 | byte[] token = bos.toByteArray(); |
| 111 | System.arraycopy(token, 0, outToken, offset, token.length); |
| 112 | return token.length; |
| 113 | } |
| 114 | |
| 115 | public byte[] encode() throws IOException, GSSException { |
| 116 | |
| 117 | // XXX Fine tune this initial size |
| 118 | ByteArrayOutputStream bos = new ByteArrayOutputStream(50); |
| 119 | encode(bos); |
| 120 | return bos.toByteArray(); |
| 121 | } |
| 122 | } |