Skip to content

Commit

Permalink
Create a copy of the encrypted key when forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
wussler authored and marinthiercelin committed Mar 22, 2023
1 parent e03ff0f commit 5e579d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions openpgp/forwarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Loop:
}
switch p := p.(type) {
case *packet.EncryptedKey:
err = p.ProxyTransform(
tp, err := p.ProxyTransform(
instance.ProxyParameter,
instance.ForwarderKeyId,
instance.ForwardeeKeyId,
Expand All @@ -194,7 +194,7 @@ Loop:

splitPoint = bytesReader.Size() - int64(bytesReader.Len())

err = p.Serialize(transformedEncryptedKey)
err = tp.Serialize(transformedEncryptedKey)
if err != nil {
t.Fatalf("error serializing transformed PKESK: %s", err)
}
Expand Down
28 changes: 19 additions & 9 deletions openpgp/packet/encrypted_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,25 +338,35 @@ func serializeEncryptedKeyAEAD(w io.Writer, rand io.Reader, header [10]byte, pub
return err
}

func (e *EncryptedKey) ProxyTransform(proxyParam []byte, forwarderKeyId, forwardeeKeyId uint64) error {
func (e *EncryptedKey) ProxyTransform(proxyParam []byte, forwarderKeyId, forwardeeKeyId uint64) (transformed *EncryptedKey, err error) {
if e.Algo != PubKeyAlgoECDH {
return errors.InvalidArgumentError("invalid PKESK")
return nil, errors.InvalidArgumentError("invalid PKESK")
}

if e.KeyId != 0 && e.KeyId != forwarderKeyId {
return errors.InvalidArgumentError("invalid key id in PKESK")
return nil, errors.InvalidArgumentError("invalid key id in PKESK")
}

ephemeral := e.encryptedMPI1.Bytes()
transformed, err := ecdh.ProxyTransform(ephemeral, proxyParam)
transformedEphemeral, err := ecdh.ProxyTransform(ephemeral, proxyParam)
if err != nil {
return err
return nil, err
}

e.encryptedMPI1 = encoding.NewMPI(transformed)
if e.KeyId != 0 {
e.KeyId = forwardeeKeyId
wrappedKey := e.encryptedMPI2.Bytes()
copiedWrappedKey := make([]byte, len(wrappedKey))
copy(copiedWrappedKey, wrappedKey)

transformed = &EncryptedKey{
KeyId: forwardeeKeyId,
Algo: e.Algo,
encryptedMPI1: encoding.NewMPI(transformedEphemeral),
encryptedMPI2: encoding.NewOID(copiedWrappedKey),
}

return nil
if e.KeyId == 0 {
e.KeyId = 0
}

return transformed, nil
}

0 comments on commit 5e579d7

Please sign in to comment.