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