summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/http/QuicSocketControl.cpp
blob: a9a142d2b542958dd3ddccacc969989bb795628c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 *
 * 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 "QuicSocketControl.h"

#include "Http3Session.h"
#include "SharedCertVerifier.h"
#include "nsISocketProvider.h"
#include "nsIWebProgressListener.h"
#include "nsNSSComponent.h"
#include "nsWeakReference.h"
#include "nsSocketTransportService2.h"
#include "nsThreadUtils.h"
#include "sslt.h"
#include "ssl.h"

namespace mozilla {
namespace net {

NS_IMPL_ISUPPORTS_INHERITED(QuicSocketControl, TransportSecurityInfo,
                            nsISSLSocketControl, QuicSocketControl)

QuicSocketControl::QuicSocketControl(uint32_t aProviderFlags)
    : CommonSocketControl(aProviderFlags) {}

void QuicSocketControl::SetCertVerificationResult(PRErrorCode errorCode) {
  if (errorCode) {
    mFailedVerification = true;
    SetCanceled(errorCode);
  }

  if (OnSocketThread()) {
    CallAuthenticated();
  } else {
    DebugOnly<nsresult> rv = gSocketTransportService->Dispatch(
        NewRunnableMethod("QuicSocketControl::CallAuthenticated", this,
                          &QuicSocketControl::CallAuthenticated),
        NS_DISPATCH_NORMAL);
  }
}

NS_IMETHODIMP
QuicSocketControl::GetSSLVersionOffered(int16_t* aSSLVersionOffered) {
  *aSSLVersionOffered = nsISSLSocketControl::TLS_VERSION_1_3;
  return NS_OK;
}

void QuicSocketControl::CallAuthenticated() {
  RefPtr<Http3Session> http3Session = do_QueryReferent(mHttp3Session);
  if (http3Session) {
    http3Session->Authenticated(GetErrorCode());
  }
  mHttp3Session = nullptr;
}

void QuicSocketControl::SetAuthenticationCallback(Http3Session* aHttp3Session) {
  mHttp3Session = do_GetWeakReference(
      static_cast<nsISupportsWeakReference*>(aHttp3Session));
}

void QuicSocketControl::HandshakeCompleted() {
  psm::RememberCertErrorsTable::GetInstance().LookupCertErrorBits(this);

  uint32_t state = nsIWebProgressListener::STATE_IS_SECURE;

  bool distrustImminent;
  MutexAutoLock lock(mMutex);
  nsresult rv =
      IsCertificateDistrustImminent(mSucceededCertChain, distrustImminent);

  if (NS_SUCCEEDED(rv) && distrustImminent) {
    state |= nsIWebProgressListener::STATE_CERT_DISTRUST_IMMINENT;
  }

  // If we're here, the TLS handshake has succeeded. Thus if any of these
  // booleans are true, the user has added an override for a certificate error.
  if (mIsDomainMismatch || mIsUntrusted || mIsNotValidAtThisTime) {
    state |= nsIWebProgressListener::STATE_CERT_USER_OVERRIDDEN;
  }

  SetSecurityState(state);
  mHandshakeCompleted = true;
}

void QuicSocketControl::SetNegotiatedNPN(const nsACString& aValue) {
  MutexAutoLock lock(mMutex);
  mNegotiatedNPN = aValue;
  mNPNCompleted = true;
}

void QuicSocketControl::SetInfo(uint16_t aCipherSuite,
                                uint16_t aProtocolVersion, uint16_t aKeaGroup,
                                uint16_t aSignatureScheme) {
  SSLCipherSuiteInfo cipherInfo;
  if (SSL_GetCipherSuiteInfo(aCipherSuite, &cipherInfo, sizeof cipherInfo) ==
      SECSuccess) {
    MutexAutoLock lock(mMutex);
    mHaveCipherSuiteAndProtocol = true;
    mCipherSuite = aCipherSuite;
    mProtocolVersion = aProtocolVersion & 0xFF;
    mKeaGroup = getKeaGroupName(aKeaGroup);
    mSignatureSchemeName = getSignatureName(aSignatureScheme);
  }
}

NS_IMETHODIMP QuicSocketControl::GetPeerId(nsACString& aResult) {
  if (!mPeerId.IsEmpty()) {
    aResult.Assign(mPeerId);
    return NS_OK;
  }

  if (mProviderFlags &
      nsISocketProvider::ANONYMOUS_CONNECT) {  // See bug 466080
    mPeerId.AppendLiteral("anon:");
  }
  if (mProviderFlags & nsISocketProvider::NO_PERMANENT_STORAGE) {
    mPeerId.AppendLiteral("private:");
  }
  if (mProviderFlags & nsISocketProvider::BE_CONSERVATIVE) {
    mPeerId.AppendLiteral("beConservative:");
  }

  mPeerId.Append(GetHostName());
  mPeerId.Append(':');
  mPeerId.AppendInt(GetPort());
  nsAutoCString suffix;
  GetOriginAttributes().CreateSuffix(suffix);
  mPeerId.Append(suffix);

  aResult.Assign(mPeerId);
  return NS_OK;
}

}  // namespace net
}  // namespace mozilla