| 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 jws |
| 15 |
|
| 16 |
import ( |
| 17 |
"time" |
| 18 |
|
| 19 |
"github.com/golang-jwt/jwt/v4" |
| 20 |
"github.com/notaryproject/notation-core-go/signature" |
| 21 |
) |
| 22 |
|
| 23 |
const ( |
| 24 |
headerKeyAlg = "alg" |
| 25 |
headerKeyCty = "cty" |
| 26 |
headerKeyCrit = "crit" |
| 27 |
headerKeyExpiry = "io.cncf.notary.expiry" |
| 28 |
headerKeySigningTime = "io.cncf.notary.signingTime" |
| 29 |
headerKeySigningScheme = "io.cncf.notary.signingScheme" |
| 30 |
headerKeyAuthenticSigningTime = "io.cncf.notary.authenticSigningTime" |
| 31 |
) |
| 32 |
|
| 33 |
// headerKeys includes all system aware keys for JWS protected header |
| 34 |
// [JWS envelope]: https://github.com/notaryproject/notaryproject/blob/main/specs/signature-envelope-jws.md#protected-headers |
| 35 |
var headerKeys = []string{ |
| 36 |
headerKeyAlg, |
| 37 |
headerKeyCty, |
| 38 |
headerKeyCrit, |
| 39 |
headerKeyExpiry, |
| 40 |
headerKeySigningTime, |
| 41 |
headerKeySigningScheme, |
| 42 |
headerKeyAuthenticSigningTime, |
| 43 |
} |
| 44 |
|
| 45 |
// jwsProtectedHeader contains the set of protected headers. |
| 46 |
type jwsProtectedHeader struct { |
| 47 |
// Defines which algorithm was used to generate the signature. |
| 48 |
Algorithm string `json:"alg"` |
| 49 |
|
| 50 |
// Media type of the secured content (the payload). |
| 51 |
ContentType string `json:"cty"` |
| 52 |
|
| 53 |
// Lists the headers that implementation MUST understand and process. |
| 54 |
Critical []string `json:"crit,omitempty"` |
| 55 |
|
| 56 |
// The "best by use" time for the artifact, as defined by the signer. |
| 57 |
Expiry *time.Time `json:"io.cncf.notary.expiry,omitempty"` |
| 58 |
|
| 59 |
// Specifies the Notary Project Signing Scheme used by the signature. |
| 60 |
SigningScheme signature.SigningScheme `json:"io.cncf.notary.signingScheme"` |
| 61 |
|
| 62 |
// The time at which the signature was generated. only valid when signing |
| 63 |
// scheme is `notary.x509`. |
| 64 |
SigningTime *time.Time `json:"io.cncf.notary.signingTime,omitempty"` |
| 65 |
|
| 66 |
// The time at which the signature was generated. only valid when signing |
| 67 |
// scheme is `notary.x509.signingAuthority`. |
| 68 |
AuthenticSigningTime *time.Time `json:"io.cncf.notary.authenticSigningTime,omitempty"` |
| 69 |
|
| 70 |
// The user defined attributes. |
| 71 |
ExtendedAttributes map[string]interface{} `json:"-"` |
| 72 |
} |
| 73 |
|
| 74 |
// jwsUnprotectedHeader contains the set of unprotected headers. |
| 75 |
type jwsUnprotectedHeader struct { |
| 76 |
// RFC3161 timestamp token Base64-encoded. |
| 77 |
TimestampSignature []byte `json:"io.cncf.notary.timestampSignature,omitempty"` |
| 78 |
|
| 79 |
// List of X.509 Base64-DER-encoded certificates |
| 80 |
// as defined at https://datatracker.ietf.org/doc/html/rfc7515#section-4.1.6. |
| 81 |
CertChain [][]byte `json:"x5c"` |
| 82 |
|
| 83 |
// SigningAgent used for signing. |
| 84 |
SigningAgent string `json:"io.cncf.notary.signingAgent,omitempty"` |
| 85 |
} |
| 86 |
|
| 87 |
// jwsEnvelope is the final Signature envelope. |
| 88 |
type jwsEnvelope struct { |
| 89 |
// JWSPayload Base64URL-encoded. Raw data should be JSON format. |
| 90 |
Payload string `json:"payload"` |
| 91 |
|
| 92 |
// jwsProtectedHeader Base64URL-encoded. |
| 93 |
Protected string `json:"protected"` |
| 94 |
|
| 95 |
// Signature metadata that is not integrity Protected |
| 96 |
Header jwsUnprotectedHeader `json:"header"` |
| 97 |
|
| 98 |
// Base64URL-encoded Signature. |
| 99 |
Signature string `json:"signature"` |
| 100 |
} |
| 101 |
|
| 102 |
var ( |
| 103 |
ps256 = jwt.SigningMethodPS256.Name |
| 104 |
ps384 = jwt.SigningMethodPS384.Name |
| 105 |
ps512 = jwt.SigningMethodPS512.Name |
| 106 |
es256 = jwt.SigningMethodES256.Name |
| 107 |
es384 = jwt.SigningMethodES384.Name |
| 108 |
es512 = jwt.SigningMethodES512.Name |
| 109 |
) |
| 110 |
|
| 111 |
var validMethods = []string{ps256, ps384, ps512, es256, es384, es512} |
| 112 |
|
| 113 |
var signatureAlgJWSAlgMap = map[signature.Algorithm]string{ |
| 114 |
signature.AlgorithmPS256: ps256, |
| 115 |
signature.AlgorithmPS384: ps384, |
| 116 |
signature.AlgorithmPS512: ps512, |
| 117 |
signature.AlgorithmES256: es256, |
| 118 |
signature.AlgorithmES384: es384, |
| 119 |
signature.AlgorithmES512: es512, |
| 120 |
} |
| 121 |
|
| 122 |
var jwsAlgSignatureAlgMap = reverseMap(signatureAlgJWSAlgMap) |
| 123 |
|
| 124 |
func reverseMap(m map[signature.Algorithm]string) map[string]signature.Algorithm { |
| 125 |
n := make(map[string]signature.Algorithm, len(m)) |
| 126 |
for k, v := range m { |
| 127 |
n[v] = k |
| 128 |
} |
| 129 |
return n |
| 130 |
} |