diff options
Diffstat (limited to 'scripts/continuum-signed')
| -rwxr-xr-x | scripts/continuum-signed | 51 |
1 files changed, 36 insertions, 15 deletions
diff --git a/scripts/continuum-signed b/scripts/continuum-signed index b66949f..b4d9498 100755 --- a/scripts/continuum-signed +++ b/scripts/continuum-signed @@ -88,6 +88,30 @@ login() { jq -r .token } +# Whether $1 is an envelope as continuum reads one: `cv2.`, then the value, the meta and +# the signature, each in base64url, the value one line of JSON and the meta one line +# holding its six fields, no more. Anything else is a plain value, replaced as one. +is_envelope() { + case $1 in + cv2.*.*.*) ;; + *) return 1 ;; + esac + [ "$(printf %s "$1" | tr -cd . | wc -c)" -eq 3 ] || return 1 + for part in 2 3; do + [ "$(printf %s "$1" | cut -d. -f$part | unb64url 2>/dev/null | wc -l)" -eq 0 ] || + return 1 + done + printf %s "$1" | cut -d. -f2 | unb64url 2>/dev/null | jq empty 2>/dev/null || return 1 + printf %s "$1" | cut -d. -f3 | unb64url 2>/dev/null | jq -e ' + type == "object" and (keys == ["at", "path", "prev", "seq", "swarm", "user"]) + and (.swarm | type == "string") and (.user | type == "string") + and (.prev | type == "string") + and (.path | type == "array" and all(type == "string")) + and ([.at, .seq] | all(type == "number" and . >= 0 and . == floor)) + ' >/dev/null 2>&1 || return 1 + printf %s "$1" | cut -d. -f4 | unb64url >/dev/null 2>&1 +} + # The envelope at $1, as stored; nothing if there is none. raw() { curl -sf -H "Authorization: Bearer $CONTINUUM_TOKEN" -H 'Accept: text/plain' \ @@ -99,13 +123,12 @@ raw() { write() { [ -n "$user" ] || die "who signs? set CONTINUUM_USER" if [ $# -ge 3 ]; then current=$3; else current=$(raw "$1"); fi - case $current in - cv2.*) - prev=$(printf %s "$current" | sha256sum | cut -d' ' -f1) - seq=$(( $(printf %s "$current" | cut -d. -f3 | unb64url | jq -r .seq) + 1 )) - ;; - *) prev= seq=0 ;; - esac + if is_envelope "$current"; then + prev=$(printf %s "$current" | sha256sum | cut -d' ' -f1) + seq=$(( $(printf %s "$current" | cut -d. -f3 | unb64url | jq -r .seq) + 1 )) + else + prev= seq=0 + fi swarm=$(curl -sf -H "Authorization: Bearer $CONTINUUM_TOKEN" "$url/auth/whoami" | jq -r .swarm) || die "no swarm from $url" # The exact bytes signed and sent: the value's line of JSON, then the meta's, no @@ -161,10 +184,11 @@ seal() { open_sealed() { command -v age >/dev/null || die "needs age" stored=$(raw "$1") - case $stored in - cv2.*) sealed=$(printf %s "$stored" | cut -d. -f2 | unb64url | jq -r '. // empty') ;; - *) sealed=$stored ;; - esac + if is_envelope "$stored"; then + sealed=$(printf %s "$stored" | cut -d. -f2 | unb64url | jq -r '. // empty') + else + sealed=$stored + fi case $sealed in cs1.*) ;; *) die "no sealed value at $1" ;; @@ -176,10 +200,7 @@ open_sealed() { verify() { envelope=$(raw "$1") - case $envelope in - cv2.*) ;; - *) die "no signed value at $1" ;; - esac + is_envelope "$envelope" || die "no signed value at $1" # What was signed: the value, a newline, the meta. printf %s "$envelope" | cut -d. -f3 | unb64url >"$work/meta" { printf %s "$envelope" | cut -d. -f2 | unb64url; echo; cat "$work/meta"; } >"$work/payload" |
