2021-06-03 22:05:04 +00:00
|
|
|
#lang racket/base
|
2021-06-04 14:20:14 +00:00
|
|
|
;;; SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
;;; SPDX-FileCopyrightText: Copyright © 2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
|
|
|
|
2021-06-03 22:05:04 +00:00
|
|
|
;; Basically Macaroons [1] in a Dataspace context
|
|
|
|
;;
|
|
|
|
;; [1]: Birgisson, Arnar, Joe Gibbs Politz, Úlfar Erlingsson, Ankur
|
|
|
|
;; Taly, Michael Vrable, and Mark Lentczner. “Macaroons: Cookies with
|
|
|
|
;; Contextual Caveats for Decentralized Authorization in the Cloud.”
|
|
|
|
;; In Network and Distributed System Security Symposium. San Diego,
|
|
|
|
;; California: Internet Society, 2014.
|
|
|
|
|
|
|
|
(provide KEY_LENGTH
|
|
|
|
new-key
|
|
|
|
sturdy-encode
|
|
|
|
sturdy-decode
|
|
|
|
mint
|
2021-06-08 07:21:54 +00:00
|
|
|
attenuate-sturdy
|
2021-06-03 22:05:04 +00:00
|
|
|
SturdyRef-valid?
|
|
|
|
validate
|
|
|
|
(all-from-out "schemas/gen/sturdy.rkt"))
|
|
|
|
|
|
|
|
(require racket/match)
|
|
|
|
(require (only-in sha hmac-sha256))
|
|
|
|
(require (only-in racket/random crypto-random-bytes))
|
|
|
|
(require preserves)
|
2021-06-08 13:38:24 +00:00
|
|
|
(require preserves-schema)
|
2021-06-03 22:05:04 +00:00
|
|
|
(require "schemas/gen/sturdy.rkt")
|
|
|
|
|
|
|
|
(define KEY_LENGTH 16) ;; 128 bits
|
|
|
|
|
|
|
|
(define (new-key) (crypto-random-bytes KEY_LENGTH))
|
|
|
|
|
|
|
|
(define (embedded-not-allowed _)
|
|
|
|
(error 'embedded-not-allowed "Embedded Ref not permitted in SturdyRef"))
|
|
|
|
|
|
|
|
(define (sturdy-encode v)
|
|
|
|
(preserve->bytes v
|
|
|
|
#:canonicalizing? #t
|
|
|
|
#:encode-embedded embedded-not-allowed
|
|
|
|
#:write-annotations? #f))
|
|
|
|
|
|
|
|
(define (sturdy-decode bs)
|
|
|
|
(bytes->preserve bs
|
|
|
|
#:read-syntax? #f
|
|
|
|
#:decode-embedded embedded-not-allowed))
|
|
|
|
|
2021-06-08 07:28:48 +00:00
|
|
|
(define (signature key data)
|
|
|
|
(subbytes (hmac-sha256 key data) 0 KEY_LENGTH))
|
|
|
|
|
2021-06-03 22:05:04 +00:00
|
|
|
(define (mint oid key)
|
2021-06-08 07:28:48 +00:00
|
|
|
(SturdyRef oid '() (signature key (sturdy-encode oid))))
|
2021-06-03 22:05:04 +00:00
|
|
|
|
2021-06-08 07:21:54 +00:00
|
|
|
(define (attenuate-sturdy r . attenuation)
|
2021-06-03 22:05:04 +00:00
|
|
|
(match-define (SturdyRef oid caveatChain sig) r)
|
|
|
|
(SturdyRef oid
|
|
|
|
(append caveatChain (list attenuation))
|
2021-06-08 13:38:24 +00:00
|
|
|
(signature sig (sturdy-encode (->preserve attenuation)))))
|
2021-06-03 22:05:04 +00:00
|
|
|
|
|
|
|
(define (SturdyRef-valid? r key)
|
|
|
|
(match-define (SturdyRef oid caveatChain actual-sig) r)
|
|
|
|
(define expected-sig
|
2021-06-08 07:28:48 +00:00
|
|
|
(for/fold [(sig (signature key (sturdy-encode oid)))]
|
2021-06-03 22:05:04 +00:00
|
|
|
[(attenuation (in-list caveatChain))]
|
2021-06-08 13:38:24 +00:00
|
|
|
(signature sig (sturdy-encode (->preserve attenuation)))))
|
2021-06-03 22:05:04 +00:00
|
|
|
(equal? expected-sig actual-sig))
|
|
|
|
|
|
|
|
(define (validate r key)
|
|
|
|
(when (not (SturdyRef-valid? r key))
|
|
|
|
(error 'validate "Invalid SturdyRef"))
|
|
|
|
r)
|