ipc/keystore/KeyStore.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial