我正在尝试通过 SSL 与服务器通信。PEM 客户端文件由证书和 rsa 私钥组成。
我设法将证书和密钥都转换为二进制 DER。我成功地将 DER 证书加载到 SecureSocket(使用函数 addBinaryChainBuildingCertificate),但是当我尝试连接到服务器时,出现“主要不匹配”错误。如果我尝试使用上述函数加载 DER 密钥,则会收到“错误参数”错误。
我想“主要不匹配”是因为我没有加载私钥。但我看不到将 RSA 密钥加载到 SecureSocket 的功能。有什么解决办法吗?我是否需要仅使用证书与服务器通信,但从等式中删除密钥?
编辑 :
代码 :
package {
import flash.display.Sprite;
import flash.net.SecureSocket;
import flash.net.URLLoader;
import flash.events.ProgressEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;
public class TestSSL2 extends Sprite {
private var mSocket:SecureSocket = new SecureSocket();
private var certFile:String = "ca.der";
private var keyFile:String = "key.der";
private var cert:ByteArray;
private var key:ByteArray;
public function TestSSL2() {
trace("SecureSocket.isSupported",SecureSocket.isSupported);
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, certLoaded, false, 0, true);
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(new URLRequest(certFile));
}
private function certLoaded(e:Event):void {
cert = (e.target as URLLoader).data;
trace("certificate",cert.length);
mSocket.addBinaryChainBuildingCertificate(cert, true);
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, keyLoaded, false, 0, true);
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(new URLRequest(keyFile));
}
private function keyLoaded(e:Event):void {
key = (e.target as URLLoader).data;
trace("key",key.length);
mSocket.addBinaryChainBuildingCertificate(key, true);
mSocket.connect("127.0.0.1", 3000);
mSocket.addEventListener(Event.CONNECT, socketConnected);
mSocket.addEventListener(IOErrorEvent.IO_ERROR, onError);
mSocket.addEventListener(ProgressEvent.SOCKET_DATA, socketData );
}
private function onError(error:IOErrorEvent):void {
trace("ERROR!",error.text,":",mSocket.serverCertificateStatus);
}
private function socketConnected(e:Event):void {
trace("Connected", e);
}
private function socketData(e:*):void {
var data:String;
data = mSocket.readUTFBytes(mSocket.bytesAvailable);
trace(data);
}
}
}
结果 :
SecureSocket.isSupported true
certificate 497
key 607
ArgumentError: Error #2004: One of the parameters is invalid.
at flash.net::SecureSocket/addBinaryChainBuildingCertificate()
at TestSSL2/keyLoaded()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
如果我评论该行:
//mSocket.addBinaryChainBuildingCertificate(key, true);
我得到:
SecureSocket.isSupported true
certificate 497
key 607
ERROR! Error #2031: Socket Error. URL: 127.0.0.1 : principalMismatch