]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sbin/iscontrol/auth_subr.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sbin / iscontrol / auth_subr.c
1 /*-
2  * Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 /*
29  | $Id: auth_subr.c,v 2.2 2007/06/01 08:09:37 danny Exp $
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
39
40 #include <netinet/in.h>
41 #include <netinet/tcp.h>
42 #include <arpa/inet.h>
43 #if __FreeBSD_version < 500000
44 #include <sys/time.h>
45 #endif
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <fcntl.h>
51
52 #include <md5.h>
53 #include <sha.h>
54
55 #include "iscsi.h"
56 #include "iscontrol.h"
57
58 static int
59 chapMD5(char id, char *cp, char *chapSecret, unsigned char *digest)
60 {
61      MD5_CTX    ctx;
62      char       *tmp;
63      int        len;
64
65      debug_called(3);
66
67      MD5Init(&ctx);
68
69      MD5Update(&ctx, &id, 1);
70
71      if((len = str2bin(chapSecret, &tmp)) == 0) {
72           // print error
73           return -1;
74      }
75      MD5Update(&ctx, tmp, len);
76      free(tmp);
77
78      if((len = str2bin(cp, &tmp)) == 0) {
79           // print error
80           return -1;
81      }
82      MD5Update(&ctx, tmp, len);
83      free(tmp);
84
85      MD5Final(digest, &ctx);
86      
87
88      return 0;
89 }
90
91 static int
92 chapSHA1(char id, char *cp, char *chapSecret, unsigned char *digest)
93 {
94      SHA1_CTX   ctx;
95      char       *tmp;
96      int        len;
97
98      debug_called(3);
99
100      SHA1_Init(&ctx);
101      
102      SHA1_Update(&ctx, &id, 1);
103
104      if((len = str2bin(chapSecret, &tmp)) == 0) {
105           // print error
106           return -1;
107      }
108      SHA1_Update(&ctx, tmp, len);
109      free(tmp);
110
111      if((len = str2bin(cp, &tmp)) == 0) {
112           // print error
113           return -1;
114      }
115      SHA1_Update(&ctx, tmp, len);
116      free(tmp);
117
118      SHA1_Final(digest, &ctx);
119
120      return 0;
121     
122 }
123 /*
124  | the input text format can be anything that the rfc3270 defines
125  | (see section 5.1 and str2bin)
126  | digest length for md5 is 128bits, and for sha1 is 160bits.
127  | digest is an ASCII string which represents the bits in 
128  | hexadecimal or base64 according to the challenge(cp) format
129  */
130 char *
131 chapDigest(char *ap, char id, char *cp, char *chapSecret)
132 {
133      int        len;
134      unsigned   char digest[20];
135      char       encoding[3];
136
137      debug_called(3);
138
139      len = 0;
140      if(strcmp(ap, "5") == 0 && chapMD5(id, cp, chapSecret, digest) == 0)
141           len = 16;
142      else
143      if(strcmp(ap, "7") == 0 && chapSHA1(id, cp, chapSecret, digest) == 0)
144           len = 20;
145
146      if(len) {
147           sprintf(encoding, "%.2s", cp);
148           return bin2str(encoding, digest, len);
149      }
150
151      return NULL;
152 }
153
154 char *
155 genChapChallenge(char *encoding, int len)
156 {
157      int        fd;
158      unsigned   char tmp[1024];
159
160      if(len > sizeof(tmp))
161           return NULL;
162
163      if((fd = open("/dev/random", O_RDONLY)) != -1) {
164           read(fd, tmp, len);
165           close(fd);
166           return bin2str(encoding, tmp, len);
167      }
168      perror("/dev/random");
169      // make up something ...
170      return NULL;
171 }
172
173 #ifdef TEST_AUTH
174 static void
175 puke(char *str, unsigned char *dg, int len)
176 {
177      printf("%3d] %s\n     0x", len, str);
178      while(len-- > 0)
179           printf("%02x", *dg++);
180      printf("\n");
181 }
182
183 main(int cc, char **vv)
184 {
185      char *p, *ap, *ip, *cp, *chapSecret, *digest;
186      int len;
187
188 #if 0
189      ap = "5";
190      chapSecret = "0xa5aff013dd839b1edd31ee73a1df0b1b";
191 //     chapSecret = "abcdefghijklmnop";
192      len = str2bin(chapSecret, &cp);
193      puke(chapSecret, cp, len);
194
195      ip = "238";
196      cp = "0xbd456029";
197
198      
199      if((digest = chapDigest(ap, ip, cp, chapSecret)) != NULL) {
200           len = str2bin(digest, &cp);
201           puke(digest, cp, len);
202      }
203 #else
204      printf("%d] %s\n", 24, genChallenge("0X", 24));
205 #endif
206 }
207 #endif