简体   繁体   English

与浏览器和Java服务器建立客户端/服务器SSL连接?

[英]Establish client/server SSL connection with browser and my java server?

I am trying to create a HttpsServer/Client so that I can create a proxy to examine traffic coming from the browser to the server. 我正在尝试创建HttpsServer / Client,以便可以创建代理来检查从浏览器到服务器的流量。 These types of tool are invaluable to people who test web application security. 这些类型的工具对于测试Web应用程序安全性的人来说是无价的。 I have decided to use httpclient to send the requests and httpcore components for my server. 我决定使用httpclient为我的服务器发送请求和httpcore组件。 At the moment I'm simply trying to establish the ssl socket connection between the browser and the server on port 8080. I have read all over and still cannot seem to get this to work. 目前,我只是试图在浏览器和8080端口上的服务器之间建立ssl套接字连接。我已经通读了所有内容,但似乎仍然无法正常工作。 Here are the steps I did up to this point: 这是我到目前为止所做的步骤:

  1. Created a CA cert with keytool and added it to file called cacerts 使用keytool创建一个CA证书并将其添加到名为cacerts的文件中

  2. I added this cert to the firefox browser instance listening on port 8080 我将此证书添加到侦听端口8080的Firefox浏览器实例中

  3. In my code i do the following to call that cert in the server code 在我的代码中,我执行以下操作以在服务器代码中调用该证书

      KeyStore ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream("C:\\\\Program Files\\\\Java\\\\jre6\\\\bin\\\\cacerts"), "password".toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, "password".toCharArray()); SSLContext context = SSLContext.getInstance("TLS"); context.init(kmf.getKeyManagers(), null, null); ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault(); serversocket = ssocketFactory.createServerSocket(port); 

Then when I call the accept on the socket as seen below i get the following exception: 然后,当我如下图所示在套接字上调用accept时,出现以下异常:

I/O error initialising connection thread: No available certificate or key corresponds    to the SSL cipher suites which are enabled.
 javax.net.ssl.SSLException: No available certificate or key corresponds to the SSL     cipher suites which are enabled.
 at com.sun.net.ssl.internal.ssl.SSLServerSocketImpl.checkEnabledSuites(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLServerSocketImpl.accept(Unknown Source)
at DefaultHttpServer$RequestListenerThread.run(DefaultHttpServer.java:151) 

Here is line throwing the exception: 这是抛出异常的行:

   Socket socket = serversocket.accept(); 

Any ideas on what i'm doing wrong here? 关于我在这里做错的任何想法吗? Just trying to establish the ssl socket connection with the port 8080 the browser is sending its requests on. 只是尝试与端口8080建立ssl套接字连接,浏览器正在发送其请求。

Update 11/13 更新11/13

I took some of your information thus far and went to create a separate keystore file. 到目前为止,我已经获取了您的一些信息,然后创建了一个单独的密钥库文件。 This is what I did 这就是我所做的

C:\Users\Steve>keytool -genkey -alias serverprivkey -keystore privateKey.store

Then I copied this file privateKey.store from my user directory over to my project folder and did the following changes in my code: 然后,我将此文件privateKey.store从用户目录复制到我的项目文件夹中,并在代码中进行了以下更改:

        KeyStore ks = KeyStore.getInstance("JKS");  
        ks.load(new FileInputStream("privateKey.store"), "pass123".toCharArray());

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(ks, "pass123".toCharArray());

I know it is correctly grabbing that file because if passwords are wrong i get exceptions. 我知道它正确地捕获了该文件,因为如果密码错误,我会得到例外。 However, i'm still getting that same exception. 但是,我仍然遇到同样的异常。 Any ideas what to try next? 有什么想法下一步可以尝试吗?

Here is what is inside privateKey.store: 这是privateKey.store内部的内容:

 C:\Users\Steve>keytool -list -v -keystore privateKey.store
Enter keystore password:

Keystore type: JKS
 Keystore provider: SUN

Your keystore contains 1 entry

Alias name: serverprivkey
Creation date: Nov 13, 2011
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=sven rbera, OU=application developement, O=whs, L=san hjose, ST=
 ca, C=ca
 Issuer: CN=sven rbera, OU=application developement, O=whs, L=san hjose, ST
 =ca, C=ca
 Serial number: 4ec00a18
 Valid from: Sun Nov 13 10:19:04 PST 2011 until: Sat Feb 11 10:19:04 PST 2012
 Certificate fingerprints:
     MD5:  9C:A7:2B:CE:DC:AD:5B:9C:D6:B7:71:6C:EC:91:8A:24
     SHA1: 47:8F:9B:A2:E1:31:A5:D9:F6:71:8A:CA:3F:CB:BA:FC:C7:2D:F5:A8
     Signature algorithm name: SHA1withDSA
     Version: 3


I have changed my key to now use RSA as mentioned. 我已将密钥更改为现在如上所述使用RSA。 Additionally, i added the debug flag for SSL and have it available. 另外,我为SSL添加了调试标志,并使其可用。 Its a bit tricky to fully understand but it looks like it finds the key serverprivkey2 just fine. 要完全理解它有些棘手,但是看起来找到了密钥serverprivkey2就很好了。 Then it goes into trustStore and I do not see anything in that list that looks like it came from me. 然后,它进入trustStore,并且在该列表中没有任何看起来像来自我的东西。 I really dont know what i should be expecting to see in that section. 我真的不知道我应该在那部分看到什么。 Any ideas? 有任何想法吗?

            ***
            found key for : serverprivkey2
            chain [0] = [
            [
              Version: V3
              Subject: CN=steve, OU=labarbera, O=whs, L=sj, ST=ca, C=ca
              Signature Algorithm: SHA1withRSA, OID = 1.2.840.113549.1.1.5

              Key:  Sun RSA public key, 1024 bits
              modulus: 140985119594686674696976228136679950023710897166974487014150510574037897724033913877362573524361519470364814271848450916151017718803985253447854099124509296799994400199293690731598145912452994962103007955337967369473821653235218532303270695076070736956288068926075705380732910518314547899958542901647381772169
              public exponent: 65537
              Validity: [From: Sun Nov 13 14:45:44 PST 2011,
                           To: Sat Feb 11 14:45:44 PST 2012]
              Issuer: CN=steve, OU=labarbera, O=whs, L=sj, ST=ca, C=ca
              SerialNumber: [    4ec04898]

            ]
              Algorithm: [SHA1withRSA]
              Signature:
            0000: C8 81 37 74 E9 7C A4 76   9F FD EC 8A 78 69 F2 A4  ..7t...v....xi..
            0010: 64 1E C9 98 FD 99 FB 48   3D E2 C5 C5 EB A3 34 1B  d......H=.....4.
            0020: 7C BE B3 E4 F7 4D 90 F1   AB A6 4D 36 97 95 9B 95  .....M....M6....
            0030: 90 C1 B9 28 9C DE A0 4A   AD C7 10 8F 06 57 A6 2B  ...(...J.....W.+
            0040: 51 45 63 73 ED 1E AF 5F   61 E2 87 1A 7C CD 4E 3F  QEcs..._a.....N?
            0050: A7 18 15 FA 73 94 58 46   62 46 42 F9 31 12 2F C7  ....s.XFbFB.1./.
            0060: 6E 6E A0 3F 17 FA A8 24   FC 68 83 88 E2 23 EF DE  nn.?...$.h...#..
            0070: E9 F5 58 AB 16 19 1B 82   72 C6 A0 A7 7E 41 36 1C  ..X.....r....A6.

            ]
            ***

trustStore is: C:\Program Files\Java\jre6\lib\security\cacerts
trustStore type is : jks
trustStore provider is : 
init truststore
adding as trusted cert:
... bunch certs none of which look familiar?

1.Created a CA cert with keytool and added it to file called cacerts 1.使用keytool创建一个CA证书并将其添加到名为cacerts的文件中

I can not understand what you did here. 我不明白你在这里做什么。
Did you create a public/private key pair and stored it to cacerts keystore? 您是否创建了公钥/私钥对并将其存储到cacerts密钥库中?
You should not have done that. 你不应该那样做。
cacerts is meant to be used as a truststore and not as a keystore. cacerts旨在用作信任库,而不用作密钥库。

You should have created a new keystore and loaded that for your KeyManager to use. 您应该已经创建了一个新的密钥库,并加载了该密钥库以供KeyManager使用。

The exception is ambiguous. 例外是模棱两可的。
Either it can not find the private key in the cacerts and this could be related to the alias that you have chosen, or the certificate does not have proper extensions to be used for encryption. 它可能无法在cacerts找到私钥,并且这可能与您选择的别名有关,或者该证书没有正确的扩展名可用于加密。
I believe the alias problem is the most likely one. 我相信别名问题很可能是一个问题。

I recomenend to not try to debug this. 我建议不要尝试调试它。
Instead create a separate keystore for your server and load that instead. 而是为您的服务器创建一个单独的密钥库并加载它。
Don't use cacert . 不要使用cacert

UPDATE: Make sure you use RSA as the key algorithm in keytool command 更新:确保您在密钥工具命令中使用RSA作为密钥算法

Steve, you dont have to reinvent the wheel if you just need to examine the traffic. 史蒂夫,如果您只需要检查路况,就不必重新发明轮子。 Use one of the already available proxy servers like Paros for watching the Traffic. 使用已经可用的代理服务器之一(例如Paros)来监视流量。

I would normally expect to find cacerts under "C:\\Program Files\\Java\\jre6\\lib\\security". 我通常希望在“ C:\\ Program Files \\ Java \\ jre6 \\ lib \\ security”下找到cacerts。 Your program is looking for it in "C:\\Program Files\\Java\\jre6\\bin". 您的程序正在“ C:\\ Program Files \\ Java \\ jre6 \\ bin”中查找它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM