inject_hash preserves filemode

Previously, inject_hash writes output file with 0644 regardless of
input file.

Now it preserves filemode of input when writing output file.

Change-Id: I00db775e1b28f6d9a72986276e32a9b944317949
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/38844
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
diff --git a/util/fipstools/inject_hash/inject_hash.go b/util/fipstools/inject_hash/inject_hash.go
index 251df10..dbd5fb7 100644
--- a/util/fipstools/inject_hash/inject_hash.go
+++ b/util/fipstools/inject_hash/inject_hash.go
@@ -39,6 +39,8 @@
 func do(outPath, oInput string, arInput string, useSHA256 bool) error {
 	var objectBytes []byte
 	var isStatic bool
+	var perm os.FileMode
+
 	if len(arInput) > 0 {
 		isStatic = true
 
@@ -46,6 +48,12 @@
 			return fmt.Errorf("-in-archive and -in-object are mutually exclusive")
 		}
 
+		fi, err := os.Stat(arInput)
+		if err != nil {
+			return err
+		}
+		perm = fi.Mode()
+
 		arFile, err := os.Open(arInput)
 		if err != nil {
 			return err
@@ -65,7 +73,12 @@
 			objectBytes = contents
 		}
 	} else if len(oInput) > 0 {
-		var err error
+		fi, err := os.Stat(oInput)
+		if err != nil {
+			return err
+		}
+		perm = fi.Mode()
+
 		if objectBytes, err = ioutil.ReadFile(oInput); err != nil {
 			return err
 		}
@@ -237,7 +250,7 @@
 
 	copy(objectBytes[offset:], calculated)
 
-	return ioutil.WriteFile(outPath, objectBytes, 0644)
+	return ioutil.WriteFile(outPath, objectBytes, perm & 0777)
 }
 
 func main() {