これは、HttpWebRequestを動作させる方法、またはHttpWebRequestが適切な実装である場合であっても、より詳細です。私はC#と.Netのスキルが過去数年間失われてしまったので、私はそれが許されることを願っています。.HttpWebRequestを使用した.Netクライアント認証の設定
私はクライアント認証が必要な安全なWebサービスにヒットしようとしています。私はこれを打つために4つの証明書を持っています。
•ルート証明書 •中間ルート証明書の秘密鍵
• •デバイス証明書 は、サーバーは、Javaであり、これらの本命は.jksであるトラストストアとキーストアを形成します。私はそれらを.pemファイルに引っ張った。
私はC#のクライアント側では失敗しました。だから、私はちょっとしたPythonスニペットを書いて、少なくともサーバー側が期待どおりに動作していることを確認したいと思いました。 20分後、私は安全な投稿をしています。そのコードは次のとおりです。
# Keys
path = "C:\\path\\"
key = path + "device.pem"
privkey = path + "device_privkey.pem"
CACerts = path + "truststore.concat" # root & intermediate cert
def post():
url = "/url"
headers = {'Content-Type': 'application/xml'}
## This section is HTTPSConnection
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.verify_mode = ssl.CERT_OPTIONAL
context.load_cert_chain(key, privkey, password='password')
context.verify_mode = ssl.CERT_NONE
context.load_verify_locations(CACerts)
conn = http.client.HTTPSConnection(host, port=8080, context=context)
conn.request("POST", url, registrationBody, headers)
response = conn.getresponse()
regresp = response.read()
concat証明書は、ルート証明書と中間証明書の連結です。
あなたは私と一緒にいますか?
私のC#/。ネット頭痛。
これは私の試みです。私はここで何をやっているのか分かりません。
public async Task POSTSecure(string pathname, string body)
{
string path = "C:\\path";
string key = path + "device.pem";
string privkey = path + "device_privkey.pem";
string CACerts1 = path + "vtn_root.pem";
string CACerts2 = path + "vtn_int.pem";
try
{
// Create certs from files
X509Certificate2 keyCert = new X509Certificate2(key);
X509Certificate2 rootCert = new X509Certificate2(CACerts1);
X509Certificate2 intCert = new X509Certificate2(CACerts2);
HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("https://" + host + ":" + port + pathname);
ServicePoint currentServicePoint = request.ServicePoint;
// build the client chain?
request.ClientCertificates.Add(keyCert);
request.ClientCertificates.Add(rootCert);
request.ClientCertificates.Add(intCert);
Console.WriteLine("URI: {0}", currentServicePoint.Address);
// This validates the server regardless of whether it should
request.ServerCertificateValidationCallback = ValidateServerCertificate;
request.Method = "POST";
request.ContentType = "application/xml";
request.ContentLength = body.Length;
using (var sendStream = request.GetRequestStream())
{
sendStream.Write(Encoding.UTF8.GetBytes(body), 0, body.Length);
}
var response = (HttpWebResponse)request.GetResponse();
}
catch (Exception e)
{
Console.WriteLine("Post error.");
}
}
助けを借りて、まともなチュートリアルへのポインタをありがとう。
[編集]もっと詳しくサーバー側では、デバッグが空のクライアント証明書チェーンを指しています。これは、serverhelloが完了したことを報告した直後です。
キャッチブロックの例外は何ですか? –
{"リクエストが中止されました:SSL/TLSのセキュリティで保護されたチャンネルを作成できませんでした。"}これまでにこの問題が発生し、サーバー側でエラーが発生しました。私は2日間これを乱してきた。 –
セキュリティプロトコルの設定を試しましたか? 'System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 |あなたのPythonコードでは 'ssl.SSLContext(ssl.PROTOCOL_TLS)'を指定しているので、SecurityProtocolType.Tls12; –