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

make stringToKeySpecifier a real type #9

Open
wants to merge 4 commits 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
19 changes: 18 additions & 1 deletion openpgp/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ func addSubkey(e *Entity, packets *packet.Reader, pub *packet.PublicKey, priv *p
case packet.SigTypeSubkeyRevocation:
subKey.Sig = sig
case packet.SigTypeSubkeyBinding:
if subKey.Sig == nil {

if shouldReplaceSubkeySig(subKey.Sig, sig) {
subKey.Sig = sig
}
}
Expand All @@ -472,6 +473,22 @@ func addSubkey(e *Entity, packets *packet.Reader, pub *packet.PublicKey, priv *p
return nil
}

func shouldReplaceSubkeySig(existingSig, potentialNewSig *packet.Signature) bool {
if potentialNewSig == nil {
return false
}

if existingSig == nil {
return true
}

if existingSig.SigType == packet.SigTypeSubkeyRevocation {
return false // never override a revocation signature
}

return potentialNewSig.CreationTime.After(existingSig.CreationTime)
}

const defaultRSAKeyBits = 2048

// NewEntity returns an Entity that contains a fresh RSA/RSA keypair with a
Expand Down
16 changes: 16 additions & 0 deletions openpgp/packet/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,5 +727,21 @@ func (sig *Signature) buildSubpackets() (subpackets []outputSubpacket) {
subpackets = append(subpackets, outputSubpacket{true, prefCompressionSubpacket, false, sig.PreferredCompression})
}

if sig.SigType == SigTypeKeyRevocation {
reasonForRevData := bytes.NewBuffer(nil)
reasonForRevData.Write([]byte{*sig.RevocationReason})
reasonForRevData.Write([]byte(sig.RevocationReasonText))

subpackets = append(
subpackets,
outputSubpacket{
hashed: true,
subpacketType: reasonForRevocationSubpacket,
isCritical: false,
contents: reasonForRevData.Bytes(),
},
)
}

return
}
6 changes: 3 additions & 3 deletions openpgp/s2k/s2k.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/fluidkeys/crypto/openpgp/errors"
)

type stringToKeySpecifier = uint8
type stringToKeySpecifier uint8

const (
S2KCountMin = 1024
Expand Down Expand Up @@ -192,7 +192,7 @@ func Parse(r io.Reader) (f func(out, in []byte), err error) {
}
h := hash.New()

switch buf[0] {
switch stringToKeySpecifier(buf[0]) {
case SimpleS2K:
f := func(out, in []byte) {
Simple(out, h, in)
Expand Down Expand Up @@ -228,7 +228,7 @@ func Parse(r io.Reader) (f func(out, in []byte), err error) {
// nil. In that case, sensible defaults will be used.
func Serialize(w io.Writer, key []byte, rand io.Reader, passphrase []byte, c *Config) error {
var buf [11]byte
buf[0] = IteratedAndSaltedS2K
buf[0] = byte(IteratedAndSaltedS2K)
buf[1], _ = HashToHashId(c.hash())
salt := buf[2:10]
if _, err := io.ReadFull(rand, salt); err != nil {
Expand Down