Golang (GO) implementation of Javascript Object Signing and Encryption specification
Golang (GO) implementation of Javascript Object Signing and Encryption specification
Pure Golang (GO) library for generating, decoding and encrypting JSON Web Tokens. Zero dependency, relies only on standard library.
Supports full suite of signing, encryption and compression algorithms defined by JSON Web Algorithms as of July 4, 2014 version.
Extensively unit tested and cross tested (100+ tests) for compatibility with jose.4.j, Nimbus-JOSE-JWT, json-jwt and jose-jwt libraries.
Used in production. GA ready. Current version is 1.9.
v1.11 more security hardening, ECDH-ES malformed ephemeral public key (epk.x) causes nil-curve dereference panic (remote DoS)
v1.10 more security hardening, #42
v1.9 security hardening, fixed found vulnerabilities
v1.8 added experimental RSA-OAEP-384 and RSA-OAEP-512 key management algorithms
v1.7 introduced deflate decompression memory limits to avoid denial-of-service attacks aka 'deflate-bomb'. See Customizing compression section for details.
v1.6 security tuning options
v1.5 bug fix release
v1.4 changes default behavior of inserting typ=JWT header if not overriden. As of 1.4 no
extra headers added by library automatically. To mimic pre 1.4 behaviour use:
token, err := jose.Sign(..., jose.Header("typ", "JWT"))
//or
token, err := jose.Encrypt(..., jose.Header("typ", "JWT"))v1.3 fixed potential Invalid Curve Attack on NIST curves within ECDH key management. Upgrade strongly recommended.
v1.2 breaks jose.Decode interface by returning 3 values instead of 2.
v1.2 deprecates jose.Compress method in favor of using configuration options to jose.Encrypt,
the method will be removed in next release.
Pre v1.2 decoding:
payload,err := jose.Decode(token,sharedKey)Should be updated to v1.2:
payload, headers, err := jose.Decode(token,sharedKey)Pre v1.2 compression:
token,err := jose.Compress(payload,jose.DIR,jose.A128GCM,jose.DEF, key)Should be update to v1.2:
token, err := jose.Encrypt(payload, jose.DIR, jose.A128GCM, key, jose.Zip(jose.DEF))Signing
Encryption
Compression
go get github.com/dvsekhvalnov/jose2go or go get -u github.com/dvsekhvalnov/jose2go to update to latest version
import (
"github.com/dvsekhvalnov/jose2go"
)package main
import (
"fmt"
"github.com/dvsekhvalnov/jose2go"
)
func main() {
payload := `{"hello": "world"}`
token,err := jose.Sign(payload,jose.NONE, nil)
if(err==nil) {
//go use token
fmt.Printf("\nPlaintext = %v\n",token)
}
}Signing with HS256, HS384, HS512 expecting []byte array key of corresponding length:
package main
import (
"fmt"
"github.com/dvsekhvalnov/jose2go"
)
func main() {
payload := `{"hello": "world"}`
key := []byte{97,48,97,50,97,98,100,56,45,54,49,54,50,45,52,49,99,51,45,56,51,100,54,45,49,99,102,53,53,57,98,52,54,97,102,99}
token,err := jose.Sign(payload,jose.HS256,key)
if(err==nil) {
//go use token
fmt.Printf("\nHS256 = %v\n",token)
}
}Signing with RS256, RS384, RS512, PS256, PS384, PS512 expecting *rsa.PrivateKey private key of corresponding length. jose2go provides convenient utils to construct *rsa.PrivateKey instance from PEM encoded PKCS1 or PKCS8 data: Rsa.ReadPrivate([]byte) under jose2go/keys/rsa package.
package main
import (
"fmt"
"io/ioutil"
Rsa "github.com/dvsekhvalnov/jose2go/keys/rsa"
"github.com/dvsekhvalnov/jose2go"
)
func main() {
payload := `{"hello": "world"}`
keyBytes,err := ioutil.ReadFile("private.key")
if(err!=nil) {
panic("invalid key file")
}
privateKey,e:=Rsa.ReadPrivate(keyBytes)
if(e!=nil) {
panic("invalid key format")
}
token,err := jose.Sign(payload,jose.RS256, privateKey)
if(err==nil) {
//go use token
fmt.Printf("\nRS256 = %v\n",token)
}
}ES256, ES384, ES512 ECDSA signatures expecting *ecdsa.PrivateKey private elliptic curve key of corresponding length. jose2go provides convenient utils to construct *ecdsa.PrivateKey instance from PEM encoded PKCS1 or PKCS8 data: ecc.ReadPrivate([]byte) or directly from X,Y,D parameters: ecc.NewPrivate(x,y,d []byte) under jose2go/keys/ecc package.
…RSA-OAEP-512, RSA-OAEP-384, RSA-OAEP-256, RSA-OAEP and RSA1_5 key management expecting *rsa.PublicKey public key of corresponding length.
…AES128KW, AES192KW and AES256KW key management requires []byte array key of corresponding length
package main
import (
"fmt"
"github.com/dvsekhvalnov/jose2go"
)
func main() {
payload := `{"hello": "world"}`
sharedKey :=[]byte{194,164,235,6,138,248,171,239,24,216,11,22,137,199,215,133}
token,err := jose.Encrypt(payload,jose.A128KW,jose.A128GCM,sharedKey)
if(err==nil) {
//go use token
fmt.Printf("\nA128KW A128GCM = %v\n",token)
}
}AES128GCMKW, AES192GCMKW and AES256GCMKW key management requires []byte array key of corresponding length
package main
import (
"fmt"
"github.com/dvsekhvalnov/jose2go"
)
func main() {
payload := `{"hello": "world"}`
sharedKey :=[]byte{194,164,235,6,138,248,171,239,24,216,11,22,137,199,215,133}
token,err := jose.Encrypt(payload,jose.A128GCMKW,jose.A128GCM,sharedKey)
if(err==nil) {
//go use token
fmt.Printf("\nA128GCMKW A128GCM = %v\n",token)
}
}ECDH-ES and ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KW key management requires *ecdsa.PublicKey elliptic curve key of corresponding length. jose2go provides convenient utils to construct *ecdsa.PublicKey instance from PEM encoded PKCS1 X509 certificate or PKIX data: ecc.ReadPublic([]byte) or directly from X,Y parameters: ecc.NewPublic(x,y []byte)under jose2go/keys/ecc package:
…PBES2-HS256+A128KW, PBES2-HS384+A192KW, PBES2-HS512+A256KW key management requires string passphrase from which actual key will be derived
package main
import (
"fmt"
"github.com/dvsekhvalnov/jose2go"
)
func main() {
payload := `{"hello": "world"}`
passphrase := `top secret`
token,err := jose.Encrypt(payload,jose.PBES2_HS256_A128KW,jose.A256GCM,passphrase)
if(err==nil) {
//go use token
fmt.Printf("\nPBES2_HS256_A128KW A256GCM = %v\n",token)
}
}Direct key management with pre-shared symmetric keys expecting []byte array key of corresponding length:
package main
import (
"fmt"
"github.com/dvsekhvalnov/jose2go"
)
func main() {
payload := `{"hello": "world"}`
sharedKey :=[]byte{194,164,235,6,138,248,171,239,24,216,11,22,137,199,215,133}
token,err := jose.Encrypt(payload,jose.DIR,jose.A128GCM,sharedKey)
if(err==nil) {
//go use token
fmt.Printf("\nDIR A128GCM = %v\n",token)
}
}jose2go supports optional DEFLATE compression of payload before encrypting, can be used with all supported encryption and key management algorithms:
package main
import (
"fmt"
"github.com/dvsekhvalnov/jose2go"
)
func main() {
payload := `{"hello": "world"}`
sharedKey := []byte{194, 164, 235, 6, 138, 248, 171, 239, 24, 216, 11, 22, 137, 199, 215, 133}
token, err := jose.Encrypt(payload, jose.DIR, jose.A128GCM, sharedKey, jose.Zip(jose.DEF))
if err == nil {
//go use token
fmt.Printf("\nDIR A128GCM DEFLATED= %v\n", token)
}
}Decoding json web tokens is fully symmetric to creating signed or encrypted tokens (with respect to public/private cryptography), decompressing deflated payloads is handled automatically:
As of v1.2 decode method defined as jose.Decode() payload string, headers map[string]interface{}, err error and returns both payload as unprocessed string and headers as map.
HS256, HS384, HS512 signatures, A128KW, A192KW, A256KW,A128GCMKW, A192GCMKW, A256GCMKW and DIR key management algorithm expecting []byte array key:
package main
import (
"fmt"
"github.com/dvsekhvalnov/jose2go"
)
func main() {
token := "eyJhbGciOiJIUzI1NiIsImN0eSI6InRleHRcL3BsYWluIn0.eyJoZWxsbyI6ICJ3b3JsZCJ9.chIoYWrQMA8XL5nFz6oLDJyvgHk2KA4BrFGrKymjC8E"
sharedKey :=[]byte{97,48,97,50,97,98,100,56,45,54,49,54,50,45,52,49,99,51,45,56,51,100,54,45,49,99,102,53,53,57,98,52,54,97,102,99}
payload, headers, err := jose.Decode(token,sharedKey)
if(err==nil) {
//go use token
fmt.Printf("\npayload = %v\n",payload)
//and/or use headers
fmt.Printf("\nheaders = %v\n",headers)
}
}RS256, RS384, RS512,PS256, PS384, PS512 signatures expecting *rsa.PublicKey public key of corresponding length. jose2go provides convenient utils to construct *rsa.PublicKey instance from PEM encoded PKCS1 X509 certificate or PKIX data: Rsa.ReadPublic([]byte) under jose2go/keys/rsa package:
…RSA-OAEP-512, RSA-OAEP-384 ,RSA-OAEP-256, RSA-OAEP and RSA1_5 key management algorithms expecting *rsa.PrivateKey private key of corresponding length:
…PBES2-HS256+A128KW, PBES2-HS384+A192KW, PBES2-HS512+A256KW key management algorithms expects string passpharase as a key
package main
import (
"fmt"
"github.com/dvsekhvalnov/jose2go"
)
func main() {No open issues yet, or sync has not completed.