int main()
{
    struct Allocator* alloc = MallocAllocator_new(1<<20);

    struct TestFramework* tf =
        TestFramework_setUp("\xad\x7e\xa3\x26\xaa\x01\x94\x0a\x25\xbc\x9e\x01\x26\x22\xdb\x69"
                            "\x4f\xd9\xb4\x17\x7c\xf3\xf8\x91\x16\xf3\xcf\xe8\x5c\x80\xe1\x4a",
                            alloc, NULL, NULL, NULL);

    CryptoAuth_addUser(String_CONST("passwd"), 1, String_CONST("TEST"), tf->cryptoAuth);

    struct Message* message;
    struct Interface iface = {
        .sendMessage = messageFromInterface,
        .senderContext = &message,
        .allocator = alloc
    };
    SwitchCore_setRouterInterface(&iface, tf->switchCore);


    ////////////////////////

    int ret = reconnectionNewEndpointTest(tf->ifController,
                                          tf->publicKey,
                                          &message,
                                          alloc,
                                          tf->eventBase,
                                          tf->logger,
                                          &iface,
                                          tf->rand);
    Allocator_free(alloc);
    return ret;
}
Esempio n. 2
0
static void authorizedPassword(String* passwd,
                               int64_t* authType,
                               int64_t* trust,
                               uint32_t index,
                               struct Context* ctx)
{
    if (!(passwd && authType && trust)) {
        fprintf(stderr,
                "authorizedPasswords[%u] is must specify authType, password, and trust.\n",
                index);
        exit(-1);
    }
    if (*authType < 1 || *authType > 255) {
        fprintf(stderr,
                "authorizedPasswords[%u] auth must be between 1 and 255 inclusive.\n",
                index);
        exit(-1);
    }
    if (*trust < 0) {
        fprintf(stderr, "authorizedPasswords[%u] trust cannot be negative.\n", index);
        exit(-1);
    }
    printf("adding authorized password.\n");
    struct User* u = ctx->allocator->malloc(sizeof(struct User), ctx->allocator);
    u->trust = (uint64_t) *trust;
    CryptoAuth_addUser(passwd, *authType, u, ctx->ca);
}
Esempio n. 3
0
static void add(Dict* args, void* vcontext, String* txid)
{
    struct Context* context = (struct Context*) vcontext;

    String* passwd = Dict_getString(args, String_CONST("password"));
    int64_t* authType = Dict_getInt(args, String_CONST("authType"));
    int64_t one = 1;
    if (!authType) {
        authType = &one;
    } else if (*authType < 1 || *authType > 255) {
        sendResponse(String_CONST("Specified auth type is not supported."), context->admin, txid);
        return;
    }

    int32_t ret = CryptoAuth_addUser(passwd, *authType, context, context->ca);

    switch (ret) {
        case 0:
            sendResponse(String_CONST("none"), context->admin, txid);
            break;
        case CryptoAuth_addUser_INVALID_AUTHTYPE:
            sendResponse(String_CONST("Specified auth type is not supported."),
                         context->admin, txid);
            break;
        case CryptoAuth_addUser_OUT_OF_SPACE:
            sendResponse(String_CONST("Out of memory to store password."), context->admin, txid);
            break;
        case CryptoAuth_addUser_DUPLICATE:
            sendResponse(String_CONST("Password already added."), context->admin, txid);
            break;
        default:
            sendResponse(String_CONST("Unknown error."), context->admin, txid);
    }
}
Esempio n. 4
0
static void testGetUsers()
{
    struct Allocator* allocator = MallocAllocator_new(1<<20);
    struct EventBase* base = EventBase_new(allocator);
    struct CryptoAuth* ca =
        CryptoAuth_new(allocator, NULL, base, NULL, evilRandom(allocator, NULL));
    List* users = NULL;

    users = CryptoAuth_getUsers(ca, allocator);
    Assert_true(List_size(users) == 0);

    CryptoAuth_addUser(String_CONST("pass1"), String_CONST("user1"), ca);
    users = CryptoAuth_getUsers(ca, allocator);
    Assert_true(List_size(users) == 1);
    Assert_true(String_equals(String_CONST("user1"), List_getString(users,0)));

    CryptoAuth_addUser(String_CONST("pass2"), String_CONST("user2"), ca);
    users = CryptoAuth_getUsers(ca, allocator);
    Assert_true(List_size(users) == 2);
    Assert_true(String_equals(String_CONST("user2"),List_getString(users,0)));
    Assert_true(String_equals(String_CONST("user1"),List_getString(users,1)));

    Allocator_free(allocator);
}
Esempio n. 5
0
void TestFramework_linkNodes(struct TestFramework* client,
                             struct TestFramework* server,
                             bool beacon)
{
    // ifaceA is the client, ifaceB is the server
    struct TestFramework_Link* link =
        Allocator_calloc(client->alloc, sizeof(struct TestFramework_Link), 1);
    Identity_set(link);
    link->clientIf.send = sendClient;
    link->serverIf.send = sendServer;
    link->client = client;
    link->server = server;

    struct InterfaceController_Iface* clientIci = InterfaceController_newIface(
        client->nc->ifController, String_CONST("client"), client->alloc);
    link->clientIfNum = clientIci->ifNum;
    Iface_plumb(&link->clientIf, &clientIci->addrIf);

    struct InterfaceController_Iface* serverIci = InterfaceController_newIface(
        server->nc->ifController, String_CONST("server"), server->alloc);
    link->serverIfNum = serverIci->ifNum;
    Iface_plumb(&link->serverIf, &serverIci->addrIf);

    if (beacon) {
        int ret = InterfaceController_beaconState(client->nc->ifController,
                                           link->clientIfNum,
                                           InterfaceController_beaconState_newState_ACCEPT);
        Assert_true(!ret);

        ret = InterfaceController_beaconState(server->nc->ifController,
                                       link->serverIfNum,
                                       InterfaceController_beaconState_newState_SEND);
        Assert_true(!ret);
    } else {
        // Except that it has an authorizedPassword added.
        CryptoAuth_addUser(String_CONST("abcdefg123"), String_CONST("TEST"), server->nc->ca);

        // Client has pubKey and passwd for the server.
        InterfaceController_bootstrapPeer(client->nc->ifController,
                                   link->clientIfNum,
                                   server->publicKey,
                                   Sockaddr_LOOPBACK,
                                   String_CONST("abcdefg123"),
                                   NULL,
                                   NULL,
                                   client->alloc);
    }
}
int main()
{
    struct Allocator* alloc = MallocAllocator_new(1<<20);

    struct Writer* logwriter = FileWriter_new(stdout, alloc);
    struct Log* logger = &(struct Log) { .writer = logwriter };

    struct event_base* eventBase = event_base_new();

    struct CryptoAuth* ca = CryptoAuth_new(NULL, alloc, NULL, eventBase, logger);
    uint8_t publicKey[32];
    CryptoAuth_getPublicKey(publicKey, ca);
    CryptoAuth_addUser(String_CONST("passwd"), 1, (void*)0x01, ca);

    struct SwitchCore* switchCore = SwitchCore_new(logger, alloc);
    struct Message* message;
    struct Interface iface = {
        .sendMessage = messageFromInterface,
        .senderContext = &message,
        .allocator = alloc
    };
    SwitchCore_setRouterInterface(&iface, switchCore);

    // These are unused.
    struct DHTModuleRegistry* registry = DHTModuleRegistry_new(alloc);
    struct RouterModule* rm =
        RouterModule_register(registry, alloc, publicKey, eventBase, logger, NULL);

    struct InterfaceController* ifController =
        DefaultInterfaceController_new(ca, switchCore, rm, logger, eventBase, NULL, alloc);

    ////////////////////////

    return reconnectionNewEndpointTest(ifController,
                                       publicKey,
                                       &message,
                                       alloc,
                                       eventBase,
                                       logger,
                                       &iface);
}
Esempio n. 7
0
static void add(Dict* args, void* vcontext, String* txid)
{
    struct Context* context = (struct Context*) vcontext;

    String* passwd = Dict_getString(args, String_CONST("password"));
    int64_t* authType = Dict_getInt(args, String_CONST("authType"));

    if (!(passwd && authType)) {
        sendResponse(String_CONST("Must specify authType, and password."), context->admin, txid);
    } else if (*authType < 1 || *authType > 255) {
        sendResponse(String_CONST("Auth must be between 1 and 255 inclusive."), context->admin, txid);
    } else {
        struct User* u = context->allocator->malloc(sizeof(struct User), context->allocator);
        // At some point this will be implemented...
        u->trust = 0;
        int32_t ret = CryptoAuth_addUser(passwd, *authType, u, context->ca);

        switch (ret) {
            case 0:
                sendResponse(String_CONST("none"), context->admin, txid);
                break;
            case CryptoAuth_addUser_INVALID_AUTHTYPE:
                sendResponse(String_CONST("Specified auth type is not supported."),
                             context->admin,
                             txid);
                break;
            case CryptoAuth_addUser_OUT_OF_SPACE:
                sendResponse(String_CONST("Out of memory to store password."), context->admin, txid);
                break;
            case CryptoAuth_addUser_DUPLICATE:
                sendResponse(String_CONST("Password already added."), context->admin, txid);
                break;
            default:
                sendResponse(String_CONST("Unknown error."), context->admin, txid);
        };
    }
}
Esempio n. 8
0
        .pingInterval = (switchPinger)
            ? Timeout_setInterval(pingCallback,
                                  out,
                                  PING_INTERVAL_MILLISECONDS,
                                  eventBase,
                                  alloc)
            : NULL

    }), sizeof(struct InterfaceController_pvt));
    Identity_set(out);

    out->icis = ArrayList_OfIfaces_new(alloc);

    out->eventEmitterIf.send = incomingFromEventEmitterIf;
    EventEmitter_regCore(ee, &out->eventEmitterIf, PFChan_Pathfinder_PEERS);

    // Add the beaconing password.
    Random_bytes(rand, out->beacon.password, Headers_Beacon_PASSWORD_LEN);
    String strPass = { .bytes=(char*)out->beacon.password, .len=Headers_Beacon_PASSWORD_LEN };
    int ret = CryptoAuth_addUser(&strPass, String_CONST("Local Peers"), ca);
    if (ret) {
        Log_warn(logger, "CryptoAuth_addUser() returned [%d]", ret);
    }
    Bits_memcpy(out->beacon.publicKey, ca->publicKey, 32);
    out->beacon.version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);

    Timeout_setTimeout(beaconInterval, out, BEACON_INTERVAL, eventBase, alloc);

    return &out->pub;
}
Esempio n. 9
0
static void switching(struct Context* ctx)
{
    Log_info(ctx->log, "Setting up salsa20/poly1305 benchmark (encryption and decryption only)");
    struct Allocator* alloc = Allocator_child(ctx->alloc);;

    struct SwitchingContext* sc = Allocator_calloc(alloc, sizeof(struct SwitchingContext), 1);
    Identity_set(sc);
    sc->benchmarkCtx = ctx;
    sc->aliceIf.send = aliceToBob;
    sc->bobIf.send = bobToAlice;
    sc->aliceCtrlIf.send = aliceCtrlRecv;

    struct NetCore* alice = NetCore_new(SECRETA, alloc, ctx->base, ctx->rand, ctx->log);
    struct InterfaceController_Iface* aliceIci =
        InterfaceController_newIface(alice->ifController, String_CONST("alice"), alloc);
    Iface_plumb(&sc->aliceIf, &aliceIci->addrIf);

    struct NetCore* bob = NetCore_new(SECRETB, alloc, ctx->base, ctx->rand, ctx->log);
    struct InterfaceController_Iface* bobIci =
        InterfaceController_newIface(bob->ifController, String_CONST("bob"), alloc);
    Iface_plumb(&sc->bobIf, &bobIci->addrIf);

    CryptoAuth_addUser(String_CONST("abcdefg123"), 1, String_CONST("TEST"), bob->ca);

    // Client has pubKey and passwd for the server.
    int ret = InterfaceController_bootstrapPeer(alice->ifController,
                                                aliceIci->ifNum,
                                                bob->ca->publicKey,
                                                Sockaddr_LOOPBACK,
                                                String_CONST("abcdefg123"),
                                                alloc);
    Assert_true(!ret);

    Iface_unplumb(alice->switchAdapter->controlIf.connectedIf, &alice->switchAdapter->controlIf);
    Iface_plumb(&alice->switchAdapter->controlIf, &sc->aliceCtrlIf);

    struct Message* msg = Message_new(Control_Ping_MIN_SIZE + Control_Header_SIZE, 256, alloc);
    struct Control_Header* ch = (struct Control_Header*) msg->bytes;
    struct Control_Ping* ping = (struct Control_Ping*) &ch[1];
    ping->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
    Message_push32(msg, 0xffffffff, NULL);
    uint32_t* handle_be = (uint32_t*)msg->bytes;
    Message_push(msg, NULL, SwitchHeader_SIZE, NULL);
    struct SwitchHeader* sh = (struct SwitchHeader*) msg->bytes;
    // TODO(cjd): this will fail with a different encoding scheme
    sh->label_be = Endian_hostToBigEndian64(0x13);

    for (int i = 1; i < 6; i++) {
        ping->magic = Control_Ping_MAGIC;
        ch->type_be = Control_PING_be;
        ch->checksum_be = 0;
        ch->checksum_be = Checksum_engine((void*)ch, Control_Ping_MIN_SIZE + Control_Header_SIZE);

        Iface_send(&sc->aliceCtrlIf, msg);

        Assert_true(sc->msgCount == i);
        Assert_true(msg->bytes == (void*)sh);
        Assert_true(ping->magic == Control_Pong_MAGIC);
        Assert_true(ch->type_be = Control_PONG_be);
        Assert_true(!Checksum_engine((void*)ch, Control_Ping_MIN_SIZE + Control_Header_SIZE));
    }

    *handle_be = 0xfffffff0;
    int count = 1000000;
    begin(ctx, "Switching", count, "packets");
    for (int i = 0; i < count; i++) {
        sh->versionAndLabelShift = SwitchHeader_CURRENT_VERSION << 6;
        Iface_send(&sc->aliceCtrlIf, msg);
        Assert_true(msg->bytes == (void*)sh);
    }
    done(ctx);

    Log_info(ctx->log, "DONE");
    Allocator_free(alloc);
}