3 * Reg_svr protocol and encryption/decryption routines
5 * Copyright (C) 1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
11 #include <mit-copyright.h>
33 R_RSA_PRIVATE_KEY *rsa_key;
34 char *emsg[NUM_REG_ERRORS], *ename[NUM_REG_ERRORS];
39 void (*handler)(reg_client *rc, int argc, char **argv);
50 void parse_pdu(reg_client *rc, long len, char *buf);
51 void printhex(unsigned char *buf, int len);
53 int read_rsa_key(void)
58 if (stat(REG_SVR_RSA_KEY, &statbuf))
61 fd = open(REG_SVR_RSA_KEY, O_RDONLY);
65 rsa_key = malloc(statbuf.st_size);
69 if (read(fd, rsa_key, statbuf.st_size) != statbuf.st_size)
82 errs = fopen(REG_SVR_ERROR_MESSAGES, "r");
85 for (i = 0; i < NUM_REG_ERRORS && !feof(errs); i++)
87 if (errbuf[0] != '#' || errbuf[1] != ' ')
88 sprintf(errbuf, "# %d", i);
89 ename[i] = strdup(errbuf + 2);
90 if (ename[i][strlen(ename[i]) - 1] == '\n')
91 ename[i][strlen(ename[i]) - 1] = '\0';
93 if (!ename[i] || !emsg[i])
96 if (!fgets(errbuf, sizeof(errbuf) - 1, errs))
101 if ((p = strchr(errbuf, '\n')) > errbuf)
106 emsg[i] = realloc(emsg[i], strlen(emsg[i]) + strlen(errbuf) + 1);
109 strcat(emsg[i], errbuf);
114 if (i < NUM_REG_ERRORS)
116 com_err(whoami, 0, "Not enough error messages in %s",
117 REG_SVR_ERROR_MESSAGES);
123 void parse_packet(reg_client *rc, int type, long len, char *buf, int sleeping)
127 case REG_RSA_ENCRYPTED_KEY:
129 unsigned char key[MAX_ENCRYPTED_KEY_LEN];
132 if (RSAPrivateDecrypt(key, &keylen, buf, len, rsa_key) || keylen != 8)
134 reply(rc, ENCRYPT_KEY, "INIT", "c", NULL);
137 des_key_sched(key, rc->sched);
141 reply(rc, DATABASE_CLOSED, "INIT", "c", NULL);
143 reply(rc, NO_MESSAGE, "GETN", "c", NULL);
149 char *outbuf, iv[8] = {0, 0, 0, 0, 0, 0, 0, 0};
153 reply(rc, INTERNAL_ERROR, "INIT", "c", NULL,
154 "Encrypted packet unexpected");
158 outbuf = malloc(len + 7);
161 reply(rc, INTERNAL_ERROR, "INIT", "c", NULL, "Out of memory");
164 des_cbc_encrypt(buf, outbuf, len, rc->sched, iv, 0);
166 /* Undo PKCS#5 padding */
167 len -= outbuf[len - 1];
169 parse_pdu(rc, len - 8, outbuf + 8);
174 #ifdef ALLOW_UNENCRYPTED
175 case REG_UNENCRYPTED:
176 parse_pdu(rc, len, buf);
181 com_err(whoami, 0, "Bad packet (type %d, len %d)", type, len);
186 void parse_pdu(reg_client *rc, long len, char *buf)
190 void (*handler)(reg_client *rc, int argc, char **argv) = NULL;
192 if (len < 8 || strcmp(buf, "v1"))
194 com_err(whoami, 0, "Bad packet version number %s", buf);
195 reply(rc, PROTOCOL_ERROR, "INIT", "c", NULL);
201 for (i = 0; handlers[i].name; i++)
203 if (!strcmp(buf, handlers[i].name))
205 handler = handlers[i].handler;
211 com_err(whoami, 0, "Bad packet request %s", buf);
212 reply(rc, PROTOCOL_ERROR, "INIT", "c", NULL);
218 for (argc = 0, p = buf; p < buf + len; p++)
224 argv = malloc(argc * sizeof(char *));
227 com_err(whoami, 0, "in parse_pdu");
228 reply(rc, INTERNAL_ERROR, "INIT", "c", NULL, "Out of memory");
232 fprintf(stderr, "%s[#%d]: %s", whoami, rc->clientid, handlers[i].name);
233 for (argc = 0, p = buf - 1; p < buf + len - 1; p++)
237 argv[argc++] = p + 1;
238 if (strcmp(handlers[i].name, "PSWD") != 0)
239 fprintf(stderr, " '%s'", p + 1);
242 fprintf(stderr, "\n");
245 for (i = 0; i < argc; i++)
247 handler(rc, argc, argv);
251 void reply(reg_client *rc, int msg, char *state, char *clean, char *data,
254 /* reply() can't malloc, since it might be returning an "out of memory"
255 error. We'll use a static buffer which is much larger than any
256 message we'd be returning, and callers have to make sure that any
257 user-generated data is length-limited. */
258 static char buf[8192], outbuf[8192];
260 int len, pad, pcount;
263 unsigned short *nrand;
265 com_err(whoami, 0, "Reply: %s, go to state %s %s", ename[msg], state, clean);
269 memcpy(buf + 3, &junk, 4);
271 memcpy(buf + 7, &junk, 4);
272 nrand = seed48(rc->random);
273 memcpy(rc->random, nrand, 6);
275 memcpy(buf + 11, "v1", 3);
276 memcpy(buf + 14, state, len = strlen(state) + 1);
279 p += vsprintf(p, emsg[msg], ap);
282 memcpy(p, clean, len = strlen(clean) + 1);
286 memcpy(p, data, len = strlen(data) + 1);
292 for (pcount = pad; pcount; pcount--)
293 buf[3 + len++] = pad;
297 char iv[8] = {0, 0, 0, 0, 0, 0, 0, 0};
299 des_cbc_encrypt(buf + 3, outbuf + 3, len, rc->sched, iv, 1);
306 *p = REG_UNENCRYPTED;
311 write(rc->fd, p, len + 3);
313 /* If we're going to INIT, set lastmod to 0 to cause the connection
314 to be closed once we return to the main loop */
315 if (!strcmp(state, "INIT"))
319 char hexd[] = { '0', '1', '2', '3', '4', '5', '6', '7',
320 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
322 void printhex(unsigned char *buf, int len)
326 printf("%c%c", hexd[*buf>>4], hexd[*buf%0x10]);