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, MERCHANTIBILITY 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 $
31 * $DragonFly: src/usr.sbin/keyserv/setkey.c,v 1.6 2004/05/20 19:24:42 cpressey Exp $
35 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
39 * Do the real work of the keyserver.
40 * Store secret keys. Compute common keys,
41 * and use them to decrypt and encrypt DES keys.
42 * Cache the common keys, so the expensive computation is avoided.
49 #include <sys/types.h>
51 #include <rpc/key_prot.h>
52 #include <rpc/des_crypt.h>
54 #include <sys/errno.h>
58 static char *fetchsecretkey( uid_t );
59 static void writecache( char *, char *, des_block * );
60 static int readcache( char *, char *, des_block * );
61 static void extractdeskey ( MINT *, des_block * );
62 static int storesecretkey( uid_t, keybuf );
63 static keystatus pk_crypt( uid_t, char *, netobj *, des_block *, int);
64 static int nodefaultkeys = 0;
68 * prohibit the nobody key on this machine k (the -d flag)
71 pk_nodefaultkeys(void)
77 * Set the modulus for all our Diffie-Hellman operations
80 setmodulus(char *modx)
86 * Set the secretkey key for this uid
89 pk_setkey(uid_t uid, keybuf skey)
91 if (!storesecretkey(uid, skey)) {
92 return (KEY_SYSTEMERR);
98 * Encrypt the key using the public key associated with remote_name and the
99 * secret key associated with uid.
102 pk_encrypt(uid_t uid, char *remote_name, netobj *remote_key, des_block *key)
104 return (pk_crypt(uid, remote_name, remote_key, key, DES_ENCRYPT));
108 * Decrypt the key using the public key associated with remote_name and the
109 * secret key associated with uid.
112 pk_decrypt(uid_t uid, char *remote_name, netobj *remote_key, des_block *key)
114 return (pk_crypt(uid, remote_name, remote_key, key, DES_DECRYPT));
117 static int store_netname( uid_t, key_netstarg * );
118 static int fetch_netname( uid_t, key_netstarg * );
121 pk_netput(uid_t uid, key_netstarg *netstore)
123 if (!store_netname(uid, netstore)) {
124 return (KEY_SYSTEMERR);
126 return (KEY_SUCCESS);
130 pk_netget(uid_t uid, key_netstarg *netstore)
132 if (!fetch_netname(uid, netstore)) {
133 return (KEY_SYSTEMERR);
135 return (KEY_SUCCESS);
140 * Do the work of pk_encrypt && pk_decrypt
143 pk_crypt(uid_t uid, char *remote_name, netobj *remote_key, des_block *key,
148 char xsecret_hold[1024];
156 xsecret = fetchsecretkey(uid);
157 if (xsecret == NULL || xsecret[0] == 0) {
158 memset(zero, 0, sizeof (zero));
159 xsecret = xsecret_hold;
161 return (KEY_NOSECRET);
163 if (!getsecretkey("nobody", xsecret, zero) || xsecret[0] == 0) {
164 return (KEY_NOSECRET);
168 memcpy(xpublic, remote_key->n_bytes, remote_key->n_len);
170 bzero((char *)&xpublic, sizeof(xpublic));
171 if (!getpublickey(remote_name, xpublic)) {
172 if (nodefaultkeys || !getpublickey("nobody", xpublic))
173 return (KEY_UNKNOWN);
177 if (!readcache(xpublic, xsecret, &deskey)) {
178 public = xtom(xpublic);
179 secret = xtom(xsecret);
180 /* Sanity Check on public and private keys */
181 if ((public == NULL) || (secret == NULL))
182 return (KEY_SYSTEMERR);
185 pow(public, secret, MODULUS, common);
186 extractdeskey(common, &deskey);
187 writecache(xpublic, xsecret, &deskey);
192 err = ecb_crypt((char *)&deskey, (char *)key, sizeof (des_block),
194 if (DES_FAILED(err)) {
195 return (KEY_SYSTEMERR);
197 return (KEY_SUCCESS);
201 pk_get_conv_key(uid_t uid, keybuf xpublic, cryptkeyres *result)
204 char xsecret_hold[1024];
211 xsecret = fetchsecretkey(uid);
213 if (xsecret == NULL || xsecret[0] == 0) {
214 memset(zero, 0, sizeof (zero));
215 xsecret = xsecret_hold;
217 return (KEY_NOSECRET);
219 if (!getsecretkey("nobody", xsecret, zero) ||
221 return (KEY_NOSECRET);
224 if (!readcache(xpublic, xsecret, &result->cryptkeyres_u.deskey)) {
225 public = xtom(xpublic);
226 secret = xtom(xsecret);
227 /* Sanity Check on public and private keys */
228 if ((public == NULL) || (secret == NULL))
229 return (KEY_SYSTEMERR);
232 pow(public, secret, MODULUS, common);
233 extractdeskey(common, &result->cryptkeyres_u.deskey);
234 writecache(xpublic, xsecret, &result->cryptkeyres_u.deskey);
240 return (KEY_SUCCESS);
244 * Choose middle 64 bits of the common key to use as our des key, possibly
245 * overwriting the lower order bits by setting parity.
248 extractdeskey(MINT *ck, des_block *deskey)
253 short base = (1 << 8);
262 for (i = 0; i < ((KEYSIZE - 64) / 2) / 8; i++) {
263 sdiv(a, base, a, &r);
266 for (i = 0; i < 8; i++) {
267 sdiv(a, base, a, &r);
271 des_setparity((char *)deskey);
275 * Key storage management
280 struct secretkey_netname_list {
282 key_netstarg keynetdata;
284 struct secretkey_netname_list *next;
289 static struct secretkey_netname_list *g_secretkey_netname;
292 * Store the keys and netname for this uid
295 store_netname(uid_t uid, key_netstarg *netstore)
297 struct secretkey_netname_list *new;
298 struct secretkey_netname_list **l;
300 for (l = &g_secretkey_netname; *l != NULL && (*l)->uid != uid;
304 new = (struct secretkey_netname_list *)malloc(sizeof (*new));
313 if (new->keynetdata.st_netname)
314 (void) free (new->keynetdata.st_netname);
316 memcpy(new->keynetdata.st_priv_key, netstore->st_priv_key,
318 memcpy(new->keynetdata.st_pub_key, netstore->st_pub_key, HEXKEYBYTES);
320 if (netstore->st_netname)
321 new->keynetdata.st_netname = strdup(netstore->st_netname);
323 new->keynetdata.st_netname = (char *)NULL;
324 new->sc_flag = KEY_NAME;
330 * Fetch the keys and netname for this uid
334 fetch_netname(uid_t uid, struct key_netstarg *key_netst)
336 struct secretkey_netname_list *l;
338 for (l = g_secretkey_netname; l != NULL; l = l->next) {
339 if ((l->uid == uid) && (l->sc_flag == KEY_NAME)){
341 memcpy(key_netst->st_priv_key,
342 l->keynetdata.st_priv_key, HEXKEYBYTES);
344 memcpy(key_netst->st_pub_key,
345 l->keynetdata.st_pub_key, HEXKEYBYTES);
347 if (l->keynetdata.st_netname)
348 key_netst->st_netname =
349 strdup(l->keynetdata.st_netname);
351 key_netst->st_netname = NULL;
360 fetchsecretkey(uid_t uid)
362 struct secretkey_netname_list *l;
364 for (l = g_secretkey_netname; l != NULL; l = l->next) {
366 return (l->keynetdata.st_priv_key);
373 * Store the secretkey for this uid
376 storesecretkey(uid_t uid, keybuf key)
378 struct secretkey_netname_list *new;
379 struct secretkey_netname_list **l;
381 for (l = &g_secretkey_netname; *l != NULL && (*l)->uid != uid;
385 new = (struct secretkey_netname_list *) malloc(sizeof (*new));
390 new->sc_flag = KEY_ONLY;
391 memset(new->keynetdata.st_pub_key, 0, HEXKEYBYTES);
392 new->keynetdata.st_netname = NULL;
399 memcpy(new->keynetdata.st_priv_key, key,
407 return ("0123456789abcdef"[val]);
411 bin2hex(unsigned char *bin, unsigned char *hex, int size)
415 for (i = 0; i < size; i++) {
416 *hex++ = hexdigit(*bin >> 4);
417 *hex++ = hexdigit(*bin++ & 0xf);
424 if ('0' <= dig && dig <= '9') {
426 } else if ('a' <= dig && dig <= 'f') {
427 return (dig - 'a' + 10);
428 } else if ('A' <= dig && dig <= 'F') {
429 return (dig - 'A' + 10);
436 hex2bin(unsigned char *hex, unsigned char *bin, int size)
440 for (i = 0; i < size; i++) {
441 *bin = hexval(*hex++) << 4;
442 *bin++ |= hexval(*hex++);
447 * Exponential caching management
449 struct cachekey_list {
453 struct cachekey_list *next;
455 static struct cachekey_list *g_cachedkeys;
458 * cache result of expensive multiple precision exponential operation
461 writecache(char *pub, char *sec, des_block *deskey)
463 struct cachekey_list *new;
465 new = (struct cachekey_list *) malloc(sizeof (struct cachekey_list));
469 memcpy(new->public, pub, sizeof (keybuf));
470 memcpy(new->secret, sec, sizeof (keybuf));
471 new->deskey = *deskey;
472 new->next = g_cachedkeys;
477 * Try to find the common key in the cache
480 readcache(char *pub, char *sec, des_block *deskey)
482 struct cachekey_list *found;
483 struct cachekey_list **l;
485 #define cachehit(pub, sec, list) \
486 (memcmp(pub, (list)->public, sizeof (keybuf)) == 0 && \
487 memcmp(sec, (list)->secret, sizeof (keybuf)) == 0)
489 for (l = &g_cachedkeys; (*l) != NULL && !cachehit(pub, sec, *l);
497 found->next = g_cachedkeys;
498 g_cachedkeys = found;
499 *deskey = found->deskey;