This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
artifact.rkt
330 lines (285 loc) · 11.7 KB
/
artifact.rkt
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
#lang racket/base
(require racket/contract
"integrity.rkt"
"signature.rkt")
(provide
(struct-out artifact)
(struct-out $artifact)
(struct-out $artifact:signature)
(struct-out $artifact:integrity)
(contract-out
[install-artifact
(-> artifact?
path-string?
(subprogram/c (cons/c path-record? path-record?)))]
[verify-artifact
(-> artifact?
path-record?
(subprogram/c void?))]
[fetch-artifact
(-> string?
artifact?
(subprogram/c path-record?))]
[make-artifact
(->* (source-variant?)
((or/c #f integrity?)
(or/c #f signature?))
artifact?)]
[lock-artifact
(->* (artifact?)
(exhaust/c
#:content? any/c
#:integrity? any/c
#:signature? any/c
#:content-budget budget/c
#:digest-budget budget/c
#:public-key-budget budget/c
#:signature-budget budget/c)
artifact?)]))
(require racket/match
"crypto.rkt"
"format.rkt"
"state.rkt"
"subprogram.rkt"
"message.rkt"
"monad.rkt"
"port.rkt"
"printer.rkt"
"source.rkt")
(define-message $artifact ())
(define-message $artifact:integrity (status chf-symbol))
(define-message $artifact:signature (status public-key))
(struct artifact
(source ; Defines where bytes come from
integrity ; Integrity information: Did I get the right bytes?
signature)) ; Signature for authentication: Did the bytes come from someone I trust?
(define (make-artifact source [integrity #f] [signature #f])
(artifact source integrity signature))
(define (install-artifact arti link-path)
(mdo file-record := (fetch-artifact (format "~a" link-path) arti)
(verify-artifact arti file-record) ; Security critical
(subprogram-unit (cons (make-addressable-link file-record link-path)
file-record))))
(define (verify-artifact arti record)
(mdo (check-artifact-integrity arti (path-record-path record))
(check-artifact-signature arti (path-record-path record))
(subprogram-unit (void))))
(define (fetch-artifact name arti)
(subprogram-fetch name
(coerce-source (artifact-source arti))
(λ (in est-size)
(make-addressable-file
#:cache-key (make-source-key (coerce-source (artifact-source arti)))
#:max-size (mebibytes->bytes (DENXI_FETCH_TOTAL_SIZE_MB))
#:buffer-size (mebibytes->bytes (DENXI_FETCH_BUFFER_SIZE_MB))
#:timeout-ms (DENXI_FETCH_TIMEOUT_MS)
#:on-status (make-on-status (current-message-formatter))
name
in est-size))))
(define (lock-artifact #:content? [content? #t]
#:integrity? [integrity? #t]
#:signature? [signature? #t]
#:content-budget [content-budget (* 1024 200)]
#:digest-budget [digest-budget +inf.0]
#:public-key-budget [public-key-budget +inf.0]
#:signature-budget [signature-budget +inf.0]
arti
[exhaust raise])
(call/cc
(λ (abort)
(define (exhaust* v)
(abort (exhaust v)))
(let ([lock (λ (? s c) (if (and ? s) (c s) s))])
(artifact (lock content?
(artifact-source arti)
(λ (content)
(lock-source content
content-budget
exhaust*)))
(lock integrity?
(artifact-integrity arti)
(λ (intinfo)
(lock-integrity #:digest-budget digest-budget
intinfo
exhaust*)))
(lock signature?
(artifact-signature arti)
(λ (siginfo)
(lock-signature #:public-key-budget public-key-budget
#:signature-budget signature-budget
siginfo
exhaust*))))))))
(define ((make-on-status formatter) m)
(if ($transfer:progress? ($transfer:scope-message m))
(printf "\r~a~a" (formatter m)
(if (equal? ($transfer:progress-bytes-read ($transfer:scope-message m))
($transfer:progress-max-size ($transfer:scope-message m)))
"\n" ""))
(write-message #:newline? #f m formatter)))
(define-subprogram (check-artifact-integrity arti workspace-relative-path)
(match-define (artifact content int sig) arti)
(define-values (int/use chf)
(if (well-formed-integrity? int)
(values (lock-integrity int)
(integrity-chf-symbol int))
(values #f #f)))
(define status
(check-integrity
#:trust-bad-digest (DENXI_TRUST_BAD_DIGEST)
(make-user-chf-trust-predicate)
int/use
(and chf
(make-digest (build-workspace-path workspace-relative-path)
(integrity-chf-symbol (artifact-integrity arti))))))
($attach (or (integrity-check-passed? status) FAILURE)
($artifact:integrity status chf)))
(define-subprogram (check-artifact-signature arti path)
(match-define (artifact content int sig) arti)
(define sig/use
(and (well-formed-signature? sig)
(lock-signature sig)))
(define int/use
(and (well-formed-integrity? int)
(lock-integrity int)))
(define trust-public-key?
(if (DENXI_TRUST_ANY_PUBLIC_KEY)
(λ (p) #t)
(bind-trust-list (DENXI_TRUST_PUBLIC_KEYS))))
(define status
(check-signature #:trust-unsigned (DENXI_TRUST_UNSIGNED)
#:trust-bad-digest (DENXI_TRUST_BAD_DIGEST)
#:trust-public-key? trust-public-key?
#:verify-signature (current-verify-signature)
sig/use
int/use))
($attach (or (signature-check-passed? status) FAILURE)
($artifact:signature status
(and (signature? sig/use)
(signature-public-key sig/use)))))
(module+ test
(require rackunit
racket/file
(submod "state.rkt" test)
(submod "subprogram.rkt" test))
(test-workspace "Fetch artifacts"
(define data #"abc")
(parameterize ([current-output-port (open-output-nowhere)])
(call-with-snake-oil-chf-trust
(λ ()
(check-subprogram
(fetch-artifact "anon" (make-artifact (byte-source data)))
(λ (record messages)
(check-pred path-record? record)
(check-equal? (file->bytes (path-record-path record)) data)
(test-case "Verify artifacts"
(define intinfo
(make-trusted-integrity data))
(define siginfo
(make-snake-oil-signature
(integrity-digest intinfo)
(integrity-chf-symbol intinfo)))
(define arti
(artifact (byte-source data) intinfo siginfo))
(check-subprogram (verify-artifact arti record)
(λ (result messages)
(check-equal? result FAILURE)))
(call-with-snake-oil-cipher-trust
(λ ()
(check-subprogram (verify-artifact arti record)
(λ (result messages)
(check-pred void? result))))))))))))
(test-case "Lock artifacts"
(define with-content
(artifact (text-source "qr")
(integrity 'sha1 (text-source "st"))
(signature (text-source "uv")
(text-source "wx"))))
(define (try content?
integrity?
signature?
content-budget
digest-budget
public-key-budget
signature-budget
[arti with-content])
(lock-artifact #:content? content?
#:integrity? integrity?
#:signature? signature?
#:content-budget content-budget
#:digest-budget digest-budget
#:public-key-budget public-key-budget
#:signature-budget signature-budget
arti
values))
(check-match (try #t #t #t +inf.0 +inf.0 +inf.0 +inf.0)
(artifact #"qr"
(integrity 'sha1 #"st")
(signature #"uv" #"wx")))
(check-match (try #f #t #t +inf.0 +inf.0 +inf.0 +inf.0)
(artifact (text-source "qr")
(integrity 'sha1 #"st")
(signature #"uv" #"wx")))
(check-match (try #t #f #t +inf.0 +inf.0 +inf.0 +inf.0)
(artifact #"qr"
(integrity 'sha1 (text-source "st"))
(signature #"uv" #"wx")))
(check-match (try #t #t #f +inf.0 +inf.0 +inf.0 +inf.0)
(artifact #"qr"
(integrity 'sha1 #"st")
(signature (text-source "uv")
(text-source "wx"))))
(check-match (try #t #t #t 0 +inf.0 +inf.0 +inf.0)
(artifact (text-source "qr")
(integrity 'sha1 #"st")
(signature #"uv" #"wx")))
(check-match (try #t #t #t 0 0 +inf.0 +inf.0)
(artifact (text-source "qr")
(integrity 'sha1 (text-source "st"))
(signature #"uv" #"wx")))
(check-match (try #t #t #t 0 0 0 +inf.0)
(artifact (text-source "qr")
(integrity 'sha1 (text-source "st"))
(signature (text-source "uv") #"wx")))
(define (check-exhaust arti [expected 1])
(check-equal? (lock-artifact arti values)
expected))
(check-exhaust
(artifact (exhausted-source 1)
(integrity 'sha1 #"")
(signature #"" #"")))
(check-exhaust
(artifact #""
(integrity 'sha1 (exhausted-source 1))
(signature #"" #"")))
(check-exhaust
(artifact #""
(integrity 'sha1 #"")
(signature (exhausted-source 1) #"")))
(check-exhaust
(artifact #""
(integrity 'sha1 #"")
(signature #"" (exhausted-source 1))))
(check-exhaust
(artifact (exhausted-source 1)
(integrity 'sha1 (exhausted-source 2))
(signature (exhausted-source 3)
(exhausted-source 4)))
1)
(check-exhaust
(artifact #""
(integrity 'sha1 (exhausted-source 2))
(signature (exhausted-source 3)
(exhausted-source 4)))
2)
(check-exhaust
(artifact #""
(integrity 'sha1 #"")
(signature (exhausted-source 3)
(exhausted-source 4)))
3)
(check-exhaust
(artifact #""
(integrity 'sha1 #"")
(signature #""
(exhausted-source 4)))
4)))