Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow setting the SigningKeyId to use #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions openpgp/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ func (e *Entity) encryptionKey(now time.Time) (Key, bool) {

// signingKey return the best candidate Key for signing a message with this
// Entity.
func (e *Entity) signingKey(now time.Time) (Key, bool) {
func (e *Entity) signingKey(now time.Time, id uint64) (Key, bool) {
i := e.primaryIdentity()
// If the id specified is the Entity.PrivateKey.KeyID lets skip looking at the subkeys
if e.PrivateKey != nil && e.PrivateKey.KeyId == id {
return Key{e, e.PrimaryKey, e.PrivateKey, i.SelfSignature, i.SelfSignature.GetKeyFlags()}, true
}
candidateSubkey := -1

// Iterate the keys to find the newest, non-revoked key that can
Expand All @@ -182,7 +187,8 @@ func (e *Entity) signingKey(now time.Time) (Key, bool) {
subkey.PublicKey.PubKeyAlgo.CanSign() &&
!subkey.Sig.KeyExpired(now) &&
subkey.Revocation == nil &&
(maxTime.IsZero() || subkey.Sig.CreationTime.After(maxTime)) {
(maxTime.IsZero() || subkey.Sig.CreationTime.After(maxTime)) &&
(id == 0 || subkey.PrivateKey.KeyId == id) {
candidateSubkey = i
maxTime = subkey.Sig.CreationTime
break
Expand All @@ -196,7 +202,6 @@ func (e *Entity) signingKey(now time.Time) (Key, bool) {

// If we have no candidate subkey then we assume that it's ok to sign
// with the primary key.
i := e.primaryIdentity()
if (!i.SelfSignature.FlagsValid || i.SelfSignature.FlagSign) &&
e.PrimaryKey.PubKeyAlgo.CanSign() &&
!i.SelfSignature.KeyExpired(now) &&
Expand Down
9 changes: 9 additions & 0 deletions openpgp/packet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type Config struct {
// ReuseSignatures tells us to reuse existing Signatures
// on serialized output.
ReuseSignaturesOnSerialize bool
// SigningKeyId specify signing key id to use, if not set defaults to best guess on subkeys.
SigningKeyId uint64
}

func (c *Config) Random() io.Reader {
Expand Down Expand Up @@ -96,3 +98,10 @@ func (c *Config) PasswordHashIterations() int {
func (c *Config) ReuseSignatures() bool {
return c != nil && c.ReuseSignaturesOnSerialize
}

func (c *Config) SigningKey() uint64 {
if c == nil {
return 0
}
return c.SigningKeyId
}
6 changes: 3 additions & 3 deletions openpgp/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func SignWithSigner(s packet.Signer, w io.Writer, message io.Reader, sigType pac
}

func detachSign(w io.Writer, signer *Entity, message io.Reader, sigType packet.SignatureType, config *packet.Config) (err error) {
signerSubkey, ok := signer.signingKey(config.Now())
signerSubkey, ok := signer.signingKey(config.Now(), config.SigningKey())
if !ok {
err = errors.InvalidArgumentError("no valid signing keys")
return
Expand Down Expand Up @@ -209,7 +209,7 @@ func hashToHashId(h crypto.Hash) uint8 {
func Encrypt(ciphertext io.Writer, to []*Entity, signed *Entity, hints *FileHints, config *packet.Config) (plaintext io.WriteCloser, err error) {
var signer *packet.PrivateKey
if signed != nil {
signKey, ok := signed.signingKey(config.Now())
signKey, ok := signed.signingKey(config.Now(), config.SigningKey())
if !ok {
return nil, errors.InvalidArgumentError("no valid signing keys")
}
Expand Down Expand Up @@ -443,7 +443,7 @@ func AttachedSign(out io.WriteCloser, signed Entity, hints *FileHints,

var signer *packet.PrivateKey

signKey, ok := signed.signingKey(config.Now())
signKey, ok := signed.signingKey(config.Now(), config.SigningKey())
if !ok {
err = errors.InvalidArgumentError("no valid signing keys")
return
Expand Down
6 changes: 3 additions & 3 deletions openpgp/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestSignWithSigner(t *testing.T) {
t.Error(err)
}

signerSubkey, ok := kring[0].signingKey(time.Now())
signerSubkey, ok := kring[0].signingKey(time.Now(), 0)
if !ok {
t.Error("couldn't get signer subkey")
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestSignerCanReturnErrors(t *testing.T) {
t.Error(err)
}

signerSubkey, ok := kring[0].signingKey(time.Now())
signerSubkey, ok := kring[0].signingKey(time.Now(), 0)
if !ok {
t.Error("couldn't get signer subkey")
}
Expand Down Expand Up @@ -333,7 +333,7 @@ func TestEncryption(t *testing.T) {

testTime, _ := time.Parse("2006-01-02", "2013-07-01")
if test.isSigned {
signKey, _ := kring[0].signingKey(testTime)
signKey, _ := kring[0].signingKey(testTime, 0)
expectedKeyId := signKey.PublicKey.KeyId
if md.SignedByKeyId != expectedKeyId {
t.Errorf("#%d: message signed by wrong key id, got: %d, want: %d", i, md.SignedByKeyId, expectedKeyId)
Expand Down