3

I try to switch from HTTP to HTTPS:

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

func main() {
    http.HandleFunc("/", handler)
    log.Printf("About to listen on 8080. Go to https://127.0.0.1:8080/")
    err := http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil)
    if err != nil {
        log.Fatal(err)
    }
}

And I am getting the following error:

crypto/tls: failed to parse key PEM data

My application is running in HTTP mode now and I want it to run in HTTPS mode.

Can anyone suggest how to make it work in HTTPS?

icza
  • 389,944
  • 63
  • 907
  • 827
Iranna Pattar
  • 67
  • 1
  • 7
  • 3
    Are you sure `cert.pem` and `key.pem` files are in the proper (current working directory) folder? Try passing absolute paths. – icza Jun 08 '15 at 13:03
  • @icza, yes it is in the same folder.Even if try to pass the absolute path its giving same error. – Iranna Pattar Jun 09 '15 at 04:36

1 Answers1

3

The error indicates that the key.pem file cannot be parsed (could be invalid or lacking permission to read its content). Make sure the file is valid and sufficient permissions are set.

For testing purposes, use the generate_cert.go in the crypto/tls package to generate valid cert.pem and key.pem files.

To generate, run the following command (windows):

go run %GOROOT%/src/crypto/tls/generate_cert.go -host="127.0.0.1"

Linux:

go run $GOROOT/src/crypto/tls/generate_cert.go -host="127.0.0.1"
icza
  • 389,944
  • 63
  • 907
  • 827