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