| 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 cose |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"crypto/x509" |
| 19 |
"encoding/hex" |
| 20 |
"encoding/json" |
| 21 |
"os" |
| 22 |
"reflect" |
| 23 |
"sort" |
| 24 |
"testing" |
| 25 |
"time" |
| 26 |
|
| 27 |
"github.com/notaryproject/notation-core-go/signature" |
| 28 |
"github.com/notaryproject/notation-core-go/testhelper" |
| 29 |
"github.com/veraison/go-cose" |
| 30 |
) |
| 31 |
|
| 32 |
type sign1 struct { |
| 33 |
Payload string `json:"payload"` |
| 34 |
ProtectedHeaders *cborStruct `json:"protectedHeaders"` |
| 35 |
UnprotectedHeaders *cborStruct `json:"unprotectedHeaders"` |
| 36 |
Output cborStruct `json:"expectedOutput"` |
| 37 |
} |
| 38 |
|
| 39 |
type cborStruct struct { |
| 40 |
CBORHex string `json:"cborHex"` |
| 41 |
CBORDiag string `json:"cborDiag"` |
| 42 |
} |
| 43 |
|
| 44 |
func TestConformance(t *testing.T) { |
| 45 |
data, err := os.ReadFile("testdata/conformance.json") |
| 46 |
if err != nil { |
| 47 |
t.Fatalf("os.ReadFile() failed. Error = %s", err) |
| 48 |
} |
| 49 |
var sign1 sign1 |
| 50 |
err = json.Unmarshal(data, &sign1) |
| 51 |
if err != nil { |
| 52 |
t.Fatalf("json.Unmarshal() failed. Error = %s", err) |
| 53 |
} |
| 54 |
testSign(t, &sign1) |
| 55 |
testVerify(t, &sign1) |
| 56 |
} |
| 57 |
|
| 58 |
// testSign does conformance check on COSE_Sign1_Tagged |
| 59 |
func testSign(t *testing.T, sign1 *sign1) { |
| 60 |
signRequest, err := getSignReq() |
| 61 |
if err != nil { |
| 62 |
t.Fatalf("getSignReq() failed. Error = %s", err) |
| 63 |
} |
| 64 |
env := createNewEnv(nil) |
| 65 |
encoded, err := env.Sign(signRequest) |
| 66 |
if err != nil || len(encoded) == 0 { |
| 67 |
t.Fatalf("Sign() faild. Error = %s", err) |
| 68 |
} |
| 69 |
newMsg := generateSign1(env.base) |
| 70 |
got, err := newMsg.MarshalCBOR() |
| 71 |
if err != nil { |
| 72 |
t.Fatalf("MarshalCBOR() faild. Error = %s", err) |
| 73 |
} |
| 74 |
|
| 75 |
// sign1.Output.CBORHex is a manually computed CBOR hex used as ground |
| 76 |
// truth in the conformance test. |
| 77 |
want := hexToBytes(sign1.Output.CBORHex) |
| 78 |
if !bytes.Equal(want, got) { |
| 79 |
t.Fatalf("unexpected output:\nwant: %x\n got: %x", want, got) |
| 80 |
} |
| 81 |
|
| 82 |
// Verify using the same envelope struct |
| 83 |
// (Verify with UnmarshalCBOR is covered in the testVerify() part) |
| 84 |
_, err = env.Verify() |
| 85 |
if err != nil { |
| 86 |
t.Fatalf("Verify() failed. Error = %s", err) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// testVerify does conformance check by decoding COSE_Sign1_Tagged object |
| 91 |
// into Sign1Message |
| 92 |
func testVerify(t *testing.T, sign1 *sign1) { |
| 93 |
signRequest, err := getSignReq() |
| 94 |
if err != nil { |
| 95 |
t.Fatalf("getSignReq() failed. Error = %s", err) |
| 96 |
} |
| 97 |
env := createNewEnv(nil) |
| 98 |
encoded, err := env.Sign(signRequest) |
| 99 |
if err != nil || len(encoded) == 0 { |
| 100 |
t.Fatalf("Sign() faild. Error = %s", err) |
| 101 |
} |
| 102 |
//Verify after UnmarshalCBOR |
| 103 |
var msg cose.Sign1Message |
| 104 |
// sign1.Output.CBORHex is a manually computed CBOR hex used as ground |
| 105 |
// truth in the conformance test. |
| 106 |
if err := msg.UnmarshalCBOR(hexToBytes(sign1.Output.CBORHex)); err != nil { |
| 107 |
t.Fatalf("msg.UnmarshalCBOR() failed. Error = %s", err) |
| 108 |
} |
| 109 |
|
| 110 |
certs := []*x509.Certificate{testhelper.GetRSALeafCertificate().Cert, testhelper.GetRSARootCertificate().Cert} |
| 111 |
certChain := make([]any, len(certs)) |
| 112 |
for i, c := range certs { |
| 113 |
certChain[i] = c.Raw |
| 114 |
} |
| 115 |
msg.Headers.Unprotected[cose.HeaderLabelX5Chain] = certChain |
| 116 |
msg.Signature = env.base.Signature |
| 117 |
|
| 118 |
newEnv := createNewEnv(&msg) |
| 119 |
content, err := newEnv.Verify() |
| 120 |
if err != nil { |
| 121 |
t.Fatalf("Verify() failed. Error = %s", err) |
| 122 |
} |
| 123 |
verifyPayload(&content.Payload, signRequest, t) |
| 124 |
verifySignerInfo(&content.SignerInfo, signRequest, t) |
| 125 |
} |
| 126 |
|
| 127 |
func getSignReq() (*signature.SignRequest, error) { |
| 128 |
certs := []*x509.Certificate{testhelper.GetRSALeafCertificate().Cert, testhelper.GetRSARootCertificate().Cert} |
| 129 |
signer, err := signature.NewLocalSigner(certs, testhelper.GetRSALeafCertificate().PrivateKey) |
| 130 |
if err != nil { |
| 131 |
return &signature.SignRequest{}, err |
| 132 |
} |
| 133 |
signRequest := &signature.SignRequest{ |
| 134 |
Payload: signature.Payload{ |
| 135 |
ContentType: "application/vnd.cncf.notary.payload.v1+json", |
| 136 |
Content: []byte("hello COSE"), |
| 137 |
}, |
| 138 |
Signer: signer, |
| 139 |
SigningTime: time.Unix(1661321924, 0), |
| 140 |
Expiry: time.Unix(1661408324, 0), |
| 141 |
ExtendedSignedAttributes: []signature.Attribute{ |
| 142 |
{Key: "signedCritKey1", Value: "signedCritValue1", Critical: true}, |
| 143 |
{Key: "signedKey1", Value: "signedValue1", Critical: false}, |
| 144 |
}, |
| 145 |
SigningAgent: "NotationConformanceTest/1.0.0", |
| 146 |
SigningScheme: "notary.x509", |
| 147 |
} |
| 148 |
return signRequest, nil |
| 149 |
} |
| 150 |
|
| 151 |
func hexToBytes(s string) []byte { |
| 152 |
b, err := hex.DecodeString(s) |
| 153 |
if err != nil { |
| 154 |
panic(err) |
| 155 |
} |
| 156 |
return b |
| 157 |
} |
| 158 |
|
| 159 |
func verifySignerInfo(signInfo *signature.SignerInfo, request *signature.SignRequest, t *testing.T) { |
| 160 |
if request.SigningAgent != signInfo.UnsignedAttributes.SigningAgent { |
| 161 |
t.Fatalf("SigningAgent: expected value %q but found %q", request.SigningAgent, signInfo.UnsignedAttributes.SigningAgent) |
| 162 |
} |
| 163 |
|
| 164 |
if request.SigningTime.Format(time.RFC3339) != signInfo.SignedAttributes.SigningTime.Format(time.RFC3339) { |
| 165 |
t.Fatalf("SigningTime: expected value %q but found %q", request.SigningTime, signInfo.SignedAttributes.SigningTime) |
| 166 |
} |
| 167 |
|
| 168 |
if request.Expiry.Format(time.RFC3339) != signInfo.SignedAttributes.Expiry.Format(time.RFC3339) { |
| 169 |
t.Fatalf("Expiry: expected value %q but found %q", request.SigningTime, signInfo.SignedAttributes.Expiry) |
| 170 |
} |
| 171 |
|
| 172 |
if !areAttrEqual(request.ExtendedSignedAttributes, signInfo.SignedAttributes.ExtendedAttributes) { |
| 173 |
if !(len(request.ExtendedSignedAttributes) == 0 && len(signInfo.SignedAttributes.ExtendedAttributes) == 0) { |
| 174 |
t.Fatalf("Mistmatch between expected and actual ExtendedAttributes") |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
signer, err := getSigner(request.Signer) |
| 179 |
if err != nil { |
| 180 |
t.Fatalf("getSigner() failed. Error = %s", err) |
| 181 |
} |
| 182 |
certs := signer.CertificateChain() |
| 183 |
if err != nil || !reflect.DeepEqual(certs, signInfo.CertificateChain) { |
| 184 |
t.Fatalf("Mistmatch between expected and actual CertificateChain") |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
func verifyPayload(payload *signature.Payload, request *signature.SignRequest, t *testing.T) { |
| 189 |
if request.Payload.ContentType != payload.ContentType { |
| 190 |
t.Fatalf("PayloadContentType: expected value %q but found %q", request.Payload.ContentType, payload.ContentType) |
| 191 |
} |
| 192 |
|
| 193 |
if !bytes.Equal(request.Payload.Content, payload.Content) { |
| 194 |
t.Fatalf("Payload: expected value %q but found %q", request.Payload.Content, payload.Content) |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
func areAttrEqual(u []signature.Attribute, v []signature.Attribute) bool { |
| 199 |
sortCOSEAttributes(u) |
| 200 |
sortCOSEAttributes(v) |
| 201 |
return reflect.DeepEqual(u, v) |
| 202 |
} |
| 203 |
|
| 204 |
func generateSign1(msg *cose.Sign1Message) *cose.Sign1Message { |
| 205 |
newMsg := cose.NewSign1Message() |
| 206 |
newMsg.Headers.Protected = msg.Headers.Protected |
| 207 |
newMsg.Headers.Unprotected["io.cncf.notary.signingAgent"] = msg.Headers.Unprotected["io.cncf.notary.signingAgent"] |
| 208 |
newMsg.Payload = msg.Payload |
| 209 |
newMsg.Signature = hexToBytes("5bfec0a345098b9b9b6fb7358face7ab76d191b648ccd19e36fb03c2085ea072ec050d9c6e4fa4845478386d0831a2360d343a1ff027bdd56d496f996b90ac2db9da2460baffec21db7c0ca759ba83ab35cdf521c0926138681bde05277e2976cedbeb4040c930908ef2b113d935378bd3c5e7740119b2b81c59e9c6c24411abdf699547864f68f2e0f6346eeff627bf0d971abdf94e67e12a10134ccbbadfa0ab4031b18705696a9567a0f1f061247fdd00d343ea3a45f63da7f80771612b38fc9877375bcbce28aef1f3ee2b25869722c24737c49d8c6711376dd62b3d32b24d489746e2ba5d25fa76febcc6abf9d2baee67221c85a7a8f8763dadc5e20bb8c5c03a75c68211557813d2d6adea56ec5526f78c18460b1944c8307a4b0ed64a6d6b4abed5067de5a5ad38948a2ea140b01a7762c15b3e63d7d7bdc8962e6c4bff18b34d2a19fc627f02ebf88daf7fb25c55ce1b9ca06ade02f9d60ad16cb306f433f692e598132d67b5d0a02193191d5c9cd52ad81f4e31917e5b5d40ef5ce7") |
| 210 |
return newMsg |
| 211 |
} |
| 212 |
|
| 213 |
func sortCOSEAttributes(u []signature.Attribute) { |
| 214 |
sort.Slice(u, func(p, q int) bool { |
| 215 |
switch k1 := u[p].Key.(type) { |
| 216 |
case int: |
| 217 |
switch k2 := u[q].Key.(type) { |
| 218 |
case int: |
| 219 |
return k1 < k2 |
| 220 |
case string: |
| 221 |
return false |
| 222 |
} |
| 223 |
case string: |
| 224 |
switch k2 := u[q].Key.(type) { |
| 225 |
case int: |
| 226 |
return true |
| 227 |
case string: |
| 228 |
return k1 < k2 |
| 229 |
} |
| 230 |
} |
| 231 |
return false |
| 232 |
}) |
| 233 |
} |