impossible
impossible

Reputation: 2500

Failed to find any PEM data in key

I am trying to run an HTTPS rest server locally. I followed this help: Golang TLS

It works fine when using self-signed certificates as in the above tutorial. But when I replace these certificates with one I generated from GoDaddy for one of my websites, I get ListenAndServe: tls: failed to find any PEM data in key input

These certificates work properly on the apache server but not on my local Go server.

package main
import (
    "log"
    "net/http"
)

func HelloServer(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "text/plain")
    _, _ = w.Write([]byte("This is an example server.\n"))
}

func main() {
    //https://localhost:443/hello
    http.HandleFunc("/hello", HelloServer)
    err := http.ListenAndServeTLS(":443",
        "../../../../../sslcert/server.crt",
        "../../../../../sslcert/server.key",
        nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

Upvotes: 7

Views: 53589

Answers (2)

M. Gopal
M. Gopal

Reputation: 454

For me additional byte was added at the end, while serializing the data from the terminal, used below snippet to correct it.

func convert(input []byte) (output []byte) {
    if input[len(input)-1] == 0 {
        input = input[:len(input)-1]
    }
    return input
}

Upvotes: 0

impossible
impossible

Reputation: 2500

The issue was with the key file. The beginning of the file had some issue (like UTF-8 BOM at the start of the file or similar) as @SteffenUllrich mentioned. To fix this, I added an empty line just above the key file and it worked.

Finally, the key looks like:

<Empty line>
-----BEGIN RSA PRIVATE KEY-----
wlWPpSnGEdNjRapfW/6+xzjDVAaKC41c5b07OAviFchwqGI+88
aZGwBJnTgkbsLddddddd=
-----END RSA PRIVATE KEY-----

Upvotes: 14

Related Questions