2014-05-02 02:07:55 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "OCSPRequestor.h"
|
|
|
|
|
2014-05-30 07:17:53 +04:00
|
|
|
#include <limits>
|
|
|
|
|
2016-03-25 04:30:37 +03:00
|
|
|
#include "ScopedNSSTypes.h"
|
2014-05-22 02:42:21 +04:00
|
|
|
#include "mozilla/Base64.h"
|
2016-05-19 07:20:56 +03:00
|
|
|
#include "mozilla/Casting.h"
|
2014-05-02 02:07:55 +04:00
|
|
|
#include "nsIURLParser.h"
|
|
|
|
#include "nsNSSCallbacks.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "secerr.h"
|
|
|
|
|
2016-01-28 21:36:00 +03:00
|
|
|
extern mozilla::LazyLogModule gCertVerifierLog;
|
2014-05-22 02:42:21 +04:00
|
|
|
|
2015-04-13 08:57:48 +03:00
|
|
|
namespace mozilla {
|
2014-05-02 02:07:55 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseHttpServerSession(nsNSSHttpServerSession* httpServerSession)
|
|
|
|
{
|
|
|
|
delete httpServerSession;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseHttpRequestSession(nsNSSHttpRequestSession* httpRequestSession)
|
|
|
|
{
|
|
|
|
httpRequestSession->Release();
|
|
|
|
}
|
2015-04-13 08:57:48 +03:00
|
|
|
|
2016-03-25 04:30:37 +03:00
|
|
|
MOZ_TYPE_SPECIFIC_UNIQUE_PTR_TEMPLATE(UniqueHTTPServerSession,
|
|
|
|
nsNSSHttpServerSession,
|
|
|
|
ReleaseHttpServerSession)
|
2015-04-13 08:57:48 +03:00
|
|
|
|
2016-03-25 04:30:37 +03:00
|
|
|
MOZ_TYPE_SPECIFIC_UNIQUE_PTR_TEMPLATE(UniqueHTTPRequestSession,
|
|
|
|
nsNSSHttpRequestSession,
|
|
|
|
ReleaseHttpRequestSession)
|
2015-04-13 08:57:48 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
namespace mozilla { namespace psm {
|
2014-05-02 02:07:55 +04:00
|
|
|
|
2014-05-22 02:42:21 +04:00
|
|
|
static nsresult
|
|
|
|
AppendEscapedBase64Item(const SECItem* encodedRequest, nsACString& path)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsDependentCSubstring requestAsSubstring(
|
2016-05-19 07:20:56 +03:00
|
|
|
BitwiseCast<char*, unsigned char*>(encodedRequest->data),
|
|
|
|
encodedRequest->len);
|
2014-05-22 02:42:21 +04:00
|
|
|
nsCString base64Request;
|
|
|
|
rv = Base64Encode(requestAsSubstring, base64Request);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gCertVerifierLog, LogLevel::Debug,
|
2014-05-22 02:42:21 +04:00
|
|
|
("Setting up OCSP GET path, pre path =%s\n",
|
|
|
|
PromiseFlatCString(path).get()));
|
|
|
|
|
|
|
|
// The path transformation is not a direct url encoding. Three characters
|
|
|
|
// need change '+' -> "%2B", '/' -> "%2F", and '=' -> '%3D'.
|
|
|
|
// http://tools.ietf.org/html/rfc5019#section-5
|
|
|
|
base64Request.ReplaceSubstring("+", "%2B");
|
|
|
|
base64Request.ReplaceSubstring("/", "%2F");
|
|
|
|
base64Request.ReplaceSubstring("=", "%3D");
|
|
|
|
path.Append(base64Request);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-19 07:11:03 +03:00
|
|
|
Result
|
2016-04-05 03:35:24 +03:00
|
|
|
DoOCSPRequest(const UniquePLArenaPool& arena, const char* url,
|
2017-01-12 19:38:48 +03:00
|
|
|
const OriginAttributes& originAttributes,
|
2017-01-14 08:12:43 +03:00
|
|
|
const SECItem* encodedRequest, TimeDuration timeout,
|
2016-11-14 13:26:15 +03:00
|
|
|
bool useGET,
|
2016-03-19 07:11:03 +03:00
|
|
|
/*out*/ SECItem*& encodedResponse)
|
2014-05-02 02:07:55 +04:00
|
|
|
{
|
2016-04-05 03:35:24 +03:00
|
|
|
MOZ_ASSERT(arena.get());
|
|
|
|
MOZ_ASSERT(url);
|
|
|
|
MOZ_ASSERT(encodedRequest);
|
|
|
|
MOZ_ASSERT(encodedRequest->data);
|
|
|
|
if (!arena.get() || !url || !encodedRequest || !encodedRequest->data) {
|
2016-03-19 07:11:03 +03:00
|
|
|
return Result::FATAL_ERROR_INVALID_ARGS;
|
2014-05-30 07:17:53 +04:00
|
|
|
}
|
|
|
|
uint32_t urlLen = PL_strlen(url);
|
|
|
|
if (urlLen > static_cast<uint32_t>(std::numeric_limits<int32_t>::max())) {
|
2016-03-19 07:11:03 +03:00
|
|
|
return Result::FATAL_ERROR_INVALID_ARGS;
|
2014-05-30 07:17:53 +04:00
|
|
|
}
|
|
|
|
|
2014-05-02 02:07:55 +04:00
|
|
|
nsCOMPtr<nsIURLParser> urlParser = do_GetService(NS_STDURLPARSER_CONTRACTID);
|
|
|
|
if (!urlParser) {
|
2016-03-19 07:11:03 +03:00
|
|
|
return Result::FATAL_ERROR_LIBRARY_FAILURE;
|
2014-05-02 02:07:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t schemePos;
|
|
|
|
int32_t schemeLen;
|
|
|
|
uint32_t authorityPos;
|
|
|
|
int32_t authorityLen;
|
|
|
|
uint32_t pathPos;
|
|
|
|
int32_t pathLen;
|
2016-03-19 07:11:03 +03:00
|
|
|
nsresult nsrv = urlParser->ParseURL(url, static_cast<int32_t>(urlLen),
|
|
|
|
&schemePos, &schemeLen,
|
|
|
|
&authorityPos, &authorityLen,
|
|
|
|
&pathPos, &pathLen);
|
|
|
|
if (NS_FAILED(nsrv)) {
|
|
|
|
return Result::ERROR_CERT_BAD_ACCESS_LOCATION;
|
2014-05-02 02:07:55 +04:00
|
|
|
}
|
2014-05-17 00:53:14 +04:00
|
|
|
if (schemeLen < 0 || authorityLen < 0) {
|
2016-03-19 07:11:03 +03:00
|
|
|
return Result::ERROR_CERT_BAD_ACCESS_LOCATION;
|
2014-05-17 00:53:14 +04:00
|
|
|
}
|
2014-05-30 07:17:53 +04:00
|
|
|
nsAutoCString scheme(url + schemePos,
|
|
|
|
static_cast<nsAutoCString::size_type>(schemeLen));
|
2014-05-17 00:53:14 +04:00
|
|
|
if (!scheme.LowerCaseEqualsLiteral("http")) {
|
2016-03-19 07:11:03 +03:00
|
|
|
// We don't support HTTPS to avoid loops. See Bug 92923.
|
|
|
|
// We also in general only support HTTP.
|
|
|
|
return Result::ERROR_CERT_BAD_ACCESS_LOCATION;
|
2014-05-17 00:53:14 +04:00
|
|
|
}
|
|
|
|
|
2014-05-02 02:07:55 +04:00
|
|
|
uint32_t hostnamePos;
|
|
|
|
int32_t hostnameLen;
|
|
|
|
int32_t port;
|
2016-03-19 07:11:03 +03:00
|
|
|
// We ignore user:password sections: if one is present, we send an OCSP
|
|
|
|
// request to the URL as normal without sending the username or password.
|
|
|
|
nsrv = urlParser->ParseAuthority(url + authorityPos, authorityLen,
|
|
|
|
nullptr, nullptr, nullptr, nullptr,
|
|
|
|
&hostnamePos, &hostnameLen, &port);
|
|
|
|
if (NS_FAILED(nsrv)) {
|
|
|
|
return Result::ERROR_CERT_BAD_ACCESS_LOCATION;
|
2014-05-02 02:07:55 +04:00
|
|
|
}
|
2014-05-17 00:53:14 +04:00
|
|
|
if (hostnameLen < 0) {
|
2016-03-19 07:11:03 +03:00
|
|
|
return Result::ERROR_CERT_BAD_ACCESS_LOCATION;
|
2014-05-17 00:53:14 +04:00
|
|
|
}
|
2014-05-02 02:07:55 +04:00
|
|
|
if (port == -1) {
|
|
|
|
port = 80;
|
2014-05-30 07:17:53 +04:00
|
|
|
} else if (port < 0 || port > 0xffff) {
|
2016-03-19 07:11:03 +03:00
|
|
|
return Result::ERROR_CERT_BAD_ACCESS_LOCATION;
|
2014-05-02 02:07:55 +04:00
|
|
|
}
|
2014-05-30 07:17:53 +04:00
|
|
|
nsAutoCString
|
|
|
|
hostname(url + authorityPos + hostnamePos,
|
2017-03-10 05:17:23 +03:00
|
|
|
static_cast<nsACString::size_type>(hostnameLen));
|
2014-05-22 02:42:21 +04:00
|
|
|
|
2016-05-19 04:58:40 +03:00
|
|
|
nsNSSHttpServerSession* serverSessionPtr = nullptr;
|
2016-03-19 07:11:03 +03:00
|
|
|
Result rv = nsNSSHttpInterface::createSessionFcn(
|
|
|
|
hostname.BeginReading(), static_cast<uint16_t>(port), &serverSessionPtr);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
2014-05-02 02:07:55 +04:00
|
|
|
}
|
2016-05-19 04:58:40 +03:00
|
|
|
UniqueHTTPServerSession serverSession(serverSessionPtr);
|
2014-05-22 02:42:21 +04:00
|
|
|
|
2014-05-17 00:53:14 +04:00
|
|
|
nsAutoCString path;
|
|
|
|
if (pathLen > 0) {
|
2014-05-30 07:17:53 +04:00
|
|
|
path.Assign(url + pathPos, static_cast<nsAutoCString::size_type>(pathLen));
|
2014-05-17 00:53:14 +04:00
|
|
|
} else {
|
|
|
|
path.Assign("/");
|
|
|
|
}
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gCertVerifierLog, LogLevel::Debug,
|
2014-05-22 02:42:21 +04:00
|
|
|
("Setting up OCSP request: pre all path =%s pathlen=%d\n", path.get(),
|
|
|
|
pathLen));
|
|
|
|
nsAutoCString method("POST");
|
|
|
|
if (useGET) {
|
|
|
|
method.Assign("GET");
|
|
|
|
if (!StringEndsWith(path, NS_LITERAL_CSTRING("/"))) {
|
|
|
|
path.Append("/");
|
|
|
|
}
|
2017-02-23 18:19:55 +03:00
|
|
|
nsrv = AppendEscapedBase64Item(encodedRequest, path);
|
2016-03-19 07:11:03 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(nsrv))) {
|
|
|
|
return Result::FATAL_ERROR_LIBRARY_FAILURE;
|
2014-05-22 02:42:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 04:58:40 +03:00
|
|
|
nsNSSHttpRequestSession* requestSessionPtr;
|
2016-03-19 07:11:03 +03:00
|
|
|
rv = nsNSSHttpInterface::createFcn(serverSession.get(), "http", path.get(),
|
2016-11-14 13:26:15 +03:00
|
|
|
method.get(), originAttributes, timeout,
|
2016-11-03 12:53:52 +03:00
|
|
|
&requestSessionPtr);
|
2016-03-19 07:11:03 +03:00
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
2014-05-02 02:07:55 +04:00
|
|
|
}
|
2014-05-30 07:17:53 +04:00
|
|
|
|
2016-05-19 04:58:40 +03:00
|
|
|
UniqueHTTPRequestSession requestSession(requestSessionPtr);
|
2014-05-22 02:42:21 +04:00
|
|
|
|
|
|
|
if (!useGET) {
|
2016-03-19 07:11:03 +03:00
|
|
|
rv = nsNSSHttpInterface::setPostDataFcn(
|
2016-05-19 07:20:56 +03:00
|
|
|
requestSession.get(),
|
|
|
|
BitwiseCast<char*, unsigned char*>(encodedRequest->data),
|
2016-03-19 07:11:03 +03:00
|
|
|
encodedRequest->len, "application/ocsp-request");
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
2014-05-22 02:42:21 +04:00
|
|
|
}
|
2014-05-02 02:07:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t httpResponseCode;
|
|
|
|
const char* httpResponseData;
|
|
|
|
uint32_t httpResponseDataLen = 0; // 0 means any response size is acceptable
|
2016-03-19 07:11:03 +03:00
|
|
|
rv = nsNSSHttpInterface::trySendAndReceiveFcn(requestSession.get(), nullptr,
|
2016-12-28 11:25:22 +03:00
|
|
|
&httpResponseCode,
|
2016-03-19 07:11:03 +03:00
|
|
|
nullptr, &httpResponseData,
|
|
|
|
&httpResponseDataLen);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
2014-05-02 02:07:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (httpResponseCode != 200) {
|
2016-03-19 07:11:03 +03:00
|
|
|
return Result::ERROR_OCSP_SERVER_ERROR;
|
2014-05-02 02:07:55 +04:00
|
|
|
}
|
|
|
|
|
2016-04-05 03:35:24 +03:00
|
|
|
encodedResponse = SECITEM_AllocItem(arena.get(), nullptr, httpResponseDataLen);
|
2014-05-02 02:07:55 +04:00
|
|
|
if (!encodedResponse) {
|
2016-03-19 07:11:03 +03:00
|
|
|
return Result::FATAL_ERROR_NO_MEMORY;
|
2014-05-02 02:07:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(encodedResponse->data, httpResponseData, httpResponseDataLen);
|
2016-03-19 07:11:03 +03:00
|
|
|
return Success;
|
2014-05-02 02:07:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace mozilla::psm
|