michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set sw=2 ts=8 et ft=cpp: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_ipc_KeyStore_h michael@0: #define mozilla_ipc_KeyStore_h 1 michael@0: michael@0: #include "mozilla/ipc/UnixSocket.h" michael@0: #include michael@0: #include michael@0: michael@0: #include "cert.h" michael@0: michael@0: namespace mozilla { michael@0: namespace ipc { michael@0: michael@0: enum ResponseCode { michael@0: SUCCESS = 1, michael@0: LOCKED = 2, michael@0: UNINITIALIZED = 3, michael@0: SYSTEM_ERROR = 4, michael@0: PROTOCOL_ERROR = 5, michael@0: PERMISSION_DENIED = 6, michael@0: KEY_NOT_FOUND = 7, michael@0: VALUE_CORRUPTED = 8, michael@0: UNDEFINED_ACTION = 9, michael@0: WRONG_PASSWORD_0 = 10, michael@0: WRONG_PASSWORD_1 = 11, michael@0: WRONG_PASSWORD_2 = 12, michael@0: WRONG_PASSWORD_3 = 13, // MAX_RETRY = 4 michael@0: NO_RESPONSE michael@0: }; michael@0: michael@0: static const int MAX_PARAM = 2; michael@0: static const int KEY_SIZE = ((NAME_MAX - 15) / 2); michael@0: static const int VALUE_SIZE = 32768; michael@0: static const int PASSWORD_SIZE = VALUE_SIZE; michael@0: michael@0: static const char *CA_BEGIN = "-----BEGIN ", michael@0: *CA_END = "-----END ", michael@0: *CA_TAILER = "-----\n"; michael@0: static const int CA_LINE_SIZE = 64; michael@0: michael@0: struct ProtocolCommand { michael@0: int8_t command; michael@0: int paramNum; michael@0: }; michael@0: michael@0: static const struct ProtocolCommand commands[] = { michael@0: {'g', 1}, // Get CA, command "g CERT_NAME" michael@0: { 0, 0} michael@0: }; michael@0: michael@0: struct ProtocolParam{ michael@0: uint length; michael@0: int8_t data[VALUE_SIZE]; michael@0: }; michael@0: michael@0: typedef enum { michael@0: STATE_IDLE, michael@0: STATE_READ_PARAM_LEN, michael@0: STATE_READ_PARAM_DATA, michael@0: STATE_PROCESSING michael@0: } ProtocolHandlerState; michael@0: michael@0: class KeyStoreConnector : public mozilla::ipc::UnixSocketConnector michael@0: { michael@0: public: michael@0: KeyStoreConnector() michael@0: {} michael@0: michael@0: virtual ~KeyStoreConnector() michael@0: {} michael@0: michael@0: virtual int Create(); michael@0: virtual bool CreateAddr(bool aIsServer, michael@0: socklen_t& aAddrSize, michael@0: sockaddr_any& aAddr, michael@0: const char* aAddress); michael@0: virtual bool SetUp(int aFd); michael@0: virtual bool SetUpListenSocket(int aFd); michael@0: virtual void GetSocketAddr(const sockaddr_any& aAddr, michael@0: nsAString& aAddrStr); michael@0: }; michael@0: michael@0: class KeyStore : public mozilla::ipc::UnixSocketConsumer michael@0: { michael@0: public: michael@0: KeyStore(); michael@0: virtual ~KeyStore() {} michael@0: michael@0: void Shutdown(); michael@0: michael@0: private: michael@0: virtual void ReceiveSocketData(nsAutoPtr& aMessage); michael@0: michael@0: virtual void OnConnectSuccess(); michael@0: virtual void OnConnectError(); michael@0: virtual void OnDisconnect(); michael@0: michael@0: private: michael@0: struct { michael@0: ProtocolHandlerState state; michael@0: uint8_t command; michael@0: struct ProtocolParam param[MAX_PARAM]; michael@0: int paramCount; michael@0: const struct ProtocolCommand *commandPattern; michael@0: } mHandlerInfo; michael@0: void ResetHandlerInfo(); michael@0: void Listen(); michael@0: michael@0: void FormatCaData(const uint8_t *caData, int caDataLength, const char *name, michael@0: const uint8_t **formatData, int &formatDataLength); michael@0: michael@0: bool CheckSize(UnixSocketRawData *aMessage, size_t aExpectSize); michael@0: bool ReadCommand(UnixSocketRawData *aMessage); michael@0: bool ReadLength(UnixSocketRawData *aMessage); michael@0: bool ReadData(UnixSocketRawData *aMessage); michael@0: void SendResponse(ResponseCode response); michael@0: void SendData(const uint8_t *data, int length); michael@0: michael@0: bool mShutdown; michael@0: michael@0: CERTCertDBHandle *certdb; michael@0: }; michael@0: michael@0: } // namespace ipc michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozilla_ipc_KeyStore_h