Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et ft=cpp: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_ipc_KeyStore_h
8 #define mozilla_ipc_KeyStore_h 1
10 #include "mozilla/ipc/UnixSocket.h"
11 #include <sys/socket.h>
12 #include <sys/un.h>
14 #include "cert.h"
16 namespace mozilla {
17 namespace ipc {
19 enum ResponseCode {
20 SUCCESS = 1,
21 LOCKED = 2,
22 UNINITIALIZED = 3,
23 SYSTEM_ERROR = 4,
24 PROTOCOL_ERROR = 5,
25 PERMISSION_DENIED = 6,
26 KEY_NOT_FOUND = 7,
27 VALUE_CORRUPTED = 8,
28 UNDEFINED_ACTION = 9,
29 WRONG_PASSWORD_0 = 10,
30 WRONG_PASSWORD_1 = 11,
31 WRONG_PASSWORD_2 = 12,
32 WRONG_PASSWORD_3 = 13, // MAX_RETRY = 4
33 NO_RESPONSE
34 };
36 static const int MAX_PARAM = 2;
37 static const int KEY_SIZE = ((NAME_MAX - 15) / 2);
38 static const int VALUE_SIZE = 32768;
39 static const int PASSWORD_SIZE = VALUE_SIZE;
41 static const char *CA_BEGIN = "-----BEGIN ",
42 *CA_END = "-----END ",
43 *CA_TAILER = "-----\n";
44 static const int CA_LINE_SIZE = 64;
46 struct ProtocolCommand {
47 int8_t command;
48 int paramNum;
49 };
51 static const struct ProtocolCommand commands[] = {
52 {'g', 1}, // Get CA, command "g CERT_NAME"
53 { 0, 0}
54 };
56 struct ProtocolParam{
57 uint length;
58 int8_t data[VALUE_SIZE];
59 };
61 typedef enum {
62 STATE_IDLE,
63 STATE_READ_PARAM_LEN,
64 STATE_READ_PARAM_DATA,
65 STATE_PROCESSING
66 } ProtocolHandlerState;
68 class KeyStoreConnector : public mozilla::ipc::UnixSocketConnector
69 {
70 public:
71 KeyStoreConnector()
72 {}
74 virtual ~KeyStoreConnector()
75 {}
77 virtual int Create();
78 virtual bool CreateAddr(bool aIsServer,
79 socklen_t& aAddrSize,
80 sockaddr_any& aAddr,
81 const char* aAddress);
82 virtual bool SetUp(int aFd);
83 virtual bool SetUpListenSocket(int aFd);
84 virtual void GetSocketAddr(const sockaddr_any& aAddr,
85 nsAString& aAddrStr);
86 };
88 class KeyStore : public mozilla::ipc::UnixSocketConsumer
89 {
90 public:
91 KeyStore();
92 virtual ~KeyStore() {}
94 void Shutdown();
96 private:
97 virtual void ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage);
99 virtual void OnConnectSuccess();
100 virtual void OnConnectError();
101 virtual void OnDisconnect();
103 private:
104 struct {
105 ProtocolHandlerState state;
106 uint8_t command;
107 struct ProtocolParam param[MAX_PARAM];
108 int paramCount;
109 const struct ProtocolCommand *commandPattern;
110 } mHandlerInfo;
111 void ResetHandlerInfo();
112 void Listen();
114 void FormatCaData(const uint8_t *caData, int caDataLength, const char *name,
115 const uint8_t **formatData, int &formatDataLength);
117 bool CheckSize(UnixSocketRawData *aMessage, size_t aExpectSize);
118 bool ReadCommand(UnixSocketRawData *aMessage);
119 bool ReadLength(UnixSocketRawData *aMessage);
120 bool ReadData(UnixSocketRawData *aMessage);
121 void SendResponse(ResponseCode response);
122 void SendData(const uint8_t *data, int length);
124 bool mShutdown;
126 CERTCertDBHandle *certdb;
127 };
129 } // namespace ipc
130 } // namespace mozilla
132 #endif // mozilla_ipc_KeyStore_h