Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams 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",
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
                @impossible: Is the key also in PEM format? Since this is what golang complains about and not about the certificate.
– Steffen Ullrich
                Aug 21, 2019 at 18:06
                It is actually complaining about the key, what about the private-key file? Maybe it is encrypted key?
– Burak Serdar
                Aug 21, 2019 at 18:08
                @Steffen Yes. The key as well is in PEM format. -----BEGIN RSA PRIVATE KEY----- <content> -----END RSA PRIVATE KEY-----
– impossible
                Aug 21, 2019 at 18:10

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-----
                "I could solve the error Failed to find any PEM data in key by creating the private key by appending csr certificate and private key one below other, respectively." - this is pretty sure not the cause of the problem.  Something was wrong with your key file, but there should be no need to combine certificate and key into a file just to load the key.
– Steffen Ullrich
                Aug 22, 2019 at 19:38
                Very likely there was a problem with the key file which you've fixed by manually adding the certificate to it (like UTF-8 encoding, some UTF-8 BOM, missing new line at end of file or other strange things). Since it is not clear how exactly you've added the cert and what exactly the key file looked at the byte level before you did the changes, it cannot be said what the problem was. But it is definitely not that the certificate information were missing. My guess is that it still works if you manually delete the certificate from this file again.
– Steffen Ullrich
                Aug 23, 2019 at 6:35
                I am still working on it. I will update this answer after little more debugging and conclusions.  Thanks you for helping me out @SteffenUllrich
– impossible
                Aug 23, 2019 at 9:36

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
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.