ipc/keystore/KeyStore.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/keystore/KeyStore.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set sw=2 ts=8 et ft=cpp: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef mozilla_ipc_KeyStore_h
    1.11 +#define mozilla_ipc_KeyStore_h 1
    1.12 +
    1.13 +#include "mozilla/ipc/UnixSocket.h"
    1.14 +#include <sys/socket.h>
    1.15 +#include <sys/un.h>
    1.16 +
    1.17 +#include "cert.h"
    1.18 +
    1.19 +namespace mozilla {
    1.20 +namespace ipc {
    1.21 +
    1.22 +enum ResponseCode {
    1.23 +  SUCCESS           =  1,
    1.24 +  LOCKED            =  2,
    1.25 +  UNINITIALIZED     =  3,
    1.26 +  SYSTEM_ERROR      =  4,
    1.27 +  PROTOCOL_ERROR    =  5,
    1.28 +  PERMISSION_DENIED =  6,
    1.29 +  KEY_NOT_FOUND     =  7,
    1.30 +  VALUE_CORRUPTED   =  8,
    1.31 +  UNDEFINED_ACTION  =  9,
    1.32 +  WRONG_PASSWORD_0  = 10,
    1.33 +  WRONG_PASSWORD_1  = 11,
    1.34 +  WRONG_PASSWORD_2  = 12,
    1.35 +  WRONG_PASSWORD_3  = 13, // MAX_RETRY = 4
    1.36 +  NO_RESPONSE
    1.37 +};
    1.38 +
    1.39 +static const int MAX_PARAM = 2;
    1.40 +static const int KEY_SIZE = ((NAME_MAX - 15) / 2);
    1.41 +static const int VALUE_SIZE = 32768;
    1.42 +static const int PASSWORD_SIZE = VALUE_SIZE;
    1.43 +
    1.44 +static const char *CA_BEGIN = "-----BEGIN ",
    1.45 +                  *CA_END   = "-----END ",
    1.46 +                  *CA_TAILER = "-----\n";
    1.47 +static const int CA_LINE_SIZE = 64;
    1.48 +
    1.49 +struct ProtocolCommand {
    1.50 +  int8_t  command;
    1.51 +  int     paramNum;
    1.52 +};
    1.53 +
    1.54 +static const struct ProtocolCommand commands[] = {
    1.55 +  {'g', 1}, // Get CA, command "g CERT_NAME"
    1.56 +  { 0,  0}
    1.57 +};
    1.58 +
    1.59 +struct ProtocolParam{
    1.60 +  uint    length;
    1.61 +  int8_t  data[VALUE_SIZE];
    1.62 +};
    1.63 +
    1.64 +typedef enum {
    1.65 +  STATE_IDLE,
    1.66 +  STATE_READ_PARAM_LEN,
    1.67 +  STATE_READ_PARAM_DATA,
    1.68 +  STATE_PROCESSING
    1.69 +} ProtocolHandlerState;
    1.70 +
    1.71 +class KeyStoreConnector : public mozilla::ipc::UnixSocketConnector
    1.72 +{
    1.73 +public:
    1.74 +  KeyStoreConnector()
    1.75 +  {}
    1.76 +
    1.77 +  virtual ~KeyStoreConnector()
    1.78 +  {}
    1.79 +
    1.80 +  virtual int Create();
    1.81 +  virtual bool CreateAddr(bool aIsServer,
    1.82 +                          socklen_t& aAddrSize,
    1.83 +                          sockaddr_any& aAddr,
    1.84 +                          const char* aAddress);
    1.85 +  virtual bool SetUp(int aFd);
    1.86 +  virtual bool SetUpListenSocket(int aFd);
    1.87 +  virtual void GetSocketAddr(const sockaddr_any& aAddr,
    1.88 +                             nsAString& aAddrStr);
    1.89 +};
    1.90 +
    1.91 +class KeyStore : public mozilla::ipc::UnixSocketConsumer
    1.92 +{
    1.93 +public:
    1.94 +  KeyStore();
    1.95 +  virtual ~KeyStore() {}
    1.96 +
    1.97 +  void Shutdown();
    1.98 +
    1.99 +private:
   1.100 +  virtual void ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage);
   1.101 +
   1.102 +  virtual void OnConnectSuccess();
   1.103 +  virtual void OnConnectError();
   1.104 +  virtual void OnDisconnect();
   1.105 +
   1.106 +private:
   1.107 +  struct {
   1.108 +    ProtocolHandlerState          state;
   1.109 +    uint8_t                       command;
   1.110 +    struct ProtocolParam          param[MAX_PARAM];
   1.111 +    int                           paramCount;
   1.112 +    const struct ProtocolCommand  *commandPattern;
   1.113 +  } mHandlerInfo;
   1.114 +  void ResetHandlerInfo();
   1.115 +  void Listen();
   1.116 +
   1.117 +  void FormatCaData(const uint8_t *caData, int caDataLength, const char *name,
   1.118 +                    const uint8_t **formatData, int &formatDataLength);
   1.119 +
   1.120 +  bool CheckSize(UnixSocketRawData *aMessage, size_t aExpectSize);
   1.121 +  bool ReadCommand(UnixSocketRawData *aMessage);
   1.122 +  bool ReadLength(UnixSocketRawData *aMessage);
   1.123 +  bool ReadData(UnixSocketRawData *aMessage);
   1.124 +  void SendResponse(ResponseCode response);
   1.125 +  void SendData(const uint8_t *data, int length);
   1.126 +
   1.127 +  bool mShutdown;
   1.128 +
   1.129 +  CERTCertDBHandle *certdb;
   1.130 +};
   1.131 +
   1.132 +} // namespace ipc
   1.133 +} // namespace mozilla
   1.134 +
   1.135 +#endif // mozilla_ipc_KeyStore_h

mercurial