001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.xnet.provider.jsse;
019:
020: import java.io.IOException;
021: import java.math.BigInteger;
022: import java.util.Arrays;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * Tests for <code>ServerKeyExchange</code> constructor and methods
028: *
029: */
030: public class ServerKeyExchangeTest extends TestCase {
031:
032: public void testServerKeyExchange_RSA_EXPORT() throws Exception {
033: BigInteger rsa_mod = new BigInteger(
034: "0620872145533812525365347773040950432706816921321053881493952289532007782427182339053847578435298266865073748931755945944874247298083566202475988854994079");
035: BigInteger rsa_exp = new BigInteger("65537");
036:
037: byte[] hash = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
038: 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
039: 1, 2, 3, 4, 5, 6 };
040: ServerKeyExchange message = new ServerKeyExchange(rsa_mod,
041: rsa_exp, null, hash);
042: assertEquals("incorrect type", Handshake.SERVER_KEY_EXCHANGE,
043: message.getType());
044:
045: assertTrue("incorrect ServerKeyExchange", Arrays.equals(
046: message.hash, hash));
047: assertEquals("incorrect ServerKeyExchange", rsa_mod,
048: message.par1);
049: assertEquals("incorrect ServerKeyExchange", rsa_exp,
050: message.par2);
051: assertNull("incorrect ServerKeyExchange", message.par3);
052:
053: HandshakeIODataStream out = new HandshakeIODataStream();
054: message.send(out);
055: byte[] encoded = out.getData(1000);
056: assertEquals("incorrect out data length", message.length(),
057: encoded.length);
058:
059: HandshakeIODataStream in = new HandshakeIODataStream();
060: in.append(encoded);
061: ServerKeyExchange message_2 = new ServerKeyExchange(in, message
062: .length(), CipherSuite.KeyExchange_RSA_EXPORT);
063:
064: assertTrue("incorrect message decoding", Arrays.equals(
065: message.hash, message_2.hash));
066: assertEquals("incorrect message decoding", message.par1,
067: message_2.par1);
068: assertEquals("incorrect message decoding", message.par2,
069: message_2.par2);
070: assertNull("incorrect message decoding", message_2.par3);
071: assertEquals("incorrect message decoding", message
072: .getRSAPublicKey(), message_2.getRSAPublicKey());
073:
074: in.append(encoded);
075: try {
076: new ServerKeyExchange(in, message.length() - 1,
077: CipherSuite.KeyExchange_RSA_EXPORT);
078: fail("Small length: No expected AlertException");
079: } catch (AlertException e) {
080: }
081:
082: in.append(encoded);
083: in.append(new byte[] { 1, 2, 3 });
084: try {
085: new ServerKeyExchange(in, message.length() + 3,
086: CipherSuite.KeyExchange_RSA_EXPORT);
087: fail("Extra bytes: No expected AlertException ");
088: } catch (AlertException e) {
089: }
090: }
091:
092: public void testServerKeyExchange_DHE_DSS() throws Exception {
093: BigInteger dh_p = new BigInteger("1234567890");
094: BigInteger dh_g = new BigInteger("987654321");
095: BigInteger dh_Ys = new BigInteger("123123123");
096: byte[] hash = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
097: 3, 4, 5, 6, 7, 8, 9, 0 };
098: ServerKeyExchange message = new ServerKeyExchange(dh_p, dh_g,
099: dh_Ys, hash);
100: assertEquals("incorrect type", Handshake.SERVER_KEY_EXCHANGE,
101: message.getType());
102:
103: assertTrue("incorrect ServerKeyExchange", Arrays.equals(
104: message.hash, hash));
105: assertEquals("incorrect ServerKeyExchange", dh_p, message.par1);
106: assertEquals("incorrect ServerKeyExchange", dh_g, message.par2);
107: assertEquals("incorrect ServerKeyExchange", dh_Ys, message.par3);
108:
109: HandshakeIODataStream out = new HandshakeIODataStream();
110: message.send(out);
111: byte[] encoded = out.getData(1000);
112: assertEquals("incorrect out data length", message.length(),
113: encoded.length);
114:
115: HandshakeIODataStream in = new HandshakeIODataStream();
116: in.append(encoded);
117: ServerKeyExchange message_2 = new ServerKeyExchange(in, message
118: .length(), CipherSuite.KeyExchange_DHE_DSS);
119:
120: assertTrue("incorrect message decoding", Arrays.equals(
121: message.hash, message_2.hash));
122: assertEquals("incorrect message decoding", message.par1,
123: message_2.par1);
124: assertEquals("incorrect message decoding", message.par2,
125: message_2.par2);
126: assertEquals("incorrect message decoding", message.par3,
127: message_2.par3);
128:
129: in.append(encoded);
130: try {
131: new ServerKeyExchange(in, message.length() - 1,
132: CipherSuite.KeyExchange_DHE_DSS);
133: fail("Small length: No expected AlertException");
134: } catch (AlertException e) {
135: }
136:
137: in.append(encoded);
138: in.append(new byte[] { 1, 2, 3 });
139: try {
140: new ServerKeyExchange(in, message.length() + 3,
141: CipherSuite.KeyExchange_DHE_DSS);
142: fail("Extra bytes: No expected AlertException ");
143: } catch (AlertException e) {
144: }
145: }
146:
147: public void testServerKeyExchange_DH_anon() throws Exception {
148: BigInteger dh_p = new BigInteger("1234567890");
149: BigInteger dh_g = new BigInteger("987654321");
150: BigInteger dh_Ys = new BigInteger("123123123");
151: ServerKeyExchange message = new ServerKeyExchange(dh_p, dh_g,
152: dh_Ys, null);
153: assertEquals("incorrect type", Handshake.SERVER_KEY_EXCHANGE,
154: message.getType());
155:
156: assertNull("incorrect ServerKeyExchange", message.hash);
157: assertEquals("incorrect ServerKeyExchange", dh_p, message.par1);
158: assertEquals("incorrect ServerKeyExchange", dh_g, message.par2);
159: assertEquals("incorrect ServerKeyExchange", dh_Ys, message.par3);
160:
161: HandshakeIODataStream out = new HandshakeIODataStream();
162: message.send(out);
163: byte[] encoded = out.getData(1000);
164: assertEquals("incorrect out data length", message.length(),
165: encoded.length);
166:
167: HandshakeIODataStream in = new HandshakeIODataStream();
168: in.append(encoded);
169: ServerKeyExchange message_2 = new ServerKeyExchange(in, message
170: .length(), CipherSuite.KeyExchange_DH_anon);
171:
172: assertNull("incorrect message decoding", message_2.hash);
173: assertEquals("incorrect message decoding", message.par1,
174: message_2.par1);
175: assertEquals("incorrect message decoding", message.par2,
176: message_2.par2);
177: assertEquals("incorrect message decoding", message.par3,
178: message_2.par3);
179:
180: in.append(encoded);
181: try {
182: new ServerKeyExchange(in, message.length() - 1,
183: CipherSuite.KeyExchange_DH_anon);
184: fail("Small length: No expected AlertException");
185: } catch (AlertException e) {
186: }
187:
188: in.append(encoded);
189: in.append(new byte[] { 1, 2, 3 });
190: try {
191: new ServerKeyExchange(in, message.length() + 3,
192: CipherSuite.KeyExchange_DH_anon);
193: fail("Extra bytes: No expected AlertException ");
194: } catch (AlertException e) {
195: }
196: }
197:
198: }
|