gpt4 book ai didi

flutter - CERTIFICATE_VERIFY_FAILED 虽然它应该是有效的?

转载 作者:行者123 更新时间:2023-12-04 22:36:54 39 4
gpt4 key购买 nike

你知道为什么会调用 badCertificateCallback 吗?证书( Dart 声称无效)与我设置为受信任证书的证书完全相同(相等性检查为真!)。

import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:http/http.dart' as http;
import 'package:http/io_client.dart';

final PEM = new File(
'../server_keys/cert.pem',
).readAsBytesSync();

Future<http.Response> listDir(String path) async {
SecurityContext context = new SecurityContext(withTrustedRoots: false);

context.setTrustedCertificatesBytes(PEM);

print("context setup");
final httpClient = HttpClient(context: context);

httpClient.badCertificateCallback = ((cert, host, port) {
print("In bad certificate callback.");
print('Subject: ${cert.subject}');
print('Issuer: ${cert.issuer}');
print('Expires: ${cert.endValidity}');
print('Host: ${host}');
print('Port: ${port}');
return String.fromCharCodes(PEM) == cert.pem;
});

print("get url");

final client = IOClient(httpClient);
print("POSTING");
return client
.post(
Uri.parse('https://127.0.0.1:5000/list'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'path': path,
}),
)
.timeout(const Duration(seconds: 5));
}

void main() async {
await listDir("admin/user/test").then((response) {
print(response.body);
});
print("finished");
}
此外,这工作得很好:
curl -v --cacert ../server_keys/cert.pem https://127.0.0.1:5000/list
也许你可以给我一个提示。
更多信息:
这是证书:
-----BEGIN CERTIFICATE-----
MIIE0jCCAroCEQCpB3UQ/DdGjoFQykLNzSJcMA0GCSqGSIb3DQEBDQUAMCcxETAP
BgNVBAoMCFhZWkNsb3VkMRIwEAYDVQQDDAkxMjcuMC4wLjEwHhcNMjIwNjE3MTI1
ODM2WhcNMzIwNjE0MTI1ODM2WjAnMREwDwYDVQQKDAhYWVpDbG91ZDESMBAGA1UE
AwwJMTI3LjAuMC4xMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAq8Hg
MaLMUzrDFb5SkpkwpT/0u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ekDDaN
CvhDf9BOtLrbFRnZJqxWKITxZw9U58yEeBgIXFQviYUL81tcWk8QyOBrocNBohMz
C+SypMIGPL21+Xv87MwMk/ied5J4V0rY93NOC+MDC6mTbJ1EOr/XEs+TxTu8AnDt
0EhjzXlPj4G100fmaP9BHjMPy8ReSy09bzAU4GPs8dAl7x6aNutyi39E7WeUgJNm
Odv4Wz7LwxX3t/cWkgSoKFT5DuDpBlafbI7ACWCjVe5mgd7cx75mv6pMTqJI8fEA
/OW44WN6IaLHyjG8BeHEt1yVld9RbCeyUY7A4cx94wwpD7TfPlI=
-----END CERTIFICATE-----
这是 python flask 服务器代码(现在使用 localhost):
import os
from flask import Flask, jsonify
import ssl

app = Flask(__name__)

@app.route("/list", methods = ['POST', 'GET'])
def list_route():
return jsonify(isError= False,
message= "Success",
statusCode= 200,
data={
"path": "hihi"
}), 200


if __name__ == "__main__":
server_keys = {
"url": "localhost",
"cert_file": "server_keys/cert.pem",
"key_file": "server_keys/key.pem",
}
os.system("openssl req -x509 -nodes -new -sha256 -days 390 -newkey rsa:4096 -keyout server_keys/key.pem -out server_keys/cert.pem -subj '/C=de/CN=localhost'")
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(server_keys["cert_file"], server_keys["key_file"])
app.run(debug=True, ssl_context=context, host=server_keys["url"], port="5000")
它正在使用 curl:
curl -v --cacert ../server_keys/cert.pem https://localhost:5000/list
但仍然不适合我使用 Dart (使用英特尔 Mac OSX 货币)。

最佳答案

看起来你偶然发现了 Dart 的一个错误。有an open issue关于这个确切的问题。它只出现在 macOS 上,但在 Windows/Linux/Android 上运行完美......
下面是生成 bash 中的 key 的命令.我自己测试了这个脚本,它可以在 Windows 上运行,但在 macOS 上失败。

openssl req -x509 -out localhost.crt -keyout localhost.key \
-newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' -extensions EXT \
-config <(printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")

关于flutter - CERTIFICATE_VERIFY_FAILED 虽然它应该是有效的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72659221/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com