2013-10-01 04:09:56 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-12-03 15:26:29 +00:00
|
|
|
/* vim: set sw=2 ts=2 et ft=cpp: tw=80: */
|
2013-10-01 04:09:56 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_ipc_KeyStore_h
|
|
|
|
#define mozilla_ipc_KeyStore_h 1
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include "cert.h"
|
2014-12-03 15:26:29 +00:00
|
|
|
#include "mozilla/ipc/ListenSocket.h"
|
2015-05-26 11:24:20 +00:00
|
|
|
#include "mozilla/ipc/ListenSocketConsumer.h"
|
2014-12-03 15:26:29 +00:00
|
|
|
#include "mozilla/ipc/StreamSocket.h"
|
2015-05-26 11:24:20 +00:00
|
|
|
#include "mozilla/ipc/StreamSocketConsumer.h"
|
2015-02-28 13:54:24 +00:00
|
|
|
#include "nsNSSShutDown.h"
|
2013-10-01 04:09:56 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
enum ResponseCode {
|
|
|
|
SUCCESS = 1,
|
|
|
|
LOCKED = 2,
|
|
|
|
UNINITIALIZED = 3,
|
|
|
|
SYSTEM_ERROR = 4,
|
|
|
|
PROTOCOL_ERROR = 5,
|
|
|
|
PERMISSION_DENIED = 6,
|
|
|
|
KEY_NOT_FOUND = 7,
|
|
|
|
VALUE_CORRUPTED = 8,
|
|
|
|
UNDEFINED_ACTION = 9,
|
|
|
|
WRONG_PASSWORD_0 = 10,
|
|
|
|
WRONG_PASSWORD_1 = 11,
|
|
|
|
WRONG_PASSWORD_2 = 12,
|
|
|
|
WRONG_PASSWORD_3 = 13, // MAX_RETRY = 4
|
|
|
|
NO_RESPONSE
|
|
|
|
};
|
|
|
|
|
2014-07-03 07:08:01 +00:00
|
|
|
void FormatCaData(const uint8_t *aCaData, int aCaDataLength,
|
|
|
|
const char *aName, const uint8_t **aFormatData,
|
2015-02-28 13:54:24 +00:00
|
|
|
size_t *aFormatDataLength);
|
2014-07-03 07:08:01 +00:00
|
|
|
|
|
|
|
ResponseCode getCertificate(const char *aCertName, const uint8_t **aCertData,
|
2015-02-28 13:54:24 +00:00
|
|
|
size_t *aCertDataLength);
|
|
|
|
ResponseCode getPrivateKey(const char *aKeyName, const uint8_t **aKeyData,
|
|
|
|
size_t *aKeyDataLength);
|
|
|
|
ResponseCode getPublicKey(const char *aKeyName, const uint8_t **aKeyData,
|
|
|
|
size_t *aKeyDataLength);
|
|
|
|
ResponseCode signData(const char *aKeyName, const uint8_t *data, size_t length,
|
|
|
|
uint8_t **out, size_t *outLength);
|
2014-07-03 07:08:01 +00:00
|
|
|
|
|
|
|
bool checkPermission(uid_t uid);
|
|
|
|
|
2013-10-01 04:09:56 +00:00
|
|
|
static const int MAX_PARAM = 2;
|
|
|
|
static const int KEY_SIZE = ((NAME_MAX - 15) / 2);
|
|
|
|
static const int VALUE_SIZE = 32768;
|
|
|
|
static const int PASSWORD_SIZE = VALUE_SIZE;
|
|
|
|
|
|
|
|
static const int CA_LINE_SIZE = 64;
|
|
|
|
|
|
|
|
struct ProtocolCommand {
|
|
|
|
int8_t command;
|
|
|
|
int paramNum;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct ProtocolCommand commands[] = {
|
|
|
|
{'g', 1}, // Get CA, command "g CERT_NAME"
|
|
|
|
{ 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ProtocolParam{
|
|
|
|
uint length;
|
|
|
|
int8_t data[VALUE_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
STATE_IDLE,
|
|
|
|
STATE_READ_PARAM_LEN,
|
|
|
|
STATE_READ_PARAM_DATA,
|
|
|
|
STATE_PROCESSING
|
|
|
|
} ProtocolHandlerState;
|
|
|
|
|
2015-05-26 11:24:20 +00:00
|
|
|
class KeyStore final
|
|
|
|
: public StreamSocketConsumer
|
2015-05-26 11:24:20 +00:00
|
|
|
, public ListenSocketConsumer
|
2015-05-26 11:24:20 +00:00
|
|
|
, public nsNSSShutDownObject
|
2013-10-01 04:09:56 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-12-03 15:26:29 +00:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(KeyStore)
|
|
|
|
|
2013-10-01 04:09:56 +00:00
|
|
|
KeyStore();
|
|
|
|
|
|
|
|
void Shutdown();
|
|
|
|
|
2015-02-28 13:54:24 +00:00
|
|
|
protected:
|
|
|
|
virtual void virtualDestroyNSSReference() {}
|
|
|
|
|
2013-10-01 04:09:56 +00:00
|
|
|
private:
|
2014-12-03 15:26:29 +00:00
|
|
|
enum SocketType {
|
|
|
|
LISTEN_SOCKET,
|
|
|
|
STREAM_SOCKET
|
|
|
|
};
|
|
|
|
|
|
|
|
~KeyStore();
|
|
|
|
|
2013-10-01 04:09:56 +00:00
|
|
|
struct {
|
|
|
|
ProtocolHandlerState state;
|
|
|
|
uint8_t command;
|
|
|
|
struct ProtocolParam param[MAX_PARAM];
|
|
|
|
int paramCount;
|
|
|
|
const struct ProtocolCommand *commandPattern;
|
|
|
|
} mHandlerInfo;
|
|
|
|
void ResetHandlerInfo();
|
|
|
|
void Listen();
|
|
|
|
|
2015-04-23 11:48:47 +00:00
|
|
|
bool CheckSize(UnixSocketBuffer *aMessage, size_t aExpectSize);
|
|
|
|
ResponseCode ReadCommand(UnixSocketBuffer *aMessage);
|
|
|
|
ResponseCode ReadLength(UnixSocketBuffer *aMessage);
|
|
|
|
ResponseCode ReadData(UnixSocketBuffer *aMessage);
|
2013-10-01 04:09:56 +00:00
|
|
|
void SendResponse(ResponseCode response);
|
|
|
|
void SendData(const uint8_t *data, int length);
|
|
|
|
|
2015-05-26 11:24:20 +00:00
|
|
|
// Methods for |StreamSocketConsumer|
|
|
|
|
//
|
|
|
|
|
|
|
|
void ReceiveSocketData(int aIndex,
|
|
|
|
nsAutoPtr<UnixSocketBuffer>& aMessage) override;
|
|
|
|
void OnConnectSuccess(int aIndex) override;
|
|
|
|
void OnConnectError(int aIndex) override;
|
|
|
|
void OnDisconnect(int aIndex) override;
|
|
|
|
|
2013-10-01 04:09:56 +00:00
|
|
|
bool mShutdown;
|
2014-12-03 15:26:29 +00:00
|
|
|
|
|
|
|
nsRefPtr<ListenSocket> mListenSocket;
|
|
|
|
nsRefPtr<StreamSocket> mStreamSocket;
|
2013-10-01 04:09:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_ipc_KeyStore_h
|