]> git.scottworley.com Git - overonion/blobdiff - overonion
Move hash initiation into overonion-make-key
[overonion] / overonion
index 271938c8d05e8de5ceab2a1562132493ccdd59de..c54048a3351b4103544c8ee9ee345f887fa9ecb8 100755 (executable)
--- a/overonion
+++ b/overonion
@@ -1,5 +1,9 @@
 #!/bin/bash
 
+umask 077
+
+hash_dir=$(mktemp -d)
+
 function die() {
   echo "$*" >&2
   exit 1
@@ -40,10 +44,14 @@ function go() {
   if (( layer == 0 || layer > num_layers ));then
     cat
   else
-    operation=$(sed -n "${layer}s/ .*//p" "$keyfile")
+    operation=$(sed -n "${layer}{;s/ .*//;p;}" "$keyfile")
     if [[ "$operation" == openssl-enc ]];then
       openssl enc $openssl_decrypt "-$(sed -rn "${layer}s/[^ ]+ ([^ ]+) .*/\\1/p" "$keyfile")" \
               -pass fd:37 37< <(sed -rn "${layer}s/^[^ ]+ [^ ]+ //p" "$keyfile")
+    elif [[ "$operation" == reverse ]];then
+      reverse
+    elif [[ "$operation" == openssl-dgst ]];then
+      tee >(echo "$(sed -n "${layer}p" "$keyfile") $(openssl dgst -binary "-$(sed -rn "${layer}s/^[^ ]+ ([^ ]+).*/\\1/p" "$keyfile")" | base64 --wrap=0)" > "$hash_dir/$layer")
     else
       die "Unknown operation"
     fi |
@@ -52,3 +60,23 @@ function go() {
 }
 
 go "$first_layer"
+
+for hash_result in "$hash_dir"/*;do
+  layer=$(basename "$hash_result")
+  if [[ "$mode" == e ]];then
+    # Add the hashes to keyfile
+    key_aside_dir=$(mktemp -d "$keyfile.XXXXXXXXXX")
+    key_aside="$key_aside_dir/key.orig"
+    mv "$keyfile" "$key_aside"
+    sed "${layer}s,.*,$(< "$hash_result")," "$key_aside" > "$keyfile"
+    shred -u "$key_aside"
+    rmdir "$key_aside_dir"
+  else
+    # Verify the hashes
+    if [[ "$(awk '{ print $3 == $4 ? "hash ok" : "mismatch" }' "$hash_result")" != "hash ok" ]];then
+      die "Hash check $layer failed"
+    fi
+  fi
+done
+
+rm -r "$hash_dir"