user6901586
user6901586

Reputation: 88

Unable to parse ECDSA key with Go X509 pkg

I'm trying to parse an ECDSA secp384r1 curve key using Go's crypto/x509 package using the following code (https://play.golang.org/p/opFLBMaKhDv):

import (
    "crypto/x509"
    "encoding/pem"
    "fmt"
)

var b = `-----BEGIN EC PRIVATE KEY-----
MIGkAgEBBDBsee4GAKz9Jo603xmGZ0uYEbJAoUgNqCYjDiLfj6zG4fvVSiCVxoTx
rVcvW2lmVcmgBwYFK4EEACKhZANiAATm6yBej3NVXnXAydMdLvrIB0PMr/fT6VCD
MB2pXzqxbQs8tYt3Rqd0HnzAZyYb1KhOX5lG0MyBDohhPRXqWE3gMhEq47BdzC7G
gEftttcFKXX/PSxsZUDL6GiliaB0/9E=
-----END EC PRIVATE KEY-----`

func main() {
    blk, _ := pem.Decode([]byte(b))
    _, err := x509.ParseECPrivateKey(blk.Bytes)
    fmt.Println((err.Error()))
}

but hitting the following panic

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x10e9366]

Can someone pls tell me what i'm doing wrong here?

Upvotes: 2

Views: 1928

Answers (1)

sh.seo
sh.seo

Reputation: 1610

add error check.

func main() {
    blk, _ := pem.Decode([]byte(b))
    key, err := x509.ParseECPrivateKey(blk.Bytes)
    if err != nil {
        fmt.Println((err.Error()))
    }

    fmt.Println(key)
}

Upvotes: 5

Related Questions