diff options
Diffstat (limited to 'scripts/continuum-signed')
| -rwxr-xr-x | scripts/continuum-signed | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/scripts/continuum-signed b/scripts/continuum-signed index 21eb376..b66949f 100755 --- a/scripts/continuum-signed +++ b/scripts/continuum-signed @@ -94,22 +94,27 @@ raw() { "$url/store/$1?raw=true" || true } -# Writes JSON $2 at $1, signed: replacing the envelope there, if any (`prev`; $3, if -# given, instead of reading it). +# Writes JSON $2 at $1, signed: replacing the envelope there, if any (`prev`), and +# numbered after it (`seq`; $3, if given, instead of reading it). write() { [ -n "$user" ] || die "who signs? set CONTINUUM_USER" if [ $# -ge 3 ]; then current=$3; else current=$(raw "$1"); fi case $current in - cv1.*) prev=$(printf %s "$current" | sha256sum | cut -d' ' -f1) ;; - *) prev= ;; + 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 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: one line of JSON, no newline. - jq -cnj --arg swarm "$swarm" --arg path "$1" --argjson value "$2" --arg user "$user" \ - --argjson at "$(date +%s)000" --arg prev "$prev" \ - '{swarm: $swarm, path: ($path | split("/")), value: $value, user: $user, at: $at, prev: $prev}' \ - >"$work/payload" || die "not JSON: $2" + # The exact bytes signed and sent: the value's line of JSON, then the meta's, no + # newline after it. + { jq -cn --argjson value "$2" '$value' || die "not JSON: $2" + jq -cnj --arg swarm "$swarm" --arg path "$1" --arg user "$user" \ + --argjson at "$(date +%s)000" --argjson seq "$seq" --arg prev "$prev" \ + '{swarm: $swarm, path: ($path | split("/")), user: $user, at: $at, seq: $seq, prev: $prev}' + } >"$work/payload" sign "$work/payload" continuum-value { cat "$work/payload"; echo; cat "$work/payload.sig"; } | curl -sS --fail-with-body -X PUT "$url/store/$1" \ @@ -157,7 +162,7 @@ open_sealed() { command -v age >/dev/null || die "needs age" stored=$(raw "$1") case $stored in - cv1.*) sealed=$(printf %s "$stored" | cut -d. -f2 | unb64url | jq -r '.value // empty') ;; + cv2.*) sealed=$(printf %s "$stored" | cut -d. -f2 | unb64url | jq -r '. // empty') ;; *) sealed=$stored ;; esac case $sealed in @@ -172,16 +177,18 @@ open_sealed() { verify() { envelope=$(raw "$1") case $envelope in - cv1.*) ;; + cv2.*) ;; *) die "no signed value at $1" ;; esac - printf %s "$envelope" | cut -d. -f2 | unb64url >"$work/payload" - printf %s "$envelope" | cut -d. -f3 | unb64url | base64 | tr -d '\n' | fold -w 70 >"$work/blob" + # 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" + printf %s "$envelope" | cut -d. -f4 | unb64url | base64 | tr -d '\n' | fold -w 70 >"$work/blob" echo >>"$work/blob" { echo '-----BEGIN SSH SIGNATURE-----'; cat "$work/blob" echo '-----END SSH SIGNATURE-----'; } >"$work/payload.sig" - signer=$(jq -r .user "$work/payload") - signed_for=$(jq -r '.path | join("/")' "$work/payload") + signer=$(jq -r .user "$work/meta") + signed_for=$(jq -r '.path | join("/")' "$work/meta") [ "$signed_for" = "$1" ] || die "signed for $signed_for, not $1" curl -sf -H "Authorization: Bearer $CONTINUUM_TOKEN" "$url/store/_continuum/users/$signer" | jq -r --arg user "$signer" \ @@ -190,7 +197,7 @@ verify() { ssh-keygen -q -Y verify -f "$work/allowed" -I "$signer" -n continuum-value \ -s "$work/payload.sig" <"$work/payload" >/dev/null 2>&1 || die "the signature of $1 doesn't check against $signer's keys" - echo "$1: signed by $signer at $(jq -r .at "$work/payload")" + echo "$1: signed by $signer at $(jq -r .at "$work/meta")" } # The user's own entry with key $1 set to the line in file $2, or removed (no $2), |
