Q:

Golang, sending OCSP request returns

I'm writing certificate revocation validation using OCSP request to get actual status. I send request, but getting error. ERROR: ocsp: error from server: malformed. I found error return in source: MalformedRequestErrorResponse = []byte{0x30, 0x03, 0x0A, 0x01, 0x01}, but don't understand what causes error??
func CheckRevocation(cert *x509.Certificate) error {
    var (
        buffer       []byte
        output       []byte
        OCSPResponse *ocsp.Response
        OCSPURL      string
        IssuerURL    string
        httpRequest  *http.Request
        httpResponse *http.Response
    )
    if len(cert.OCSPServer) > 0 {
        OCSPURL = cert.OCSPServer[0]
    }
    if len(cert.IssuingCertificateURL) > 0 {
        IssuerURL = cert.IssuingCertificateURL[0]
    }
    issuer, err := DownloadCert(IssuerURL) // return *x509.Certificate, err
    if err != nil {
        return err
    }
    opts := &ocsp.RequestOptions{
        Hash: crypto.SHA256,
    }
    buffer, err = ocsp.CreateRequest(cert, issuer, opts)
    if err != nil {
        return err
    }
    httpRequest, err = http.NewRequest("GET", OCSPURL, nil)
    if err != nil {
        return err
    }
    httpRequest.Header.Add("Content-Type", "application/ocsp-request")
    httpRequest.Header.Add("Accept", "application/ocsp-response")
    writer := bytes.NewBuffer(buffer)
    httpRequest.Write(writer)
    httpClient := &http.Client{}
    httpResponse, err = httpClient.Do(httpRequest)
    if err != nil {
        return err
    }
    defer httpResponse.Body.Close()
    output, err = ioutil.ReadAll(httpResponse.Body)
    if err != nil {
        return err
    }
    OCSPResponse, err = ocsp.ParseResponse(output, issuer)
    if err != nil {
        return err
    }
    debugPrint("OCSP", "result: %d", OCSPResponse.Status)
    return nil
}

EXAMPLE OF CERTIFICATE:
...
<ds:X509Certificate>
MIIG9DCCBNygAwIBAgIUTFVCYjrFFRdWjgJW0G8fUs6LLk4wDQYJKoZIhvcNAQELBQAwgc4xCzAJ
BgNVBAYTAktaMRUwEwYDVQQHDAzQkNCh0KLQkNCd0JAxFTATBgNVBAgMDNCQ0KHQotCQ0J3QkDFM
MEoGA1UECgxD0KDQnNCaIMKr0JzQldCc0JvQldCa0JXQotC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KEFPzGJN1O7rYd8q0Gl9vQtKMjIsETZ7BRYbXxuzMVA2CMhxiXJS929qH2
ce48JWaAdMkyzyjcF8t3tVY=
</ds:X509Certificate>

This certificate is valid, but cannot call OCSP to check it's status. May be my call is incorrect. Does anyone have ideas?

A:

The standard allows for requests to made via GET or POST.  See the code below for an example that works with the POST method. This example works in my company's corporate PKI and almost works with the NIST PIV test cards (https://csrc.nist.gov/projects/piv/nist-personal-identity-verification-test-cards), but trips over a bug in Go that may have been resolved in 1.11 (see https://github.com/golang/go/issues/21527)
Note that some OCSP responders require the host header.  Also, both of the OSCP responders in my environment want SHA1 for the hash algorithm and fail when SHA256 is used.
func isCertificateRevokedByOCSP(commonName string, clientCert, issuerCert *x509.Certificate, ocspServer string) bool {
    opts := &ocsp.RequestOptions{Hash: crypto.SHA1}
    buffer, err := ocsp.CreateRequest(clientCert, issuerCert, opts)
    if err != nil {
        return false
    }
    httpRequest, err := http.NewRequest(http.MethodPost, ocspServer, bytes.NewBuffer(buffer))
    if err != nil {
        return false
    }
    ocspUrl, err := url.Parse(ocspServer)
    if err != nil {
        return false
    }
    httpRequest.Header.Add("Content-Type", "application/ocsp-request")
    httpRequest.Header.Add("Accept", "application/ocsp-response")
    httpRequest.Header.Add("host", ocspUrl.Host)
    httpClient := &http.Client{}
    httpResponse, err := httpClient.Do(httpRequest)
    if err != nil {
        return false
    }
    defer httpResponse.Body.Close()
    output, err := ioutil.ReadAll(httpResponse.Body)
    if err != nil {
        return false
    }
    ocspResponse, err := ocsp.ParseResponse(output, issuerCert)
    if err != nil {
        return false
    }
    if ocspResponse.Status == ocsp.Revoked {
        logger.GetLogger().Warnf("certificate '%s' has been revoked by OCSP server %s, refusing connection", commonName, ocspServer)
        return true
    } else {
        return false
    }
}

