Debian Codegraph @

libinetutils/shishi.c

raw
1 /* shishi.c -- functions to use kerberos V with shishi
2 Copyright (C) 2005-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 #include <config.h>
20
21 #ifdef SHISHI
22 # include <string.h>
23 # include <stdlib.h>
24 # include <errno.h>
25 # include <shishi.h>
26 # include <syslog.h>
27 # include <unistd.h>
28 # include <shishi_def.h>
29
30 /* shishi authentication, client side */
31 int
32 shishi_auth (Shishi **handle, int verbose, char **cname,
33 const char *sname, int sock, char *cmd,
34 unsigned short port, Shishi_key **enckey, const char *realm)
35 {
36 Shishi_ap *ap;
37 Shishi_tkt *tkt;
38 Shishi_tkts_hint hint;
39 Shishi *h;
40
41 int rc;
42 char *out, *p;
43 size_t outlen;
44 int krb5len, msglen;
45 char *tmpserver;
46 char auth;
47 /* KERBEROS 5 SENDAUTH MESSAGE */
48 char krb5sendauth[] = "KRB5_SENDAUTH_V1.0";
49 /* PROTOCOL VERSION */
50 char krb5sendclient[] = "KCMDV0.2";
51 /* to store error msg sent by server */
52 char errormsg[101];
53 char cksumdata[101];
54
55 if (!shishi_check_version (SHISHI_VERSION))
56 {
57 fprintf (stderr, "shishi_check_version() failed:\n"
58 "Header file incompatible with shared library.\n");
59 return SHISHI_INVALID_ARGUMENT;
60 }
61
62 rc = shishi_init (handle);
63 if (rc != SHISHI_OK)
64 {
65 fprintf (stderr,
66 "error initializing shishi: %s\n", shishi_strerror (rc));
67 return rc;
68 }
69
70 if (realm)
71 shishi_realm_default_set (*handle, realm);
72
73 h = *handle;
74
75 if (!*cname)
76 *cname = (char *) shishi_principal_default (h);
77
78 /* size of KRB5 auth message */
79 krb5len = strlen (krb5sendauth) + 1;
80 msglen = htonl (krb5len);
81 write (sock, &msglen, sizeof (int));
82 /* KRB5 authentication message */
83 write (sock, krb5sendauth, krb5len);
84 /* size of client message */
85 krb5len = strlen (krb5sendclient) + 1;
86 msglen = htonl (krb5len);
87 write (sock, &msglen, sizeof (int));
88 /* KRB5 client message */
89 write (sock, krb5sendclient, krb5len);
90
91 /* get answer from server 0 = ok, 1 = error with message */
92 read (sock, &auth, 1);
93 if (auth)
94 {
95 ssize_t n;
96
97 errormsg[0] = '\0';
98 n = read (sock, errormsg, sizeof (errormsg) - 1);
99
100 if (n >= 0 && n < (ssize_t) sizeof (errormsg))
101 errormsg[n] = '\0';
102 else
103 errormsg[sizeof (errormsg) - 1] = '\0';
104
105 fprintf (stderr, "Error during server authentication : %s\n", errormsg);
106 return SHISHI_VERIFY_FAILED;
107 }
108
109 if (verbose)
110 {
111 printf ("Client: %s\n", *cname);
112 printf ("Server: %s\n", sname);
113 }
114
115 /* Get a ticket for the server. */
116
117 memset (&hint, 0, sizeof (hint));
118
119 tmpserver = malloc (strlen (SERVICE) + strlen (sname) + 2);
120 if (!tmpserver)
121 {
122 perror ("shishi_auth()");
123 return SHISHI_TOO_SMALL_BUFFER;
124 }
125
126 p = strchr (sname, '/');
127 if (p && (p != sname))
128 strcpy (tmpserver, sname); /* Non-empty prefix. */
129 else
130 sprintf (tmpserver, "%s/%s", SERVICE, sname + (p ? 1 : 0));
131
132 /* Retrieve realm assigned to this server as per configuration,
133 * unless an explicit domain was passed in the call.
134 */
135 if (!realm)
136 {
137 if (!p)
138 p = (char *) sname;
139 else if (*p == '/')
140 ++p;
141
142 shishi_realm_default_set (h, shishi_realm_for_server (h, p));
143 }
144
145 hint.client = (char *) *cname;
146 hint.server = (char *) tmpserver;
147
148 tkt = shishi_tkts_get (shishi_tkts_default (h), &hint);
149 if (!tkt)
150 {
151 fprintf (stderr, "cannot find ticket for \"%s@%s\"\n",
152 tmpserver, shishi_realm_default (h));
153 free (tmpserver);
154 return SHISHI_INVALID_TICKET;
155 }
156
157 free (tmpserver);
158
159 if (verbose)
160 shishi_tkt_pretty_print (tkt, stderr);
161
162 /* Create Authentication context */
163
164 rc = shishi_ap_tktoptions (h, &ap, tkt, SHISHI_APOPTIONS_MUTUAL_REQUIRED);
165 if (rc != SHISHI_OK)
166 {
167 fprintf (stderr, "cannot create authentication context\n");
168 return rc;
169 }
170
171
172 /* checksum = port: terminal name */
173
174 snprintf (cksumdata, sizeof (cksumdata) - 1,
175 "%u:%s%s", ntohs (port), cmd, *cname);
176
177 /* add checksum to authenticator */
178
179 shishi_ap_authenticator_cksumdata_set (ap, cksumdata, strlen (cksumdata));
180 /* To be compatible with MIT rlogind */
181 shishi_ap_authenticator_cksumtype_set (ap, SHISHI_RSA_MD5);
182
183 /* create der encoded AP-REQ */
184
185 rc = shishi_ap_req_der (ap, &out, &outlen);
186 if (rc != SHISHI_OK)
187 {
188 fprintf (stderr, "cannot build authentication request: %s\n",
189 shishi_strerror (rc));
190
191 return rc;
192 }
193
194 if (verbose)
195 shishi_authenticator_print (h, stderr, shishi_ap_authenticator (ap));
196
197 /* extract subkey if present from ap exchange for secure connection */
198
199 if (enckey)
200 shishi_authenticator_get_subkey (h, shishi_ap_authenticator (ap), enckey);
201
202 /* send size of AP-REQ to the server */
203
204 msglen = outlen;
205 msglen = htonl (msglen);
206 write (sock, (char *) &msglen, sizeof (int));
207
208 /* send AP-REQ to the server */
209
210 write (sock, out, outlen);
211
212 /* read response from server - what ? */
213
214 read (sock, &rc, sizeof (rc));
215 if (rc)
216 return SHISHI_APREP_VERIFY_FAILED;
217
218 /* For mutual authentication, wait for server reply. */
219
220 if (shishi_apreq_mutual_required_p (h, shishi_ap_req (ap)))
221 {
222 if (verbose)
223 printf ("Waiting for server to authenticate itself...\n");
224
225 /* read size of the AP-REP */
226
227 read (sock, (char *) &msglen, sizeof (int));
228
229 /* read AP-REP */
230 outlen = ntohl (msglen);
231 outlen = read (sock, out, outlen);
232
233 rc = shishi_ap_rep_verify_der (ap, out, outlen);
234 if (rc == SHISHI_OK)
235 {
236 if (verbose)
237 printf ("AP-REP verification OK...\n");
238 }
239 else
240 {
241 if (rc == SHISHI_APREP_VERIFY_FAILED)
242 fprintf (stderr, "AP-REP verification failed...\n");
243 else
244 fprintf (stderr, "AP-REP verification error: %s\n",
245 shishi_strerror (rc));
246 return rc;
247 }
248
249 /* The server is authenticated. */
250 if (verbose)
251 printf ("Server authenticated.\n");
252 }
253
254 /* We are now authenticated. */
255 if (verbose)
256 printf ("User authenticated.\n");
257
258 return SHISHI_OK;
259
260 }
261
262 /*
263 * XXX: Is this ever needed?
264 *
265 static void
266 senderror (int s, char type, char *buf)
267 {
268 write (s, &type, sizeof (char));
269 write (s, buf, strlen (buf));
270 }
271 */
272
273 /* shishi authentication, server side */
274 int
275 get_auth (int infd, Shishi **handle, Shishi_ap **ap,
276 Shishi_key **enckey, const char **err_msg, int *protoversion,
277 int *cksumtype, char **cksum, size_t *cksumlen, char *srvname)
278 {
279 Shishi_key *key;
280 char *out;
281 size_t outlen;
282 char *buf;
283 int buflen;
284 int len;
285 int rc;
286 int error;
287 /* KERBEROS 5 SENDAUTH MESSAGE */
288 char krb5sendauth[] = "KRB5_SENDAUTH_V1.0";
289 /* PROTOCOL VERSION */
290 char krb5kcmd1[] = "KCMDV0.1";
291 char krb5kcmd2[] = "KCMDV0.2";
292 char *servername, *server = NULL, *realm = NULL;
293
294 *err_msg = NULL;
295 /* Get key for the server. */
296
297 if (!shishi_check_version (SHISHI_VERSION))
298 {
299 *err_msg =
300 "shishi_check_version() failed: header file incompatible with shared library.";
301 return SHISHI_INVALID_ARGUMENT;
302 }
303
304 rc = shishi_init_server (handle);
305 if (rc != SHISHI_OK)
306 return rc;
307
308 if (srvname && *srvname)
309 {
310 rc = shishi_parse_name (*handle, srvname, &server, &realm);
311 if (rc != SHISHI_OK)
312 {
313 *err_msg = shishi_strerror (rc);
314 return rc;
315 }
316 }
317
318 if (server && *server)
319 {
320 char *p;
321
322 servername = malloc (sizeof (SERVICE) + strlen (server) + 2);
323 if (!servername)
324 {
325 *err_msg = "Not enough memory";
326 return SHISHI_TOO_SMALL_BUFFER;
327 }
328
329 p = strchr (server, '/');
330 if (p && (p != server))
331 sprintf (servername, "%s", server); /* Non-empty prefix. */
332 else
333 sprintf (servername, "%s/%s", SERVICE, server + (p ? 1 : 0)); /* Remove initial slash. */
334 }
335 else
336 servername = shishi_server_for_local_service (*handle, SERVICE);
337
338 if (realm && *realm)
339 shishi_realm_default_set (*handle, realm);
340
341 free (server);
342 free (realm);
343
344 /* Enable use of `~/.k5login'. */
345 if (shishi_check_version ("1.0.2")) /* Faulty in version 1.0.1. */
346 {
347 rc = shishi_cfg_authorizationtype_set (*handle, "k5login basic");
348 if (rc != SHISHI_OK)
349 {
350 *err_msg = shishi_error (*handle);
351 return rc;
352 }
353 }
354
355 key = shishi_hostkeys_for_serverrealm (*handle, servername,
356 shishi_realm_default (*handle));
357 free (servername);
358 if (!key)
359 {
360 *err_msg = shishi_error (*handle);
361 return SHISHI_INVALID_KEY;
362 }
363
364 /* Read Kerberos 5 sendauth message */
365 rc = read (infd, &len, sizeof (int));
366 if (rc != sizeof (int))
367 {
368 *err_msg = "Error reading message size";
369 return SHISHI_IO_ERROR;
370 }
371
372 buflen = ntohl (len);
373 buf = malloc (buflen);
374 if (!buf)
375 {
376 *err_msg = "Not enough memory";
377 return SHISHI_TOO_SMALL_BUFFER;
378 }
379
380 rc = read (infd, buf, buflen);
381 if (rc != buflen)
382 {
383 *err_msg = "Error reading authentication message";
384 return SHISHI_IO_ERROR;
385 }
386
387 len = strlen (krb5sendauth);
388 rc = strncmp (buf, krb5sendauth, buflen >= len ? len : buflen);
389 if (rc)
390 {
391 *err_msg = "Invalid authentication type";
392 /* Authentication type is wrong. */
393 write (infd, "\001", 1);
394 return SHISHI_VERIFY_FAILED;
395 }
396
397 free (buf);
398
399 /* Read protocol version */
400 rc = read (infd, &len, sizeof (int));
401 if (rc != sizeof (int))
402 {
403 *err_msg = "Error reading protocol message size";
404 return SHISHI_IO_ERROR;
405 }
406 buflen = ntohl (len);
407 buf = malloc (buflen);
408 if (!buf)
409 {
410 *err_msg = "Not enough memory";
411 return SHISHI_TOO_SMALL_BUFFER;
412 }
413
414 rc = read (infd, buf, buflen);
415 if (rc != buflen)
416 {
417 *err_msg = "Error reading protocol message";
418 return SHISHI_IO_ERROR;
419 }
420
421 len = strlen (krb5kcmd1);
422 rc = strncmp (buf, krb5kcmd1, buflen >= len ? len : buflen);
423 if (rc)
424 {
425 len = strlen (krb5kcmd2);
426 rc = strncmp (buf, krb5kcmd2, buflen >= len ? len : buflen);
427 if (rc)
428 {
429 *err_msg = "Protocol version not supported";
430 /* Protocol version is wrong. */
431 write (infd, "\002", 1);
432 return SHISHI_VERIFY_FAILED;
433 }
434 *protoversion = 2;
435 }
436 else
437 *protoversion = 1;
438
439 free (buf);
440
441 /* Authentication type is ok */
442
443 write (infd, "\0", 1);
444
445 /* Read Authentication request from client */
446
447 rc = read (infd, &len, sizeof (int));
448 if (rc != sizeof (int))
449 {
450 *err_msg = "Error reading authentication request size";
451 return SHISHI_IO_ERROR;
452 }
453
454 buflen = ntohl (len);
455 buf = malloc (buflen);
456 if (!buf)
457 {
458 *err_msg = "Not enough memory";
459 return SHISHI_TOO_SMALL_BUFFER;
460 }
461
462 rc = read (infd, buf, buflen);
463 if (rc != buflen)
464 {
465 *err_msg = "Error reading authentication request";
466 return SHISHI_IO_ERROR;
467 }
468
469 /* Create Authentication context */
470
471 rc = shishi_ap_nosubkey (*handle, ap);
472 if (rc != SHISHI_OK)
473 return rc;
474
475 /* Store request in context */
476
477 rc = shishi_ap_req_der_set (*ap, buf, buflen);
478 if (rc != SHISHI_OK)
479 return rc;
480
481 free (buf);
482
483 /* Process authentication request */
484
485 rc = shishi_ap_req_process (*ap, key);
486 if (rc != SHISHI_OK)
487 return rc;
488
489 # ifdef ENCRYPTION
490
491 /* extract subkey if present from ap exchange for secure connection */
492 if (*protoversion == 2)
493 {
494 *enckey = NULL;
495 shishi_authenticator_get_subkey (*handle,
496 shishi_ap_authenticator (*ap), enckey);
497 }
498
499 # endif
500
501 /* Get authenticator checksum */
502 rc = shishi_authenticator_cksum (*handle,
503 shishi_ap_authenticator (*ap),
504 cksumtype, cksum, cksumlen);
505 if (rc != SHISHI_OK)
506 return rc;
507
508 /* User is authenticated. */
509 error = 0;
510 write (infd, &error, sizeof (int));
511
512 /* Authenticate ourself to client, if requested. */
513
514 if (shishi_apreq_mutual_required_p (*handle, shishi_ap_req (*ap)))
515 {
516 int len;
517
518 rc = shishi_ap_rep_der (*ap, &out, &outlen);
519 if (rc != SHISHI_OK)
520 return rc;
521
522 len = outlen;
523 len = htonl (len);
524 rc = write (infd, &len, sizeof (len));
525 if (rc != sizeof (int))
526 {
527 *err_msg = "Error sending AP-REP";
528 free (out);
529 return SHISHI_IO_ERROR;
530 }
531
532 rc = write (infd, out, ntohl (len));
533 if (rc != (int) ntohl (len))
534 {
535 *err_msg = "Error sending AP-REP";
536 free (out);
537 return SHISHI_IO_ERROR;
538 }
539
540 free (out);
541
542 /* We are authenticated to client */
543 }
544
545 # ifdef ENCRYPTION
546 if (*protoversion == 1)
547 {
548 Shishi_tkt *tkt;
549
550 tkt = shishi_ap_tkt (*ap);
551 if (tkt == NULL)
552 {
553 *err_msg = "Could not get tkt from AP-REQ";
554 return SHISHI_INVALID_TICKET;
555 }
556
557 rc = shishi_encticketpart_get_key (*handle,
558 shishi_tkt_encticketpart (tkt),
559 enckey);
560 if (rc != SHISHI_OK)
561 return rc;
562 }
563 # endif
564
565 return SHISHI_OK;
566 }
567
568 # ifdef ENCRYPTION
569
570 /* read encrypted data on socket */
571 int
572 readenc (Shishi *h, int sock, char *buf, int *len, shishi_ivector *iv,
573 Shishi_key *enckey, int proto)
574 {
575 char *out;
576 char *outbis;
577
578 int rc;
579 int val;
580 size_t outlen;
581 int dlen = 0, blocksize, enctype, hashsize;
582
583 /* read size of message */
584 read (sock, &dlen, sizeof (int));
585
586 dlen = ntohl (dlen);
587 /* if 0 put read size to 0 */
588 if (!dlen)
589 {
590 *len = dlen;
591 return SHISHI_OK;
592 }
593
594 if (proto == 1)
595 *len = dlen;
596
597 /* convert size to encryption size */
598 enctype = shishi_key_type (enckey);
599
600 blocksize = shishi_cipher_blocksize (enctype);
601 hashsize =
602 shishi_checksum_cksumlen (shishi_cipher_defaultcksumtype (enctype));
603
604 switch (enctype)
605 {
606 case SHISHI_AES128_CTS_HMAC_SHA1_96:
607 case SHISHI_AES256_CTS_HMAC_SHA1_96:
608 dlen += 4 + hashsize + blocksize;
609 break;
610 case SHISHI_ARCFOUR_HMAC:
611 case SHISHI_ARCFOUR_HMAC_EXP:
612 dlen += 4 + 8 + blocksize - 1;
613 dlen /= blocksize;
614 dlen *= blocksize;
615 dlen += hashsize;
616 break;
617 case SHISHI_DES3_CBC_HMAC_SHA1_KD:
618 dlen += 4 + 2 * blocksize - 1;
619 dlen /= blocksize;
620 dlen *= blocksize;
621 dlen += hashsize;
622 break;
623 case SHISHI_DES_CBC_CRC:
624 dlen += 2 * blocksize - 1;
625 if (proto == 2)
626 dlen += 4;
627 dlen += hashsize;
628 dlen /= blocksize;
629 dlen *= blocksize;
630 break;
631 default:
632 dlen += blocksize - 1;
633 if (proto == 2)
634 dlen += 4;
635 dlen += hashsize;
636 dlen /= blocksize;
637 dlen *= blocksize;
638 break;
639 }
640
641 /* read encrypted data */
642 outbis = malloc (dlen);
643 if (outbis == NULL)
644 {
645 perror ("readenc()");
646 return 1;
647 }
648
649 rc = read (sock, outbis, dlen);
650 if (rc != dlen)
651 {
652 fprintf (stderr, "Error during read socket\n");
653 free (outbis);
654 return 1;
655 }
656
657 if (proto == 1)
658 {
659 rc =
660 shishi_decrypt (h, enckey, iv->keyusage, outbis, dlen, &out, &outlen);
661 if (rc != SHISHI_OK)
662 {
663 fprintf (stderr, "decryption error\n");
664 free (outbis);
665 return 1;
666 }
667
668 val = 0;
669 }
670 else
671 {
672 rc = shishi_crypto_decrypt (iv->ctx, outbis, dlen, &out, &outlen);
673 if (rc != SHISHI_OK)
674 {
675 fprintf (stderr, "decryption error\n");
676 free (outbis);
677 return 1;
678 }
679
680 /* in KCMDV0.2 first 4 bytes of decrypted data = len of data */
681 *len = ntohl (*((int *) out));
682 val = sizeof (int);
683 }
684
685 memset (buf, 0, SHISHI_ENCRYPT_BUFLEN);
686
687 /* copy decrypted data to output */
688 memcpy (buf, out + val, outlen - val);
689
690 free (out);
691 free (outbis);
692
693 return SHISHI_OK;
694 }
695
696 /* write encrypted data to socket */
697 int
698 writeenc (Shishi *h, int sock, char *buf, int wlen, int *len,
699 shishi_ivector *iv, Shishi_key *enckey, int proto)
700 {
701 char *out;
702 char *bufbis;
703
704 int rc;
705 int dlen;
706 size_t outlen;
707
708 dlen = wlen;
709 dlen = htonl (dlen);
710
711 /* data to encrypt = size + data */
712 if (proto == 2)
713 {
714 bufbis = malloc (wlen + sizeof (int));
715 if (!bufbis)
716 {
717 perror ("writeenc");
718 return 1;
719 }
720 memcpy (bufbis, (char *) &dlen, sizeof (int));
721 memcpy (bufbis + sizeof (int), buf, wlen);
722
723 /* encrypt it */
724 rc =
725 shishi_crypto_encrypt (iv->ctx, bufbis, wlen + sizeof (int), &out,
726 &outlen);
727 }
728 else
729 {
730 bufbis = malloc (wlen);
731 if (!bufbis)
732 {
733 perror ("bufbis");
734 return 1;
735 }
736 memcpy (bufbis, buf, wlen);
737
738 /* data to encrypt = size + data */
739 rc =
740 shishi_encrypt (h, enckey, iv->keyusage, bufbis, wlen, &out, &outlen);
741 }
742
743 if (rc != SHISHI_OK)
744 {
745 fprintf (stderr, "decryption error\n");
746 free (bufbis);
747 return 1;
748 }
749
750 free (bufbis);
751
752 /* data to send = original size + encrypted data */
753 /* send it */
754 write (sock, &dlen, sizeof (int));
755 write (sock, out, outlen);
756
757 *len = wlen;
758
759 free (out);
760
761 return SHISHI_OK;
762
763
764 }
765 # endif/* ENCRYPTION */
766 #endif