1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46:
47: import ;
48:
49:
57: public final class EncryptedPreMasterSecret extends ExchangeKeys implements Builder
58: {
59: private final ProtocolVersion version;
60:
61: public EncryptedPreMasterSecret(ByteBuffer buffer, ProtocolVersion version)
62: {
63: super(buffer);
64: version.getClass();
65: this.version = version;
66: }
67:
68: public EncryptedPreMasterSecret(byte[] encryptedSecret, ProtocolVersion version)
69: {
70: this(ByteBuffer.allocate(version == ProtocolVersion.SSL_3
71: ? encryptedSecret.length
72: : encryptedSecret.length + 2), version);
73: ByteBuffer b = buffer.duplicate();
74: if (version != ProtocolVersion.SSL_3)
75: b.putShort((short) encryptedSecret.length);
76: b.put(encryptedSecret);
77: }
78:
79: public ByteBuffer buffer()
80: {
81: return (ByteBuffer) buffer.duplicate().rewind();
82: }
83:
84: public byte[] encryptedSecret()
85: {
86: byte[] secret;
87: if (version == ProtocolVersion.SSL_3)
88: {
89: buffer.position (0);
90: secret = new byte[buffer.limit ()];
91: buffer.get(secret);
92: }
93: else
94: {
95: int len = buffer.getShort(0) & 0xFFFF;
96: secret = new byte[len];
97: buffer.position(2);
98: buffer.get(secret);
99: }
100: return secret;
101: }
102:
103: public void setEncryptedSecret(final byte[] secret, final int offset, final int length)
104: {
105: if (version == ProtocolVersion.SSL_3)
106: {
107: buffer.position(0);
108: buffer.put(secret, offset, length);
109: buffer.rewind();
110: }
111: else
112: {
113: buffer.putShort(0, (short) length);
114: buffer.position(2);
115: buffer.put(secret, offset, length);
116: buffer.rewind();
117: }
118: }
119:
120: public int length ()
121: {
122: if (version == ProtocolVersion.SSL_3)
123: {
124: return buffer.capacity();
125: }
126: else
127: {
128: return (buffer.getShort(0) & 0xFFFF) + 2;
129: }
130: }
131:
132: public String toString ()
133: {
134: return toString (null);
135: }
136:
137: public String toString (final String prefix)
138: {
139: StringWriter str = new StringWriter();
140: PrintWriter out = new PrintWriter(str);
141: if (prefix != null) out.print(prefix);
142: out.println("struct {");
143: if (prefix != null) out.print(prefix);
144: out.println(" pre_master_secret = ");
145: out.print(Util.hexDump(encryptedSecret(), prefix != null ? prefix + " "
146: : " "));
147: if (prefix != null) out.print(prefix);
148: out.print("} EncryptedPreMasterSecret;");
149: return str.toString();
150: }