Go - Golang openpg - create a key pair and create a signature

I am currently working on openpgp in conjunction with golang. I use the following code to create a new key pair and create my own signature on the resulting public key:

package main

import (
    "bytes"
    "crypto"
    "time"
    "golang.org/x/crypto/openpgp"
    "golang.org/x/crypto/openpgp/armor"
    "golang.org/x/crypto/openpgp/packet"
    "fmt"
)

//Create ASscii Armor from openpgp.Entity
func PubEntToAsciiArmor(pubEnt *openpgp.Entity) (asciiEntity string) {
    gotWriter := bytes.NewBuffer(nil)
    wr, errEncode := armor.Encode(gotWriter, openpgp.PublicKeyType, nil)
    if errEncode != nil {
        fmt.Println("Encoding Armor ", errEncode.Error())
        return
    }
    errSerial := pubEnt.Serialize(wr)
    if errSerial != nil {
        fmt.Println("Serializing PubKey ", errSerial.Error())
    }
    errClosing := wr.Close()
    if errClosing != nil {
        fmt.Println("Closing writer ", errClosing.Error())
    }
    asciiEntity = gotWriter.String()
    return
}


func main() {

    var entity *openpgp.Entity
    entity, err := openpgp.NewEntity("itis", "test", "itis@itis3.com", nil)
    if err != nil {
        fmt.Println("ERROR")
    }

    usrIdstring := ""
    for _, uIds := range entity.Identities {
        usrIdstring = uIds.Name

    }

    var priKey = entity.PrivateKey
    var sig = new(packet.Signature)    
    //Prepare sign with our configs/////IS IT A MUST ??
    sig.Hash = crypto.SHA1
    sig.PubKeyAlgo = priKey.PubKeyAlgo
    sig.CreationTime = time.Now()
    dur := new(uint32)
    *dur = uint32(365 * 24 * 60 * 60)
    sig.SigLifetimeSecs = dur //a year
    issuerUint := new(uint64)
    *issuerUint = priKey.KeyId
    sig.IssuerKeyId = issuerUint
    sig.SigType = packet.SigTypeGenericCert


    err = sig.SignKey(entity.PrimaryKey, entity.PrivateKey, nil)
    if err != nil {
        fmt.Println("ERROR")
    }
    err = sig.SignUserId(usrIdstring, entity.PrimaryKey, entity.PrivateKey, nil)
    if err != nil {
        fmt.Println("ERROR")
    }

    entity.SignIdentity(usrIdstring, entity, nil)

    var copy = entity
    var asciiSignedKey = PubEntToAsciiArmor(copy)
    fmt.Println(asciiSignedKey)
}

1.) When I serialize the public key (to get its armored version), I get the following error message:

Serialization PubKey openpgp: invalid argument: Signature: need to call Sign, SignUserId or SignKey before Serialize

I thought I just used all the possible ways to create a signature on this key?

2.) 1, , . . , , , .. (: https://pgp.mit.edu/pks/lookup?search=0xbe6ee21e94a73ba5&op=index). ? 1?

PS: golang, .

+4
3

, , . : openpgp; , . gpg --import.

package main

import (
        "fmt"
        "os"

        "golang.org/x/crypto/openpgp"
        "golang.org/x/crypto/openpgp/armor"
)

func main() {
        var e *openpgp.Entity
        e, err := openpgp.NewEntity("itis", "test", "itis@itis3.com", nil)
        if err != nil {
                fmt.Println(err)
                return
        }

        // Add more identities here if you wish

        // Sign all the identities
        for _, id := range e.Identities {
                err := id.SelfSignature.SignUserId(id.UserId.Id, e.PrimaryKey, e.PrivateKey, nil)
                if err != nil {
                        fmt.Println(err)
                        return
                }
        }

        w, err := armor.Encode(os.Stdout, openpgp.PublicKeyType, nil)
        if err != nil {
                fmt.Println(err)
                return
        }
        defer w.Close()

        e.Serialize(w)
}
+1

, -, : https://github.com/golang/go/issues/6483. SerializePrivate -, .

+2

I wrote https://github.com/alokmenghrajani/gpgeez just for this purpose. This is a Go library that makes it easy to create keys or create a key as an armored line.

Here is its essence, without error checking:

func CreateKey() *openpgp.Entity {
  key, _ := openpgp.NewEntity(name, comment, email, nil)

  for _, id := range key.Identities {
    id.SelfSignature.PreferredSymmetric = []uint8{...}    
    id.SelfSignature.PreferredHash = []uint8{...}    
    id.SelfSignature.PreferredCompression = []uint8{...}

    id.SelfSignature.SignUserId(id.UserId.Id, key.PrimaryKey, key.PrivateKey, nil)
  }

  // Self-sign the Subkeys
  for _, subkey := range key.Subkeys {
    subkey.Sig.SignKey(subkey.PublicKey, key.PrivateKey, nil)
  }

  return r
}
+2
source

All Articles