-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticate_alexa.pl
164 lines (139 loc) · 5.08 KB
/
authenticate_alexa.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
:-module(authenticate_alexa,[authenticate_alexa/1]).
:- use_module(library(base64)).
:- use_module(library(clpfd)).
:- use_module(library(crypto)).
:- use_module(library(dif)).
:- use_module(library(listing)).
:- use_module(library(ssl)).
:- use_module(library(uri)).
authenticate_alexa(Request):-
check_sigchainurl(Request,URL),
portray_clause(myout,URL),
get_certs(URL,[ACert|CertsRest]),
checkcertvalid_time(ACert),
checkchain([ACert|CertsRest]),
memberchk(key(Key),ACert),
base64decodesig_encryptedSig(Request,ESig),
% httpsbody(Request,Body), %how do we read the body of the request?
httpsbodybytes(Request,Body),
portray_clause(myout,'Http Body from httpsbody(Request,Body) :'),
portray_clause(myout,Body),
crypto_data_hash(Body,Hash,[encoding(octet)]),
portray_clause(myout,'Serviced signed Hash:'),
portray_clause(myout,Hash),
atom_string(Hash,HashString),
string_concat("0x",HashString,HashString2),
number_string(HashNumber,HashString2),
portray_clause(myout,'Hash as Dec'),
portray_clause(myout,HashNumber),
signature_pow(ESig,0x010001,Key,ClpfdHash),
portray_clause(myout,'clpfdhash:'),
portray_clause(myout,'Got here'),
portray_clause(myout,ClpfdHash),
%rsa_verify(Key,Hash,ESig,[type(sha1)]),
portray_clause(myout,done).
get_certs(URL,Certs):-
setup_call_cleanup(
http_open(URL,Stream,[]),
ssl_peer_certificate_chain(Stream,Certs),
close(Stream)
).
signature_pow(Sig, Exp, P, Pow) :-
portray_clause(myout,'ESig:'),
portray_clause(myout,Sig),
(atom(Sig) -> portray_clause(myout,'it is an atom');portray_clause(myout,'it is not an atom')),
(number(Sig) -> portray_clause(myout,'it is a number');portray_clause(myout,'it is not a number')),
atom_string(Sig,Sigstring),
string_concat("0x",Sigstring,SigString2),
number_string(SigNumber,SigString2),
portray_clause(myout,'Exp:'),
portray_clause(myout,Exp),
%portray_clause(myout,'P:'),
%portray_clause(myout,P),
P =public_key(rsa(P2,_EXP2,_,_,_,_,_,_)),
%portray_clause(myout,'P just number:'),
%portray_clause(myout,P2),
(string(P2) -> portray_clause(myout,'String hex P number');portray_clause(myout,notstring)),
string_concat("0x",P2,NewString),
%portray_clause(myout,NewString),
number_string(P3,NewString),
portray_clause(myout,'P number as dec:'),
portray_clause(myout,P3),
Pow #= SigNumber^Exp mod P3,
portray_clause(myout,'Pow'),
portray_clause(myout,Pow),
portray_clause(myout,verified).
%! check_sigchainurl(+Request:compound, -Url:atom) is semidet.
%
% Verify the Signature Certificate URL.
check_sigchainurl(Request,URL):-
memberchk(signaturecertchainurl(URL),Request),
% The protocol is equal to `https` (case insensitive).
uri_components(URL, uri_components(https,Authority,Path,_,_)),
% The hostname is equal to `s3.amazonaws.com` (case insensitive).
uri_authority_components(
Authority,
uri_authority(_,_,'s3.amazonaws.com',Port)
),
% The path starts with `/echo.api/` (case sensitive).
atom_prefix(Path, '/echo.api/'),
% If a port is defined in the URL, the port is equal to 443.
(var(Port) -> true ; Port =:= 443).
checkcertvalid_time(Acert):-
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
%portray_clause(myout,Chain),
%checkchain_h(Chain).
checkchain_h([_]). %Reached the root.
checkchain_h(Chain):-
Chain =[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]).
base64decodesig_encryptedSig(Request,Hex):-
memberchk(signature(B64Sig),Request),
portray_clause(myout,'base64 encoded sig:'),
portray_clause(myout,B64Sig),
base64(ESig,B64Sig),
atom_codes(ESig,Bytes),
hex_bytes(Hex,Bytes).
%portray_clause(myout,'Esig is base64 sig decoded using base64//2:'),
%portray_clause(myout,ESig).
%portray_clause(myout,'Can not print esig').
httpsbodybytes(Request,BodyBytes2):-
memberchk(input(In),Request),
portray_clause(myout,"Bytes:"),
mygetbody2(In,BodyBytes,0),
list_butlast(BodyBytes,BodyBytes2).
mygetbody2(_Stream,[],-1).
mygetbody2(Stream,[Byte|Bytes],Check):-
dif(Check,-1),
get_byte(Stream,Byte),
%portray_clause(myout,Byte),
mygetbody2(Stream,Bytes,Byte).
list_butlast([X|Xs], Ys) :- % use auxiliary predicate ...
list_butlast_prev(Xs, Ys, X). % ... which lags behind by one item
list_butlast_prev([], [], _).
list_butlast_prev([X1|Xs], [X0|Ys], X0) :-
list_butlast_prev(Xs, Ys, X1).
httpsbody(Request,Body):-
memberchk(input(In),Request),
portray_clause(myout,In),
mygetbody(In,Body).
%http_read_data(Request,Body,[]). %I think this is for data posted
mygetbody(Stream,String1):-
read_string(Stream,"\n","",_E,String1),
portray_clause(myout,'E Value of read string:'),
portray_clause(myout,'not printable').
%portray_clause(myout,E).