Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add COSE_Key support #146

Merged
merged 4 commits into from
Jun 29, 2023
Merged

Add COSE_Key support #146

merged 4 commits into from
Jun 29, 2023

Conversation

setrofim
Copy link
Contributor

@setrofim setrofim commented Jun 7, 2023

Add support for marshalling/unmarshalling COSE_Key structures, and using them for signing and verification. At the moment, only RFC8152 is implemented (OKP and EC2 signatures); RFC8230 (RSASSA-PSS signatures) is NOT implemented.

@codecov
Copy link

codecov bot commented Jun 7, 2023

Codecov Report

Merging #146 (899013e) into main (354ac99) will decrease coverage by 1.90%.
The diff coverage is 88.29%.

@@            Coverage Diff             @@
##             main     #146       /-   ##
==========================================
- Coverage   92.76%   90.87%   -1.90%     
==========================================
  Files          10       12        2     
  Lines        1065     1611      546     
==========================================
  Hits          988     1464      476     
- Misses         51      109       58     
- Partials       26       38       12     
Impacted Files Coverage Δ
key.go 87.50% <87.50%> (ø)
common.go 90.56% <90.56%> (ø)
algorithm.go 88.70% <95.23%> (-11.30%) ⬇️
headers.go 93.21% <100.00%> ( 0.16%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@setrofim setrofim force-pushed the setrofim branch 2 times, most recently from 931818c to 3d9a56f Compare June 7, 2023 08:53
@setrofim setrofim requested review from shizhMSFT, yogeshbdeshpande and thomas-fossati and removed request for shizhMSFT June 7, 2023 08:56
Copy link
Contributor

@thomas-fossati thomas-fossati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much, this is useful!

In general it looks good to me. I've left a bunch of comments for your consideration.

algorithm.go Outdated Show resolved Hide resolved
algorithm.go Outdated Show resolved Hide resolved
algorithm.go Outdated Show resolved Hide resolved
algorithm.go Outdated Show resolved Hide resolved
common.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
Copy link
Contributor

@thomas-fossati thomas-fossati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much, this is useful!

In general it looks good to me. I've left a bunch of comments for your consideration.

common.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
common.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
Copy link
Contributor

@yogeshbdeshpande yogeshbdeshpande left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments for you to consider!

@setrofim setrofim force-pushed the setrofim branch 2 times, most recently from ea7debe to ea37670 Compare June 7, 2023 17:59
@setrofim setrofim mentioned this pull request Jun 7, 2023
@setrofim setrofim force-pushed the setrofim branch 7 times, most recently from 029dda4 to 47d4b1a Compare June 8, 2023 15:13
key.go Outdated Show resolved Hide resolved
Copy link
Contributor

@qmuntal qmuntal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve with the following concerns:

  • The Key struct contains all known key parameters, that is, from EC2, OKP and Symmetric Keys. This doesn't scale well, as in the future we might want to support new keys that, for example, defines D as a tstr instead of bstr, in which case it will be hard to retrofit the new key. In fact, this already seems to happen with EC2 y-coordinate, we only support bstr but the spec also supports bool. IMO it would have been better to define key parameters in separate structs, one for each key type, and have Key just contain a property like Params any, which could contain the specialized parameters struct.
  • Don't like to depend on github.com/stretchr/testify, as it indirectly depends on gopkg.in/yaml, which is prone to security vulnerabilities that will be reported in go-cose even though we only use it for testing.

Having said this, this PR already contains alot of valuable work and we have gone though many iterations to put it in a good shape. We can work on my concerns in future PRs before cutting a new version.

@setrofim
Copy link
Contributor Author

@shizhMSFT: I believe all outstanding have now been addressed. Please let me know if you are happy with the code as-is or would like to see additioinal changes.

key.go Outdated Show resolved Hide resolved
@setrofim
Copy link
Contributor Author

note: I've added a commit to disable the codecov/patch checker for now, as I don't see any other way around this (the codecov/project check still runs as before).

algorithm.go Outdated Show resolved Hide resolved
algorithm.go Outdated Show resolved Hide resolved
algorithm.go Outdated Show resolved Hide resolved
Comment on lines 99 to 107
case KeyOpMACCreate:
return "MAC create"
case KeyOpMACVerify:
return "MAC verify"
Copy link
Contributor

@shizhMSFT shizhMSFT Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The strings for MAC create and verify seem not defined in RFC docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that no string value is defined these, I'm using the Name here instead. String() is not used during serialization to CBOR, and does not represent the encoded value, it only provides a string reprsentation (for use in error messages, debug output, etc).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent misuse, can you document the special cases of "MAC create/verify"?

key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
key.go Outdated Show resolved Hide resolved
@setrofim
Copy link
Contributor Author

@shizhMSFT please could you re-review?

@OR13
Copy link
Collaborator

OR13 commented Jun 26, 2023

looking forward to JWK support :)

Copy link
Contributor

@shizhMSFT shizhMSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with some doc suggestions

Comment on lines 99 to 107
case KeyOpMACCreate:
return "MAC create"
case KeyOpMACVerify:
return "MAC verify"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent misuse, can you document the special cases of "MAC create/verify"?

// https://datatracker.ietf.org/doc/html/rfc8152#section-13.1
type Curve int64

func (c Curve) String() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you document this method noticing that it is for human reading only to prevent any misuse?

KeyTypeSymmetric KeyType = 4
)

func (kt KeyType) String() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you document this method noticing that it is for human reading only to prevent any misuse?

@shizhMSFT
Copy link
Contributor

As @qmuntal mentioned, it is better to drop the github.com/stretchr/testify dependency although we only use it for testing.

@shizhMSFT
Copy link
Contributor

As @qmuntal mentioned, it is better to drop the github.com/stretchr/testify dependency although we only use it for testing.

I tried to build a small app using the code changes in this PR:

package main

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	_ "crypto/sha256"
	"fmt"

	"github.com/veraison/go-cose"
)

func SignP256(data []byte) ([]byte, error) {
	// create a signer
	privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
	if err != nil {
		return nil, err
	}
	signer, err := cose.NewSigner(cose.AlgorithmES256, privateKey)
	if err != nil {
		return nil, err
	}

	// create message header
	headers := cose.Headers{
		Protected: cose.ProtectedHeader{
			cose.HeaderLabelAlgorithm: cose.AlgorithmES256,
		},
	}

	// sign and marshal message
	return cose.Sign1(rand.Reader, signer, headers, data, nil)
}

func main() {
	fmt.Println(SignP256([]byte("hello world")))
}

By running go mod tidy, I've got go.mod:

module test

go 1.19

require github.com/veraison/go-cose v1.1.1-0.20230623043903-afdd177c3434

require (
	github.com/fxamacker/cbor/v2 v2.4.0 // indirect
	github.com/x448/float16 v0.8.4 // indirect
)

and go.sum:

github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3 rkHYY13jYWTU97c=
github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR 5XunOP8CRUowT0pSJOwhW098ZCUyskZD88=
github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/veraison/go-cose v1.1.1-0.20230623043903-afdd177c3434 h1:0f8c2TttCjCvVGfHhbv2OhE9BK6HESa/t4QjajfE3/I=
github.com/veraison/go-cose v1.1.1-0.20230623043903-afdd177c3434/go.mod h1:/Bcd44VnE3YVGO LY3wvyvjBniAVjlX4vaUZ8gNIfE0=
github.com/x448/float16 v0.8.4 h1:qLwI1I70 NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

As you can observe in the go.sum, those yaml.v3, testify, go-spew, and go-difflib are unwanted. Those dependencies impact the downstream projects even if they are only used in the test code of go-cose.

A number of elements in RFC8152 can be represented as either integer
values or string names. This type will allow uniform handling of such
elements when (un)marshalling to/from CBOR.

Signed-off-by: Sergei Trofimov <[email protected]>
RFC8152 specifies that an algorithm may be identified either by its
integer value, or by its string name. This adds support for latter.

(note: There are currently no IANA-registered string values for COSE
algorithms, so while this support is in place, any provided algorithm
value is reported as invalid.)

Signed-off-by: Sergei Trofimov <[email protected]>
Add support for COSE_Key structure, and associated enums for key type,
ECC curve, and key ops, as defined in RFC8152.

Signed-off-by: Sergei Trofimov <[email protected]>
This seems to vary with respect to the commit size, and for some
commits, it is not possible to  meet its requirement.

Signed-off-by: Sergei Trofimov <[email protected]>
@setrofim
Copy link
Contributor Author

I've removed the github.com/stretchr/testify dependency.

@setrofim setrofim merged commit a579021 into main Jun 29, 2023
6 checks passed
@setrofim setrofim deleted the setrofim branch June 29, 2023 12:08
@qmuntal qmuntal mentioned this pull request Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants