-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add computation and verification of previous layers' hashes
This patch adds the computation of previous layers accumulated hashes on the encryption side and writes this computed hash into the private options of a layer. The private options will be encrypted then. On the decryption side it also performs the computations and, if the private options contain the previous layers' hash, which may not be the case for older images but will be the case for newer ones, it compares the expected hash against the computed one and errors if they don't match. The previous layers' digest needs to be passed from one layer encrytion step to the next. The sequence must begin with the bottom-most layer using the result of GetInitalPreviousLayersDigest() so that no other layer can be 'slid' underneath the bottom-most one. This patch at least helps fulfill the requirement that previous layers cannot be manipulated assuming the attacker can access the registry but of course not manipulate the decryption code. Signed-off-by: Stefan Berger <[email protected]>
- Loading branch information
1 parent
059f6b1
commit 22cb4e2
Showing
5 changed files
with
149 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env bash | ||
|
||
|
||
echo "Enter PreviousLayersDigest or leave empty for initial one" | ||
read previousLayersDigest | ||
if [ -z "$previousLayersDigest" ]; then | ||
previousLayersDigest=$(echo -en | sha256sum | gawk '{print $1}') | ||
fi | ||
if ! [[ $previousLayersDigest =~ ^[0-9a-fA-F]{64}$ ]]; then | ||
echo "previousLayersDigest '$previousLayersDigest' must be a sha256" | ||
exit 1 | ||
fi | ||
while :; do | ||
echo "Enter current layer's digest" | ||
read currentLayerDigest | ||
if ! [[ $currentLayerDigest =~ ^[0-9a-fA-F]{64}$ ]]; then | ||
echo "current layer digest must be a sha256" | ||
exit 1 | ||
fi | ||
|
||
dig=$(echo -n "${previousLayersDigest}${currentLayerDigest}" | | ||
sed -n 's/[0-9a-fA-F]\{2\}/\\x\0/pg' ) | ||
|
||
previousLayersDigest=$(echo -en "${dig}" | sha256sum | gawk '{print $1}') | ||
echo "digest: ${previousLayersDigest}" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright The ocicrypt Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package utils | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
|
||
"github.com/opencontainers/go-digest" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// GetInitalPreviousLayersDigest returns the initial value for previousLayersDigest | ||
func GetInitialPreviousLayersDigest() []byte { | ||
digest := sha256.Sum256(nil) | ||
return digest[:] | ||
} | ||
|
||
// GetNewLayersDigest calculates the new layer digest from the previousLayersDigest and the layerDigest. | ||
func GetNewLayersDigest(previousLayersDigest []byte, layerDigest digest.Digest) ([]byte, error) { | ||
newDigest := sha256.New() | ||
// never returns an error but linter requires us to look at it | ||
_, err := newDigest.Write(previousLayersDigest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
digest, err := hex.DecodeString(layerDigest.Encoded()) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Hex decoding digest failed") | ||
} | ||
_, err = newDigest.Write(digest) | ||
return newDigest.Sum(nil), err | ||
} |