How to get x509 certificate from http client in Go


mjb2kmn:

I've gone through the documentation at https://golang.org/pkg/ but I can't make this connection.

I am creating a client and requesting like this (error handling removed):

client := http.Client{
    CheckRedirect: func(req *http.Request, via []*http.Request) error {
        return http.ErrUseLastResponse
    },
}
req, reqErr := http.NewRequest(requestMethod, requestUrl, nil)
resp, clientErr := client.Do(req)

I need to get one x509.Certificateto read the details of the certificate returned from the server, but the certificate is still required http.Repsonse.

How can I get an x509.Certificateinstance http.Responseand sometimes only make a request?

Dave:

The response has a TLS *tls.ConnectionStatefield which in turn has:

type ConnectionState struct {
    // other fields
    PeerCertificates []*x509.Certificate   // certificate chain presented by remote peer
}

So you can do this:

resp, clientErr := client.Do(req)
if clientErr != nil {
    panic(clientErr)
}
if resp.TLS != nil {
    certificates := resp.TLS.PeerCertificates
    if len(certificates) > 0 {
        // you probably want certificates[0]
        cert := certificates[0] 
    }
} 

Related


How to get x509 certificate from http client in Go

mjb2kmn: I've gone through the documentation at https://golang.org/pkg/ but I can't make this connection. I am creating a client and requesting like this (error handling removed): client := http.Client{ CheckRedirect: func(req *http.Request, via []*http.Re

How to get x509 certificate from http client in Go

mjb2kmn: I've gone through the documentation at https://golang.org/pkg/ but I can't make this connection. I am creating a client and requesting like this (error handling removed): client := http.Client{ CheckRedirect: func(req *http.Request, via []*http.Re

How to get string from x509 certificate public key in Go?

Shocky2: If I have an *x509.Certificateobject, how can I extract the public key base64 string representation from it? ifnotak NOTE: If you already have objects, skip to #3x509.Certificate。 You need to do the following: Use decode PEM .pem.Decode() block, _ :=

How to get string from x509 certificate public key in Go?

Shocky2: If I have an *x509.Certificateobject, how can I extract the public key base64 string representation from it? ifnotak NOTE: If you already have objects, skip to #3x509.Certificate。 You need to do the following: Use decode PEM .pem.Decode() block, _ :=

How to get string from x509 certificate public key in Go?

Shocky2: If I have an *x509.Certificateobject, how can I extract the public key base64 string representation from it? ifnotak NOTE: If you already have objects, skip to #3x509.Certificate。 You need to do the following: Use decode PEM .pem.Decode() block, _ :=

How to get BasicConstraints extension from Java X509 certificate

michalk: I want to read the extension BasicConstraints for Java X509Certificate (the certificate implementation comes from the default JCE, so yes sun.security.x509.X509CertImpl). I want to get the BasicConstraint extension value to check if it's a CA: X509Cer

How to get BasicConstraints extension from Java X509 certificate

michalk: I want to read the extension BasicConstraints for Java X509Certificate (the certificate implementation comes from the default JCE, so yes sun.security.x509.X509CertImpl). I want to get the BasicConstraint extension value to check if it is a CA: X509Ce

How to get BasicConstraints extension from Java X509 certificate

michalk: I want to read the extension BasicConstraints for Java X509Certificate (the certificate implementation comes from the default JCE, so yes sun.security.x509.X509CertImpl). I want to get the BasicConstraint extension value to check if it is a CA: X509Ce

Extract client X509 certificate from secure Websocket connection

Guillaume Pansier I want to create certificate based authentication on top of websocket communication. So I created a websocket serverEndpoint and set up SSL for client authentication with the help of jetty like this: Server server = new Server(); //Create SS

Extract client X509 certificate from secure Websocket connection

Guillaume Pansier I want to create certificate based authentication on top of websocket communication. So I created a websocket serverEndpoint and set up SSL for client authentication with the help of jetty like this: Server server = new Server(); //Create SS

Get X509 certificate from mage signed manifest file

Alexandru I have a OutlookAddin.vstofile (yes, it's an Outlook add-in VSTO file) that contains a signing certificate signed with mage.exe . As far as I can tell, it successfully creates a label with the encoded public certificate in the following format:<X509C

How to extract public key from x509 certificate in python?

mugzi The code sample I followed is shown below, but the error response I get is - "Certificate cannot be loaded". from cryptography.x509 import load_pem_x509_certificate from cryptography.hazmat.backends import default_backend cert_str = '-----BEGIN CERTIFIC

How to extract public key from x509 certificate in python?

mugzi The code sample I followed is shown below, but the error response I get is - "Certificate cannot be loaded". from cryptography.x509 import load_pem_x509_certificate from cryptography.hazmat.backends import default_backend cert_str = '-----BEGIN CERTIFIC

How to extract public key from x509 certificate in python?

mugzi The code sample I followed is shown below, but the error response I get is - "Certificate cannot be loaded". from cryptography.x509 import load_pem_x509_certificate from cryptography.hazmat.backends import default_backend cert_str = '-----BEGIN CERTIFIC

How to extract public key from x509 certificate in python?

mugzi The code sample I followed is shown below, but the error response I get is - "Certificate cannot be loaded". from cryptography.x509 import load_pem_x509_certificate from cryptography.hazmat.backends import default_backend cert_str = '-----BEGIN CERTIFIC

How to extract digest algorithm from X509 certificate

Nick Russler I can extract the OID and the name of the signature algorithm from an X509Certificateinstance , "SHA256WithRSA"but how can I extract the name of the digest algorithm (for example) "SHA256". Is there any other way than string splitting "with"or fix