-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Notation Data support in subpackets (#33)
This commit adds support for parsing and serializing Notation Data signature subpackets. It adds a `Notations []*Notation` field to `Signature`, with type Notation struct { Name string Value []byte IsCritical bool IsHumanReadable bool } Additionally, it adds a `KnownNotations map[string]bool` field to `Config`, which specifies the notation names that are allowed to be present in critical Notation Data signature subpackets. --------- Co-authored-by: Daniel Huigens <[email protected]> Co-authored-by: marinthiercelin <[email protected]>
- Loading branch information
1 parent
f7f10de
commit 34c1fc3
Showing
9 changed files
with
304 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -960,6 +960,69 @@ func TestNewEntityPrivateSerialization(t *testing.T) { | |
} | ||
} | ||
|
||
func TestNotationPacket(t *testing.T) { | ||
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(keyWithNotation)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assertNotationPackets(t, keys) | ||
|
||
serializedEntity := bytes.NewBuffer(nil) | ||
err = keys[0].SerializePrivate(serializedEntity, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
keys, err = ReadKeyRing(serializedEntity) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assertNotationPackets(t, keys) | ||
} | ||
|
||
func assertNotationPackets(t *testing.T, keys EntityList) { | ||
if len(keys) != 1 { | ||
t.Errorf("Failed to accept key, %d", len(keys)) | ||
} | ||
|
||
identity := keys[0].Identities["Test <[email protected]>"] | ||
|
||
if numSigs, numExpected := len(identity.Signatures), 1; numSigs != numExpected { | ||
t.Fatalf("got %d signatures, expected %d", numSigs, numExpected) | ||
} | ||
|
||
notations := identity.Signatures[0].Notations | ||
if numSigs, numExpected := len(notations), 2; numSigs != numExpected { | ||
t.Fatalf("got %d Data Notation subpackets, expected %d", numSigs, numExpected) | ||
} | ||
|
||
if notations[0].IsHumanReadable != true { | ||
t.Fatalf("got false, expected true") | ||
} | ||
|
||
if notations[0].Name != "[email protected]" { | ||
t.Fatalf("got %s, expected [email protected]", notations[0].Name) | ||
} | ||
|
||
if string(notations[0].Value) != "test" { | ||
t.Fatalf("got %s, expected 2", string(notations[0].Value)) | ||
} | ||
|
||
if notations[1].IsHumanReadable != false { | ||
t.Fatalf("got true, expected false") | ||
} | ||
|
||
if notations[1].Name != "[email protected]" { | ||
t.Fatalf("got %s, expected [email protected]", notations[1].Name) | ||
} | ||
|
||
if !bytes.Equal(notations[1].Value, []byte{0, 1, 2, 3}) { | ||
t.Fatalf("got %s, expected 3", string(notations[1].Value)) | ||
} | ||
} | ||
|
||
func TestEntityPrivateSerialization(t *testing.T) { | ||
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(armoredPrivateKeyBlock)) | ||
if err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package packet | ||
|
||
// Notation type represents a Notation Data subpacket | ||
// see https://tools.ietf.org/html/rfc4880#section-5.2.3.16 | ||
type Notation struct { | ||
Name string | ||
Value []byte | ||
IsCritical bool | ||
IsHumanReadable bool | ||
} | ||
|
||
func (notation *Notation) getData() []byte { | ||
nameData := []byte(notation.Name) | ||
nameLen := len(nameData) | ||
valueLen := len(notation.Value) | ||
|
||
data := make([]byte, 8+nameLen+valueLen) | ||
if notation.IsHumanReadable { | ||
data[0] = 0x80 | ||
} | ||
|
||
data[4] = byte(nameLen >> 8) | ||
data[5] = byte(nameLen) | ||
data[6] = byte(valueLen >> 8) | ||
data[7] = byte(valueLen) | ||
copy(data[8:8+nameLen], nameData) | ||
copy(data[8+nameLen:], notation.Value) | ||
return data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package packet | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestNotationGetData(t *testing.T) { | ||
notation := Notation{ | ||
Name: "[email protected]", | ||
Value: []byte("test-value"), | ||
IsCritical: true, | ||
IsHumanReadable: true, | ||
} | ||
expected := []byte{0x80, 0, 0, 0, 0, 14, 0, 10} | ||
expected = append(expected, []byte(notation.Name)...) | ||
expected = append(expected, []byte(notation.Value)...) | ||
data := notation.getData() | ||
if !bytes.Equal(expected, data) { | ||
t.Fatalf("Expected %s, got %s", expected, data) | ||
} | ||
} | ||
|
||
func TestNotationGetDataNotHumanReadable(t *testing.T) { | ||
notation := Notation{ | ||
Name: "[email protected]", | ||
Value: []byte("test-value"), | ||
IsCritical: true, | ||
IsHumanReadable: false, | ||
} | ||
expected := []byte{0, 0, 0, 0, 0, 14, 0, 10} | ||
expected = append(expected, []byte(notation.Name)...) | ||
expected = append(expected, []byte(notation.Value)...) | ||
data := notation.getData() | ||
if !bytes.Equal(expected, data) { | ||
t.Fatalf("Expected %s, got %s", expected, data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -435,6 +435,63 @@ func TestDetachedSignatureExpiredCrossSig(t *testing.T) { | |
} | ||
} | ||
|
||
func TestSignatureUnknownNotation(t *testing.T) { | ||
el, err := ReadArmoredKeyRing(bytes.NewBufferString(criticalNotationSigner)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
raw, err := armor.Decode(strings.NewReader(signedMessageWithCriticalNotation)) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
md, err := ReadMessage(raw.Body, el, nil, nil) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
_, err = ioutil.ReadAll(md.UnverifiedBody) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
const expectedErr string = "openpgp: invalid signature: unknown critical notation: [email protected]" | ||
if md.SignatureError == nil || md.SignatureError.Error() != expectedErr { | ||
t.Errorf("Expected error '%s', but got error '%s'", expectedErr, md.SignatureError) | ||
} | ||
} | ||
|
||
func TestSignatureKnownNotation(t *testing.T) { | ||
el, err := ReadArmoredKeyRing(bytes.NewBufferString(criticalNotationSigner)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
raw, err := armor.Decode(strings.NewReader(signedMessageWithCriticalNotation)) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
config := &packet.Config{ | ||
KnownNotations: map[string]bool{ | ||
"[email protected]": true, | ||
}, | ||
} | ||
md, err := ReadMessage(raw.Body, el, nil, config) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
_, err = ioutil.ReadAll(md.UnverifiedBody) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
if md.SignatureError != nil { | ||
t.Error(md.SignatureError) | ||
return | ||
} | ||
} | ||
|
||
func TestReadingArmoredPrivateKey(t *testing.T) { | ||
el, err := ReadArmoredKeyRing(bytes.NewBufferString(armoredPrivateKeyBlock)) | ||
if err != nil { | ||
|
Oops, something went wrong.