Skip to content

x/crypto/openpgp: Cannot encrypt with ECDSA #30388

Closed
@ghost

Description

What version of Go are you using (go version)?

$ go version
go version go1.11 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN="/Users/samuel/Documents/code/go/bin"
GOCACHE="/Users/samuel/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/samuel/Documents/code/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/nh/t0dtwkp10y972qpr3jck0wnw0000gn/T/go-build798446226=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I created a ECDSA key based on the crypto/elliptic package.
Then I used it with openpgp: packet.NewECDSAPrivateKey(time.Now(), key)
Then I created an entity

e := &openpgp.Entity{
	PrivateKey: pv,
	PrimaryKey: &pv.PublicKey,
	Identities: map[string]*openpgp.Identity{},
}
isPrimary := true
uid := packet.NewUserId("sam", "", "")
e.Identities[uid.Id] = &openpgp.Identity{
	Name:   uid.Id,
	UserId: uid,
	SelfSignature: &packet.Signature{
		CreationTime: time.Now(),
		PubKeyAlgo:   packet.PubKeyAlgoECDSA,
		IsPrimaryId:  &isPrimary,
		IssuerKeyId:  &e.PrimaryKey.KeyId,
		Hash:         crypto.SHA256,
	},
}
e.Identities[uid.Id].SelfSignature.SignUserId(uid.Id, e.PrimaryKey, e.PrivateKey, nil)

Then I tried to encrypt using this entity

buf := new(bytes.Buffer)
res, err := openpgp.Encrypt(buf, []*openpgp.Entity{e}, nil, nil, nil)
if err != nil {
	log.Fatal(err)
}
res.Write([]byte("Hello"))
res.Close()

And I got this error: openpgp: unsupported feature: encrypting a key to public key of type 19

After searching in the codebase, I found that Encrypt call the method SerializeEncryptedKey which not handle ECDSA keys

switch pub.PubKeyAlgo {
	case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly:
		return serializeEncryptedKeyRSA(w, config.Random(), buf, pub.PublicKey.(*rsa.PublicKey), keyBlock)
	case PubKeyAlgoElGamal:
		return serializeEncryptedKeyElGamal(w, config.Random(), buf, pub.PublicKey.(*elgamal.PublicKey), keyBlock)
	case PubKeyAlgoDSA, PubKeyAlgoRSASignOnly:
		return errors.InvalidArgumentError("cannot encrypt to public key of type " + strconv.Itoa(int(pub.PubKeyAlgo)))
	}

	return errors.UnsupportedError("encrypting a key to public key of type " + strconv.Itoa(int(pub.PubKeyAlgo)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExpertNeededFrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions