1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.xnet.provider.jsse; 19 20 import java.io.IOException; 21 import java.math.BigInteger; 22 import java.util.Arrays; 23 24 import junit.framework.TestCase; 25 26 /** 27 * Tests for <code>ServerKeyExchange</code> constructor and methods 28 */ 29 public class ServerKeyExchangeTest extends TestCase { 30 31 public void testServerKeyExchange_RSA_EXPORT() throws Exception { 32 BigInteger rsa_mod = new BigInteger( 33 "0620872145533812525365347773040950432706816921321053881493952289532007782427182339053847578435298266865073748931755945944874247298083566202475988854994079"); 34 BigInteger rsa_exp = new BigInteger("65537"); 35 36 byte[] hash = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 37 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 }; 38 ServerKeyExchange message = new ServerKeyExchange(rsa_mod, rsa_exp, 39 null, hash); 40 assertEquals("incorrect type", Handshake.SERVER_KEY_EXCHANGE, message 41 .getType()); 42 43 assertTrue("incorrect ServerKeyExchange", Arrays.equals(message.hash, 44 hash)); 45 assertEquals("incorrect ServerKeyExchange", rsa_mod, message.par1); 46 assertEquals("incorrect ServerKeyExchange", rsa_exp, message.par2); 47 assertNull("incorrect ServerKeyExchange", message.par3); 48 49 HandshakeIODataStream out = new HandshakeIODataStream(); 50 message.send(out); 51 byte[] encoded = out.getData(1000); 52 assertEquals("incorrect out data length", message.length(), 53 encoded.length); 54 55 HandshakeIODataStream in = new HandshakeIODataStream(); 56 in.append(encoded); 57 ServerKeyExchange message_2 = new ServerKeyExchange(in, message 58 .length(), CipherSuite.KeyExchange_RSA_EXPORT); 59 60 assertTrue("incorrect message decoding", Arrays.equals(message.hash, 61 message_2.hash)); 62 assertEquals("incorrect message decoding", message.par1, message_2.par1); 63 assertEquals("incorrect message decoding", message.par2, message_2.par2); 64 assertNull("incorrect message decoding", message_2.par3); 65 assertEquals("incorrect message decoding", message.getRSAPublicKey(), 66 message_2.getRSAPublicKey()); 67 68 in.append(encoded); 69 try { 70 new ServerKeyExchange(in, message.length() - 1, 71 CipherSuite.KeyExchange_RSA_EXPORT); 72 fail("Small length: No expected AlertException"); 73 } catch (AlertException e) { 74 } 75 76 in.append(encoded); 77 in.append(new byte[] { 1, 2, 3 }); 78 try { 79 new ServerKeyExchange(in, message.length() + 3, 80 CipherSuite.KeyExchange_RSA_EXPORT); 81 fail("Extra bytes: No expected AlertException "); 82 } catch (AlertException e) { 83 } 84 } 85 86 public void testServerKeyExchange_DHE_DSS() throws Exception { 87 BigInteger dh_p = new BigInteger("1234567890"); 88 BigInteger dh_g = new BigInteger("987654321"); 89 BigInteger dh_Ys = new BigInteger("123123123"); 90 byte[] hash = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 91 6, 7, 8, 9, 0 }; 92 ServerKeyExchange message = new ServerKeyExchange(dh_p, dh_g, dh_Ys, 93 hash); 94 assertEquals("incorrect type", Handshake.SERVER_KEY_EXCHANGE, message 95 .getType()); 96 97 assertTrue("incorrect ServerKeyExchange", Arrays.equals(message.hash, 98 hash)); 99 assertEquals("incorrect ServerKeyExchange", dh_p, message.par1); 100 assertEquals("incorrect ServerKeyExchange", dh_g, message.par2); 101 assertEquals("incorrect ServerKeyExchange", dh_Ys, message.par3); 102 103 HandshakeIODataStream out = new HandshakeIODataStream(); 104 message.send(out); 105 byte[] encoded = out.getData(1000); 106 assertEquals("incorrect out data length", message.length(), 107 encoded.length); 108 109 HandshakeIODataStream in = new HandshakeIODataStream(); 110 in.append(encoded); 111 ServerKeyExchange message_2 = new ServerKeyExchange(in, message 112 .length(), CipherSuite.KeyExchange_DHE_DSS); 113 114 assertTrue("incorrect message decoding", Arrays.equals(message.hash, 115 message_2.hash)); 116 assertEquals("incorrect message decoding", message.par1, message_2.par1); 117 assertEquals("incorrect message decoding", message.par2, message_2.par2); 118 assertEquals("incorrect message decoding", message.par3, message_2.par3); 119 120 in.append(encoded); 121 try { 122 new ServerKeyExchange(in, message.length() - 1, 123 CipherSuite.KeyExchange_DHE_DSS); 124 fail("Small length: No expected AlertException"); 125 } catch (AlertException e) { 126 } 127 128 in.append(encoded); 129 in.append(new byte[] { 1, 2, 3 }); 130 try { 131 new ServerKeyExchange(in, message.length() + 3, 132 CipherSuite.KeyExchange_DHE_DSS); 133 fail("Extra bytes: No expected AlertException "); 134 } catch (AlertException e) { 135 } 136 } 137 138 public void testServerKeyExchange_DH_anon() throws Exception { 139 BigInteger dh_p = new BigInteger("1234567890"); 140 BigInteger dh_g = new BigInteger("987654321"); 141 BigInteger dh_Ys = new BigInteger("123123123"); 142 ServerKeyExchange message = new ServerKeyExchange(dh_p, dh_g, dh_Ys, 143 null); 144 assertEquals("incorrect type", Handshake.SERVER_KEY_EXCHANGE, message 145 .getType()); 146 147 assertNull("incorrect ServerKeyExchange", message.hash); 148 assertEquals("incorrect ServerKeyExchange", dh_p, message.par1); 149 assertEquals("incorrect ServerKeyExchange", dh_g, message.par2); 150 assertEquals("incorrect ServerKeyExchange", dh_Ys, message.par3); 151 152 HandshakeIODataStream out = new HandshakeIODataStream(); 153 message.send(out); 154 byte[] encoded = out.getData(1000); 155 assertEquals("incorrect out data length", message.length(), 156 encoded.length); 157 158 HandshakeIODataStream in = new HandshakeIODataStream(); 159 in.append(encoded); 160 ServerKeyExchange message_2 = new ServerKeyExchange(in, message 161 .length(), CipherSuite.KeyExchange_DH_anon); 162 163 assertNull("incorrect message decoding", message_2.hash); 164 assertEquals("incorrect message decoding", message.par1, message_2.par1); 165 assertEquals("incorrect message decoding", message.par2, message_2.par2); 166 assertEquals("incorrect message decoding", message.par3, message_2.par3); 167 168 in.append(encoded); 169 try { 170 new ServerKeyExchange(in, message.length() - 1, 171 CipherSuite.KeyExchange_DH_anon); 172 fail("Small length: No expected AlertException"); 173 } catch (AlertException e) { 174 } 175 176 in.append(encoded); 177 in.append(new byte[] { 1, 2, 3 }); 178 try { 179 new ServerKeyExchange(in, message.length() + 3, 180 CipherSuite.KeyExchange_DH_anon); 181 fail("Extra bytes: No expected AlertException "); 182 } catch (AlertException e) { 183 } 184 } 185 186 } 187