|
Added
Link Here
|
| 1 |
/* |
| 2 |
* Copyright (c) 2001 Markus Friedl. All rights reserved. |
| 3 |
* Copyright (c) 2007 Red Hat, Inc. All rights reserved. |
| 4 |
* Copyright (c) 2009 Pierre Ossman for Cendio AB |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "includes.h" |
| 28 |
#ifdef HAVE_LIBNSS |
| 29 |
|
| 30 |
#include <sys/types.h> |
| 31 |
|
| 32 |
#include <stdarg.h> |
| 33 |
#include <string.h> |
| 34 |
#include <unistd.h> |
| 35 |
|
| 36 |
#include <openssl/evp.h> |
| 37 |
|
| 38 |
#include <nss.h> |
| 39 |
#include <keyhi.h> |
| 40 |
#include <pk11pub.h> |
| 41 |
#include <cert.h> |
| 42 |
#include <secmod.h> |
| 43 |
#include <secerr.h> |
| 44 |
|
| 45 |
#include "xmalloc.h" |
| 46 |
#include "key.h" |
| 47 |
#include "log.h" |
| 48 |
#include "misc.h" |
| 49 |
#include "nsskeys.h" |
| 50 |
#include "pathnames.h" |
| 51 |
|
| 52 |
static char * |
| 53 |
password_cb(PK11SlotInfo *slot, PRBool retry, void *arg) |
| 54 |
{ |
| 55 |
char *password = arg; |
| 56 |
if (retry || password == NULL) |
| 57 |
return NULL; |
| 58 |
|
| 59 |
return PL_strdup(password); |
| 60 |
} |
| 61 |
|
| 62 |
int |
| 63 |
nss_init(PK11PasswordFunc pwfn) |
| 64 |
{ |
| 65 |
char *dbpath; |
| 66 |
char buf[MAXPATHLEN]; |
| 67 |
|
| 68 |
if (NSS_IsInitialized()) |
| 69 |
return 0; |
| 70 |
|
| 71 |
if ((dbpath=getenv("NSS_DB_PATH")) == NULL) { |
| 72 |
struct passwd *pw; |
| 73 |
if ((pw = getpwuid(getuid())) == NULL || |
| 74 |
pw->pw_dir == NULL) { |
| 75 |
return -1; |
| 76 |
} |
| 77 |
snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, |
| 78 |
_PATH_SSH_USER_DIR); |
| 79 |
dbpath = buf; |
| 80 |
} |
| 81 |
|
| 82 |
if (NSS_Init(dbpath) != SECSuccess) { |
| 83 |
debug("Failed to initialize NSS library. Attempting without DB..."); |
| 84 |
if (NSS_NoDB_Init(NULL) != SECSuccess) |
| 85 |
return -1; |
| 86 |
} |
| 87 |
|
| 88 |
if (pwfn == NULL) { |
| 89 |
pwfn = password_cb; |
| 90 |
} |
| 91 |
|
| 92 |
PK11_SetPasswordFunc(pwfn); |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
int |
| 98 |
nss_load_module(const char *modpath) |
| 99 |
{ |
| 100 |
char spec[MAXPATHLEN + 40]; |
| 101 |
SECMODModule *module; |
| 102 |
|
| 103 |
debug("loading PKCS#11 module '%s'", modpath); |
| 104 |
|
| 105 |
snprintf(spec, sizeof(spec), "library=\"%s\" name=\"Foobar\"", modpath); |
| 106 |
module = SECMOD_LoadUserModule(spec, NULL, PR_FALSE); |
| 107 |
if (!module || !module->loaded) { |
| 108 |
if (module) |
| 109 |
SECMOD_DestroyModule(module); |
| 110 |
return -1; |
| 111 |
} |
| 112 |
|
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
static Key * |
| 117 |
make_key_from_privkey(SECKEYPrivateKey *privk, char *password) |
| 118 |
{ |
| 119 |
Key *k; |
| 120 |
switch (SECKEY_GetPrivateKeyType(privk)) { |
| 121 |
case rsaKey: |
| 122 |
k = key_new_nss(KEY_RSA); |
| 123 |
break; |
| 124 |
case dsaKey: |
| 125 |
k = key_new_nss(KEY_DSA); |
| 126 |
break; |
| 127 |
default: |
| 128 |
return NULL; |
| 129 |
} |
| 130 |
k->nss->pubk = SECKEY_ConvertToPublicKey(privk); |
| 131 |
if (k->nss->pubk != NULL) { |
| 132 |
k->nss->privk = SECKEY_CopyPrivateKey(privk); |
| 133 |
} |
| 134 |
if (k->nss->privk != NULL) { |
| 135 |
if (password != NULL) { |
| 136 |
k->nss->privk->wincx = xstrdup(password); |
| 137 |
} |
| 138 |
return k; |
| 139 |
} |
| 140 |
key_free(k); |
| 141 |
return NULL; |
| 142 |
} |
| 143 |
|
| 144 |
static Key ** |
| 145 |
add_key_to_list(Key *k, Key **keys, size_t *i, size_t *allocated) |
| 146 |
{ |
| 147 |
if (*allocated < *i + 2) { |
| 148 |
*allocated += 16; |
| 149 |
keys = xrealloc(keys, *allocated, sizeof(k)); |
| 150 |
} |
| 151 |
keys[*i] = k; |
| 152 |
(*i)++; |
| 153 |
keys[*i] = NULL; |
| 154 |
return keys; |
| 155 |
} |
| 156 |
|
| 157 |
static int |
| 158 |
nss_convert_pubkey(Key *k) |
| 159 |
{ |
| 160 |
u_char *n; |
| 161 |
unsigned int len; |
| 162 |
char *p; |
| 163 |
|
| 164 |
switch (k->type) { |
| 165 |
case KEY_RSA: |
| 166 |
n = k->nss->pubk->u.rsa.modulus.data; |
| 167 |
len = k->nss->pubk->u.rsa.modulus.len; |
| 168 |
|
| 169 |
if (BN_bin2bn(n, len, k->rsa->n) == NULL) { |
| 170 |
fatal("nss_convert_pubkey: BN_bin2bn failed"); |
| 171 |
} |
| 172 |
|
| 173 |
n = k->nss->pubk->u.rsa.publicExponent.data; |
| 174 |
len = k->nss->pubk->u.rsa.publicExponent.len; |
| 175 |
|
| 176 |
if (BN_bin2bn(n, len, k->rsa->e) == NULL) { |
| 177 |
fatal("nss_convert_pubkey: BN_bin2bn failed"); |
| 178 |
} |
| 179 |
break; |
| 180 |
case KEY_DSA: |
| 181 |
n = k->nss->pubk->u.dsa.params.prime.data; |
| 182 |
len = k->nss->pubk->u.dsa.params.prime.len; |
| 183 |
|
| 184 |
if (BN_bin2bn(n, len, k->dsa->p) == NULL) { |
| 185 |
fatal("nss_convert_pubkey: BN_bin2bn failed"); |
| 186 |
} |
| 187 |
|
| 188 |
n = k->nss->pubk->u.dsa.params.subPrime.data; |
| 189 |
len = k->nss->pubk->u.dsa.params.subPrime.len; |
| 190 |
|
| 191 |
if (BN_bin2bn(n, len, k->dsa->q) == NULL) { |
| 192 |
fatal("nss_convert_pubkey: BN_bin2bn failed"); |
| 193 |
} |
| 194 |
|
| 195 |
n = k->nss->pubk->u.dsa.params.base.data; |
| 196 |
len = k->nss->pubk->u.dsa.params.base.len; |
| 197 |
|
| 198 |
if (BN_bin2bn(n, len, k->dsa->g) == NULL) { |
| 199 |
fatal("nss_convert_pubkey: BN_bin2bn failed"); |
| 200 |
} |
| 201 |
|
| 202 |
n = k->nss->pubk->u.dsa.publicValue.data; |
| 203 |
len = k->nss->pubk->u.dsa.publicValue.len; |
| 204 |
|
| 205 |
if (BN_bin2bn(n, len, k->dsa->pub_key) == NULL) { |
| 206 |
fatal("nss_convert_pubkey: BN_bin2bn failed"); |
| 207 |
} |
| 208 |
break; |
| 209 |
} |
| 210 |
|
| 211 |
p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX); |
| 212 |
debug("fingerprint %u %s", key_size(k), p); |
| 213 |
xfree(p); |
| 214 |
|
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
static int |
| 219 |
nss_authenticate(PK11SlotInfo *slot, char *password, int pwprompts, char **output) |
| 220 |
{ |
| 221 |
int i, quit; |
| 222 |
|
| 223 |
*output = NULL; |
| 224 |
|
| 225 |
if (!PK11_NeedLogin(slot)) |
| 226 |
return 0; |
| 227 |
|
| 228 |
for (i = 0; i < pwprompts; i++) { |
| 229 |
SECStatus rv; |
| 230 |
CK_TOKEN_INFO info; |
| 231 |
|
| 232 |
rv = PK11_GetTokenInfo(slot, &info); |
| 233 |
if (rv != SECSuccess) { |
| 234 |
error("Failed to get information for token %s", |
| 235 |
PK11_GetTokenName(slot)); |
| 236 |
return -1; |
| 237 |
} |
| 238 |
|
| 239 |
if (info.flags & CKF_USER_PIN_LOCKED) { |
| 240 |
error("Passphrase for token %s is locked", |
| 241 |
PK11_GetTokenName(slot)); |
| 242 |
return -1; |
| 243 |
} |
| 244 |
|
| 245 |
if (info.flags & CKF_USER_PIN_FINAL_TRY) |
| 246 |
debug2("Final passphrase attempt for token %s", |
| 247 |
PK11_GetTokenName(slot)); |
| 248 |
else if (info.flags & CKF_USER_PIN_COUNT_LOW) |
| 249 |
debug2("Previous failed passphrase attempt for token %s", |
| 250 |
PK11_GetTokenName(slot)); |
| 251 |
|
| 252 |
if (password != NULL) |
| 253 |
*output = xstrdup(password); |
| 254 |
else { |
| 255 |
char *prompt; |
| 256 |
if (asprintf(&prompt, "Enter passphrase for token %s: ", |
| 257 |
PK11_GetTokenName(slot)) < 0) |
| 258 |
fatal("password_cb: asprintf failed"); |
| 259 |
*output = read_passphrase(prompt, RP_ALLOW_STDIN); |
| 260 |
} |
| 261 |
|
| 262 |
if (strcmp(*output, "") == 0) { |
| 263 |
debug2("no passphrase given, ignoring slot"); |
| 264 |
quit = 1; |
| 265 |
goto cleanup; |
| 266 |
} |
| 267 |
|
| 268 |
quit = 0; |
| 269 |
|
| 270 |
rv = PK11_Authenticate(slot, PR_TRUE, *output); |
| 271 |
if (rv == SECSuccess) |
| 272 |
return 0; |
| 273 |
|
| 274 |
switch (PORT_GetError()) { |
| 275 |
case SEC_ERROR_BAD_PASSWORD: |
| 276 |
debug2("Incorrect passphrase, try again..."); |
| 277 |
break; |
| 278 |
case SEC_ERROR_INVALID_ARGS: |
| 279 |
case SEC_ERROR_BAD_DATA: |
| 280 |
debug2("Invalid passphrase, try again..."); |
| 281 |
break; |
| 282 |
#if HAVE_SEC_ERROR_LOCKED_PASSWORD |
| 283 |
case SEC_ERROR_LOCKED_PASSWORD: |
| 284 |
error("Unable to authenticate, token passphrase is locked"); |
| 285 |
quit = 1; |
| 286 |
break; |
| 287 |
#endif |
| 288 |
default: |
| 289 |
error("Failure while authenticating against token"); |
| 290 |
quit = 1; |
| 291 |
} |
| 292 |
|
| 293 |
cleanup: |
| 294 |
memset(*output, 0, strlen(*output)); |
| 295 |
xfree(*output); |
| 296 |
*output = NULL; |
| 297 |
|
| 298 |
/* No point in retrying the same password */ |
| 299 |
if (password != NULL) |
| 300 |
break; |
| 301 |
|
| 302 |
if (quit) |
| 303 |
break; |
| 304 |
} |
| 305 |
|
| 306 |
return -1; |
| 307 |
} |
| 308 |
|
| 309 |
static Key ** |
| 310 |
nss_find_privkeys(const char *tokenname, const char *keyname, |
| 311 |
char *password, int pwprompts) |
| 312 |
{ |
| 313 |
Key *k = NULL; |
| 314 |
Key **keys = NULL; |
| 315 |
PK11SlotList *slots; |
| 316 |
PK11SlotListElement *sle; |
| 317 |
size_t allocated = 0; |
| 318 |
size_t i = 0; |
| 319 |
|
| 320 |
if ((slots=PK11_FindSlotsByNames(NULL, NULL, tokenname, PR_TRUE)) == NULL) { |
| 321 |
if (tokenname == NULL) { |
| 322 |
debug("No NSS token found"); |
| 323 |
} else { |
| 324 |
debug("NSS token not found: %s", tokenname); |
| 325 |
} |
| 326 |
return NULL; |
| 327 |
} |
| 328 |
|
| 329 |
for (sle = slots->head; sle; sle = sle->next) { |
| 330 |
SECKEYPrivateKeyList *list; |
| 331 |
SECKEYPrivateKeyListNode *node; |
| 332 |
char *tmppass; |
| 333 |
|
| 334 |
if (nss_authenticate(sle->slot, password, pwprompts, &tmppass) == -1) |
| 335 |
break; |
| 336 |
|
| 337 |
debug("Looking for: %s:%s", tokenname, keyname); |
| 338 |
list = PK11_ListPrivKeysInSlot(sle->slot, (char *)keyname, |
| 339 |
tmppass); |
| 340 |
if (list == NULL && keyname != NULL) { |
| 341 |
char *fooname; |
| 342 |
/* NSS bug workaround */ |
| 343 |
if (asprintf(&fooname, "%s~", keyname) < 0) { |
| 344 |
error("nss_find_privkey: asprintf failed"); |
| 345 |
PK11_FreeSlotList(slots); |
| 346 |
return NULL; |
| 347 |
} |
| 348 |
list = PK11_ListPrivKeysInSlot(sle->slot, fooname, |
| 349 |
tmppass); |
| 350 |
free(fooname); |
| 351 |
} |
| 352 |
if (list == NULL && keyname != NULL) { |
| 353 |
CERTCertificate *cert; |
| 354 |
SECKEYPrivateKey *privk; |
| 355 |
cert = CERT_FindCertByNickname(CERT_GetDefaultCertDB(), |
| 356 |
(char *)keyname); |
| 357 |
if (cert == NULL) |
| 358 |
goto cleanup; |
| 359 |
privk = PK11_FindPrivateKeyFromCert(sle->slot, cert, tmppass); |
| 360 |
CERT_DestroyCertificate(cert); |
| 361 |
if (privk == NULL) |
| 362 |
goto cleanup; |
| 363 |
if ((k=make_key_from_privkey(privk, tmppass)) != NULL) { |
| 364 |
nss_convert_pubkey(k); |
| 365 |
keys = add_key_to_list(k, keys, &i, &allocated); |
| 366 |
} |
| 367 |
SECKEY_DestroyPrivateKey(privk); |
| 368 |
} else { |
| 369 |
if (list == NULL) |
| 370 |
goto cleanup; |
| 371 |
for (node=PRIVKEY_LIST_HEAD(list); !PRIVKEY_LIST_END(node, list); |
| 372 |
node=PRIVKEY_LIST_NEXT(node)) |
| 373 |
if ((k=make_key_from_privkey(node->key, tmppass)) != NULL) { |
| 374 |
nss_convert_pubkey(k); |
| 375 |
keys = add_key_to_list(k, keys, &i, &allocated); |
| 376 |
} |
| 377 |
SECKEY_DestroyPrivateKeyList(list); |
| 378 |
} |
| 379 |
cleanup: |
| 380 |
if (tmppass != NULL) { |
| 381 |
memset(tmppass, 0, strlen(tmppass)); |
| 382 |
xfree(tmppass); |
| 383 |
} |
| 384 |
} |
| 385 |
PK11_FreeSlotList(slots); |
| 386 |
|
| 387 |
return keys; |
| 388 |
} |
| 389 |
|
| 390 |
Key ** |
| 391 |
nss_get_keys(const char *tokenname, const char *keyname, |
| 392 |
char *password, int pwprompts, int num_modules, const char **modules) |
| 393 |
{ |
| 394 |
int i; |
| 395 |
Key **keys; |
| 396 |
|
| 397 |
if (nss_init(NULL) == -1) { |
| 398 |
error("Failed to initialize NSS library"); |
| 399 |
return NULL; |
| 400 |
} |
| 401 |
|
| 402 |
for (i = 0;i < num_modules;i++) { |
| 403 |
if (nss_load_module(modules[i]) == -1) { |
| 404 |
error("Failed to load PKCS#11 module '%s'", modules[i]); |
| 405 |
return NULL; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
keys = nss_find_privkeys(tokenname, keyname, password, pwprompts); |
| 410 |
if (keys == NULL && keyname != NULL) { |
| 411 |
error("Cannot find key in nss, token removed"); |
| 412 |
return NULL; |
| 413 |
} |
| 414 |
#if 0 |
| 415 |
keys = xcalloc(3, sizeof(Key *)); |
| 416 |
|
| 417 |
if (k->type == KEY_RSA) { |
| 418 |
n = key_new_nss_copy(KEY_RSA1, k); |
| 419 |
|
| 420 |
keys[0] = n; |
| 421 |
keys[1] = k; |
| 422 |
keys[2] = NULL; |
| 423 |
} else { |
| 424 |
keys[0] = k; |
| 425 |
keys[1] = NULL; |
| 426 |
} |
| 427 |
#endif |
| 428 |
return keys; |
| 429 |
} |
| 430 |
|
| 431 |
char * |
| 432 |
nss_get_key_label(Key *key) |
| 433 |
{ |
| 434 |
char *label, *nickname; |
| 435 |
|
| 436 |
nickname = PK11_GetPrivateKeyNickname(key->nss->privk); |
| 437 |
label = xstrdup(nickname); |
| 438 |
PORT_Free(nickname); |
| 439 |
|
| 440 |
return label; |
| 441 |
} |
| 442 |
|
| 443 |
#endif /* HAVE_LIBNSS */ |