aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/ccnx_KeystoreUtilities.c
blob: 1ae94ef6b3758d0cc1f12169cd2cd8246b18a9f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>

#include <pwd.h>
#include <unistd.h>

#include <sys/types.h>
#include <pwd.h>
#include <sys/stat.h>

#include <sys/param.h>

#include <LongBow/runtime.h>

#include <parc/algol/parc_Memory.h>
#include <ccnx/common/ccnx_KeystoreUtilities.h>
#include <parc/security/parc_Pkcs12KeyStore.h>
#include <parc/security/parc_PublicKeySigner.h>
#include <parc/security/parc_Signer.h>


struct keystore_params {
    char filename[1024];
    char password[1024];
    PARCSigner *signer;
};

#define OPT_KEYSTORE 'k'
#define OPT_PASSWORD 'p'
#define OPT_BITS     'b'
#define OPT_DAYS     'y'

static const char *emptyPassword = "";

static char *
ccnxKeystoreUtilities_ConstructPath(const char *dir, const char *file)
{
    char *path = parcMemory_AllocateAndClear(MAXPATHLEN);
    assertNotNull(path, "parcMemory_AllocateAndClear(%u) returned NULL", MAXPATHLEN);
    strncpy(path, dir, MAXPATHLEN);
    strncat(path, "/", MAXPATHLEN);
    strncat(path, file, MAXPATHLEN);
    return path;
}

static char *
ccnxKeystoreUtilities_HomeDirectoryFromEnv()
{
    char *home = getenv("HOME");
    if (home != NULL) {
        home = parcMemory_StringDuplicate(home, strlen(home) + 1);
    }
    return home;
}

static char *
ccnxKeystoreUtilities_HomeDirectoryFromPasswd()
{
    struct passwd *pw = getpwuid(getuid());
    const char *homedir = pw->pw_dir;
    return parcMemory_StringDuplicate(homedir, strlen(homedir) + 1);
}

static KeystoreParams *
ccnxKeystoreUtilities_OpenFromPath(const char *path, const char *password)
{
    KeystoreParams *params = NULL;

    // If the file exists, try to open it as a keystore
    struct stat filestat;
    int failure = stat(path, &filestat);
    if (!failure) {
        PARCPkcs12KeyStore *keyStore = parcPkcs12KeyStore_Open(path, password, PARCCryptoHashType_SHA256);
        PARCKeyStore *publicKeyStore = parcKeyStore_Create(keyStore, PARCPkcs12KeyStoreAsKeyStore);
        parcPkcs12KeyStore_Release(&keyStore);
        PARCPublicKeySigner *pksigner = parcPublicKeySigner_Create(publicKeyStore, PARCSigningAlgorithm_RSA, PARCCryptoHashType_SHA256);
        PARCSigner *signer = parcSigner_Create(pksigner, PARCPublicKeySignerAsSigner);
        parcPublicKeySigner_Release(&pksigner);

        if (signer) {
            params = ccnxKeystoreUtilities_Create(signer, path, password);

            parcSigner_Release(&signer);
            parcKeyStore_Release(&publicKeyStore);
        }
    }

    return params;
}

static KeystoreParams *
ccnxKeystoreUtilities_CreateInPath(const char *path, const char *password, int keystoreBits, int keystoreDays)
{
    KeystoreParams *params = NULL;

    bool success = parcPkcs12KeyStore_CreateFile(path, password, "ccnxuser", keystoreBits, keystoreDays);
    if (success) {
        PARCPkcs12KeyStore *keyStore = parcPkcs12KeyStore_Open(path, password, PARCCryptoHashType_SHA256);
        PARCKeyStore *publicKeyStore = parcKeyStore_Create(keyStore, PARCPkcs12KeyStoreAsKeyStore);
        parcPkcs12KeyStore_Release(&keyStore);
        PARCPublicKeySigner *pksigner = parcPublicKeySigner_Create(publicKeyStore, PARCSigningAlgorithm_RSA, PARCCryptoHashType_SHA256);
        PARCSigner *signer = parcSigner_Create(pksigner, PARCPublicKeySignerAsSigner);
        parcPublicKeySigner_Release(&pksigner);

        if (signer) {
            params = ccnxKeystoreUtilities_Create(signer, path, password);

            parcSigner_Release(&signer);
            parcKeyStore_Release(&publicKeyStore);
        }
    }
    return params;
}


static char *
ccnxKeystoreUtilities_GetHomeDirectory()
{
    char *homedir = ccnxKeystoreUtilities_HomeDirectoryFromEnv();
    if (homedir == NULL) {
        homedir = ccnxKeystoreUtilities_HomeDirectoryFromPasswd();
    }
    return homedir;
}

static KeystoreParams *
ccnxKeystoreUtilities_OpenFromHomeDirectory(const char *password)
{
    KeystoreParams *params = NULL;

    char *homedir = ccnxKeystoreUtilities_GetHomeDirectory();
    char *ccnxdir = ccnxKeystoreUtilities_ConstructPath(homedir, ".ccnx");


    char *path = ccnxKeystoreUtilities_ConstructPath(ccnxdir, ".ccnx_keystore.p12");
    params = ccnxKeystoreUtilities_OpenFromPath(path, password);
    parcMemory_Deallocate((void **) &path);

    if (params == NULL) {
        // try the older filename
        char *path = ccnxKeystoreUtilities_ConstructPath(ccnxdir, ".ccnx_keystore");
        params = ccnxKeystoreUtilities_OpenFromPath(path, password);
        parcMemory_Deallocate((void **) &path);
    }

    parcMemory_Deallocate((void **) &ccnxdir);
    parcMemory_Deallocate((void **) &homedir);
    return params;
}

static KeystoreParams *
ccnxKeystoreUtilities_CreateInHomeDirectory(const char *keystorePassword, int keystoreBits, int keystoreDays)
{
    char *homedir = ccnxKeystoreUtilities_GetHomeDirectory();
    char *ccnxdir = ccnxKeystoreUtilities_ConstructPath(homedir, ".ccnx");
    // Needs to check the return value to ensure that it was created. See case 1003.
    mkdir(ccnxdir, 0700);

    char *path = ccnxKeystoreUtilities_ConstructPath(ccnxdir, ".ccnx_keystore.p12");
    KeystoreParams *params = ccnxKeystoreUtilities_CreateInPath(path, keystorePassword, keystoreBits, keystoreDays);

    parcMemory_Deallocate((void **) &path);
    parcMemory_Deallocate((void **) &ccnxdir);
    parcMemory_Deallocate((void **) &homedir);
    return params;
}

#ifdef _ANDROID_
static char *
getpass(const char *prompt)
{
    trapNotImplemented("getpass() is not implemented on Android. See BugzId: 3864");
}
#endif

/**
 * Read a password, then zero the static memory
 *
 *   <#Discussion#>
 *
 * @param <#param1#>
 * @return An allocated string, use <code>parcMemory_Deallocate()</code> on it.
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static char *
_readPassword(const char *prompt)
{
    char *staticBuffer = getpass(prompt);
    char *password = parcMemory_StringDuplicate(staticBuffer, strlen(staticBuffer) + 1);
    memset(staticBuffer, 0, strlen(staticBuffer));
    return password;
}

KeystoreParams *
ccnxKeystoreUtilities_OpenFile(const char *keystoreFile, const char *keystorePassword)
{
    if (keystorePassword == NULL) {
        keystorePassword = emptyPassword;
    }

    KeystoreParams *params = NULL;

    if (keystoreFile == NULL) {
        params = ccnxKeystoreUtilities_OpenFromHomeDirectory(keystorePassword);
    } else {
        params = ccnxKeystoreUtilities_OpenFromPath(keystoreFile, keystorePassword);
    }

    return params;
}

KeystoreParams *
ccnxKeystoreUtilities_CreateFile(const char *keystoreFile, const char *keystorePassword, int keystoreBits, int keystoreDays)
{
    if (keystorePassword == NULL) {
        keystorePassword = emptyPassword;
    }

    KeystoreParams *params = NULL;

    if (keystoreFile == NULL) {
        params = ccnxKeystoreUtilities_CreateInHomeDirectory(keystorePassword, keystoreBits, keystoreDays);
    } else {
        params = ccnxKeystoreUtilities_CreateInPath(keystoreFile, keystorePassword, keystoreBits, keystoreDays);
    }

    return params;
}

KeystoreParams *
ccnxKeystoreUtilities_Create(PARCSigner *signer, const char *path, const char *password)
{
    KeystoreParams *params = parcMemory_AllocateAndClear(sizeof(KeystoreParams));
    assertNotNull(params, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(KeystoreParams));
    params->signer = parcSigner_Acquire(signer);
    strncpy(params->filename, path, 1024);
    strncpy(params->password, password, 1024);
    return params;
}

void
keystoreParams_Destroy(KeystoreParams **paramsPtr)
{
    KeystoreParams *params = *paramsPtr;
    if (params->signer != NULL) {
        parcSigner_Release(&params->signer);
    }
    parcMemory_Deallocate((void **) &params);
    *paramsPtr = NULL;
}

char *
ccnxKeystoreUtilities_ReadPassword(void)
{
    return _readPassword("Password: ");
}

bool
ccnxKeystoreUtilities_ConfirmPassword(const char *mustEqualPassword)
{
    bool equals = false;

    char *b = _readPassword("Confirm  : ");
    if (strcmp(mustEqualPassword, b) == 0) {
        equals = true;
    }

    memset(b, 0, strlen(b));
    parcMemory_Deallocate((void **) &b);
    return equals;
}

const char *
ccnxKeystoreUtilities_GetFileName(const KeystoreParams *params)
{
    return params->filename;
}

const char *
ccnxKeystoreUtilities_GetPassword(const KeystoreParams *params)
{
    return params->password;
}