static HttpWebRequest CreateWebRequest(string url, OcspReq ocspRequest) { var request = (HttpWebRequest)WebRequest.Create(url); request.KeepAlive = false; request.Method = "POST"; request.ContentType = "application/ocsp-request"; request.ContentLength = ocspRequest.GetEncoded().Length; WriteOcspRequest(request, ocspRequest); return request; }
public CertificateStatus Query(X509Certificate ClientCert, X509Certificate issuerCert) { // Get the OCSP url from the certificate. string ocspUrl = getOCSPUrl(ClientCert); if (ocspUrl == "" || ocspUrl == null) { return(CertificateStatus.Error); } BouncyCastleOCSP.OcspReq req = CreateOcspRequest(issuerCert, ClientCert.SerialNumber); ocspResponse = SendtoOCSP(ocspUrl, req.GetEncoded(), "application/ocsp-request", "application/ocsp-response"); return(CheckOcspResponse(ClientCert, issuerCert, ocspResponse)); }
/// <summary> /// Gets the OCSP response from the server. /// </summary> /// <remarks> /// Never returns an exception. /// </remarks> /// <param name="cert">The certificate to get the server info from</param> /// <param name="issuer">The issue certificate of the certificate to get the server info from</param> /// <returns>The OCSP response (parsed) or <c>null</c> when none found</returns> /// <exception cref="RevocationUnknownException">When the revocation info can be retreived</exception> public static async Task <BCAO.OcspResponse> GetOcspResponseAsync(this X509Certificate2 cert, X509Certificate2 issuer) { Exception lastException = null; foreach (Uri uri in cert.GetOCSPUris()) { try { BCO.OcspReq ocspReq = cert.GetOcspReqBody(issuer); byte[] ocspReqBytes = ocspReq.GetEncoded(); Stream ocspWebReqStream; var webReq = GetOcspWebRequest(uri, ocspReqBytes, out ocspWebReqStream); await ocspWebReqStream.WriteAsync(ocspReqBytes, 0, ocspReqBytes.Length); var webRsp = (HttpWebResponse)webReq.GetResponse(); Stream webRspStream = webRsp.GetResponseStream(); using (webRsp) { MemoryStream rspStream = new MemoryStream(); Task cpy = webRspStream.CopyToAsync(rspStream); VerifyOCSPRsp(webRsp); await cpy; return(ParseOCSPResponse(rspStream.ToArray())); } } catch (Exception e) { lastException = e; trace.TraceEvent(TraceEventType.Warning, 0, "Failed to manually obtain ocsp: {0}", e); } } if (lastException != null) { throw lastException; } return(null); }
static void WriteOcspRequest(WebRequest request, OcspReq ocspRequest) { using (var requestStream = request.GetRequestStream()) { byte[] encodedRequest = ocspRequest.GetEncoded(); requestStream.Write(encodedRequest, 0, encodedRequest.Length); } }