从https://stackoverflow.com/a/44524628?noredirect=1
跟进:-use_module(library(http/http_client)).
:-use_module(library(http/http_open)).
:-use_module(library(clpfd)).
url2('https://s3.amazonaws.com/echo.api/echo-api-cert-4.pem').
get_pem(Url,Certs):-
setup_call_cleanup(
http_open(Url,Stream,[]),
ssl_peer_certificate_chain(Stream,Certs),
close(Stream)
).
test(Key):-
url2(U),
get_pem(U,[A|Certs]),
checkcertvalid_time(A),
checkchain([A|Certs]),
memberchk(key(Key),A).
checkcertvalid_time(Acert):-
%what about The domain echo-api.amazon.com is present in the Subject Alternative Names (SANs) section of the signing certificate
memberchk(notbefore(NotBefore),Acert),
memberchk(notafter(NotAfter),Acert),
get_time(NowA),
Now is round(NowA),
Now #>NotBefore,
Now #<NotAfter.
checkchain(Chain):-
length(Chain,L),
L#>1, %Insure chain has more than one cert
checkchain_h(Chain).
checkchain_h([_]). %Reached the root.
checkchain_h([C1,C2|Rest]):-
memberchk(signature(Sig),C1),
memberchk(to_be_signed(Signed),C1),
memberchk(key(Key),C2),
hex_bytes(Signed,Bytes),
crypto_data_hash(Bytes,Hash,[algorithm(sha256),encoding(octet)]),
rsa_verify(Key,Hash,Sig,[type(sha256)]),
checkchain_h([C2|Rest]).
如果我拨打test/1
,则此代码在7.5.3-1-g647ce9a中有效,但在7.5.9中有效。在7.5.9中checkchain_h/1
在调用memberchk(to_be_signed(Signed),C1)
时失败。
这是在两台不同的计算机上测试的。是否存在可能导致这种差异的外部软件?
另外据我所知,还应该有一个字段,用于&#39; subject_alternative_name&#39;我无法看到。
更新: 在7.5.9:
OpenSSL 1.0.1t 3 May 2016
?- use_module(library(ssl)), current_prolog_flag(ssl_library_version, V).
V = 'OpenSSL 1.0.1t 3 May 2016'.
在7.5.3-1-g647ce9a上:
OpenSSL 1.0.2g 1 Mar 2016
?- use_module(library(ssl)), current_prolog_flag(ssl_library_version, V).
V = 'OpenSSL 1.0.2g 1 Mar 2016'.
答案 0 :(得分:5)
这是因为您使用的 OpenSSL库版本存在差异。
请参阅load_certificate/2
的文档,了解证书结构中当前可用的字段以及在何种条件下。
特别是:
使用 OpenSSL 1.0.2和更高版本,to_be_signed / 1也可用,产生证书的TBS(待签名)部分的十六进制表示。
对于您在其中一台计算机上使用的OpenSSL 1.0.1,此字段不可用。
顺便说一句,请注意OpenSSL 1.0.1是no longer supported!我强烈建议您在两台计算机上升级OpenSSL的安装。
关于您的其他问题,目前尚未在证书结构中提供主题替代名称。它们当然可以在将来使用!请密切关注最新版本的SWI文档,以便随时查看当前可用的内容。