2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)setkey.c 1.11 94/04/25 SMI
30 * $FreeBSD: src/usr.sbin/keyserv/setkey.c,v 1.3 1999/08/28 01:16:41 peter Exp $
34 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
38 * Do the real work of the keyserver.
39 * Store secret keys. Compute common keys,
40 * and use them to decrypt and encrypt DES keys.
41 * Cache the common keys, so the expensive computation is avoided.
48 #include <sys/types.h>
50 #include <rpc/key_prot.h>
51 #include <rpc/des_crypt.h>
53 #include <sys/errno.h>
55 #include <openssl/bn.h>
56 #include <openssl/crypto.h>
57 #include <openssl/err.h>
59 static BIGNUM *modulus;
60 static char *fetchsecretkey( uid_t );
61 static void writecache( char *, char *, des_block * );
62 static int readcache( char *, char *, des_block * );
63 static void extractdeskey ( BIGNUM *, des_block * );
64 static int storesecretkey( uid_t, keybuf );
65 static keystatus pk_crypt( uid_t, char *, netobj *, des_block *, int);
66 static int nodefaultkeys = 0;
70 * prohibit the nobody key on this machine k (the -d flag)
73 pk_nodefaultkeys(void)
79 * Set the modulus for all our Diffie-Hellman operations
82 setmodulus(char *modx)
85 if (BN_hex2bn(&modulus, modx) == 0)
86 errx(1, "could not convert modulus to BIGNUM: %s",
87 ERR_error_string(ERR_get_error(), 0));
91 * Set the secretkey key for this uid
94 pk_setkey(uid_t uid, keybuf skey)
96 if (!storesecretkey(uid, skey)) {
97 return (KEY_SYSTEMERR);
103 * Encrypt the key using the public key associated with remote_name and the
104 * secret key associated with uid.
107 pk_encrypt(uid_t uid, char *remote_name, netobj *remote_key, des_block *key)
109 return (pk_crypt(uid, remote_name, remote_key, key, DES_ENCRYPT));
113 * Decrypt the key using the public key associated with remote_name and the
114 * secret key associated with uid.
117 pk_decrypt(uid_t uid, char *remote_name, netobj *remote_key, des_block *key)
119 return (pk_crypt(uid, remote_name, remote_key, key, DES_DECRYPT));
122 static int store_netname( uid_t, key_netstarg * );
123 static int fetch_netname( uid_t, key_netstarg * );
126 pk_netput(uid_t uid, key_netstarg *netstore)
128 if (!store_netname(uid, netstore)) {
129 return (KEY_SYSTEMERR);
131 return (KEY_SUCCESS);
135 pk_netget(uid_t uid, key_netstarg *netstore)
137 if (!fetch_netname(uid, netstore)) {
138 return (KEY_SYSTEMERR);
140 return (KEY_SUCCESS);
145 * Do the work of pk_encrypt && pk_decrypt
148 pk_crypt(uid_t uid, char *remote_name, netobj *remote_key, des_block *key,
153 char xsecret_hold[1024];
156 BIGNUM *public, *secret, *common;
160 xsecret = fetchsecretkey(uid);
161 if (xsecret == NULL || xsecret[0] == 0) {
162 memset(zero, 0, sizeof (zero));
163 xsecret = xsecret_hold;
165 return (KEY_NOSECRET);
167 if (!getsecretkey("nobody", xsecret, zero) || xsecret[0] == 0) {
168 return (KEY_NOSECRET);
172 memcpy(xpublic, remote_key->n_bytes, remote_key->n_len);
174 bzero((char *)&xpublic, sizeof(xpublic));
175 if (!getpublickey(remote_name, xpublic)) {
176 if (nodefaultkeys || !getpublickey("nobody", xpublic))
177 return (KEY_UNKNOWN);
181 if (!readcache(xpublic, xsecret, &deskey)) {
182 if ((ctx = BN_CTX_new()) == NULL)
183 return (KEY_SYSTEMERR);
185 if (BN_hex2bn(&public, xpublic) == 0) {
187 return (KEY_SYSTEMERR);
190 if (BN_hex2bn(&secret, xsecret) == 0) {
193 return (KEY_SYSTEMERR);
196 if ((common = BN_new()) == NULL) {
200 return (KEY_SYSTEMERR);
203 BN_mod_exp(common, public, secret, modulus, ctx);
204 extractdeskey(common, &deskey);
205 writecache(xpublic, xsecret, &deskey);
211 error = ecb_crypt((char *)&deskey, (char *)key, sizeof (des_block),
213 if (DES_FAILED(error)) {
214 return (KEY_SYSTEMERR);
216 return (KEY_SUCCESS);
220 pk_get_conv_key(uid_t uid, keybuf xpublic, cryptkeyres *result)
223 char xsecret_hold[1024];
224 BIGNUM *public, *secret, *common;
229 xsecret = fetchsecretkey(uid);
231 if (xsecret == NULL || xsecret[0] == 0) {
232 memset(zero, 0, sizeof (zero));
233 xsecret = xsecret_hold;
235 return (KEY_NOSECRET);
237 if (!getsecretkey("nobody", xsecret, zero) ||
239 return (KEY_NOSECRET);
242 if (!readcache(xpublic, xsecret, &result->cryptkeyres_u.deskey)) {
243 if ((ctx = BN_CTX_new()) == NULL)
244 return (KEY_SYSTEMERR);
246 if (BN_hex2bn(&public, xpublic) == 0) {
248 return (KEY_SYSTEMERR);
251 if (BN_hex2bn(&secret, xsecret) == 0) {
254 return (KEY_SYSTEMERR);
257 if ((common = BN_new()) == NULL) {
261 return (KEY_SYSTEMERR);
264 BN_mod_exp(common, public, secret, modulus, ctx);
266 extractdeskey(common, &result->cryptkeyres_u.deskey);
267 writecache(xpublic, xsecret, &result->cryptkeyres_u.deskey);
274 return (KEY_SUCCESS);
278 * Choose middle 64 bits of the common key to use as our des key, possibly
279 * overwriting the lower order bits by setting parity.
282 extractdeskey(BIGNUM *ck, des_block *deskey)
286 BN_ULONG r, base = (1 << 8);
289 if ((a = BN_dup(ck)) == NULL)
290 errx(1, "could not copy BIGNUM");
292 for (i = 0; i < ((KEYSIZE - 64) / 2) / 8; i++) {
293 r = BN_div_word(a, base);
296 for (i = 0; i < 8; i++) {
297 r = BN_div_word(a, base);
301 des_setparity((char *)deskey);
305 * Key storage management
310 struct secretkey_netname_list {
312 key_netstarg keynetdata;
314 struct secretkey_netname_list *next;
319 static struct secretkey_netname_list *g_secretkey_netname;
322 * Store the keys and netname for this uid
325 store_netname(uid_t uid, key_netstarg *netstore)
327 struct secretkey_netname_list *new;
328 struct secretkey_netname_list **l;
330 for (l = &g_secretkey_netname; *l != NULL && (*l)->uid != uid;
334 new = (struct secretkey_netname_list *)malloc(sizeof (*new));
343 if (new->keynetdata.st_netname)
344 free(new->keynetdata.st_netname);
346 memcpy(new->keynetdata.st_priv_key, netstore->st_priv_key,
348 memcpy(new->keynetdata.st_pub_key, netstore->st_pub_key, HEXKEYBYTES);
350 if (netstore->st_netname)
351 new->keynetdata.st_netname = strdup(netstore->st_netname);
353 new->keynetdata.st_netname = NULL;
354 new->sc_flag = KEY_NAME;
360 * Fetch the keys and netname for this uid
364 fetch_netname(uid_t uid, struct key_netstarg *key_netst)
366 struct secretkey_netname_list *l;
368 for (l = g_secretkey_netname; l != NULL; l = l->next) {
369 if ((l->uid == uid) && (l->sc_flag == KEY_NAME)){
371 memcpy(key_netst->st_priv_key,
372 l->keynetdata.st_priv_key, HEXKEYBYTES);
374 memcpy(key_netst->st_pub_key,
375 l->keynetdata.st_pub_key, HEXKEYBYTES);
377 if (l->keynetdata.st_netname)
378 key_netst->st_netname =
379 strdup(l->keynetdata.st_netname);
381 key_netst->st_netname = NULL;
390 fetchsecretkey(uid_t uid)
392 struct secretkey_netname_list *l;
394 for (l = g_secretkey_netname; l != NULL; l = l->next) {
396 return (l->keynetdata.st_priv_key);
403 * Store the secretkey for this uid
406 storesecretkey(uid_t uid, keybuf key)
408 struct secretkey_netname_list *new;
409 struct secretkey_netname_list **l;
411 for (l = &g_secretkey_netname; *l != NULL && (*l)->uid != uid;
415 new = (struct secretkey_netname_list *) malloc(sizeof (*new));
420 new->sc_flag = KEY_ONLY;
421 memset(new->keynetdata.st_pub_key, 0, HEXKEYBYTES);
422 new->keynetdata.st_netname = NULL;
429 memcpy(new->keynetdata.st_priv_key, key,
437 return ("0123456789abcdef"[val]);
441 bin2hex(unsigned char *bin, unsigned char *hex, int size)
445 for (i = 0; i < size; i++) {
446 *hex++ = hexdigit(*bin >> 4);
447 *hex++ = hexdigit(*bin++ & 0xf);
454 if ('0' <= dig && dig <= '9') {
456 } else if ('a' <= dig && dig <= 'f') {
457 return (dig - 'a' + 10);
458 } else if ('A' <= dig && dig <= 'F') {
459 return (dig - 'A' + 10);
466 hex2bin(unsigned char *hex, unsigned char *bin, int size)
470 for (i = 0; i < size; i++) {
471 *bin = hexval(*hex++) << 4;
472 *bin++ |= hexval(*hex++);
477 * Exponential caching management
479 struct cachekey_list {
483 struct cachekey_list *next;
485 static struct cachekey_list *g_cachedkeys;
488 * cache result of expensive multiple precision exponential operation
491 writecache(char *pub, char *sec, des_block *deskey)
493 struct cachekey_list *new;
495 new = (struct cachekey_list *) malloc(sizeof (struct cachekey_list));
499 memcpy(new->public, pub, sizeof (keybuf));
500 memcpy(new->secret, sec, sizeof (keybuf));
501 new->deskey = *deskey;
502 new->next = g_cachedkeys;
507 * Try to find the common key in the cache
510 readcache(char *pub, char *sec, des_block *deskey)
512 struct cachekey_list *found;
513 struct cachekey_list **l;
515 #define cachehit(pub, sec, list) \
516 (memcmp(pub, (list)->public, sizeof (keybuf)) == 0 && \
517 memcmp(sec, (list)->secret, sizeof (keybuf)) == 0)
519 for (l = &g_cachedkeys; (*l) != NULL && !cachehit(pub, sec, *l);
527 found->next = g_cachedkeys;
528 g_cachedkeys = found;
529 *deskey = found->deskey;