0

I wrote a small test program that creates

  1. a custom, self-signed CA certificate#1
  2. creates a server certificate#2 issued by that CA - root certificate#1
  3. creates a server with certificate#2
  4. creates a client with RootCA pointing to certificate#1
  5. the client tries to connect to the server and gets an error:

Get "https://localhost:2000": x509: certificate signed by unknown authority (possibly because of "x509: Ed25519 verification failure" while trying to verify candidate authority certificate "test-ca")

I understand that there are dozens of examples of this. I thought I was following them pretty close, and yet here I am. I am showing here only the most relevant structures but the full text of the program can be found here:

    ...
    templateCA := &x509.Certificate{
        Subject: pkix.Name{
            CommonName:   "test-ca",
            Organization: []string{"test ca"},
            Country:      []string{"USA"},
            Province:     []string{"NY"},
            Locality:     []string{"New York City"},
        },
        SerialNumber:          serialNumber,
        NotBefore:             time.Now(),
        NotAfter:              time.Now().AddDate(0, 0, 1),
        BasicConstraintsValid: true,
        IsCA:                  true,
        SubjectKeyId:          caSubjectKeyID[:],
        DNSNames:              []string{"test-ca"},
        KeyUsage:              x509.KeyUsageCertSign
    }
    ...
    certBytes, _ := x509.CreateCertificate(rand.Reader, templateCA, templateCA, privKey.Public(), privKey)
    ...
    templateServer := &x509.Certificate{
        Subject: pkix.Name{
            CommonName:   "localhost",
            Organization: []string{"Server"},
            Country:      []string{"USA"},
            Province:     []string{"NY"},
            Locality:     []string{"New York City"},
        },
        SerialNumber:          serialNumber,
        NotBefore:             time.Now(),
        NotAfter:              time.Now().AddDate(0, 0, 1),
        BasicConstraintsValid: true,
        SubjectKeyId:          servSubjectKeyID[:],
        AuthorityKeyId:        caSubjectKeyID[:],
        DNSNames:              []string{"localhost"},
        IPAddresses:           []net.IP{{127, 0, 0, 1}},
        KeyUsage:              x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
        ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
    }
    ...
    certBytes, _ = x509.CreateCertificate(rand.Reader, templateServer, caCert, privKey.Public(), privKey)
    ...
var (
    tlsMinVersion = uint16(tls.VersionTLS12)
    tlsMaxVersion = uint16(tls.VersionTLS13)
    cipherSuites  = []uint16{
        tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
        tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
        tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
        tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
        tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
        tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
    }
    curvePreferences = []tls.CurveID{
        tls.X25519,
        tls.CurveP256,
        tls.CurveP384,
        tls.CurveP521,
    }
)
    ...
    tlsServerConfig := &tls.Config{
        Certificates:             []tls.Certificate{*tlsSrvCert},
        MinVersion:               tlsMinVersion,
        MaxVersion:               tlsMaxVersion,
        CurvePreferences:         curvePreferences,
        CipherSuites:             cipherSuites,
        PreferServerCipherSuites: true,
    }
    ...
    tlsClientConfig := &tls.Config{
        ServerName:               "localhost",
        RootCAs:                  x509.NewCertPool(),
        MinVersion:               tlsMinVersion,
        MaxVersion:               tlsMaxVersion,
        CurvePreferences:         curvePreferences,
        CipherSuites:             cipherSuites,
        PreferServerCipherSuites: true,
    }
    tlsClientConfig.RootCAs.AddCert(caCert)
    

What am I missing or doing wrong?

Valo
  • 1,872
  • 2
  • 15
  • 23
  • Changed a bit the code snippets to reflect the latest but that didn't help a bit. Still getting the same error. Went one step further: created manually a set of certificates that are almost identical with differences only in the ID-s, the serial numbers, and the bytes of the keys. The manually created certs work but the generated ones do not. – Valo Dec 28 '20 at 02:35

2 Answers2

1

You can use private key to decrypt data and to encrypt hashed data to create a digital signature.

You can use public key to encrypt data and decrypt a digital signature to verify it.

what you need to do here, is to use one key pair (public/private key) to generate CA certificate and use that certificate + same key pair to generate one or multiple certificates for you server(s).

In case you want to use a browser / curl as your client, you need to add CA certificate in the root keystores.

djoudat
  • 26
  • 3
  • Thanks for the reply. This is what I am doing here. The snippets above may be a little misleading because of the same names of the pub/priv key variables but if you look at the code, you'll see that the values are different: https://github.com/vmelamed/testServerClient/blob/master/testtls/generateCerts.go – Valo Dec 28 '20 at 17:14
  • here (https://github.com/vmelamed/testServerClient/blob/439693f6100b73dc310871b36301f471f412edae/testtls/test.go#L72) you are reassigning new value to privateKey. delete line 72 and use the first key pair in next line 73 – djoudat Dec 28 '20 at 17:28
  • Yes! You are right - now I see it! It's a bug. Thank you! – Valo Dec 28 '20 at 18:34
0

Thanks to @djoudat for pointing out my bug. I'm pasting here the corrected code snippets from above. Hopefully, they may help someone, someday.

...
templateCA := &x509.Certificate{
    Subject: pkix.Name{
        CommonName:   "test-ca",
        Organization: []string{"test ca"},
        Country:      []string{"USA"},
        Province:     []string{"NY"},
        Locality:     []string{"New York City"},
    },
    SerialNumber:          serialNumber,
    NotBefore:             time.Now(),
    NotAfter:              time.Now().AddDate(0, 0, 1),
    BasicConstraintsValid: true,
    IsCA:                  true,
    KeyUsage:              x509.KeyUsageCertSign
    DNSNames:              []string{"test-ca"},
}
...
certBytes, _ := x509.CreateCertificate(rand.Reader, templateCA, templateCA, privKeyCA.Public(), privKeyCA)
...
templateServer := &x509.Certificate{
    Subject: pkix.Name{
        CommonName:   "localhost",
        Organization: []string{"Server"},
        Country:      []string{"USA"},
        Province:     []string{"NY"},
        Locality:     []string{"New York City"},
    },
    SerialNumber:          serialNumber,
    NotBefore:             time.Now(),
    NotAfter:              time.Now().AddDate(0, 0, 1),
    BasicConstraintsValid: true,
    KeyUsage:              x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
    ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
    DNSNames:              []string{"localhost"},
}
...
certBytes, _ = x509.CreateCertificate(rand.Reader, templateServer, caCert, privKeyServer.Public(), privKeyCA)
...
var (
    tlsMinVersion = uint16(tls.VersionTLS12)
    tlsMaxVersion = uint16(tls.VersionTLS13)
    cipherSuites  = []uint16{
        tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
        tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
        tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
        tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
        tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
        tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
    }
    curvePreferences = []tls.CurveID{
        tls.X25519,
        tls.CurveP256,
        tls.CurveP384,
        tls.CurveP521,
    }
)
...
tlsServerConfig := &tls.Config{
    Certificates:             []tls.Certificate{*tlsSrvCert},
    MinVersion:               tlsMinVersion,
    MaxVersion:               tlsMaxVersion,
    CurvePreferences:         curvePreferences,
    CipherSuites:             cipherSuites,
    PreferServerCipherSuites: true,
}
...
tlsClientConfig := &tls.Config{
    ServerName:               "localhost",
    RootCAs:                  x509.NewCertPool(),
    MinVersion:               tlsMinVersion,
    MaxVersion:               tlsMaxVersion,
    CurvePreferences:         curvePreferences,
    CipherSuites:             cipherSuites,
    PreferServerCipherSuites: true,
}
tlsClientConfig.RootCAs.AddCert(caCert)
Valo
  • 1,872
  • 2
  • 15
  • 23