Frames | No Frames |
1: /* Signature.java -- SSL Signature structure. 2: Copyright (C) 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.net.ssl.provider; 40: 41: import java.io.PrintWriter; 42: import java.io.StringWriter; 43: 44: import java.nio.ByteBuffer; 45: 46: /** 47: * The signature structure. 48: * 49: * <pre> 50: select (SignatureAlgorithm) 51: { 52: case anonymous: 53: struct { }; 54: case rsa: 55: digitally-signed struct 56: { 57: opaque md5_hash[16]; 58: opaque sha_hash[20]; 59: }; 60: case dsa: 61: digitally-signed struct 62: { 63: opaque sha_hash[20]; 64: }; 65: } Signature;</pre> 66: */ 67: public class Signature implements Builder, Constructed 68: { 69: 70: // Fields. 71: // ------------------------------------------------------------------------- 72: 73: private final ByteBuffer buffer; 74: private final SignatureAlgorithm alg; 75: 76: // Constructor. 77: // ------------------------------------------------------------------------- 78: 79: public Signature (final ByteBuffer buffer, final SignatureAlgorithm alg) 80: { 81: this.buffer = buffer; 82: this.alg = alg; 83: } 84: 85: public Signature (final byte[] sigValue, final SignatureAlgorithm alg) 86: { 87: buffer = ByteBuffer.allocate(sigValue.length + 2); 88: buffer.putShort((short) sigValue.length); 89: buffer.put(sigValue); 90: buffer.position(0); 91: this.alg = alg; 92: } 93: 94: // Instance methods. 95: // ------------------------------------------------------------------------- 96: 97: public int length () 98: { 99: if (alg.equals (SignatureAlgorithm.ANONYMOUS)) 100: return 0; 101: return (buffer.getShort (0) & 0xFFFF) + 2; 102: } 103: 104: public ByteBuffer buffer() 105: { 106: return (ByteBuffer) buffer.duplicate().limit(length()); 107: } 108: 109: public byte[] signature () 110: { 111: if (alg.equals (SignatureAlgorithm.ANONYMOUS)) 112: return new byte[0]; 113: int length = buffer.getShort (0) & 0xFFFF; 114: byte[] buf = new byte[length]; 115: ((ByteBuffer) buffer.duplicate().position(2)).get(buf); 116: return buf; 117: } 118: 119: public void setSignature (final byte[] signature) 120: { 121: setSignature (signature, 0, signature.length); 122: } 123: 124: public void setSignature (final byte[] signature, final int offset, final int length) 125: { 126: if (alg.equals (SignatureAlgorithm.ANONYMOUS)) 127: return; 128: buffer.putShort (0, (short) length); 129: buffer.position (2); 130: buffer.put (signature, offset, length); 131: } 132: 133: public String toString () 134: { 135: return toString (null); 136: } 137: 138: public String toString (final String prefix) 139: { 140: StringWriter str = new StringWriter(); 141: PrintWriter out = new PrintWriter(str); 142: if (prefix != null) 143: out.print (prefix); 144: out.println("struct {"); 145: if (!alg.equals (SignatureAlgorithm.ANONYMOUS)) 146: { 147: String subprefix = " "; 148: if (prefix != null) 149: subprefix = prefix + subprefix; 150: out.print (Util.hexDump (signature (), subprefix)); 151: } 152: if (prefix != null) 153: out.print (prefix); 154: out.print ("} Signature;"); 155: return str.toString(); 156: } 157: }