Skip to content

Commit 494604d

Browse files
committed
fix: patch SermoDigital/jose to resolve Go 1.27 crypto init panic
Go 1.27 added a check to crypto.RegisterHash that panics when hash value 0 is registered (hash(0) is not a valid hash function). The abandoned library github.com/SermoDigital/jose registers hash(0) in its crypto/none.go init() for the "none" JWT algorithm, causing a panic at startup: panic: crypto: RegisterHash of unknown hash function SermoDigital/jose is a transitive dependency pulled in via code.cloudfoundry.org/cli/v8. Since the upstream library is abandoned (last commit in 2018) and all available versions share the same init(), we include a local patched copy in .go-patches/SermoDigital/jose/ with the offending init() removed. The "none" JWT signing method continues to work without it. Additionally fix two pre-existing Printf format string issues in util/http/transport.go where Go template syntax ({{.Error}}) was used in Printf calls that expect %v-style format verbs. Go vet now correctly flags these as errors, causing build failures when testing the util/http package.
1 parent afd81ac commit 494604d

67 files changed

Lines changed: 4643 additions & 4 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
2+
*.o
3+
*.a
4+
*.so
5+
6+
# Folders
7+
_obj
8+
_test
9+
10+
# Architecture specific extensions/prefixes
11+
*.[568vq]
12+
[568vq].out
13+
14+
*.cgo1.go
15+
*.cgo2.c
16+
_cgo_defun.c
17+
_cgo_gotypes.go
18+
_cgo_export.*
19+
20+
_testmain.go
21+
22+
*.exe
23+
*.test
24+
*.prof
25+
26+
*.out
27+
*.tmp
28+
tags
29+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: go
2+
3+
go:
4+
- 1.6
5+
- 1.7
6+
- tip
7+
8+
sudo: false
9+
10+
install:
11+
- go get -u github.com/golang/lint/golint
12+
13+
script:
14+
- ./_test.sh
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Sermo Digital LLC
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
JOSE
2+
============
3+
[![Build Status](https://travis-ci.org/SermoDigital/jose.svg?branch=master)](https://travis-ci.org/SermoDigital/jose)
4+
[![GoDoc](https://godoc.org/github.com/SermoDigital/jose?status.svg)](https://godoc.org/github.com/SermoDigital/jose)
5+
6+
JOSE is a comprehensive set of JWT, JWS, and JWE libraries.
7+
8+
## Why
9+
10+
The only other JWS/JWE/JWT implementations are specific to JWT, and none
11+
were particularly pleasant to work with.
12+
13+
These libraries should provide an easy, straightforward way to securely
14+
create, parse, and validate JWS, JWE, and JWTs.
15+
16+
## Notes:
17+
JWE is currently unimplemented.
18+
19+
## Version 0.9:
20+
21+
## Documentation
22+
23+
The docs can be found at [godoc.org] [docs], as usual.
24+
25+
A gopkg.in mirror can be found at https://gopkg.in/jose.v1, thanks to
26+
@zia-newversion. (For context, see #30.)
27+
28+
### [JWS RFC][jws]
29+
### [JWE RFC][jwe]
30+
### [JWT RFC][jwt]
31+
32+
## License
33+
34+
[MIT] [license].
35+
36+
[docs]: https://godoc.org/github.com/SermoDigital/jose
37+
[license]: https://github.com/SermoDigital/jose/blob/master/LICENSE.md
38+
[jws]: https://tools.ietf.org/html/rfc7515
39+
[jwe]: https://tools.ietf.org/html/rfc7516
40+
[jwt]: https://tools.ietf.org/html/rfc7519
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
go build ./...
6+
go test ./...
7+
golint ./...
8+
go vet ./...
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package jose
2+
3+
import "encoding/base64"
4+
5+
// Encoder is satisfied if the type can marshal itself into a valid
6+
// structure for a JWS.
7+
type Encoder interface {
8+
// Base64 implies T -> JSON -> RawURLEncodingBase64
9+
Base64() ([]byte, error)
10+
}
11+
12+
// Base64Decode decodes a base64-encoded byte slice.
13+
func Base64Decode(b []byte) ([]byte, error) {
14+
buf := make([]byte, base64.RawURLEncoding.DecodedLen(len(b)))
15+
n, err := base64.RawURLEncoding.Decode(buf, b)
16+
return buf[:n], err
17+
}
18+
19+
// Base64Encode encodes a byte slice.
20+
func Base64Encode(b []byte) []byte {
21+
buf := make([]byte, base64.RawURLEncoding.EncodedLen(len(b)))
22+
base64.RawURLEncoding.Encode(buf, b)
23+
return buf
24+
}
25+
26+
// EncodeEscape base64-encodes a byte slice but escapes it for JSON.
27+
// It'll return the format: `"base64"`
28+
func EncodeEscape(b []byte) []byte {
29+
buf := make([]byte, base64.RawURLEncoding.EncodedLen(len(b))+2)
30+
buf[0] = '"'
31+
base64.RawURLEncoding.Encode(buf[1:], b)
32+
buf[len(buf)-1] = '"'
33+
return buf
34+
}
35+
36+
// DecodeEscaped decodes a base64-encoded byte slice straight from a JSON
37+
// structure. It assumes it's in the format: `"base64"`, but can handle
38+
// cases where it's not.
39+
func DecodeEscaped(b []byte) ([]byte, error) {
40+
if len(b) > 1 && b[0] == '"' && b[len(b)-1] == '"' {
41+
b = b[1 : len(b)-1]
42+
}
43+
return Base64Decode(b)
44+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package jose
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
)
7+
8+
func TestBase64(t *testing.T) {
9+
encoded := []byte("SGVsbG8sIHBsYXlncm91bmQ")
10+
raw := []byte("Hello, playground")
11+
12+
testEnc := Base64Encode(raw)
13+
if !bytes.Equal(testEnc, encoded) {
14+
Error(t, encoded, testEnc)
15+
}
16+
17+
testDec, err := Base64Decode(testEnc)
18+
if err != nil {
19+
t.Error(err)
20+
}
21+
22+
if !bytes.Equal(testDec, raw) {
23+
Error(t, raw, testDec)
24+
}
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Package crypto implements "SigningMethods" and "EncryptionMethods";
2+
// that is, ways to sign and encrypt JWS and JWEs, respectively, as well
3+
// as JWTs.
4+
package crypto
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package crypto
2+
3+
import (
4+
"crypto"
5+
"crypto/ecdsa"
6+
"crypto/rand"
7+
"encoding/asn1"
8+
"encoding/json"
9+
"errors"
10+
"math/big"
11+
)
12+
13+
// ErrECDSAVerification is missing from crypto/ecdsa compared to crypto/rsa
14+
var ErrECDSAVerification = errors.New("crypto/ecdsa: verification error")
15+
16+
// SigningMethodECDSA implements the ECDSA family of signing methods signing
17+
// methods
18+
type SigningMethodECDSA struct {
19+
Name string
20+
Hash crypto.Hash
21+
_ struct{}
22+
}
23+
24+
// ECPoint is a marshalling structure for the EC points R and S.
25+
type ECPoint struct {
26+
R *big.Int
27+
S *big.Int
28+
}
29+
30+
// Specific instances of EC SigningMethods.
31+
var (
32+
// SigningMethodES256 implements ES256.
33+
SigningMethodES256 = &SigningMethodECDSA{
34+
Name: "ES256",
35+
Hash: crypto.SHA256,
36+
}
37+
38+
// SigningMethodES384 implements ES384.
39+
SigningMethodES384 = &SigningMethodECDSA{
40+
Name: "ES384",
41+
Hash: crypto.SHA384,
42+
}
43+
44+
// SigningMethodES512 implements ES512.
45+
SigningMethodES512 = &SigningMethodECDSA{
46+
Name: "ES512",
47+
Hash: crypto.SHA512,
48+
}
49+
)
50+
51+
// Alg returns the name of the SigningMethodECDSA instance.
52+
func (m *SigningMethodECDSA) Alg() string { return m.Name }
53+
54+
// Verify implements the Verify method from SigningMethod.
55+
// For this verify method, key must be an *ecdsa.PublicKey.
56+
func (m *SigningMethodECDSA) Verify(raw []byte, signature Signature, key interface{}) error {
57+
58+
ecdsaKey, ok := key.(*ecdsa.PublicKey)
59+
if !ok {
60+
return ErrInvalidKey
61+
}
62+
63+
// Unmarshal asn1 ECPoint
64+
var ecpoint ECPoint
65+
if _, err := asn1.Unmarshal(signature, &ecpoint); err != nil {
66+
return err
67+
}
68+
69+
// Verify the signature
70+
if !ecdsa.Verify(ecdsaKey, m.sum(raw), ecpoint.R, ecpoint.S) {
71+
return ErrECDSAVerification
72+
}
73+
return nil
74+
}
75+
76+
// Sign implements the Sign method from SigningMethod.
77+
// For this signing method, key must be an *ecdsa.PrivateKey.
78+
func (m *SigningMethodECDSA) Sign(data []byte, key interface{}) (Signature, error) {
79+
80+
ecdsaKey, ok := key.(*ecdsa.PrivateKey)
81+
if !ok {
82+
return nil, ErrInvalidKey
83+
}
84+
85+
r, s, err := ecdsa.Sign(rand.Reader, ecdsaKey, m.sum(data))
86+
if err != nil {
87+
return nil, err
88+
}
89+
90+
signature, err := asn1.Marshal(ECPoint{R: r, S: s})
91+
if err != nil {
92+
return nil, err
93+
}
94+
return Signature(signature), nil
95+
}
96+
97+
func (m *SigningMethodECDSA) sum(b []byte) []byte {
98+
h := m.Hash.New()
99+
h.Write(b)
100+
return h.Sum(nil)
101+
}
102+
103+
// Hasher implements the Hasher method from SigningMethod.
104+
func (m *SigningMethodECDSA) Hasher() crypto.Hash {
105+
return m.Hash
106+
}
107+
108+
// MarshalJSON is in case somebody decides to place SigningMethodECDSA
109+
// inside the Header, presumably because they (wrongly) decided it was a good
110+
// idea to use the SigningMethod itself instead of the SigningMethod's Alg
111+
// method. In order to keep things sane, marshalling this will simply
112+
// return the JSON-compatible representation of m.Alg().
113+
func (m *SigningMethodECDSA) MarshalJSON() ([]byte, error) {
114+
return []byte(`"` + m.Alg() + `"`), nil
115+
}
116+
117+
var _ json.Marshaler = (*SigningMethodECDSA)(nil)

0 commit comments

Comments
 (0)