-
Notifications
You must be signed in to change notification settings - Fork 14
/
xmldsig.pl
391 lines (337 loc) · 16.5 KB
/
xmldsig.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* Part of SWI-Prolog
Author: Jan Wielemaker and Matt Lilley
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2016, CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(xmldsig,
[ xmld_signed_DOM/3, % +DOM, -SignedDOM, +Options
xmld_verify_signature/4 % +DOM, +Signature, -Certificate, +Options
]).
:- autoload(library(base64),[base64/3,base64/2]).
:- autoload(library(c14n2),[xml_write_canonical/3]).
:- autoload(library(crypto),
[crypto_data_hash/3,rsa_sign/4,hex_bytes/2,rsa_verify/4]).
:- use_module(library(debug),[debug/3]).
:- autoload(library(error),
[type_error/2,domain_error/2,existence_error/2]).
:- autoload(library(lists),[member/2]).
:- autoload(library(option),[option/3,option/2]).
:- autoload(library(sha),[sha_hash/3]).
:- autoload(library(ssl),[load_private_key/3,certificate_field/2]).
:- autoload(library(xmlenc),[load_certificate_from_base64_string/2]).
/** <module> XML Digital signature
This library deals with _XMLDSIG_, RSA signed XML documents.
@see http://www.di-mgt.com.au/xmldsig.html
@see https://www.bmt-online.org/geekisms/RSA_verify
@see http://stackoverflow.com/questions/5576777/whats-the-difference-between-nid-sha-and-nid-sha1-in-openssl
*/
xmldsig_ns('http://www.w3.org/2000/09/xmldsig#').
%! xmld_signed_DOM(+DOM, -SignedDOM, +Options) is det.
%
% Translate an XML DOM structure in a signed version. Options:
%
% - key_file(+File)
% File holding the private key needed to sign
% - key_password(+Password)
% String holding the password to op the private key.
%
% The SignedDOM must be emitted using xml_write/3 or
% xml_write_canonical/3. If xml_write/3 is used, the option
% layout(false) is needed to avoid changing the layout of the
% =SignedInfo= element and the signed DOM, which will cause the
% signature to be invalid.
xmld_signed_DOM(DOM, SignedDOM, Options) :-
dom_hash(DOM, ODOM, Hash, Options),
signed_info(Hash, Signature, SDOM, KeyDOM, Options),
signed_xml_dom(ODOM, SDOM, KeyDOM, Signature, SignedDOM, Options).
%! dom_hash(+DOM, -ODOM, -Hash, +Options) is det.
%
% Compute the digest for DOM.
%
% @arg Hash is the base64 encoded version of the selected SHA
% algorithm.
dom_hash(DOM, ODOM, Hash, Options) :-
object_c14n(DOM, ODOM, C14N),
hash(C14N, Hash, Options).
object_c14n(DOM, ODOM, C14N) :-
object_dom(DOM, ODOM),
with_output_to(
string(C14N),
xml_write_canonical(current_output, ODOM, [])).
object_dom(DOM0,
element(NS:'Object', ['Id'='object', xmlns=NS], DOM)) :-
xmldsig_ns(NS),
to_list(DOM0, DOM).
to_list(DOM, DOM) :- DOM = [_|_].
to_list(DOM, [DOM]).
hash(C14N, Hash, Options) :-
option(hash(Algo), Options, sha1),
sha_hash(C14N, HashCodes, [algorithm(Algo)]),
phrase(base64(HashCodes), Base64Codes),
string_codes(Hash, Base64Codes).
%! signed_info(+Hash, -Signature, -SDOM, -KeyDOM, +Options)
signed_info(Hash, Signature, SDOM, KeyDOM, Options) :-
signed_info_dom(Hash, SDOM, Options),
with_output_to(
string(SignedInfo),
xml_write_canonical(current_output, SDOM, [])),
rsa_signature(SignedInfo, Signature, KeyDOM, Options).
%! signed_info_dom(+Hash, -SDOM, +Options) is det.
%
% True when SDOM is the xmldsign:Signature DOM for an object with
% the given Hash.
signed_info_dom(Hash, SDOM, _Options) :-
SDOM = element(NS:'SignedInfo', [xmlns=NS],
[ '\n ',
element(NS:'CanonicalizationMethod',
['Algorithm'=C14NAlgo], []),
'\n ',
element(NS:'SignatureMethod',
['Algorithm'=SignatureMethod], []),
'\n ',
Reference,
'\n'
]),
Reference = element(NS:'Reference', ['URI'='#object'],
[ '\n ',
element(NS:'DigestMethod',
['Algorithm'=DigestMethod], []),
'\n ',
element(NS:'DigestValue', [], [Hash]),
'\n '
]),
xmldsig_ns(NS),
DigestMethod='http://www.w3.org/2000/09/xmldsig#sha1',
C14NAlgo='http://www.w3.org/TR/2001/REC-xml-c14n-20010315',
SignatureMethod='http://www.w3.org/2000/09/xmldsig#rsa-sha1'.
%! rsa_signature(+SignedInfo:string, -Signature, -KeyDOM, +Options)
rsa_signature(SignedInfo, Signature, KeyDOM, Options) :-
option(algorithm(Algorithm), Options, sha1),
crypto_data_hash(SignedInfo, Digest, [algorithm(Algorithm)]),
string_upper(Digest, DIGEST),
debug(xmldsig, 'SignedInfo ~w digest = ~p', [Algorithm, DIGEST]),
private_key(Key, Options),
rsa_key_dom(Key, KeyDOM),
rsa_sign(Key, Digest, String,
[ type(Algorithm)
]),
string_length(String, Len),
debug(xmldsig, 'RSA signatute length: ~p', [Len]),
string_codes(String, Codes),
phrase(base64(Codes), Codes64),
string_codes(Signature, Codes64).
private_key(Key, Options) :-
option(key_file(File), Options),
option(key_password(Password), Options),
!,
setup_call_cleanup(
open(File, read, In, [type(binary)]),
load_private_key(In, Password, Key),
close(In)).
private_key(_Key, Options) :-
\+ option(key_file(_), Options),
!,
throw(error(existence_error(option, key_file, Options),_)).
private_key(_Key, Options) :-
throw(error(existence_error(option, key_password, Options),_)).
%! rsa_key_dom(+Key, -DOM) is det.
%
% Produce the KeyInfo node from the private key.
rsa_key_dom(Key,
element(NS:'KeyInfo', [xmlns=NS],
[ element(NS:'KeyValue', [],
[ '\n ',
element(NS:'RSAKeyValue', [],
[ '\n ',
element(NS:'Modulus', [], [Modulus]),
'\n ',
element(NS:'Exponent', [], [Exponent]),
'\n '
]),
'\n'
])
])) :-
key_info(Key, Info),
_{modulus:Modulus, exponent:Exponent} :< Info,
xmldsig_ns(NS).
%! key_info(+Key, -Info) is det.
%
% Extract the RSA modulus and exponent from a private key. These
% are the first end second field of the rsa term. They are
% represented as hexadecimal encoded bytes. We must recode this to
% base64.
%
% @tbd Provide better support from library(ssl).
key_info(private_key(Key), rsa{modulus:Modulus, exponent:Exponent}) :-
!,
base64_bignum_arg(1, Key, Modulus),
base64_bignum_arg(2, Key, Exponent).
key_info(Key, _) :-
type_error(private_key, Key).
base64_bignum_arg(I, Key, Value) :-
arg(I, Key, HexModulesString),
string_codes(HexModulesString, HexModules),
hex_bytes(HexModules, Bytes),
phrase(base64(Bytes), Bytes64),
string_codes(Value, Bytes64).
signed_xml_dom(ObjectDOM, SDOM, KeyDOM, Signature, SignedDOM, _Options) :-
SignedDOM = element(NS:'Signature', [xmlns=NS],
[ '\n', SDOM,
'\n', element(NS:'SignatureValue', [], [Signature]),
'\n', KeyDOM,
'\n', ObjectDOM,
'\n'
]),
xmldsig_ns(NS).
%! xmld_verify_signature(+DOM, +SignatureDOM, -Certificate, +Options) is det.
%
% Confirm that an `ds:Signature` element contains a valid
% signature. Certificate is bound to the certificate that appears
% in the element if the signature is valid. It is up to the caller
% to determine if the certificate is trusted or not.
%
% *Note*: The DOM and SignatureDOM must have been obtained using
% the load_structure/3 option keep_prefix(true) otherwise it is
% impossible to generate an identical document for checking the
% signature. See also xml_write_canonical/3.
xmld_verify_signature(DOM, SignatureDOM, Certificate, Options) :-
signature_info(DOM, SignatureDOM, SignedInfo, Algorithm, Signature,
PublicKey, Certificate, CanonicalizationMethod),
base64(RawSignature, Signature),
( Algorithm = rsa(HashType)
-> with_output_to(string(C14N),
xml_write_canonical(current_output, SignedInfo,
[method(CanonicalizationMethod)|Options])),
crypto_data_hash(C14N, Digest, [algorithm(HashType)]),
atom_codes(RawSignature, Codes),
hex_bytes(HexSignature, Codes),
rsa_verify(PublicKey, Digest, HexSignature, [type(HashType)])
; domain_error(supported_signature_algorithm, Algorithm)
).
ssl_algorithm('http://www.w3.org/2000/09/xmldsig#rsa-sha1', rsa(sha1)).
ssl_algorithm('http://www.w3.org/2000/09/xmldsig#dsa-sha1', dsa(sha1)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#hmac-md5', hmac(md5)). % NB: Requires a parameter
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#hmac-sha224', hmac(sha224)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#hmac-sha256', hmac(sha256)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#hmac-sha384', hmac(sha384)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#hmac-sha512', hmac(sha512)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#rsa-md5', rsa(md5)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#rsa-sha256', rsa(sha256)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#rsa-sha384', rsa(sha384)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#rsa-sha512', rsa(sha512)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#rsa-ripemd160', rsa(ripemd160)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha1', ecdsa(sha1)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha224', ecdsa(sha224)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256', ecdsa(sha256)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha384', ecdsa(sha384)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha512', ecdsa(sha512)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#esign-sha1', esign(sha1)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#esign-sha224', esign(sha224)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#esign-sha256', esign(sha256)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#esign-sha384', esign(sha384)).
ssl_algorithm('http://www.w3.org/2001/04/xmldsig-more#esign-sha512', esign(sha512)).
digest_method('http://www.w3.org/2000/09/xmldsig#sha1', sha1).
digest_method('http://www.w3.org/2001/04/xmlenc#sha256', sha256).
signature_info(DOM, Signature, SignedData, Algorithm, SignatureValue,
PublicKey, Certificate, CanonicalizationMethod) :-
xmldsig_ns(NSRef),
memberchk(element(ns(_, NSRef):'SignatureValue', _, [RawSignatureValue]), Signature),
atom_codes(RawSignatureValue, RawSignatureCodes),
delete_newlines(RawSignatureCodes, SignatureCodes),
string_codes(SignatureValue, SignatureCodes),
memberchk(element(ns(Prefix, NSRef):'SignedInfo', SignedInfoAttributes, SignedInfo), Signature),
SignedData = element(ns(Prefix, NSRef):'SignedInfo', SignedInfoAttributes, SignedInfo),
memberchk(element(ns(_, NSRef):'CanonicalizationMethod', CanonicalizationMethodAttributes, _), SignedInfo),
memberchk('Algorithm'=CanonicalizationMethod, CanonicalizationMethodAttributes),
forall(memberchk(element(ns(_, NSRef):'Reference', ReferenceAttributes, Reference), SignedInfo),
verify_digest(ReferenceAttributes, CanonicalizationMethod, Reference, DOM)),
memberchk(element(ns(_, NSRef):'SignatureMethod', SignatureMethodAttributes, []), SignedInfo),
memberchk('Algorithm'=XMLAlgorithm, SignatureMethodAttributes),
ssl_algorithm(XMLAlgorithm, Algorithm),
memberchk(element(ns(_, NSRef):'KeyInfo', _, KeyInfo), Signature),
( memberchk(element(ns(_, NSRef):'X509Data', _, X509Data), KeyInfo),
memberchk(element(ns(_, NSRef):'X509Certificate', _, [X509Certificate]), X509Data)->
load_certificate_from_base64_string(X509Certificate, Certificate),
certificate_field(Certificate, public_key(PublicKey))
; throw(not_implemented)
).
delete_newlines([], []):- !.
delete_newlines([13|As], B):- !, delete_newlines(As, B).
delete_newlines([10|As], B):- !, delete_newlines(As, B).
delete_newlines([A|As], [A|B]):- !, delete_newlines(As, B).
verify_digest(ReferenceAttributes, CanonicalizationMethod, Reference, DOM):-
xmldsig_ns(NSRef),
memberchk('URI'=URI, ReferenceAttributes),
atom_concat('#', Id, URI),
% Find the relevant bit of the DOM
resolve_reference(DOM, Id, Digestible, _NSMap),
( memberchk(element(ns(_, NSRef):'Transforms', _, Transforms), Reference)
-> findall(TransformAttributes-Transform,
member(element(ns(_, NSRef):'Transform', TransformAttributes, Transform), Transforms),
TransformList)
; TransformList = []
),
apply_transforms(TransformList, Digestible, TransformedDigestible),
memberchk(element(ns(_, NSRef):'DigestMethod', DigestMethodAttributes, _), Reference),
memberchk(element(ns(_, NSRef):'DigestValue', _, [DigestBase64]), Reference),
memberchk('Algorithm'=Algorithm, DigestMethodAttributes),
( digest_method(Algorithm, DigestMethod)
-> true
; domain_error(supported_digest_method, DigestMethod)
),
with_output_to(string(XMLString), xml_write_canonical(current_output, TransformedDigestible, [method(CanonicalizationMethod)])),
sha_hash(XMLString, DigestBytes, [algorithm(DigestMethod)]),
base64(ExpectedDigest, DigestBase64),
atom_codes(ExpectedDigest, ExpectedDigestBytes),
( ExpectedDigestBytes == DigestBytes
-> true
; throw(error(invalid_digest, _))
).
resolve_reference([element(Tag, Attributes, Children)|_], ID, element(Tag, Attributes, Children), []):-
memberchk('ID'=ID, Attributes),
!.
resolve_reference([element(_, Attributes, Children)|Siblings], ID, Element, Map):-
( findall(xmlns:Prefix=URI,
member(xmlns:Prefix=URI, Attributes),
Map,
Tail),
resolve_reference(Children, ID, Element, Tail)
; resolve_reference(Siblings, ID, Element, Map)
).
apply_transforms([], X, X):- !.
apply_transforms([Attributes-Children|Transforms], In, Out):-
memberchk('Algorithm'=Algorithm, Attributes),
( apply_transform(Algorithm, Children, In, I1)
-> true
; existence_error(transform_algorithm, Algorithm)
),
apply_transforms(Transforms, I1, Out).
apply_transform('http://www.w3.org/2001/10/xml-exc-c14n#', [], X, X).
apply_transform('http://www.w3.org/2000/09/xmldsig#enveloped-signature', [], element(Tag, Attributes, Children), element(Tag, Attributes, NewChildren)):-
delete_signature_element(Children, NewChildren).
delete_signature_element([element(ns(_, 'http://www.w3.org/2000/09/xmldsig#'):'Signature', _, _)|Siblings], Siblings):- !.
delete_signature_element([A|Siblings], [A|NewSiblings]):-
delete_signature_element(Siblings, NewSiblings).