1a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root/*
2a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * Copyright (C) 2009 The Android Open Source Project
3a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root *
4a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * Licensed under the Apache License, Version 2.0 (the "License");
5a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * you may not use this file except in compliance with the License.
6a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * You may obtain a copy of the License at
7a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root *
8a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root *      http://www.apache.org/licenses/LICENSE-2.0
9a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root *
10a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * Unless required by applicable law or agreed to in writing, software
11a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * distributed under the License is distributed on an "AS IS" BASIS,
12a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * See the License for the specific language governing permissions and
14a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * limitations under the License.
15a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root */
16a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
17a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include <stdio.h>
18a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include <stdint.h>
19a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include <string.h>
20a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include <sys/types.h>
21a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include <sys/socket.h>
22a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
23a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include <cutils/sockets.h>
24a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
25a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include "keystore.h"
26a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
27a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Rootstatic const char* responses[] = {
28a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    NULL,
29a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [NO_ERROR]           = */ "No error",
30a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [LOCKED]             = */ "Locked",
31a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [UNINITIALIZED]      = */ "Uninitialized",
32a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [SYSTEM_ERROR]       = */ "System error",
33a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [PROTOCOL_ERROR]     = */ "Protocol error",
34a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [PERMISSION_DENIED]  = */ "Permission denied",
35a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [KEY_NOT_FOUND]      = */ "Key not found",
36a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [VALUE_CORRUPTED]    = */ "Value corrupted",
37a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [UNDEFINED_ACTION]   = */ "Undefined action",
38a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [WRONG_PASSWORD]     = */ "Wrong password (last chance)",
39a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [WRONG_PASSWORD + 1] = */ "Wrong password (2 tries left)",
40a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [WRONG_PASSWORD + 2] = */ "Wrong password (3 tries left)",
41a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    /* [WRONG_PASSWORD + 3] = */ "Wrong password (4 tries left)",
42a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root};
43a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
44a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Rootint main(int argc, char* argv[])
45a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root{
46a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    if (argc < 2) {
47a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        printf("Usage: %s action [parameter ...]\n", argv[0]);
48a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        return 0;
49a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    }
50a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
51a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    int sock = socket_local_client("keystore", ANDROID_SOCKET_NAMESPACE_RESERVED,
52a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root                                   SOCK_STREAM);
53a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    if (sock == -1) {
54a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        puts("Failed to connect");
55a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        return 1;
56a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    }
57a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
58a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    send(sock, argv[1], 1, 0);
59a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    uint8_t bytes[65536];
60a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    for (int i = 2; i < argc; ++i) {
61a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        uint16_t length = strlen(argv[i]);
62a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        bytes[0] = length >> 8;
63a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        bytes[1] = length;
64a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        send(sock, &bytes, 2, 0);
65a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        send(sock, argv[i], length, 0);
66a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    }
67a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    shutdown(sock, SHUT_WR);
68a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
69a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    uint8_t code;
70a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    if (recv(sock, &code, 1, 0) != 1) {
71a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        puts("Failed to receive");
72a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        return 1;
73a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    }
74a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    printf("%d %s\n", code , responses[code] ? responses[code] : "Unknown");
75a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    int i;
76a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    while ((i = recv(sock, &bytes[0], 1, 0)) == 1) {
77a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        int length;
78a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        int offset;
79a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        if ((i = recv(sock, &bytes[1], 1, 0)) != 1) {
80a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root            puts("Failed to receive");
81a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root            return 1;
82a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        }
83a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        length = bytes[0] << 8 | bytes[1];
84a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        for (offset = 0; offset < length; offset += i) {
85a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root            i = recv(sock, &bytes[offset], length - offset, 0);
86a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root            if (i <= 0) {
87a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root                puts("Failed to receive");
88a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root                return 1;
89a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root            }
90a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        }
91a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        fwrite(bytes, 1, length, stdout);
92a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        puts("");
93a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    }
94a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    return 0;
95a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root}
96