Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图理解Java中的SSL证书_Java_Ssl_Rtmp - Fatal编程技术网

试图理解Java中的SSL证书

试图理解Java中的SSL证书,java,ssl,rtmp,Java,Ssl,Rtmp,在前言中,我没有很好的(阅读:任何)安全背景,我认为缺乏理解可能是这个问题的根源 我在玩一些设置RTMPS连接的代码。基本上,我希望了解它是如何工作的,虽然我对RTMP协议没有任何问题,但我很难理解SSL代码是如何编写的。它似乎会从服务器获取证书,然后将其存储在cacerts文件或sens文件夹中 我一直在尝试跟踪连接过程,以查看在何处检索和存储证书,但一直无法找到它。以下是代码的一部分,据我所知,它运行正常: 注意:SavingTrustManager只是X509TrustManager的包装

在前言中,我没有很好的(阅读:任何)安全背景,我认为缺乏理解可能是这个问题的根源

我在玩一些设置RTMPS连接的代码。基本上,我希望了解它是如何工作的,虽然我对RTMP协议没有任何问题,但我很难理解SSL代码是如何编写的。它似乎会从服务器获取证书,然后将其存储在cacerts文件或sens文件夹中

我一直在尝试跟踪连接过程,以查看在何处检索和存储证书,但一直无法找到它。以下是代码的一部分,据我所知,它运行正常:

注意:SavingTrustManager只是X509TrustManager的包装器,它还存储证书链

/**
*使用默认证书或以前保存的证书打开套接字
*
*@如有必要,返回一个特殊的TrustManager来保存证书
*@抛出异常
*/
private SavingTrustManager openSocketWithCert()引发IOException{
试一试{
//加载默认密钥库或保存的密钥库
KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType());
File File=新文件(“certs/”+服务器+“.cert”);
如果(!file.exists()| |!file.isFile())
file=新文件(System.getProperty(“java.home”)+“/lib/security/cacerts”);
InputStream in=新文件InputStream(文件);
ks.load(in,密码短语);
//使用密钥库设置套接字工厂
SSLContext context=SSLContext.getInstance(“TLS”);
TrustManagerFactory tmf=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
X509TrustManager defaultTrustManager=(X509TrustManager)tmf.getTrustManager()[0];
SavingTrustManager tm=新的SavingTrustManager(defaultTrustManager);
init(null,new TrustManager[]{tm},null);
SSLSocketFactory=context.getSocketFactory();
sslsocket=(sslsocket)factory.createSocket(服务器,端口);
返回tm;
}
捕获(例外e){
//在这里遇到异常是非常糟糕的,因为我们可能不会
//恢复
//(除非是连接问题)
//作为IOException重播
抛出新IOException(例如getMessage());
}
}
/**
*如有必要,下载并安装证书
*
*@抛出异常
*/
私有void getCertificate()引发IOException{
试一试{
SavingTrustManager tm=openSocketWithCert();
//试着和插座握手
布尔成功=假;
试一试{
sslsocket.startHandshake();
成功=真实;
}
捕获(SSE){
sslsocket.close();
}
//如果握手失败,请保存获得的证书并重试
//再次
如果(!成功){
//如果需要,请设置目录
文件目录=新文件(“证书”);
如果(!dir.isDirectory()){
dir.delete();
dir.mkdir();
}
//重新加载(默认)密钥库
KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType());
File File=新文件(System.getProperty(“java.home”)+“/lib/security/cacerts”);
InputStream in=新文件InputStream(文件);
ks.load(in,密码短语);
//添加证书
X509Certificate[]chain=tm.chain;
如果(链==null)
抛出新异常(“获取服务器证书链失败”);
X509证书证书证书=链[0];
字符串别名=服务器+“-1”;
ks.setCertificateEntry(别名、证书);
//保存证书
OutputStream out=新文件OutputStream(“certs/”+server+“.cert”);
ks.存储(输出,密码短语);
out.close();
System.out.println(“为“+服务器安装的证书”);
}
}
捕获(例外e){
//在这里遇到异常是非常糟糕的,因为我们可能不会
//恢复
//(除非是连接问题)
//作为IOException重播
e、 printStackTrace();
抛出新IOException(例如getMessage());
}
}
/**
*尝试连接之前的连接信息
*
*@抛出异常
*/
public void connect()引发IOException{
试一试{
sslsocket=(sslsocket)SSLSocketFactory.getDefault().createSocket(服务器,端口);
in=新的BufferedInputStream(sslsocket.getInputStream());
out=新的DataOutputStream(sslsocket.getOutputStream());
doHandshake();
}
捕获(IOE异常){
//如果我们设置套接字失败,那么假设是因为我们需要
//证书
getCertificate();
//并使用证书
openSocketWithCert();
//试着再次握手
in=新的BufferedInputStream(sslsocket.getInputStream());
out=新的DataOutputStream(sslsocket.getOutputStream());
doHandshake();
}
//开始阅读答案
pr=新的RTMPPacketReader(英寸);
//处理预连接消息?
//--02 | 00 00 | 00 05 | 06 00 00 | 00 03 D0 90 02
//连接
Map params=新的HashMap();
参数put(“app”,app);
参数put(“闪络器”、“赢10,1,85,3”);
参数put(“swfUrl”,swfUrl);
参数put(“tcUrl”,“rtmps://”+服务器+:“+端口);
参数put(“fpad”,假);
参数put(“能力”,239);
参数put(“音频编解码器”,3191);
参数put(“视频编解码器”,252);
参数put(“视频功能”,1);
参数put(“pageUrl”,pageUrl);
参数put(“objectEncoding”,3);
字节[]连接=aec.encodeConnect(参数);
out.write(connect,0,connect.length);
out.flush();
而(!results.containsKey(1))
睡眠(10);
TypedObject result=results.get(1);
DSId=result.getTO(“数据”).getString(“id”);
连接=真;
}
我想我无法理解服务器的证书是如何收集的。从我的角度来看,似乎从未调用过
getCertificate()
方法。这是因为证书已存储在某个位置吗?没有certs目录,所以我假设它在accents文件中。如果我清除了cace,它会被调用吗
Below key points you can google and understand.

1. Trusted certificates and keystores/truststores
2. Concept of private and public key. How browser send the certificate key to server and it gets authenticated.
3. Keytool utility, to convert certificates from one form to another.
4. Importing the trusted certificates and keys to trustore, though java has its "cacerts" file by default, but you can create you own and import certificates into it.
5. Explore certificates, read about thumbprint, algorithms used
6. Create you own custom factory as well.