Bumping manifests a=b2g-bump
[gecko.git] / ipc / keystore / KeyStore.h
blob028e0ff9a6c7c201a9a06f3c62dc4175c27fe819
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
36 void FormatCaData(const uint8_t *aCaData, int aCaDataLength,
37 const char *aName, const uint8_t **aFormatData,
38 int *aFormatDataLength);
40 ResponseCode getCertificate(const char *aCertName, const uint8_t **aCertData,
41 int *aCertDataLength);
43 bool checkPermission(uid_t uid);
45 static const int MAX_PARAM = 2;
46 static const int KEY_SIZE = ((NAME_MAX - 15) / 2);
47 static const int VALUE_SIZE = 32768;
48 static const int PASSWORD_SIZE = VALUE_SIZE;
50 static const char *CA_BEGIN = "-----BEGIN ",
51 *CA_END = "-----END ",
52 *CA_TAILER = "-----\n";
53 static const int CA_LINE_SIZE = 64;
55 struct ProtocolCommand {
56 int8_t command;
57 int paramNum;
60 static const struct ProtocolCommand commands[] = {
61 {'g', 1}, // Get CA, command "g CERT_NAME"
62 { 0, 0}
65 struct ProtocolParam{
66 uint length;
67 int8_t data[VALUE_SIZE];
70 typedef enum {
71 STATE_IDLE,
72 STATE_READ_PARAM_LEN,
73 STATE_READ_PARAM_DATA,
74 STATE_PROCESSING
75 } ProtocolHandlerState;
77 class KeyStoreConnector : public mozilla::ipc::UnixSocketConnector
79 public:
80 KeyStoreConnector()
83 virtual ~KeyStoreConnector()
86 virtual int Create();
87 virtual bool CreateAddr(bool aIsServer,
88 socklen_t& aAddrSize,
89 sockaddr_any& aAddr,
90 const char* aAddress);
91 virtual bool SetUp(int aFd);
92 virtual bool SetUpListenSocket(int aFd);
93 virtual void GetSocketAddr(const sockaddr_any& aAddr,
94 nsAString& aAddrStr);
97 class KeyStore : public mozilla::ipc::UnixSocketConsumer
99 public:
100 KeyStore();
102 void Shutdown();
104 private:
105 virtual ~KeyStore();
107 virtual void ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage);
109 virtual void OnConnectSuccess();
110 virtual void OnConnectError();
111 virtual void OnDisconnect();
113 struct {
114 ProtocolHandlerState state;
115 uint8_t command;
116 struct ProtocolParam param[MAX_PARAM];
117 int paramCount;
118 const struct ProtocolCommand *commandPattern;
119 } mHandlerInfo;
120 void ResetHandlerInfo();
121 void Listen();
123 bool CheckSize(UnixSocketRawData *aMessage, size_t aExpectSize);
124 ResponseCode ReadCommand(UnixSocketRawData *aMessage);
125 ResponseCode ReadLength(UnixSocketRawData *aMessage);
126 ResponseCode ReadData(UnixSocketRawData *aMessage);
127 void SendResponse(ResponseCode response);
128 void SendData(const uint8_t *data, int length);
130 bool mShutdown;
133 } // namespace ipc
134 } // namespace mozilla
136 #endif // mozilla_ipc_KeyStore_h