-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
What version of Go are you using (go version)?
go version go1.10.2 linux/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env)?
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/rbt/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/rbt/go"
GORACE=""
GOROOT="/home/rbt/go1.10"
GOTMPDIR=""
GOTOOLDIR="/home/rbt/go1.10/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build778390304=/tmp/go-build -gno-record-gcc-switches"
What did you do?
I ran this program that I wrote. It encrypts a file to PGP keys, but some keys fail with the error below even though they are valid and have encryption sub-keys.
If possible, provide a recipe for reproducing the error.
import (
"io"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/packet"
)
func main() {
filePath := os.Args[1]
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
// Read Public Key files from /home/user/keys
// These keys **are not** ascii armored
// gpg --export 83378a94fa6c4994 > /home/rbt/keys/phil.pub.key
keyFiles, err := ioutil.ReadDir(usr.HomeDir + "/keys/")
if err != nil {
log.Fatal(err)
}
entities := []*openpgp.Entity{}
for _, keyFile := range keyFiles {
log.Print("Using key file: " + usr.HomeDir + "/keys/" + keyFile.Name())
kf, err := os.Open(usr.HomeDir + "/keys/" + keyFile.Name())
if err != nil {
log.Fatalf("Open pub.key %s\n", err)
}
defer kf.Close()
keyReader := packet.NewReader(kf)
theEntity, err := openpgp.ReadEntity(keyReader)
if err != nil {
log.Fatalf("ReadEntity %s\n", err)
}
entities = append(entities, theEntity)
}
hints := &openpgp.FileHints{
IsBinary: true,
}
for _, entity := range entities {
log.Printf("Encrypting to Key FP: %X", entity.PrimaryKey.Fingerprint)
}
efilePath := "/tmp/" + filepath.Base(filePath) + ".gpg"
out, err := os.Create(efilePath)
if err != nil {
log.Fatal(err)
}
defer out.Close()
packetConfig := &packet.Config{
DefaultCipher: packet.CipherAES256,
}
wc, err := openpgp.Encrypt(out, entities, nil, hints, packetConfig)
if err != nil {
log.Fatal(err)
}
defer wc.Close()
plainTextFile, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
defer plainTextFile.Close()
buf := make([]byte, 64*1024)
n, err := io.CopyBuffer(wc, plainTextFile, buf)
if err != nil {
log.Fatal(err)
}
log.Printf("%d bytes read into wc.\n", n)
plainTextFile.Close()
wc.Close()
out.Close()
}
What did you expect to see?
I expected the file to be encrypted. The key is valid and has an encryption sub-key. And, many other keys work just fine, but several do not.
2018/07/19 09:45:19 Using key file: /home/rbt/keys/brad.pub.key
2018/07/19 09:45:19 Using key file: /home/rbt/keys/itso.pub.key
2018/07/19 09:45:19 Using key file: /home/rbt/keys/jeff.pub.key
2018/07/19 09:45:19 Using key file: /home/rbt/keys/phil.pub.key
2018/07/19 09:45:19 Using key file: /home/rbt/keys/tester.pub.key
2018/07/19 09:45:19 Encrypting to Key FP: 83CBAF6B683329125FE436CCE915EE8B2FE6EC56
2018/07/19 09:45:19 Encrypting to Key FP: F3D2F6714EF6B251BDFF18947279C76A0FAC6413
2018/07/19 09:45:19 Encrypting to Key FP: 4952772637B2B44012070E47B87FE76E05BAA569
2018/07/19 09:45:19 Encrypting to Key FP: 5CD5EFA3E1C520B1B0EDE38C83378A94FA6C4994
2018/07/19 09:45:19 Encrypting to Key FP: E2958B99360A0F93AD440FD01E7854496A3E0199
2018/07/19 09:45:19 openpgp: invalid argument: cannot encrypt a message to key id 83378a94fa6c4994 because it has no encryption keys
What did you see instead?
openpgp: invalid argument: cannot encrypt a message to key id 83378a94fa6c4994 because it has no encryption keys
Get the key that causes the error
This is a public key on public key servers. You can download it and re-create the issue.
gpg --recv-key 83378a94fa6c4994
gpg: key 83378A94FA6C4994: 171 signatures not checked due to missing keys
gpg: key 83378A94FA6C4994: 1 bad signature
gpg: key 83378A94FA6C4994: "Phillip E Benchoff <benchoff@n3pb.org>" not changed
gpg: Total number processed: 1
gpg: unchanged: 1