Debian Codegraph @

internal/envelope/envelope.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package envelope
15
16 import (
17 "errors"
18 "fmt"
19 "time"
20
21 "github.com/notaryproject/notation-core-go/signature"
22 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
23 )
24
25 // MediaTypePayloadV1 is the supported content type for signature's payload.
26 const (
27 MediaTypePayloadV1 = "application/vnd.cncf.notary.payload.v1+json"
28 AnnotationX509ChainThumbprint = "io.cncf.notary.x509chain.thumbprint#S256"
29 )
30
31 // Payload describes the content that gets signed.
32 type Payload struct {
33 TargetArtifact ocispec.Descriptor `json:"targetArtifact"`
34 }
35
36 // ValidatePayloadContentType validates signature payload's content type.
37 func ValidatePayloadContentType(payload *signature.Payload) error {
38 switch payload.ContentType {
39 case MediaTypePayloadV1:
40 return nil
41 default:
42 return fmt.Errorf("payload content type %q not supported", payload.ContentType)
43 }
44 }
45
46 // SanitizeTargetArtifact filters out unrelated ocispec.Descriptor fields based
47 // on notation spec (https://github.com/notaryproject/notaryproject/blob/main/specs/signature-specification.md#payload).
48 func SanitizeTargetArtifact(targetArtifact ocispec.Descriptor) ocispec.Descriptor {
49 return ocispec.Descriptor{
50 MediaType: targetArtifact.MediaType,
51 Digest: targetArtifact.Digest,
52 Size: targetArtifact.Size,
53 Annotations: targetArtifact.Annotations,
54 }
55 }
56
57 // SigningTime returns the signing time of a signature envelope blob
58 func SigningTime(signerInfo *signature.SignerInfo) (time.Time, error) {
59 // sanity check
60 if signerInfo == nil {
61 return time.Time{}, errors.New("failed to generate annotations: signerInfo cannot be nil")
62 }
63 signingTime := signerInfo.SignedAttributes.SigningTime
64 if signingTime.IsZero() {
65 return time.Time{}, errors.New("signing time is missing")
66 }
67 return signingTime.UTC(), nil
68 }