-
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.
Randomize v4 and v5 signatures via custom notation (#209)
* Randomize v4 and v5 signatures via custom notation with the name from openpgpjs
- Loading branch information
Showing
7 changed files
with
115 additions
and
9 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
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 |
---|---|---|
|
@@ -35,6 +35,8 @@ const ( | |
KeyFlagGroupKey | ||
) | ||
|
||
const SaltNotationName = "[email protected]" | ||
|
||
// Signature represents a signature. See RFC 4880, section 5.2. | ||
type Signature struct { | ||
Version int | ||
|
@@ -882,6 +884,20 @@ func (sig *Signature) Sign(h hash.Hash, priv *PrivateKey, config *Config) (err e | |
} | ||
sig.Version = priv.PublicKey.Version | ||
sig.IssuerFingerprint = priv.PublicKey.Fingerprint | ||
if sig.Version < 6 && config.RandomizeSignaturesViaNotation() { | ||
sig.removeNotationsWithName(SaltNotationName) | ||
salt, err := SignatureSaltForHash(sig.Hash, config.Random()) | ||
if err != nil { | ||
return err | ||
} | ||
notation := Notation{ | ||
Name: SaltNotationName, | ||
Value: salt, | ||
IsCritical: false, | ||
IsHumanReadable: false, | ||
} | ||
sig.Notations = append(sig.Notations, ¬ation) | ||
} | ||
sig.outSubpackets, err = sig.buildSubpackets(priv.PublicKey) | ||
if err != nil { | ||
return err | ||
|
@@ -1409,3 +1425,17 @@ func SignatureSaltForHash(hash crypto.Hash, randReader io.Reader) ([]byte, error | |
} | ||
return salt, nil | ||
} | ||
|
||
// removeNotationsWithName removes all notations in this signature with the given name. | ||
func (sig *Signature) removeNotationsWithName(name string) { | ||
if sig == nil || sig.Notations == nil { | ||
return | ||
} | ||
updatedNotations := make([]*Notation, 0, len(sig.Notations)) | ||
for _, notation := range sig.Notations { | ||
if notation.Name != name { | ||
updatedNotations = append(updatedNotations, notation) | ||
} | ||
} | ||
sig.Notations = updatedNotations | ||
} |
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 |
---|---|---|
|
@@ -86,6 +86,7 @@ func TestSignDetachedWithNotation(t *testing.T) { | |
IsHumanReadable: true, | ||
}, | ||
}, | ||
NonDeterministicSignaturesViaNotation: packet.BoolPointer(false), | ||
} | ||
err := DetachSign(signature, kring[0], message, config) | ||
if err != nil { | ||
|
@@ -137,6 +138,7 @@ func TestSignDetachedWithCriticalNotation(t *testing.T) { | |
IsCritical: true, | ||
}, | ||
}, | ||
NonDeterministicSignaturesViaNotation: packet.BoolPointer(false), | ||
} | ||
err := DetachSign(signature, kring[0], message, config) | ||
if err != nil { | ||
|
@@ -180,7 +182,6 @@ func TestSignDetachedWithCriticalNotation(t *testing.T) { | |
} | ||
|
||
func TestNewEntity(t *testing.T) { | ||
|
||
// Check bit-length with no config. | ||
e, err := NewEntity("Test User", "test", "[email protected]", nil) | ||
if err != nil { | ||
|
@@ -211,8 +212,11 @@ func TestNewEntity(t *testing.T) { | |
t.Errorf("BitLength %v, expected %v", bl, cfg.RSABits) | ||
} | ||
|
||
keySerializeConfig := &packet.Config{ | ||
NonDeterministicSignaturesViaNotation: packet.BoolPointer(false), | ||
} | ||
w := bytes.NewBuffer(nil) | ||
if err := e.SerializePrivate(w, nil); err != nil { | ||
if err := e.SerializePrivate(w, keySerializeConfig); err != nil { | ||
t.Errorf("failed to serialize entity: %s", err) | ||
return | ||
} | ||
|
@@ -229,7 +233,7 @@ func TestNewEntity(t *testing.T) { | |
} | ||
|
||
w = bytes.NewBuffer(nil) | ||
if err := e.SerializePrivate(w, nil); err != nil { | ||
if err := e.SerializePrivate(w, keySerializeConfig); err != nil { | ||
t.Errorf("failed to serialize entity second time: %s", err) | ||
return | ||
} | ||
|
@@ -247,7 +251,7 @@ func TestNewEntity(t *testing.T) { | |
} | ||
|
||
w = bytes.NewBuffer(nil) | ||
if err := e.SerializePrivate(w, nil); err != nil { | ||
if err := e.SerializePrivate(w, keySerializeConfig); err != nil { | ||
t.Errorf("failed to serialize after encryption round: %s", err) | ||
return | ||
} | ||
|