summaryrefslogtreecommitdiff
path: root/security/nss/cmd/lib/dongle.c
blob: 1c819ee51f5cabc893a80d54745af53e15f834c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is the Netscape security libraries.
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 */
#include "secutil.h"
#include "sechash.h"

#ifdef XP_UNIX
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#endif
#ifdef XP_WIN
#include <windows.h>
#include <winsock.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
#ifdef XP_MAC
#include <unistd.h>
#endif

#ifdef XP_UNIX
#include "prnetdb.h"
#else
#define PRHostEnt struct hostent
#define PR_NETDB_BUF_SIZE 5
#endif

static RC4Context *
sec_MakeDongleKey(void)
{
    RC4Context *rc4;
    char hostname[64];
    PRHostEnt hpbuf, *hent;
    char dbbuf[PR_NETDB_BUF_SIZE];
    int ipaddr;
    struct stat s;
    int found = 1; /* Whether /vmunix, etc. found */
    unsigned char *buf, *totalbuf;
    MD5Context *md5 = 0;
    unsigned char digest[16];
    unsigned int digestLen;

    /* NOTE: Fix MAC and WIN to stat something comparable to kernel on UNIX */
#ifdef XP_MAC
    return NULL;
#elif defined(XP_WIN)
    return NULL;
#endif

    /* gather system data */
    if( gethostname( hostname, 64 ) < 0 ) {
	return(NULL);
    }

#ifdef XP_UNIX
#ifndef NSPR20
    hent = PR_gethostbyname(hostname, &hpbuf, dbbuf, sizeof(dbbuf), 0);
#else
    if (PR_GetHostByName(hostname, dbbuf, sizeof(dbbuf), &hpbuf) == PR_SUCCESS)
	hent = &hpbuf;
    else
	hent = NULL;
#endif
#else
    hent = gethostbyname(hostname);
#endif
    if (hent == NULL) {
	return(NULL);
    }

    ipaddr = htonl(((struct in_addr *)hent->h_addr)->s_addr);

    /*
    ** /unix         IRIX & AIX & SCO
    ** /vmunix       SunOS & OSF
    ** /kernel/unix  Solaris
    ** /vmlinuz      Linux
    ** /hp-ux        HP-UX
    ** /bsd          BSD
    */
    if ( (stat("/unix", &s) == -1 ) && (stat("/vmunix", &s) == -1) &&
	 (stat("/bsd", &s) == -1) && (stat("kernel/unix", &s)== -1) &&
	 (stat("/hp-ux", &s) == -1) && (stat("/vmlinuz", &s) == -1) ) {
	found = 0;
    }
								      
    buf = totalbuf = (unsigned char *)
	XP_CALLOC(1, 1000 + sizeof(ipaddr) + sizeof(s) + PORT_Strlen(hostname));
    
    if ( buf == 0 ) {
	return(NULL);
    }

    PORT_Memcpy(buf, hostname, PORT_Strlen(hostname));
    buf += PORT_Strlen(hostname);
    
    PORT_Memcpy(buf, &ipaddr, sizeof(ipaddr));
    buf += sizeof(ipaddr);
   
    if (found) {
	PORT_Memcpy(buf, &s.st_mode, sizeof(s.st_mode));
	buf += sizeof(s.st_mode);
    
	PORT_Memcpy(buf, &s.st_ino, sizeof(s.st_ino));
	buf += sizeof(s.st_ino);
	
	PORT_Memcpy(buf, &s.st_dev, sizeof(s.st_dev));
	buf += sizeof(s.st_dev);
	
	PORT_Memcpy(buf, &s.st_mtime, sizeof(s.st_mtime));
	buf += sizeof(s.st_mtime);
    }

    /* Digest the system info using MD5 */
    md5 = MD5_NewContext();
    if (md5 == NULL) {
	return (NULL);
    }

    MD5_Begin(md5);
    MD5_Update(md5, totalbuf, PORT_Strlen((char *)totalbuf));
    MD5_End(md5, digest, &digestLen, MD5_LENGTH);

    /* Make an RC4 key using the digest */
    rc4 = RC4_CreateContext(digest, digestLen);

    /* Zero out information */
    MD5_DestroyContext(md5, PR_TRUE);
    PORT_Memset(digest, 0 , sizeof(digest));

   
    return(rc4);
}

extern unsigned char *
SEC_ReadDongleFile(int fd)
{
    RC4Context *rc4;
    struct stat s;
    unsigned char *inbuf, *pw;
    int nb;
    unsigned int pwlen, inlen;
    SECStatus rv;
    
    rc4 = sec_MakeDongleKey();
    if (rc4 == NULL) {
	return(0);
    }

    /* get size of file */
    if ( fstat(fd, &s) < 0 ) {
	return(0);
    }

    inlen = s.st_size;
    
    inbuf = (unsigned char *) PORT_Alloc(inlen);
    if (!inbuf) {
	return(0);
    }

    nb = read(fd, (char *)inbuf, inlen);
    if (nb != inlen) {
	return(0);
    }

    pw = (unsigned char *) PORT_Alloc(inlen);
    if (pw == 0) {
	return(0);
    }

    rv = RC4_Decrypt(rc4, pw, &pwlen, inlen, inbuf, inlen);
    if (rv) {
	return(0);
    }

    PORT_Free(inbuf);

    return(pw);
}

extern SECStatus
SEC_WriteDongleFile(int fd, unsigned char *pw)
{
    RC4Context *rc4;
    unsigned char *outbuf;
    unsigned int outlen;
    SECStatus rv;
    int nb;
    
    rc4 = sec_MakeDongleKey();
    if (rc4 == NULL) {
	return(SECFailure);
    }

    outbuf = (unsigned char *) PORT_Alloc(PORT_Strlen((char *)pw) + 1);
    if (!outbuf) {
	return(SECFailure);
    }

    rv = RC4_Encrypt(rc4, outbuf, &outlen, PORT_Strlen((char *)pw), pw,
		     PORT_Strlen((char *)pw));
    if (rv) {
	return(SECFailure);
    }

    RC4_DestroyContext(rc4, PR_TRUE);

    nb = write(fd, (char *)outbuf, outlen);
    if (nb != outlen) {
	return(SECFailure);
    }

    PORT_Free(outbuf);

    return(SECSuccess);
}