]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/lib/ZFmtAuth.c
21b133b6480e5572e8c268f3761ed7f5b588bdce
[1ts-debian.git] / zephyr / lib / ZFmtAuth.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains source for the ZFormatAuthenticNotice function.
3  *
4  *      Created by:     Robert French
5  *
6  *      $Id$
7  *
8  *      Copyright (c) 1987,1988 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #ifndef lint
14 static const char rcsid_ZFormatAuthenticNotice_c[] = "$Id$";
15 #endif
16
17 #include <internal.h>
18
19 #if defined(HAVE_KRB4) || defined(HAVE_KRB5)
20 Code_t
21 ZFormatAuthenticNotice(ZNotice_t *notice,
22                        char *buffer,
23                        int buffer_len,
24                        int *len,
25                        C_Block session)
26 {
27     ZNotice_t newnotice;
28     char *ptr;
29     int retval, hdrlen;
30
31     newnotice = *notice;
32     newnotice.z_auth = 1;
33     newnotice.z_authent_len = 0;
34     newnotice.z_ascii_authent = "";
35
36     if ((retval = Z_FormatRawHeader(&newnotice, buffer, buffer_len,
37                                     &hdrlen, &ptr, NULL)) != ZERR_NONE)
38         return (retval);
39
40 #ifdef NOENCRYPTION
41     newnotice.z_checksum = 0;
42 #else
43     newnotice.z_checksum =
44         (ZChecksum_t)des_quad_cksum((unsigned char *)buffer, NULL, ptr - buffer, 0, (C_Block *)session);
45 #endif
46     if ((retval = Z_FormatRawHeader(&newnotice, buffer, buffer_len,
47                                     &hdrlen, NULL, NULL)) != ZERR_NONE)
48         return (retval);
49
50     ptr = buffer+hdrlen;
51
52     if (newnotice.z_message_len+hdrlen > buffer_len)
53         return (ZERR_PKTLEN);
54
55     (void) memcpy(ptr, newnotice.z_message, newnotice.z_message_len);
56
57     *len = hdrlen+newnotice.z_message_len;
58
59     if (*len > Z_MAXPKTLEN)
60         return (ZERR_PKTLEN);
61
62     return (ZERR_NONE);
63 }
64 #endif
65
66 #ifdef HAVE_KRB5
67 Code_t
68 ZFormatAuthenticNoticeV5(ZNotice_t *notice,
69                          register char *buffer,
70                          register int buffer_len,
71                          int *len,
72                          krb5_keyblock *keyblock)
73 {
74     ZNotice_t newnotice;
75     char *ptr;
76     int retval, hdrlen, hdr_adj;
77     krb5_enctype enctype;
78     krb5_cksumtype cksumtype;
79     int key_len;
80     char *cksum_start, *cstart, *cend;
81     int cksum_len;
82     
83     key_len = Z_keylen(keyblock);
84     retval = Z_ExtractEncCksum(keyblock, &enctype, &cksumtype);
85     if (retval)
86          return (ZAUTH_FAILED);
87
88     if (key_len == 8 && (enctype == ENCTYPE_DES_CBC_CRC || 
89                          enctype == ENCTYPE_DES_CBC_MD4 ||
90                          enctype == ENCTYPE_DES_CBC_MD5)) {
91          C_Block tmp;
92          memcpy(&tmp, Z_keydata(keyblock), key_len);
93          return ZFormatAuthenticNotice(notice, buffer, buffer_len, len,
94                                        tmp);
95     }
96          
97     newnotice = *notice;
98     newnotice.z_auth = 1;
99     newnotice.z_authent_len = 0;
100     newnotice.z_ascii_authent = "";
101
102     if ((retval = Z_NewFormatRawHeader(&newnotice, buffer, buffer_len,
103                                        &hdrlen, 
104                                        &cksum_start, &cksum_len, &cstart, 
105                                        &cend)) != ZERR_NONE)
106         return (retval);
107      
108     retval = Z_InsertZcodeChecksum(keyblock, &newnotice, buffer, 
109                                    cksum_start, cksum_len, cstart, cend, 
110                                    buffer_len, &hdr_adj);
111      if (retval)
112           return retval;
113      
114      hdrlen += hdr_adj;
115      
116      ptr = buffer+hdrlen;
117
118      if (newnotice.z_message_len+hdrlen > buffer_len)
119           return (ZERR_PKTLEN);
120      
121      (void) memcpy(ptr, newnotice.z_message, newnotice.z_message_len);
122      
123      *len = hdrlen+newnotice.z_message_len;
124      
125      if (*len > Z_MAXPKTLEN)
126           return (ZERR_PKTLEN);
127      
128      return (ZERR_NONE);
129 }
130 #endif