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