Path: blob/master/test/jdk/sun/security/ssl/ClientHandshaker/RSAExport.java
41152 views
/*1* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223// SunJSSE does not support dynamic system properties, no way to re-use24// system properties in samevm/agentvm mode.2526/*27* @test28* @bug 669001829* @summary RSAClientKeyExchange NullPointerException30* @run main/othervm RSAExport31*/3233/*34* Certificates and key used in the test.35*36* TLS server certificate:37* server private key:38* -----BEGIN RSA PRIVATE KEY-----39* Proc-Type: 4,ENCRYPTED40* DEK-Info: DES-EDE3-CBC,97EC03A2D031B7BC41*42* 22wrD+DPv3VF8xg9xoeBqHzFnOVbTLQgVulzaCECDF4zWdxElYKy4yYyY6dMDehi43* XT77NTsq1J14zjJHPp2/U6B5OpZxnf97ZSD0ZC9/DDe/2gjW4fY1Lv0TVP0PdXnm44* cj84RaDiiSk/cERlFzFJ5L8ULMwxdOtYwXwZ4upITw2lT+8zDlBD2i3zZ4TcWrzE45* /su5Kpu+Mp3wthfGX+ZGga2T/NS8ZCKZE+gJDPKQZ/x34VBw+YANQGyCJPv1iMaE46* RyagnpApH9OPSrRIp2iR6uWT6836CET2erbfPaC1odyd8IsbnLldVs9CklH7EgXL47* Nms+DqrQEbNmvMuQYEFyZEHN9D1fGONeacx+cjI85FyMSHSEO65JJmasAxgQe4nF48* /yVz3rNQ2qAGqBhjsjP/WaXuB2aLZiAli/HjN17EJws=49* -----END RSA PRIVATE KEY-----50*51* -----BEGIN RSA PRIVATE KEY-----52* MIIBOQIBAAJBALlfGg/5ZweJcW5zqLdnQ2uyircqDDlENKnv9FABOm/j0wnlPHqX53* CCqFBLoM7tG8ohci1SPy6fLJ5dqLf5FOH2sCAwEAAQJATO0/hpOMgx8xmJGc2Yeb54* /gyY7kwfyIAajs9Khw0LcDTYTo2EAI+vMmDpU+dvmOCLUqq/Z2tiKJhGyrmcBlxr55* kQIhAPYkbYovtvWHslxRb78x4eCrn2p1H7iolNKbyepjCI3zAiEAwMufJlLI9Q0O56* BIr7fPnUhbs9NyMHLIvIQAf/hXYubqkCIGJZR9NxIT+VyrSMbYQNoF0u9fGJfvU/57* lsdYLCOVEnP1AiAsSFjUx50K1CXNG1MqYIPU963W1T/Xln+3XV7ue7esiQIgW2Lu58* xGvz2dAUsGId+Xr2GZXb7ZucY/cPt4o5qdP1m7c=59* -----END RSA PRIVATE KEY-----60*61* Private-Key: (512 bit)62* modulus:63* 00:b9:5f:1a:0f:f9:67:07:89:71:6e:73:a8:b7:67:64* 43:6b:b2:8a:b7:2a:0c:39:44:34:a9:ef:f4:50:01:65* 3a:6f:e3:d3:09:e5:3c:7a:97:08:2a:85:04:ba:0c:66* ee:d1:bc:a2:17:22:d5:23:f2:e9:f2:c9:e5:da:8b:67* 7f:91:4e:1f:6b68* publicExponent: 65537 (0x10001)69* privateExponent:70* 4c:ed:3f:86:93:8c:83:1f:31:98:91:9c:d9:87:9b:71* fe:0c:98:ee:4c:1f:c8:80:1a:8e:cf:4a:87:0d:0b:72* 70:34:d8:4e:8d:84:00:8f:af:32:60:e9:53:e7:6f:73* 98:e0:8b:52:aa:bf:67:6b:62:28:98:46:ca:b9:9c:74* 06:5c:6b:9175* prime1:76* 00:f6:24:6d:8a:2f:b6:f5:87:b2:5c:51:6f:bf:31:77* e1:e0:ab:9f:6a:75:1f:b8:a8:94:d2:9b:c9:ea:63:78* 08:8d:f379* prime2:80* 00:c0:cb:9f:26:52:c8:f5:0d:0e:04:8a:fb:7c:f9:81* d4:85:bb:3d:37:23:07:2c:8b:c8:40:07:ff:85:76:82* 2e:6e:a983* exponent1:84* 62:59:47:d3:71:21:3f:95:ca:b4:8c:6d:84:0d:a0:85* 5d:2e:f5:f1:89:7e:f5:3f:96:c7:58:2c:23:95:12:86* 73:f587* exponent2:88* 2c:48:58:d4:c7:9d:0a:d4:25:cd:1b:53:2a:60:83:89* d4:f7:ad:d6:d5:3f:d7:96:7f:b7:5d:5e:ee:7b:b7:90* ac:8991* coefficient:92* 5b:62:ee:c4:6b:f3:d9:d0:14:b0:62:1d:f9:7a:f6:93* 19:95:db:ed:9b:9c:63:f7:0f:b7:8a:39:a9:d3:f5:94* 9b:b795*96*97* server certificate:98* Data:99* Version: 3 (0x2)100* Serial Number: 11 (0xb)101* Signature Algorithm: sha1WithRSAEncryption102* Issuer: C=US, ST=Some-State, O=Some Org, CN=Someone103* Validity104* Not Before: Apr 18 15:07:30 2008 GMT105* Not After : Jan 4 15:07:30 2028 GMT106* Subject: C=US, ST=Some-State, O=Some Org, CN=SomeoneExport107* Subject Public Key Info:108* Public Key Algorithm: rsaEncryption109* RSA Public Key: (512 bit)110* Modulus (512 bit):111* 00:b9:5f:1a:0f:f9:67:07:89:71:6e:73:a8:b7:67:112* 43:6b:b2:8a:b7:2a:0c:39:44:34:a9:ef:f4:50:01:113* 3a:6f:e3:d3:09:e5:3c:7a:97:08:2a:85:04:ba:0c:114* ee:d1:bc:a2:17:22:d5:23:f2:e9:f2:c9:e5:da:8b:115* 7f:91:4e:1f:6b116* Exponent: 65537 (0x10001)117* X509v3 extensions:118* X509v3 Basic Constraints:119* CA:FALSE120* X509v3 Key Usage:121* Digital Signature, Non Repudiation, Key Encipherment122* X509v3 Subject Key Identifier:123* F1:30:98:BE:7C:AA:F9:B1:91:38:60:AE:13:5F:67:9C:0A:32:9E:31124* X509v3 Authority Key Identifier:125* keyid:B5:32:43:D7:00:24:92:BA:E9:95:E5:F9:A3:64:6C:84:EE:33:2E:15126*127* -----BEGIN CERTIFICATE-----128* MIICIDCCAYmgAwIBAgIBCzANBgkqhkiG9w0BAQUFADBHMQswCQYDVQQGEwJVUzET129* MBEGA1UECBMKU29tZS1TdGF0ZTERMA8GA1UEChMIU29tZSBPcmcxEDAOBgNVBAMT130* B1NvbWVvbmUwHhcNMDgwNDE4MTUwNzMwWhcNMjgwMTA0MTUwNzMwWjBNMQswCQYD131* VQQGEwJVUzETMBEGA1UECBMKU29tZS1TdGF0ZTERMA8GA1UEChMIU29tZSBPcmcx132* FjAUBgNVBAMTDVNvbWVvbmVFeHBvcnQwXDANBgkqhkiG9w0BAQEFAANLADBIAkEA133* uV8aD/lnB4lxbnOot2dDa7KKtyoMOUQ0qe/0UAE6b+PTCeU8epcIKoUEugzu0byi134* FyLVI/Lp8snl2ot/kU4fawIDAQABo1owWDAJBgNVHRMEAjAAMAsGA1UdDwQEAwIF135* 4DAdBgNVHQ4EFgQU8TCYvnyq+bGROGCuE19nnAoynjEwHwYDVR0jBBgwFoAUtTJD136* 1wAkkrrpleX5o2RshO4zLhUwDQYJKoZIhvcNAQEFBQADgYEAFU+fP9FSTQNVZOhv137* eJ+zq6wI/biwzTgPbAq3yu2gb5kT85z4nzqBhPd2LWWFXhUW/D8QyNZ54X30y0Ug138* 3NfUAvOANW7CgUbHBmm77KQiF4nWdh338qqq9HzLGrPqcxX0dmiq2RBVPy9wb2Ea139* FTZiU2v+9pkoLoSDnCOfPCg/4Q4=140* -----END CERTIFICATE-----141*142*143* Trusted CA certificate:144* Certificate:145* Data:146* Version: 3 (0x2)147* Serial Number: 0 (0x0)148* Signature Algorithm: md5WithRSAEncryption149* Issuer: C=US, ST=Some-State, O=Some Org, CN=Someone150* Validity151* Not Before: Mar 30 11:44:47 2001 GMT152* Not After : Apr 27 11:44:47 2028 GMT153* Subject: C=US, ST=Some-State, O=Some Org, CN=Someone154* Subject Public Key Info:155* Public Key Algorithm: rsaEncryption156* RSA Public Key: (1024 bit)157* Modulus (1024 bit):158* 00:c1:98:e4:7a:87:53:0f:94:87:dc:da:f3:59:39:159* 3e:36:95:e8:77:58:ff:46:8a:81:1b:5e:c5:4c:fa:160* b6:91:19:30:be:5b:ef:4c:aa:84:30:a4:9a:d4:68:161* af:ef:fa:b4:2c:76:8b:29:33:46:cf:38:74:7c:79:162* d5:07:a6:43:39:84:52:39:4f:8a:1c:f3:73:19:12:163* 40:cf:ee:a1:77:43:01:02:be:8d:32:11:28:70:f4:164* cf:ab:43:75:e4:fb:74:f1:8c:2e:43:24:ba:85:3f:165* 66:3a:05:ea:f7:ce:5b:97:e2:34:a3:f0:87:f4:f8:166* d1:59:12:5a:68:b7:78:64:a9167* Exponent: 65537 (0x10001)168* X509v3 extensions:169* X509v3 Subject Key Identifier:170* B5:32:43:D7:00:24:92:BA:E9:95:E5:F9:A3:64:6C:84:EE:33:2E:15171* X509v3 Authority Key Identifier:172* keyid:B5:32:43:D7:00:24:92:BA:E9:95:E5:F9:A3:64:6C:84:EE:33:2E:15173* DirName:/C=US/ST=Some-State/O=Some Org/CN=Someone174* serial:00175*176* X509v3 Basic Constraints:177* CA:TRUE178*179* -----BEGIN CERTIFICATE-----180* MIICpjCCAg+gAwIBAgIBADANBgkqhkiG9w0BAQQFADBHMQswCQYDVQQGEwJVUzET181* MBEGA1UECBMKU29tZS1TdGF0ZTERMA8GA1UEChMIU29tZSBPcmcxEDAOBgNVBAMT182* B1NvbWVvbmUwHhcNMDEwMzMwMTE0NDQ3WhcNMjgwNDI3MTE0NDQ3WjBHMQswCQYD183* VQQGEwJVUzETMBEGA1UECBMKU29tZS1TdGF0ZTERMA8GA1UEChMIU29tZSBPcmcx184* EDAOBgNVBAMTB1NvbWVvbmUwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMGY185* 5HqHUw+Uh9za81k5PjaV6HdY/0aKgRtexUz6tpEZML5b70yqhDCkmtRor+/6tCx2186* iykzRs84dHx51QemQzmEUjlPihzzcxkSQM/uoXdDAQK+jTIRKHD0z6tDdeT7dPGM187* LkMkuoU/ZjoF6vfOW5fiNKPwh/T40VkSWmi3eGSpAgMBAAGjgaEwgZ4wHQYDVR0O188* BBYEFLUyQ9cAJJK66ZXl+aNkbITuMy4VMG8GA1UdIwRoMGaAFLUyQ9cAJJK66ZXl189* +aNkbITuMy4VoUukSTBHMQswCQYDVQQGEwJVUzETMBEGA1UECBMKU29tZS1TdGF0190* ZTERMA8GA1UEChMIU29tZSBPcmcxEDAOBgNVBAMTB1NvbWVvbmWCAQAwDAYDVR0T191* BAUwAwEB/zANBgkqhkiG9w0BAQQFAAOBgQBhf3PX0xWxtaUwZlWCO7GfPwCKgBWr192* CXqlqjtWHCshaaU7wUsDOwxFDWwKjFrMerQLsLuBlhdXEbNfSPjychkQtfezQHcS193* q0Atq7+KVSmRbDw6oKVRs5v1BBzLCupy+o16fNz3/hwreAWwQnSMtAh/osNS9w1b194* QeVWU+JV47H+vg==195* -----END CERTIFICATE-----196*197*/198199import java.io.*;200import java.net.*;201import java.security.Security;202import java.security.KeyStore;203import java.security.KeyFactory;204import java.security.cert.Certificate;205import java.security.cert.CertificateFactory;206import java.security.spec.*;207import java.security.interfaces.*;208import javax.net.ssl.*;209import java.math.BigInteger;210211public class RSAExport {212213/*214* =============================================================215* Set the various variables needed for the tests, then216* specify what tests to run on each side.217*/218219220/*221* Should we run the client or server in a separate thread?222* Both sides can throw exceptions, but do you have a preference223* as to which side should be the main thread.224*/225static boolean separateServerThread = true;226227/*228* Where do we find the keystores?229*/230static String trusedCertStr =231"-----BEGIN CERTIFICATE-----\n" +232"MIICpjCCAg+gAwIBAgIBADANBgkqhkiG9w0BAQQFADBHMQswCQYDVQQGEwJVUzET\n" +233"MBEGA1UECBMKU29tZS1TdGF0ZTERMA8GA1UEChMIU29tZSBPcmcxEDAOBgNVBAMT\n" +234"B1NvbWVvbmUwHhcNMDEwMzMwMTE0NDQ3WhcNMjgwNDI3MTE0NDQ3WjBHMQswCQYD\n" +235"VQQGEwJVUzETMBEGA1UECBMKU29tZS1TdGF0ZTERMA8GA1UEChMIU29tZSBPcmcx\n" +236"EDAOBgNVBAMTB1NvbWVvbmUwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMGY\n" +237"5HqHUw+Uh9za81k5PjaV6HdY/0aKgRtexUz6tpEZML5b70yqhDCkmtRor+/6tCx2\n" +238"iykzRs84dHx51QemQzmEUjlPihzzcxkSQM/uoXdDAQK+jTIRKHD0z6tDdeT7dPGM\n" +239"LkMkuoU/ZjoF6vfOW5fiNKPwh/T40VkSWmi3eGSpAgMBAAGjgaEwgZ4wHQYDVR0O\n" +240"BBYEFLUyQ9cAJJK66ZXl+aNkbITuMy4VMG8GA1UdIwRoMGaAFLUyQ9cAJJK66ZXl\n" +241"+aNkbITuMy4VoUukSTBHMQswCQYDVQQGEwJVUzETMBEGA1UECBMKU29tZS1TdGF0\n" +242"ZTERMA8GA1UEChMIU29tZSBPcmcxEDAOBgNVBAMTB1NvbWVvbmWCAQAwDAYDVR0T\n" +243"BAUwAwEB/zANBgkqhkiG9w0BAQQFAAOBgQBhf3PX0xWxtaUwZlWCO7GfPwCKgBWr\n" +244"CXqlqjtWHCshaaU7wUsDOwxFDWwKjFrMerQLsLuBlhdXEbNfSPjychkQtfezQHcS\n" +245"q0Atq7+KVSmRbDw6oKVRs5v1BBzLCupy+o16fNz3/hwreAWwQnSMtAh/osNS9w1b\n" +246"QeVWU+JV47H+vg==\n" +247"-----END CERTIFICATE-----";248249static String serverCertStr =250"-----BEGIN CERTIFICATE-----\n" +251"MIICIDCCAYmgAwIBAgIBCzANBgkqhkiG9w0BAQUFADBHMQswCQYDVQQGEwJVUzET\n" +252"MBEGA1UECBMKU29tZS1TdGF0ZTERMA8GA1UEChMIU29tZSBPcmcxEDAOBgNVBAMT\n" +253"B1NvbWVvbmUwHhcNMDgwNDE4MTUwNzMwWhcNMjgwMTA0MTUwNzMwWjBNMQswCQYD\n" +254"VQQGEwJVUzETMBEGA1UECBMKU29tZS1TdGF0ZTERMA8GA1UEChMIU29tZSBPcmcx\n" +255"FjAUBgNVBAMTDVNvbWVvbmVFeHBvcnQwXDANBgkqhkiG9w0BAQEFAANLADBIAkEA\n" +256"uV8aD/lnB4lxbnOot2dDa7KKtyoMOUQ0qe/0UAE6b+PTCeU8epcIKoUEugzu0byi\n" +257"FyLVI/Lp8snl2ot/kU4fawIDAQABo1owWDAJBgNVHRMEAjAAMAsGA1UdDwQEAwIF\n" +258"4DAdBgNVHQ4EFgQU8TCYvnyq+bGROGCuE19nnAoynjEwHwYDVR0jBBgwFoAUtTJD\n" +259"1wAkkrrpleX5o2RshO4zLhUwDQYJKoZIhvcNAQEFBQADgYEAFU+fP9FSTQNVZOhv\n" +260"eJ+zq6wI/biwzTgPbAq3yu2gb5kT85z4nzqBhPd2LWWFXhUW/D8QyNZ54X30y0Ug\n" +261"3NfUAvOANW7CgUbHBmm77KQiF4nWdh338qqq9HzLGrPqcxX0dmiq2RBVPy9wb2Ea\n" +262"FTZiU2v+9pkoLoSDnCOfPCg/4Q4=\n" +263"-----END CERTIFICATE-----";264265static byte privateExponent[] = {266(byte)0x4c, (byte)0xed, (byte)0x3f, (byte)0x86,267(byte)0x93, (byte)0x8c, (byte)0x83, (byte)0x1f,268(byte)0x31, (byte)0x98, (byte)0x91, (byte)0x9c,269(byte)0xd9, (byte)0x87, (byte)0x9b, (byte)0xfe,270(byte)0x0c, (byte)0x98, (byte)0xee, (byte)0x4c,271(byte)0x1f, (byte)0xc8, (byte)0x80, (byte)0x1a,272(byte)0x8e, (byte)0xcf, (byte)0x4a, (byte)0x87,273(byte)0x0d, (byte)0x0b, (byte)0x70, (byte)0x34,274(byte)0xd8, (byte)0x4e, (byte)0x8d, (byte)0x84,275(byte)0x00, (byte)0x8f, (byte)0xaf, (byte)0x32,276(byte)0x60, (byte)0xe9, (byte)0x53, (byte)0xe7,277(byte)0x6f, (byte)0x98, (byte)0xe0, (byte)0x8b,278(byte)0x52, (byte)0xaa, (byte)0xbf, (byte)0x67,279(byte)0x6b, (byte)0x62, (byte)0x28, (byte)0x98,280(byte)0x46, (byte)0xca, (byte)0xb9, (byte)0x9c,281(byte)0x06, (byte)0x5c, (byte)0x6b, (byte)0x91282};283284static byte modulus[] = {285(byte)0x00,286(byte)0xb9, (byte)0x5f, (byte)0x1a, (byte)0x0f,287(byte)0xf9, (byte)0x67, (byte)0x07, (byte)0x89,288(byte)0x71, (byte)0x6e, (byte)0x73, (byte)0xa8,289(byte)0xb7, (byte)0x67, (byte)0x43, (byte)0x6b,290(byte)0xb2, (byte)0x8a, (byte)0xb7, (byte)0x2a,291(byte)0x0c, (byte)0x39, (byte)0x44, (byte)0x34,292(byte)0xa9, (byte)0xef, (byte)0xf4, (byte)0x50,293(byte)0x01, (byte)0x3a, (byte)0x6f, (byte)0xe3,294(byte)0xd3, (byte)0x09, (byte)0xe5, (byte)0x3c,295(byte)0x7a, (byte)0x97, (byte)0x08, (byte)0x2a,296(byte)0x85, (byte)0x04, (byte)0xba, (byte)0x0c,297(byte)0xee, (byte)0xd1, (byte)0xbc, (byte)0xa2,298(byte)0x17, (byte)0x22, (byte)0xd5, (byte)0x23,299(byte)0xf2, (byte)0xe9, (byte)0xf2, (byte)0xc9,300(byte)0xe5, (byte)0xda, (byte)0x8b, (byte)0x7f,301(byte)0x91, (byte)0x4e, (byte)0x1f, (byte)0x6b302};303304static char passphrase[] = "passphrase".toCharArray();305306/*307* Is the server ready to serve?308*/309volatile static boolean serverReady = false;310311/*312* Turn on SSL debugging?313*/314static boolean debug = false;315316/*317* If the client or server is doing some kind of object creation318* that the other side depends on, and that thread prematurely319* exits, you may experience a hang. The test harness will320* terminate all hung threads after its timeout has expired,321* currently 3 minutes by default, but you might try to be322* smart about it....323*/324325/*326* Define the server side of the test.327*328* If the server prematurely exits, serverReady will be set to true329* to avoid infinite hangs.330*/331void doServerSide() throws Exception {332SSLServerSocketFactory sslssf =333getSSLContext(true).getServerSocketFactory();334SSLServerSocket sslServerSocket =335(SSLServerSocket) sslssf.createServerSocket(serverPort);336337serverPort = sslServerSocket.getLocalPort();338339/*340* Signal Client, we're ready for this connect.341*/342serverReady = true;343344// Enable RSA_EXPORT cipher suites only.345try {346String enabledSuites[] = {347"SSL_RSA_EXPORT_WITH_RC4_40_MD5",348"SSL_RSA_EXPORT_WITH_DES40_CBC_SHA"};349sslServerSocket.setEnabledCipherSuites(enabledSuites);350} catch (IllegalArgumentException iae) {351// ignore the exception a cipher suite is unsupported.352}353354SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();355InputStream sslIS = sslSocket.getInputStream();356OutputStream sslOS = sslSocket.getOutputStream();357358sslIS.read();359sslOS.write(85);360sslOS.flush();361362363sslSocket.close();364}365366/*367* Define the client side of the test.368*369* If the server prematurely exits, serverReady will be set to true370* to avoid infinite hangs.371*/372void doClientSide() throws Exception {373374/*375* Wait for server to get started.376*/377while (!serverReady) {378Thread.sleep(50);379}380381SSLSocketFactory sslsf =382getSSLContext(false).getSocketFactory();383SSLSocket sslSocket = (SSLSocket)384sslsf.createSocket("localhost", serverPort);385386// Enable RSA_EXPORT cipher suites only.387try {388String enabledSuites[] = {389"SSL_RSA_EXPORT_WITH_RC4_40_MD5",390"SSL_RSA_EXPORT_WITH_DES40_CBC_SHA"};391sslSocket.setEnabledCipherSuites(enabledSuites);392} catch (IllegalArgumentException iae) {393// ignore the exception a cipher suite is unsupported.394}395396InputStream sslIS = sslSocket.getInputStream();397OutputStream sslOS = sslSocket.getOutputStream();398399sslOS.write(280);400sslOS.flush();401sslIS.read();402403sslSocket.close();404}405406/*407* =============================================================408* The remainder is just support stuff409*/410411// use any free port by default412volatile int serverPort = 0;413414volatile Exception serverException = null;415volatile Exception clientException = null;416417public static void main(String[] args) throws Exception {418// reset the security property to make sure that the algorithms419// and keys used in this test are not disabled.420Security.setProperty("jdk.certpath.disabledAlgorithms", "MD2");421Security.setProperty("jdk.tls.disabledAlgorithms", "MD2");422423if (debug)424System.setProperty("javax.net.debug", "all");425426/*427* Start the tests.428*/429new RSAExport();430}431432Thread clientThread = null;433Thread serverThread = null;434435/*436* Primary constructor, used to drive remainder of the test.437*438* Fork off the other side, then do your work.439*/440RSAExport() throws Exception {441if (separateServerThread) {442startServer(true);443startClient(false);444} else {445startClient(true);446startServer(false);447}448449/*450* Wait for other side to close down.451*/452if (separateServerThread) {453serverThread.join();454} else {455clientThread.join();456}457458/*459* When we get here, the test is pretty much over.460*461* If the main thread excepted, that propagates back462* immediately. If the other thread threw an exception, we463* should report back.464*/465if (serverException != null)466throw serverException;467if (clientException != null)468throw clientException;469}470471void startServer(boolean newThread) throws Exception {472if (newThread) {473serverThread = new Thread() {474public void run() {475try {476doServerSide();477} catch (Exception e) {478/*479* Our server thread just died.480*481* Release the client, if not active already...482*/483System.err.println("Server died..." + e);484serverReady = true;485serverException = e;486}487}488};489serverThread.start();490} else {491doServerSide();492}493}494495void startClient(boolean newThread) throws Exception {496if (newThread) {497clientThread = new Thread() {498public void run() {499try {500doClientSide();501} catch (Exception e) {502/*503* Our client thread just died.504*/505System.err.println("Client died...");506clientException = e;507}508}509};510clientThread.start();511} else {512doClientSide();513}514}515516// Get the SSL context517private SSLContext getSSLContext(boolean authnRequired) throws Exception {518// generate certificate from cert string519CertificateFactory cf = CertificateFactory.getInstance("X.509");520521ByteArrayInputStream is =522new ByteArrayInputStream(trusedCertStr.getBytes());523Certificate trustedCert = cf.generateCertificate(is);524525// create a key store526KeyStore ks = KeyStore.getInstance("JKS");527ks.load(null, null);528529// import the trusted cert530ks.setCertificateEntry("RSA Export Signer", trustedCert);531532if (authnRequired) {533// generate the private key.534RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(535new BigInteger(modulus),536new BigInteger(privateExponent));537KeyFactory kf = KeyFactory.getInstance("RSA");538RSAPrivateKey priKey =539(RSAPrivateKey)kf.generatePrivate(priKeySpec);540541// generate certificate chain542is = new ByteArrayInputStream(serverCertStr.getBytes());543Certificate serverCert = cf.generateCertificate(is);544545Certificate[] chain = new Certificate[2];546chain[0] = serverCert;547chain[1] = trustedCert;548549// import the key entry.550ks.setKeyEntry("RSA Export", priKey, passphrase, chain);551}552553// create SSL context554TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");555tmf.init(ks);556557SSLContext ctx = SSLContext.getInstance("TLS");558if (authnRequired) {559KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");560kmf.init(ks, passphrase);561562ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);563} else {564ctx.init(null, tmf.getTrustManagers(), null);565}566567return ctx;568}569570}571572573