Skip to content

Commit

Permalink
Rename header struct, clean up nits
Browse files Browse the repository at this point in the history
  • Loading branch information
csstaub committed Dec 23, 2014
1 parent f76b1ad commit c36e59e
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 51 deletions.
20 changes: 10 additions & 10 deletions asymmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (ctx rsaEncrypterVerifier) encryptKey(cek []byte, alg KeyAlgorithm) (recipi

return recipientInfo{
encryptedKey: encryptedKey,
header: &JoseHeader{},
header: &Header{},
}, nil
}

Expand All @@ -153,7 +153,7 @@ func (ctx rsaEncrypterVerifier) encrypt(cek []byte, alg KeyAlgorithm) ([]byte, e
}

// Decrypt the given payload and return the content encryption key.
func (ctx rsaDecrypterSigner) decryptKey(headers JoseHeader, recipient *recipientInfo, generator keyGenerator) ([]byte, error) {
func (ctx rsaDecrypterSigner) decryptKey(headers Header, recipient *recipientInfo, generator keyGenerator) ([]byte, error) {
return ctx.decrypt(recipient.encryptedKey, KeyAlgorithm(headers.Alg), generator)
}

Expand Down Expand Up @@ -245,7 +245,7 @@ func (ctx rsaDecrypterSigner) signPayload(payload []byte, alg SignatureAlgorithm

return signatureInfo{
signature: out,
protected: &JoseHeader{},
protected: &Header{},
}, nil
}

Expand Down Expand Up @@ -286,7 +286,7 @@ func (ctx ecEncrypterVerifier) encryptKey(cek []byte, alg KeyAlgorithm) (recipie
case ECDH_ES:
// ECDH-ES mode doesn't wrap a key, the shared secret is used directly as the key.
return recipientInfo{
header: &JoseHeader{},
header: &Header{},
}, nil
case ECDH_ES_A128KW, ECDH_ES_A192KW, ECDH_ES_A256KW:
default:
Expand Down Expand Up @@ -329,28 +329,28 @@ func (ctx ecKeyGenerator) keySize() int {
}

// Get a content encryption key for ECDH-ES
func (ctx ecKeyGenerator) genKey() ([]byte, JoseHeader, error) {
func (ctx ecKeyGenerator) genKey() ([]byte, Header, error) {
priv, err := ecdsa.GenerateKey(ctx.publicKey.Curve, rand.Reader)
if err != nil {
return nil, JoseHeader{}, err
return nil, Header{}, err
}

out := josecipher.DeriveECDHES(ctx.algID, []byte{}, []byte{}, priv, ctx.publicKey, ctx.size)

epk, err := serializeECPublicKey(&priv.PublicKey)
if err != nil {
return nil, JoseHeader{}, err
return nil, Header{}, err
}

headers := JoseHeader{
headers := Header{
Epk: epk,
}

return out, headers, nil
}

// Decrypt the given payload and return the content encryption key.
func (ctx ecDecrypterSigner) decryptKey(headers JoseHeader, recipient *recipientInfo, generator keyGenerator) ([]byte, error) {
func (ctx ecDecrypterSigner) decryptKey(headers Header, recipient *recipientInfo, generator keyGenerator) ([]byte, error) {
publicKey, err := parseECPublicKey(headers.Epk)
if err != nil {
return nil, err
Expand Down Expand Up @@ -417,7 +417,7 @@ func (ctx ecDecrypterSigner) signPayload(payload []byte, alg SignatureAlgorithm)

return signatureInfo{
signature: out,
protected: &JoseHeader{},
protected: &Header{},
}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions asymmetric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ func (ctx failingKeyGenerator) keySize() int {
return 0
}

func (ctx failingKeyGenerator) genKey() ([]byte, JoseHeader, error) {
return nil, JoseHeader{}, errors.New("failed to generate key")
func (ctx failingKeyGenerator) genKey() ([]byte, Header, error) {
return nil, Header{}, errors.New("failed to generate key")
}

func TestPKCSKeyGeneratorFailure(t *testing.T) {
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestInvalidECDecrypt(t *testing.T) {
generator := randomKeyGenerator{size: 16}

// Missing epk header
headers := JoseHeader{
headers := Header{
Alg: string(ECDH_ES),
}

Expand Down
6 changes: 3 additions & 3 deletions crypter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type contentCipher interface {
// A key generator (for generating/getting a CEK)
type keyGenerator interface {
keySize() int
genKey() ([]byte, JoseHeader, error)
genKey() ([]byte, Header, error)
}

// A generic key encrypter
Expand All @@ -58,7 +58,7 @@ type keyEncrypter interface {

// A generic key decrypter
type keyDecrypter interface {
decryptKey(headers JoseHeader, recipient *recipientInfo, generator keyGenerator) ([]byte, error) // Decrypt a key
decryptKey(headers Header, recipient *recipientInfo, generator keyGenerator) ([]byte, error) // Decrypt a key
}

// A generic encrypter based on the given key encrypter and content cipher.
Expand Down Expand Up @@ -216,7 +216,7 @@ func (ctx *genericEncrypter) EncryptWithAuthData(plaintext, aad []byte) (*JsonWe
obj := &JsonWebEncryption{}
obj.aad = aad

obj.protected = &JoseHeader{
obj.protected = &Header{
Enc: ctx.contentAlg,
}
obj.recipients = make([]recipientInfo, len(ctx.recipients))
Expand Down
16 changes: 8 additions & 8 deletions jwe.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
// rawJsonWebEncryption represents a raw JWE JSON object. Used for parsing/serializing.
type rawJsonWebEncryption struct {
Protected *encodedBuffer `json:"protected,omitempty"`
Unprotected *JoseHeader `json:"unprotected,omitempty"`
Header *JoseHeader `json:"header,omitempty"`
Unprotected *Header `json:"unprotected,omitempty"`
Header *Header `json:"header,omitempty"`
Recipients []rawRecipientInfo `json:"recipients,omitempty"`
Aad *encodedBuffer `json:"aad,omitempty"`
EncryptedKey *encodedBuffer `json:"encrypted_key,omitempty"`
Expand All @@ -37,21 +37,21 @@ type rawJsonWebEncryption struct {

// rawRecipientInfo represents a raw JWE Per-Recipient Header JSON object. Used for parsing/serializing.
type rawRecipientInfo struct {
Header *JoseHeader `json:"header,omitempty"`
Header *Header `json:"header,omitempty"`
EncryptedKey string `json:"encrypted_key,omitempty"`
}

// JsonWebEncryption represents an encrypted JWE object after parsing.
type JsonWebEncryption struct {
protected, unprotected *JoseHeader
protected, unprotected *Header
recipients []recipientInfo
aad, iv, ciphertext, tag []byte
original *rawJsonWebEncryption
}

// recipientInfo represents a raw JWE Per-Recipient Header JSON object after parsing.
type recipientInfo struct {
header *JoseHeader
header *Header
encryptedKey []byte
}

Expand All @@ -67,8 +67,8 @@ func (obj JsonWebEncryption) GetAuthData() []byte {
}

// Get the merged header values
func (obj JsonWebEncryption) mergedHeaders(recipient *recipientInfo) JoseHeader {
out := JoseHeader{}
func (obj JsonWebEncryption) mergedHeaders(recipient *recipientInfo) Header {
out := Header{}
out.merge(obj.protected)
out.merge(obj.unprotected)

Expand Down Expand Up @@ -174,7 +174,7 @@ func parseEncryptedCompact(input string) (*JsonWebEncryption, error) {
return nil, err
}

var protected JoseHeader
var protected Header
err = json.Unmarshal(rawProtected, &protected)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions jwe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestFullParseJWE(t *testing.T) {

func TestMissingInvalidHeaders(t *testing.T) {
obj := &JsonWebEncryption{
protected: &JoseHeader{Enc: A128GCM},
protected: &Header{Enc: A128GCM},
recipients: []recipientInfo{
recipientInfo{},
},
Expand All @@ -132,7 +132,7 @@ func TestMissingInvalidHeaders(t *testing.T) {
t.Error("should detect invalid key")
}

obj.protected = &JoseHeader{Alg: string(RSA1_5)}
obj.protected = &Header{Alg: string(RSA1_5)}

_, err = obj.Decrypt(rsaTestKey)
if err == nil || err == ErrCryptoFailure {
Expand All @@ -143,7 +143,7 @@ func TestMissingInvalidHeaders(t *testing.T) {
func TestCompactSerialize(t *testing.T) {
// Compact serialization must fail if we have unprotected headers
obj := &JsonWebEncryption{
unprotected: &JoseHeader{Alg: "XYZ"},
unprotected: &Header{Alg: "XYZ"},
}

_, err := obj.CompactSerialize()
Expand Down
14 changes: 7 additions & 7 deletions jws.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type rawJsonWebSignature struct {
// rawSignatureInfo represents a single JWS signature over the JWS payload and protected header.
type rawSignatureInfo struct {
Protected *encodedBuffer `json:"protected,omitempty"`
Header *JoseHeader `json:"header,omitempty"`
Header *Header `json:"header,omitempty"`
Signature *encodedBuffer `json:"signature,omitempty"`
}

Expand All @@ -43,8 +43,8 @@ type JsonWebSignature struct {

// signatureInfo represents a single JWS signature over the JWS payload and protected header after parsing.
type signatureInfo struct {
protected *JoseHeader
header *JoseHeader
protected *Header
header *Header
signature []byte
original *rawSignatureInfo
}
Expand All @@ -60,8 +60,8 @@ func ParseSigned(input string) (*JsonWebSignature, error) {
}

// Get a header value
func (sig signatureInfo) mergedHeaders() JoseHeader {
out := JoseHeader{}
func (sig signatureInfo) mergedHeaders() Header {
out := Header{}
out.merge(sig.protected)
out.merge(sig.header)
return out
Expand Down Expand Up @@ -100,7 +100,7 @@ func parseSignedFull(input string) (*JsonWebSignature, error) {
obj.signatures = make([]signatureInfo, len(parsed.Signatures))
for i, sig := range parsed.Signatures {
if sig.Protected != nil && len(sig.Protected.bytes()) > 0 {
obj.signatures[i].protected = &JoseHeader{}
obj.signatures[i].protected = &Header{}
err = json.Unmarshal(sig.Protected.bytes(), obj.signatures[i].protected)
if err != nil {
return nil, err
Expand Down Expand Up @@ -131,7 +131,7 @@ func parseSignedCompact(input string) (*JsonWebSignature, error) {
return nil, err
}

var protected JoseHeader
var protected Header
err = json.Unmarshal(rawProtected, &protected)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ const (
DEFLATE = CompressionAlgorithm("DEF") // DEFLATE (RFC 1951)
)

// JsonWebEncryptionHeader represents the JOSE header for JWE objects.
type JoseHeader struct {
// Header represents the JOSE header for JWE/JWS objects.
type Header struct {
Alg string `json:"alg,omitempty"`
Enc ContentEncryption `json:"enc,omitempty"`
Zip CompressionAlgorithm `json:"zip,omitempty"`
Expand All @@ -121,7 +121,7 @@ type JoseHeader struct {
}

// Merge headers from src into dst, giving precedence to headers from l.
func (dst *JoseHeader) merge(src *JoseHeader) {
func (dst *Header) merge(src *Header) {
if src == nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (ctx *genericSigner) Sign(payload []byte) (*JsonWebSignature, error) {
obj.signatures = make([]signatureInfo, len(ctx.recipients))

for i, recipient := range ctx.recipients {
protected := &JoseHeader{
protected := &Header{
Alg: string(recipient.sigAlg),
}

Expand Down
6 changes: 3 additions & 3 deletions signing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestInvalidJWS(t *testing.T) {
}

obj, err := signer.Sign([]byte("Lorem ipsum dolor sit amet"))
obj.signatures[0].header = &JoseHeader{
obj.signatures[0].header = &Header{
Crit: []string{"TEST"},
}

Expand All @@ -230,8 +230,8 @@ func TestInvalidJWS(t *testing.T) {
}

// Try without alg header
obj.signatures[0].protected = &JoseHeader{}
obj.signatures[0].header = &JoseHeader{}
obj.signatures[0].protected = &Header{}
obj.signatures[0].header = &Header{}

_, err = obj.Verify(&rsaTestKey.PublicKey)
if err == nil {
Expand Down
20 changes: 10 additions & 10 deletions symmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ func newSymmetricSigner(sigAlg SignatureAlgorithm, key []byte) (recipientSigInfo
}

// Generate a random key for the given content cipher
func (ctx randomKeyGenerator) genKey() ([]byte, JoseHeader, error) {
func (ctx randomKeyGenerator) genKey() ([]byte, Header, error) {
key := make([]byte, ctx.size)
_, err := io.ReadFull(randReader, key)
if err != nil {
return nil, JoseHeader{}, err
return nil, Header{}, err
}

return key, JoseHeader{}, nil
return key, Header{}, nil
}

// Key size for random generator
Expand All @@ -162,10 +162,10 @@ func (ctx randomKeyGenerator) keySize() int {
}

// Generate a static key (for direct mode)
func (ctx staticKeyGenerator) genKey() ([]byte, JoseHeader, error) {
func (ctx staticKeyGenerator) genKey() ([]byte, Header, error) {
cek := make([]byte, len(ctx.key))
copy(cek, ctx.key)
return cek, JoseHeader{}, nil
return cek, Header{}, nil
}

// Key size for static generator
Expand Down Expand Up @@ -218,7 +218,7 @@ func (ctx *symmetricKeyCipher) encryptKey(cek []byte, alg KeyAlgorithm) (recipie
switch alg {
case DIRECT:
return recipientInfo{
header: &JoseHeader{},
header: &Header{},
}, nil
case A128GCMKW, A192GCMKW, A256GCMKW:
aead := newAESGCM(len(ctx.key))
Expand All @@ -229,7 +229,7 @@ func (ctx *symmetricKeyCipher) encryptKey(cek []byte, alg KeyAlgorithm) (recipie
}

return recipientInfo{
header: &JoseHeader{
header: &Header{
Iv: newBuffer(parts.iv),
Tag: newBuffer(parts.tag),
},
Expand All @@ -243,15 +243,15 @@ func (ctx *symmetricKeyCipher) encryptKey(cek []byte, alg KeyAlgorithm) (recipie

return recipientInfo{
encryptedKey: jek,
header: &JoseHeader{},
header: &Header{},
}, nil
}

return recipientInfo{}, ErrUnsupportedAlgorithm
}

// Decrypt the content encryption key.
func (ctx *symmetricKeyCipher) decryptKey(headers JoseHeader, recipient *recipientInfo, generator keyGenerator) ([]byte, error) {
func (ctx *symmetricKeyCipher) decryptKey(headers Header, recipient *recipientInfo, generator keyGenerator) ([]byte, error) {
switch KeyAlgorithm(headers.Alg) {
case DIRECT:
cek := make([]byte, len(ctx.key))
Expand Down Expand Up @@ -292,7 +292,7 @@ func (ctx symmetricMac) signPayload(payload []byte, alg SignatureAlgorithm) (sig

return signatureInfo{
signature: mac,
protected: &JoseHeader{},
protected: &Header{},
}, nil
}

Expand Down

0 comments on commit c36e59e

Please sign in to comment.