1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Red Hat, Inc.
20 : * Portions created by the Initial Developer are Copyright (C) 2006
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Kai Engert <kengert@redhat.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "nsSSLStatus.h"
41 : #include "plstr.h"
42 : #include "nsIClassInfoImpl.h"
43 : #include "nsIProgrammingLanguage.h"
44 : #include "nsIObjectOutputStream.h"
45 : #include "nsIObjectInputStream.h"
46 :
47 : NS_IMETHODIMP
48 0 : nsSSLStatus::GetServerCert(nsIX509Cert** _result)
49 : {
50 0 : NS_ASSERTION(_result, "non-NULL destination required");
51 :
52 0 : *_result = mServerCert;
53 0 : NS_IF_ADDREF(*_result);
54 :
55 0 : return NS_OK;
56 : }
57 :
58 : NS_IMETHODIMP
59 0 : nsSSLStatus::GetKeyLength(PRUint32* _result)
60 : {
61 0 : NS_ASSERTION(_result, "non-NULL destination required");
62 0 : if (!mHaveKeyLengthAndCipher)
63 0 : return NS_ERROR_NOT_AVAILABLE;
64 :
65 0 : *_result = mKeyLength;
66 :
67 0 : return NS_OK;
68 : }
69 :
70 : NS_IMETHODIMP
71 0 : nsSSLStatus::GetSecretKeyLength(PRUint32* _result)
72 : {
73 0 : NS_ASSERTION(_result, "non-NULL destination required");
74 0 : if (!mHaveKeyLengthAndCipher)
75 0 : return NS_ERROR_NOT_AVAILABLE;
76 :
77 0 : *_result = mSecretKeyLength;
78 :
79 0 : return NS_OK;
80 : }
81 :
82 : NS_IMETHODIMP
83 0 : nsSSLStatus::GetCipherName(char** _result)
84 : {
85 0 : NS_ASSERTION(_result, "non-NULL destination required");
86 0 : if (!mHaveKeyLengthAndCipher)
87 0 : return NS_ERROR_NOT_AVAILABLE;
88 :
89 0 : *_result = ToNewCString(mCipherName);
90 :
91 0 : return NS_OK;
92 : }
93 :
94 : NS_IMETHODIMP
95 4 : nsSSLStatus::GetIsDomainMismatch(bool* _result)
96 : {
97 4 : NS_ASSERTION(_result, "non-NULL destination required");
98 :
99 4 : *_result = mHaveCertErrorBits && mIsDomainMismatch;
100 :
101 4 : return NS_OK;
102 : }
103 :
104 : NS_IMETHODIMP
105 4 : nsSSLStatus::GetIsNotValidAtThisTime(bool* _result)
106 : {
107 4 : NS_ASSERTION(_result, "non-NULL destination required");
108 :
109 4 : *_result = mHaveCertErrorBits && mIsNotValidAtThisTime;
110 :
111 4 : return NS_OK;
112 : }
113 :
114 : NS_IMETHODIMP
115 4 : nsSSLStatus::GetIsUntrusted(bool* _result)
116 : {
117 4 : NS_ASSERTION(_result, "non-NULL destination required");
118 :
119 4 : *_result = mHaveCertErrorBits && mIsUntrusted;
120 :
121 4 : return NS_OK;
122 : }
123 :
124 : NS_IMETHODIMP
125 0 : nsSSLStatus::Read(nsIObjectInputStream* stream)
126 : {
127 0 : nsCOMPtr<nsISupports> cert;
128 0 : nsresult rv = stream->ReadObject(true, getter_AddRefs(cert));
129 0 : NS_ENSURE_SUCCESS(rv, rv);
130 :
131 0 : mServerCert = do_QueryInterface(cert);
132 0 : if (!mServerCert)
133 0 : return NS_NOINTERFACE;
134 :
135 0 : rv = stream->Read32(&mKeyLength);
136 0 : NS_ENSURE_SUCCESS(rv, rv);
137 0 : rv = stream->Read32(&mSecretKeyLength);
138 0 : NS_ENSURE_SUCCESS(rv, rv);
139 0 : rv = stream->ReadCString(mCipherName);
140 0 : NS_ENSURE_SUCCESS(rv, rv);
141 :
142 0 : rv = stream->ReadBoolean(&mIsDomainMismatch);
143 0 : NS_ENSURE_SUCCESS(rv, rv);
144 0 : rv = stream->ReadBoolean(&mIsNotValidAtThisTime);
145 0 : NS_ENSURE_SUCCESS(rv, rv);
146 0 : rv = stream->ReadBoolean(&mIsUntrusted);
147 0 : NS_ENSURE_SUCCESS(rv, rv);
148 :
149 0 : rv = stream->ReadBoolean(&mHaveKeyLengthAndCipher);
150 0 : NS_ENSURE_SUCCESS(rv, rv);
151 0 : rv = stream->ReadBoolean(&mHaveCertErrorBits);
152 0 : NS_ENSURE_SUCCESS(rv, rv);
153 :
154 0 : return NS_OK;
155 : }
156 :
157 : NS_IMETHODIMP
158 4 : nsSSLStatus::Write(nsIObjectOutputStream* stream)
159 : {
160 : nsresult rv = stream->WriteCompoundObject(mServerCert,
161 : NS_GET_IID(nsIX509Cert),
162 4 : true);
163 4 : NS_ENSURE_SUCCESS(rv, rv);
164 :
165 4 : rv = stream->Write32(mKeyLength);
166 4 : NS_ENSURE_SUCCESS(rv, rv);
167 4 : rv = stream->Write32(mSecretKeyLength);
168 4 : NS_ENSURE_SUCCESS(rv, rv);
169 4 : rv = stream->WriteStringZ(mCipherName.get());
170 4 : NS_ENSURE_SUCCESS(rv, rv);
171 :
172 4 : rv = stream->WriteBoolean(mIsDomainMismatch);
173 4 : NS_ENSURE_SUCCESS(rv, rv);
174 4 : rv = stream->WriteBoolean(mIsNotValidAtThisTime);
175 4 : NS_ENSURE_SUCCESS(rv, rv);
176 4 : rv = stream->WriteBoolean(mIsUntrusted);
177 4 : NS_ENSURE_SUCCESS(rv, rv);
178 :
179 4 : rv = stream->WriteBoolean(mHaveKeyLengthAndCipher);
180 4 : NS_ENSURE_SUCCESS(rv, rv);
181 4 : rv = stream->WriteBoolean(mHaveCertErrorBits);
182 4 : NS_ENSURE_SUCCESS(rv, rv);
183 :
184 4 : return NS_OK;
185 : }
186 :
187 : NS_IMETHODIMP
188 0 : nsSSLStatus::GetInterfaces(PRUint32 *count, nsIID * **array)
189 : {
190 0 : *count = 0;
191 0 : *array = nsnull;
192 0 : return NS_OK;
193 : }
194 :
195 : NS_IMETHODIMP
196 0 : nsSSLStatus::GetHelperForLanguage(PRUint32 language, nsISupports **_retval)
197 : {
198 0 : *_retval = nsnull;
199 0 : return NS_OK;
200 : }
201 :
202 : NS_IMETHODIMP
203 0 : nsSSLStatus::GetContractID(char * *aContractID)
204 : {
205 0 : *aContractID = nsnull;
206 0 : return NS_OK;
207 : }
208 :
209 : NS_IMETHODIMP
210 0 : nsSSLStatus::GetClassDescription(char * *aClassDescription)
211 : {
212 0 : *aClassDescription = nsnull;
213 0 : return NS_OK;
214 : }
215 :
216 : NS_IMETHODIMP
217 0 : nsSSLStatus::GetClassID(nsCID * *aClassID)
218 : {
219 0 : *aClassID = (nsCID*) nsMemory::Alloc(sizeof(nsCID));
220 0 : if (!*aClassID)
221 0 : return NS_ERROR_OUT_OF_MEMORY;
222 0 : return GetClassIDNoAlloc(*aClassID);
223 : }
224 :
225 : NS_IMETHODIMP
226 0 : nsSSLStatus::GetImplementationLanguage(PRUint32 *aImplementationLanguage)
227 : {
228 0 : *aImplementationLanguage = nsIProgrammingLanguage::CPLUSPLUS;
229 0 : return NS_OK;
230 : }
231 :
232 : NS_IMETHODIMP
233 0 : nsSSLStatus::GetFlags(PRUint32 *aFlags)
234 : {
235 0 : *aFlags = 0;
236 0 : return NS_OK;
237 : }
238 :
239 : static NS_DEFINE_CID(kSSLStatusCID, NS_SSLSTATUS_CID);
240 :
241 : NS_IMETHODIMP
242 4 : nsSSLStatus::GetClassIDNoAlloc(nsCID *aClassIDNoAlloc)
243 : {
244 4 : *aClassIDNoAlloc = kSSLStatusCID;
245 4 : return NS_OK;
246 : }
247 :
248 :
249 :
250 4 : nsSSLStatus::nsSSLStatus()
251 : : mKeyLength(0), mSecretKeyLength(0)
252 : , mIsDomainMismatch(false)
253 : , mIsNotValidAtThisTime(false)
254 : , mIsUntrusted(false)
255 : , mHaveKeyLengthAndCipher(false)
256 4 : , mHaveCertErrorBits(false)
257 : {
258 4 : mCipherName = "";
259 4 : }
260 :
261 76 : NS_IMPL_THREADSAFE_ISUPPORTS3(nsSSLStatus, nsISSLStatus, nsISerializable, nsIClassInfo)
262 :
263 8 : nsSSLStatus::~nsSSLStatus()
264 : {
265 16 : }
|