| package main | |
| import ( | |
| "crypto/tls" | |
| "crypto/x509" | |
| "flag" | |
| "io/ioutil" | |
| "log" | |
| "net/http" | |
| ) | |
| var ( | |
| certFile = flag.String("cert", "someCertFile", "A PEM eoncoded certificate file.") | |
| keyFile = flag.String("key", "someKeyFile", "A PEM encoded private key file.") | |
| caFile = flag.String("CA", "someCertCAFile", "A PEM eoncoded CA's certificate file.") | |
| ) | |
| func main() { | |
| flag.Parse() | |
| // Load client cert | |
| cert, err := tls.LoadX509KeyPair(*certFile, *keyFile) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| // Load CA cert | |
| caCert, err := ioutil.ReadFile(*caFile) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| caCertPool := x509.NewCertPool() | |
| caCertPool.AppendCertsFromPEM(caCert) | |
| // Setup HTTPS client | |
| tlsConfig := &tls.Config{ | |
| Certificates: []tls.Certificate{cert}, | |
| RootCAs: caCertPool, | |
| } | |
| tlsConfig.BuildNameToCertificate() | |
| transport := &http.Transport{TLSClientConfig: tlsConfig} | |
| client := &http.Client{Transport: transport} | |
| // Do GET something | |
| resp, err := client.Get("https://goldportugal.local:8443") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer resp.Body.Close() | |
| // Dump response | |
| data, err := ioutil.ReadAll(resp.Body) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| log.Println(string(data)) | |
| } |
This comment has been minimized.
This comment has been minimized.
kentoj
commented
May 21, 2015
|
Why doesn't the extra comma on line 38 cause an issue? |
This comment has been minimized.
This comment has been minimized.
mholt
commented
May 26, 2015
|
@kentoj That's correct Go syntax. Omitting the comma will cause a parse error. |
This comment has been minimized.
This comment has been minimized.
ghost
commented
Aug 27, 2015
|
What should be the file extensions for the certificate, key, and CA file? |
This comment has been minimized.
This comment has been minimized.
magiconair
commented
Nov 1, 2015
|
.pem ? |
This comment has been minimized.
This comment has been minimized.
SrinivasChilveri
commented
Nov 15, 2015
|
May I know how to generate these 3 configuration .pem files? |
This comment has been minimized.
This comment has been minimized.
gernest
commented
Jan 5, 2016
|
@SrinivasChilveri To generate self sigend .pem files run this go run $GOROOT/src/crypto/tls/generate_cert.goIt will generate |
This comment has been minimized.
This comment has been minimized.
unknownsuperuser
commented
Jan 8, 2016
|
Does this require ssl renegotiation? |
This comment has been minimized.
This comment has been minimized.
adisheshsm
commented
Jan 17, 2016
|
how to get/generate ca.pem file for testing purpose |
This comment has been minimized.
This comment has been minimized.
wmark
commented
Feb 14, 2016
|
@unknownsuperuser No, renegotiation is not required for this, nor is it implemented in Golang. @adisheshsm Technically you don't need a CA.pem for client TLS authentication. You can get an exemplary full tree using my script, which you can download here: https://gist.github.com/wmark/c758ce1c2b8222afd69d (top right, »download ZIP«). Just remember: Don't use golang for authentication using DH/RSA. (Writing this at a time when 1.6 is the most recent version.) |
This comment has been minimized.
This comment has been minimized.
brandong954
commented
Jul 28, 2016
|
Amazing! Thank you! |
This comment has been minimized.
This comment has been minimized.
VimleshS
commented
Aug 30, 2016
•
|
good read, Thanks |
This comment has been minimized.
This comment has been minimized.
denofiend
commented
Oct 13, 2016
|
nqs erro: certificate signed by unknown authority |
This comment has been minimized.
This comment has been minimized.
jeyaramashok
commented
Dec 9, 2016
|
Thank you! |
This comment has been minimized.
This comment has been minimized.
antman1p
commented
Feb 13, 2017
|
Can someone tell me what these 3 vars are made of exactly exactly? I don't understand if these are supposed to be paths to pem files or what. |
This comment has been minimized.
This comment has been minimized.
AlexGoja
commented
Mar 27, 2017
•
|
@antman1p most likely the path to the .pem files to be used as command line arguments. Something like |
This comment has been minimized.
This comment has been minimized.
duckie
commented
Mar 2, 2018
|
Very nice snippet thank you ! |
This comment has been minimized.
This comment has been minimized.
davenemeth
commented
Apr 5, 2018
|
It simply works as expected, thank you! |
This comment has been minimized.
This comment has been minimized.
vinchauhan
commented
Oct 25, 2018
|
What if the key is password protected ? |
This comment has been minimized.
Xeoncross commentedJan 8, 2015
If you name it
____.gogist will highlight the code for you.