1 : //* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Url Classifier code
16 : *
17 : * The Initial Developer of the Original Code is
18 : * the Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Dave Camp <dcamp@mozilla.com>
24 : * Gian-Carlo Pascutto <gpascutto@mozilla.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 : #ifndef ProtocolParser_h__
41 : #define ProtocolParser_h__
42 :
43 : #include "HashStore.h"
44 : #include "nsICryptoHMAC.h"
45 :
46 : namespace mozilla {
47 : namespace safebrowsing {
48 :
49 : /**
50 : * Some helpers for parsing the safe
51 : */
52 : class ProtocolParser {
53 : public:
54 32 : struct ForwardedUpdate {
55 : nsCString table;
56 : nsCString url;
57 : nsCString mac;
58 : };
59 :
60 : ProtocolParser(PRUint32 aHashKey);
61 : ~ProtocolParser();
62 :
63 181 : nsresult Status() const { return mUpdateStatus; }
64 :
65 : nsresult Init(nsICryptoHash* aHasher);
66 :
67 : nsresult InitHMAC(const nsACString& aClientKey,
68 : const nsACString& aServerMAC);
69 : nsresult FinishHMAC();
70 :
71 : void SetCurrentTable(const nsACString& aTable);
72 :
73 : nsresult Begin();
74 : nsresult AppendStream(const nsACString& aData);
75 :
76 : // Forget the table updates that were created by this pass. It
77 : // becomes the caller's responsibility to free them. This is shitty.
78 : TableUpdate *GetTableUpdate(const nsACString& aTable);
79 126 : void ForgetTableUpdates() { mTableUpdates.Clear(); }
80 80 : nsTArray<TableUpdate*> &GetTableUpdates() { return mTableUpdates; }
81 :
82 : // Update information.
83 80 : const nsTArray<ForwardedUpdate> &Forwards() const { return mForwards; }
84 160 : int32 UpdateWait() { return mUpdateWait; }
85 80 : bool ResetRequested() { return mResetRequested; }
86 87 : bool RekeyRequested() { return mRekeyRequested; }
87 :
88 : private:
89 : nsresult ProcessControl(bool* aDone);
90 : nsresult ProcessMAC(const nsCString& aLine);
91 : nsresult ProcessExpirations(const nsCString& aLine);
92 : nsresult ProcessChunkControl(const nsCString& aLine);
93 : nsresult ProcessForward(const nsCString& aLine);
94 : nsresult AddForward(const nsACString& aUrl, const nsACString& aMac);
95 : nsresult ProcessChunk(bool* done);
96 : nsresult ProcessPlaintextChunk(const nsACString& aChunk);
97 : nsresult ProcessShaChunk(const nsACString& aChunk);
98 : nsresult ProcessHostAdd(const Prefix& aDomain, PRUint8 aNumEntries,
99 : const nsACString& aChunk, PRUint32* aStart);
100 : nsresult ProcessHostSub(const Prefix& aDomain, PRUint8 aNumEntries,
101 : const nsACString& aChunk, PRUint32* aStart);
102 : nsresult ProcessHostAddComplete(PRUint8 aNumEntries, const nsACString& aChunk,
103 : PRUint32 *aStart);
104 : nsresult ProcessHostSubComplete(PRUint8 numEntries, const nsACString& aChunk,
105 : PRUint32* start);
106 : bool NextLine(nsACString& aLine);
107 :
108 : void CleanupUpdates();
109 :
110 : enum ParserState {
111 : PROTOCOL_STATE_CONTROL,
112 : PROTOCOL_STATE_CHUNK
113 : };
114 : ParserState mState;
115 :
116 : enum ChunkType {
117 : CHUNK_ADD,
118 : CHUNK_SUB
119 : };
120 :
121 : struct ChunkState {
122 : ChunkType type;
123 : uint32 num;
124 : uint32 hashSize;
125 : uint32 length;
126 87 : void Clear() { num = 0; hashSize = 0; length = 0; }
127 : };
128 : ChunkState mChunkState;
129 :
130 : PRUint32 mHashKey;
131 : nsCOMPtr<nsICryptoHash> mCryptoHash;
132 :
133 : nsresult mUpdateStatus;
134 : nsCString mPending;
135 :
136 : nsCOMPtr<nsICryptoHMAC> mHMAC;
137 : nsCString mServerMAC;
138 :
139 : uint32 mUpdateWait;
140 : bool mResetRequested;
141 : bool mRekeyRequested;
142 :
143 : nsTArray<ForwardedUpdate> mForwards;
144 : nsTArray<TableUpdate*> mTableUpdates;
145 : TableUpdate *mTableUpdate;
146 : };
147 :
148 : }
149 : }
150 :
151 : #endif
|