OCSPRequestor.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "OCSPRequestor.h"
  6. #include <limits>
  7. #include "ScopedNSSTypes.h"
  8. #include "mozilla/Base64.h"
  9. #include "mozilla/Casting.h"
  10. #include "nsIURLParser.h"
  11. #include "nsNSSCallbacks.h"
  12. #include "nsNetCID.h"
  13. #include "nsServiceManagerUtils.h"
  14. #include "secerr.h"
  15. extern mozilla::LazyLogModule gCertVerifierLog;
  16. namespace mozilla {
  17. void
  18. ReleaseHttpServerSession(nsNSSHttpServerSession* httpServerSession)
  19. {
  20. delete httpServerSession;
  21. }
  22. void
  23. ReleaseHttpRequestSession(nsNSSHttpRequestSession* httpRequestSession)
  24. {
  25. httpRequestSession->Release();
  26. }
  27. MOZ_TYPE_SPECIFIC_UNIQUE_PTR_TEMPLATE(UniqueHTTPServerSession,
  28. nsNSSHttpServerSession,
  29. ReleaseHttpServerSession)
  30. MOZ_TYPE_SPECIFIC_UNIQUE_PTR_TEMPLATE(UniqueHTTPRequestSession,
  31. nsNSSHttpRequestSession,
  32. ReleaseHttpRequestSession)
  33. } // namespace mozilla
  34. namespace mozilla { namespace psm {
  35. static nsresult
  36. AppendEscapedBase64Item(const SECItem* encodedRequest, nsACString& path)
  37. {
  38. nsresult rv;
  39. nsDependentCSubstring requestAsSubstring(
  40. BitwiseCast<char*, unsigned char*>(encodedRequest->data),
  41. encodedRequest->len);
  42. nsCString base64Request;
  43. rv = Base64Encode(requestAsSubstring, base64Request);
  44. if (NS_WARN_IF(NS_FAILED(rv))) {
  45. return rv;
  46. }
  47. MOZ_LOG(gCertVerifierLog, LogLevel::Debug,
  48. ("Setting up OCSP GET path, pre path =%s\n",
  49. PromiseFlatCString(path).get()));
  50. // The path transformation is not a direct url encoding. Three characters
  51. // need change '+' -> "%2B", '/' -> "%2F", and '=' -> '%3D'.
  52. // http://tools.ietf.org/html/rfc5019#section-5
  53. base64Request.ReplaceSubstring("+", "%2B");
  54. base64Request.ReplaceSubstring("/", "%2F");
  55. base64Request.ReplaceSubstring("=", "%3D");
  56. path.Append(base64Request);
  57. return NS_OK;
  58. }
  59. Result
  60. DoOCSPRequest(const UniquePLArenaPool& arena, const char* url,
  61. const NeckoOriginAttributes& originAttributes,
  62. const SECItem* encodedRequest, PRIntervalTime timeout,
  63. bool useGET,
  64. /*out*/ SECItem*& encodedResponse)
  65. {
  66. MOZ_ASSERT(arena.get());
  67. MOZ_ASSERT(url);
  68. MOZ_ASSERT(encodedRequest);
  69. MOZ_ASSERT(encodedRequest->data);
  70. if (!arena.get() || !url || !encodedRequest || !encodedRequest->data) {
  71. return Result::FATAL_ERROR_INVALID_ARGS;
  72. }
  73. uint32_t urlLen = PL_strlen(url);
  74. if (urlLen > static_cast<uint32_t>(std::numeric_limits<int32_t>::max())) {
  75. return Result::FATAL_ERROR_INVALID_ARGS;
  76. }
  77. nsCOMPtr<nsIURLParser> urlParser = do_GetService(NS_STDURLPARSER_CONTRACTID);
  78. if (!urlParser) {
  79. return Result::FATAL_ERROR_LIBRARY_FAILURE;
  80. }
  81. uint32_t schemePos;
  82. int32_t schemeLen;
  83. uint32_t authorityPos;
  84. int32_t authorityLen;
  85. uint32_t pathPos;
  86. int32_t pathLen;
  87. nsresult nsrv = urlParser->ParseURL(url, static_cast<int32_t>(urlLen),
  88. &schemePos, &schemeLen,
  89. &authorityPos, &authorityLen,
  90. &pathPos, &pathLen);
  91. if (NS_FAILED(nsrv)) {
  92. return Result::ERROR_CERT_BAD_ACCESS_LOCATION;
  93. }
  94. if (schemeLen < 0 || authorityLen < 0) {
  95. return Result::ERROR_CERT_BAD_ACCESS_LOCATION;
  96. }
  97. nsAutoCString scheme(url + schemePos,
  98. static_cast<nsAutoCString::size_type>(schemeLen));
  99. if (!scheme.LowerCaseEqualsLiteral("http")) {
  100. // We don't support HTTPS to avoid loops. See Bug 92923.
  101. // We also in general only support HTTP.
  102. return Result::ERROR_CERT_BAD_ACCESS_LOCATION;
  103. }
  104. uint32_t hostnamePos;
  105. int32_t hostnameLen;
  106. int32_t port;
  107. // We ignore user:password sections: if one is present, we send an OCSP
  108. // request to the URL as normal without sending the username or password.
  109. nsrv = urlParser->ParseAuthority(url + authorityPos, authorityLen,
  110. nullptr, nullptr, nullptr, nullptr,
  111. &hostnamePos, &hostnameLen, &port);
  112. if (NS_FAILED(nsrv)) {
  113. return Result::ERROR_CERT_BAD_ACCESS_LOCATION;
  114. }
  115. if (hostnameLen < 0) {
  116. return Result::ERROR_CERT_BAD_ACCESS_LOCATION;
  117. }
  118. if (port == -1) {
  119. port = 80;
  120. } else if (port < 0 || port > 0xffff) {
  121. return Result::ERROR_CERT_BAD_ACCESS_LOCATION;
  122. }
  123. nsAutoCString
  124. hostname(url + authorityPos + hostnamePos,
  125. static_cast<nsACString_internal::size_type>(hostnameLen));
  126. nsNSSHttpServerSession* serverSessionPtr = nullptr;
  127. Result rv = nsNSSHttpInterface::createSessionFcn(
  128. hostname.BeginReading(), static_cast<uint16_t>(port), &serverSessionPtr);
  129. if (rv != Success) {
  130. return rv;
  131. }
  132. UniqueHTTPServerSession serverSession(serverSessionPtr);
  133. nsAutoCString path;
  134. if (pathLen > 0) {
  135. path.Assign(url + pathPos, static_cast<nsAutoCString::size_type>(pathLen));
  136. } else {
  137. path.Assign("/");
  138. }
  139. MOZ_LOG(gCertVerifierLog, LogLevel::Debug,
  140. ("Setting up OCSP request: pre all path =%s pathlen=%d\n", path.get(),
  141. pathLen));
  142. nsAutoCString method("POST");
  143. if (useGET) {
  144. method.Assign("GET");
  145. if (!StringEndsWith(path, NS_LITERAL_CSTRING("/"))) {
  146. path.Append("/");
  147. }
  148. nsresult nsrv = AppendEscapedBase64Item(encodedRequest, path);
  149. if (NS_WARN_IF(NS_FAILED(nsrv))) {
  150. return Result::FATAL_ERROR_LIBRARY_FAILURE;
  151. }
  152. }
  153. nsNSSHttpRequestSession* requestSessionPtr;
  154. rv = nsNSSHttpInterface::createFcn(serverSession.get(), "http", path.get(),
  155. method.get(), originAttributes, timeout,
  156. &requestSessionPtr);
  157. if (rv != Success) {
  158. return rv;
  159. }
  160. UniqueHTTPRequestSession requestSession(requestSessionPtr);
  161. if (!useGET) {
  162. rv = nsNSSHttpInterface::setPostDataFcn(
  163. requestSession.get(),
  164. BitwiseCast<char*, unsigned char*>(encodedRequest->data),
  165. encodedRequest->len, "application/ocsp-request");
  166. if (rv != Success) {
  167. return rv;
  168. }
  169. }
  170. uint16_t httpResponseCode;
  171. const char* httpResponseData;
  172. uint32_t httpResponseDataLen = 0; // 0 means any response size is acceptable
  173. rv = nsNSSHttpInterface::trySendAndReceiveFcn(requestSession.get(), nullptr,
  174. &httpResponseCode, nullptr,
  175. nullptr, &httpResponseData,
  176. &httpResponseDataLen);
  177. if (rv != Success) {
  178. return rv;
  179. }
  180. if (httpResponseCode != 200) {
  181. return Result::ERROR_OCSP_SERVER_ERROR;
  182. }
  183. encodedResponse = SECITEM_AllocItem(arena.get(), nullptr, httpResponseDataLen);
  184. if (!encodedResponse) {
  185. return Result::FATAL_ERROR_NO_MEMORY;
  186. }
  187. memcpy(encodedResponse->data, httpResponseData, httpResponseDataLen);
  188. return Success;
  189. }
  190. } } // namespace mozilla::psm