An implementation of JOSE standards (JWE, JWS, JWT) in Go
  • Go 99.3%
  • Perl 0.6%
Find a file
Ameer Ghani a0111f8274
Merge commit from fork
* Bound RSA parameters when parsing

If we don't bound these, degenerate RSA parameters could cause us to use
excessive CPU.

This fix is not ideal. Really we should be separating RSA computation from
JSON parsing. We leave this larger-scale improvement for v5.

This is technically a breaking change, since anyone who is parsing RSA
keys greater than 4096 will break. There is no practical use for RSA keys
greater than 4096, so we don't expect this to break anyone. We leave an
escape hatch `MaxRSAModulusBits` for those who need this functionality.

* Move getJWK() to test code only

It's only used in test code, and unexported, so move it there. This lets
us avoid modifying it to care about whether it's parsing a private key or
not.

* Set unprotected header before merging in general serialization

This fixes a bug where the unprotected header was not populated before
invoking sanitized().

If we don't fix this now, the check that a JWK doesn't contain private
parameters could be bypassed.

* Check JWS and JWE algorithms before sanitizing headers

Not doing this breaks tests for the future change, where we're checking
for an invalid combination of parameters. Rather than fix those tests,
let's move the algorithm check before sanitization. The former is less
expensive than the latter, so it's cleaner to do it this way.

* Reject JWK/EPKs containing private keys in header parsing

For JWS and JWE, private material inside the JWK/EPK makes no sense and is
not allowed by the spec. Reject private parameters during parsing.

If we don't take this change, we are exposed to expensive computation,
particularly around RSA validation. For header parsing, this is not
acceptable.

This _is not_ a breaking change, because we rejected non-public keys after
parsing in the first place. We're just trying to avoid expensive parsing
steps.

* Wrap JWE header sanitization errors

Avoids hiding the new ErrEmbeddedPrivateKey error.

* Update jwk.go

Co-authored-by: Jacob Hoffman-Andrews <github@hoffman-andrews.com>

* PR feedback

---------

Co-authored-by: Jacob Hoffman-Andrews <github@hoffman-andrews.com>
History 2026-10-14 17:46:21 -05:00
.github Add a tests containerfile and threat model (#300) 2026-10-08 16:44:15 -04:00
cipher cipher/cbc_hmac: don't panic on empty ciphertext (#292) 2026-09-03 09:14:36 -07:00
cryptosigner jws: Add expected algorithms for signatures (#74) 2023-11-30 09:37:19 -08:00
fuzz Add fuzz tests for JWS, JWE & JWT parsing (#245) 2026-06-09 21:25:22 -04:00
jose-util Update jose-util to use /v4, drop kingpin (#98) 2024-02-26 10:49:37 -08:00
json json: limit stack depth (#290) 2026-09-03 09:14:16 -07:00
jwt Fix error message for future issued tokens (#303) 2026-10-13 12:10:03 -05:00
testutils testutils/assert: remove True, Nil, NotNil (#202) 2025-07-18 10:04:53 -07:00
.gitignore add comments documenting cram is used for jose-util testing 2019-11-13 14:50:12 -07:00
.golangci.yml Fix decoding x5t parameters (#304) 2020-04-30 11:02:04 -07:00
.travis.yml travis: update config 2020-03-08 12:51:08 -07:00
asymmetric.go Check alg against pubkey curve during verify. (#293) 2026-09-03 10:35:19 -07:00
asymmetric_test.go Check alg against pubkey curve during verify. (#293) 2026-09-03 10:35:19 -07:00
CONTRIBUTING.md Remove CLA mentions from CONTRIBUTING.md (#160) 2025-01-14 09:41:06 -08:00
crypter.go Reject mismatched JWE CEK lengths (#266) 2026-09-03 16:05:40 -07:00
crypter_test.go remove err == nil pattern (#223) 2026-04-02 10:14:49 -07:00
doc.go gofmt 1.20 (#50) 2023-06-28 18:35:35 -07:00
doc_test.go jws: Add expected algorithms for signatures (#74) 2023-11-30 09:37:19 -08:00
encoding.go jwe: limit maximum output from decompressing (#106) 2024-03-07 11:46:13 -08:00
encoding_test.go jwe: limit maximum output from decompressing (#106) 2024-03-07 11:46:13 -08:00
go.mod Remove Go 1.23 support (#205) 2025-08-13 17:08:13 -04:00
go.sum Remove Go 1.23 support (#205) 2025-08-13 17:08:13 -04:00
jwe.go Merge commit from fork 2026-10-14 17:46:21 -05:00
jwe_test.go Merge commit from fork 2026-10-14 17:46:21 -05:00
jwk.go Merge commit from fork 2026-10-14 17:46:21 -05:00
jwk_test.go Merge commit from fork 2026-10-14 17:46:21 -05:00
jws.go Merge commit from fork 2026-10-14 17:46:21 -05:00
jws_test.go Merge commit from fork 2026-10-14 17:46:21 -05:00
LICENSE Initial commit 2014-12-19 10:31:51 -08:00
opaque.go Verify OpaqueSigner's Public() return is public (#260) 2026-06-17 19:40:48 -04:00
opaque_test.go Verify OpaqueSigner's Public() return is public (#260) 2026-06-17 19:40:48 -04:00
README.md Remove Go 1.23 support (#205) 2025-08-13 17:08:13 -04:00
SECURITY.md Add a security.md doc for contacting us about potential security vulnerabilities (#65) 2023-11-08 17:49:03 -08:00
shared.go Merge commit from fork 2026-10-14 17:46:21 -05:00
shared_test.go Merge commit from fork 2026-10-14 17:46:21 -05:00
signing.go Reject typed nil at Verify time 2026-09-03 11:06:52 -07:00
signing_test.go Reject typed nil at Verify time 2026-09-03 11:06:52 -07:00
symmetric.go un-export randReader (#226) 2026-04-01 17:17:46 -07:00
symmetric_test.go un-export randReader (#226) 2026-04-01 17:17:46 -07:00
tests.Containerfile Add a tests containerfile and threat model (#300) 2026-10-08 16:44:15 -04:00
threat_model.md Add a tests containerfile and threat model (#300) 2026-10-08 16:44:15 -04:00
utils_test.go un-export randReader (#226) 2026-04-01 17:17:46 -07:00

Go JOSE

godoc godoc license

Package jose aims to provide an implementation of the Javascript Object Signing and Encryption set of standards. This includes support for JSON Web Encryption, JSON Web Signature, and JSON Web Token standards.

Overview

The implementation follows the JSON Web Encryption (RFC 7516), JSON Web Signature (RFC 7515), and JSON Web Token (RFC 7519) specifications. Tables of supported algorithms are shown below. The library supports both the compact and JWS/JWE JSON Serialization formats, and has optional support for multiple recipients. It also comes with a small command-line utility (jose-util) for dealing with JOSE messages in a shell.

Note: We use a forked version of the encoding/json package from the Go standard library which uses case-sensitive matching for member names (instead of case-insensitive matching). This is to avoid differences in interpretation of messages between go-jose and libraries in other languages.

Versions

The forthcoming Version 5 will be released with several breaking API changes, and will require Golang's encoding/json/v2, which is currently requires Go 1.25 built with GOEXPERIMENT=jsonv2.

Version 4 is the current stable version:

import "github.com/go-jose/go-jose/v4"

It supports at least the current and previous Golang release. Currently it requires Golang 1.24.

Version 3 is only receiving critical security updates. Migration to Version 4 is recommended.

Versions 1 and 2 are obsolete, but can be found in the old repository, square/go-jose.

Supported algorithms

See below for a table of supported algorithms. Algorithm identifiers match the names in the JSON Web Algorithms standard where possible. The Godoc reference has a list of constants.

Key encryption Algorithm identifier(s)
RSA-PKCS#1v1.5 RSA1_5
RSA-OAEP RSA-OAEP, RSA-OAEP-256
AES key wrap A128KW, A192KW, A256KW
AES-GCM key wrap A128GCMKW, A192GCMKW, A256GCMKW
ECDH-ES + AES key wrap ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KW
ECDH-ES (direct) ECDH-ES1
Direct encryption dir1

1. Not supported in multi-recipient mode

Signing / MAC Algorithm identifier(s)
RSASSA-PKCS#1v1.5 RS256, RS384, RS512
RSASSA-PSS PS256, PS384, PS512
HMAC HS256, HS384, HS512
ECDSA ES256, ES384, ES512
Ed25519 EdDSA2

2. Only available in version 2 of the package

Content encryption Algorithm identifier(s)
AES-CBC+HMAC A128CBC-HS256, A192CBC-HS384, A256CBC-HS512
AES-GCM A128GCM, A192GCM, A256GCM
Compression Algorithm identifiers(s)
DEFLATE (RFC 1951) DEF

Supported key types

See below for a table of supported key types. These are understood by the library, and can be passed to corresponding functions such as NewEncrypter or NewSigner. Each of these keys can also be wrapped in a JWK if desired, which allows attaching a key id.

Algorithm(s) Corresponding types
RSA *rsa.PublicKey, *rsa.PrivateKey
ECDH, ECDSA *ecdsa.PublicKey, *ecdsa.PrivateKey
EdDSA1 ed25519.PublicKey, ed25519.PrivateKey
AES, HMAC []byte

1. Only available in version 2 or later of the package

Examples

godoc godoc

Examples can be found in the Godoc reference for this package. The jose-util subdirectory also contains a small command-line utility which might be useful as an example as well.