hi there,
I am trying to issue a GET request on some server, using a x509
certificate for authentication (beware: I am a total ignorant in this
matter so I may use incorrect terms and vocabulary)
I got a certificate (from CERN, if that matters) and I am pretty
confident it all works (I used curl to test everything)
here is what I did to get PEM files:
$ openssl pkcs12 -in mycert.pfx -clcerts -nokeys -out usercert.pem
$ openssl pkcs12 -in mycert.pfx -nocerts -out userkey.pem
$ chmod 400 userkey.pem
$ chmod 444 usercert.pem
$ curl -G "https://somewhere.at.cern.ch:someport/somepage" -k --cert
usercert.pem --key userkey.pem
Enter PEM pass phrase: <secret>
<lines of output as I expected>
now, trying to do the same using net/http:
```go
package main
import (
"fmt"
"net/http"
"net/url"
"crypto/tls"
"os"
)
func checkError(err error, hdr string) {
if err != nil {
fmt.Printf("[%s] Fatal error: %v\n", hdr, err.Error())
os.Exit(1)
}
}
func main() {
rurl, err := url.Parse("https://somewhere.at.cern.ch:someport/somepage")
checkError(err, "url.Parse")
cert, err := tls.LoadX509KeyPair("usercert.pem", "userkey.pem")
checkError(err, "loadcert")
tr := &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate {cert},
InsecureSkipVerify: true,
ClientAuth: tls.RequireAnyClientCert,
},
}
client := &http.Client{Transport: tr}
req, err := http.NewRequest("GET", rurl.String(), nil)
checkError(err, "http.NewRequest")
fmt.Printf("request: %v\n", *req)
resp, err := client.Do(req)
checkError(err, "client.Do")
if resp.Status != "200 OK" {
fmt.Println(resp.Status)
os.Exit(2)
}
var buf [512]byte
reader := resp.Body
fmt.Println("got body")
for {
n, err := reader.Read(buf[0:])
if err != nil {
os.Exit(0)
}
fmt.Print(string(buf[0:n]))
}
os.Exit(0)
}
```
$ go run ./nuts.go
[loadcert] Fatal error: crypto/tls: failed to parse key: ASN.1
structure error: tags don't match (2 vs {class:0 tag:16 length:64
isCompound:true}) {optional:false explicit:false application:false
defaultValue:<nil> tag:<nil> stringType:0 set:false omitEmpty:false}
int @2
exit status 1
so, I am obviously not doing something right...
any idea what I should do to fix this ?
thx,
-s
--