]> andersk Git - moira.git/blob - util/gdss/lib/crypto/read_privkey.c
Remove incorrect krb_get_lrealm() prototype.
[moira.git] / util / gdss / lib / crypto / read_privkey.c
1 /*
2  * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
3  * ALL RIGHTS RESERVED
4  *
5  * "Digital Equipment Corporation authorizes the reproduction,
6  * distribution and modification of this software subject to the following
7  * restrictions:
8  * 
9  * 1.  Any partial or whole copy of this software, or any modification
10  * thereof, must include this copyright notice in its entirety.
11  *
12  * 2.  This software is supplied "as is" with no warranty of any kind,
13  * expressed or implied, for any purpose, including any warranty of fitness 
14  * or merchantibility.  DIGITAL assumes no responsibility for the use or
15  * reliability of this software, nor promises to provide any form of 
16  * support for it on any basis.
17  *
18  * 3.  Distribution of this software is authorized only if no profit or
19  * remuneration of any kind is received in exchange for such distribution.
20  * 
21  * 4.  This software produces public key authentication certificates
22  * bearing an expiration date established by DIGITAL and RSA Data
23  * Security, Inc.  It may cease to generate certificates after the expiration
24  * date.  Any modification of this software that changes or defeats
25  * the expiration date or its effect is unauthorized.
26  * 
27  * 5.  Software that will renew or extend the expiration date of
28  * authentication certificates produced by this software may be obtained
29  * from RSA Data Security, Inc., 10 Twin Dolphin Drive, Redwood City, CA
30  * 94065, (415)595-8782, or from DIGITAL"
31  *
32  */
33
34 #include <stdio.h>
35 #include <ctype.h>
36
37 #include "BigNum.h"
38 #include "BigRSA.h"
39 #include "random.h"
40 #include "hashes.h"
41 #include "read_password.h"
42
43 #define MAX_NAME 80
44 #define MAX_UID 80
45 #define MAX_KEY 2048
46 #define MAX_HASH 8
47
48 #ifdef DEBUG
49 #undef DEBUG
50 #endif
51
52 int read_privkey_messages =0;
53 RSAKeyStorage RSAKeys ;
54
55 int read_privkey_verbose(filename,name,uid,uid_len,hashkey,key)
56 char *filename, *name, *hashkey;
57 unsigned char *uid;
58 int *uid_len;
59 RSAKeyStorage *key;
60 {
61 int save = read_privkey_messages ,x;
62   read_privkey_messages = 1;
63   x=read_privkey (filename,name,uid,uid_len,hashkey,key);
64   read_privkey_messages = save;
65   return(x);
66 }
67
68 int read_privkey(filename,name,uid,uid_len,hashkey,key)
69 char *filename, *name, *hashkey;
70 unsigned char *uid;
71 int *uid_len;
72 RSAKeyStorage *key;
73 {
74         static unsigned char buffer [MAX_KEY];
75         DESblock pwkey ;
76         char *ptr;
77         unsigned char *uptr;
78         int i,j,c;
79         FILE *fp;
80         char tempname[80], prompt[80];
81
82         strcpy(tempname,filename);
83         strcat(tempname,"_privkey");
84
85         if((fp=fopen(tempname,"r"))==NULL) {
86                 if (read_privkey_messages) 
87                         printf("\n%s: Can't open file %s.\n", __FILE__,tempname);
88                 return(0);
89         }
90
91         ptr=name;
92         for(i=0,j=0;i<MAX_NAME;i++) {
93         if((c=getc(fp))==EOF) {
94                 if (read_privkey_messages) 
95                         printf("\n%s: Unexpected end of file %s.\n",__FILE__,tempname);
96                 return(0);
97             }
98         switch (*ptr++ = (char) c){
99            case '{': j++; 
100                      break;
101            case '}': j--;
102                      if(j==0) goto next;
103                      break;
104            case '\n': if(j==0) {ptr--; goto next;}
105                       break;
106            }
107         }       
108         next:
109         if(i>=MAX_NAME-1) {
110                 if (read_privkey_messages) 
111                         printf("\n%s: Issuer name too long.\n", __FILE__);
112                 return(0);
113         }
114         *ptr='\0';
115
116 #ifdef DEBUG
117 printf("\n%s: issuer name= %s",__FILE__,name);
118 #endif
119
120         uptr=uid;
121         for(i=0;i<MAX_UID;i++)
122                 if(fscanf(fp,"%2x",&j)==1) *uptr++ =j; else break;
123         if(i==MAX_UID)return(0);
124
125         *uid_len = i;
126         
127 #ifdef DEBUG
128 printf("\n%s: uid is ",__FILE__);
129 dumphex(uid,i);
130 #endif
131
132         while(getc(fp)!=';');
133         for(i=0;i<MAX_HASH;i++)
134                 if(fscanf(fp,"%2x",&j)==1) hashkey[i]=j ;
135                 else break;
136 #ifdef DEBUG
137 printf("\n%s: Size of hash: %d\n", __FILE__,i);
138 dumphex(hashkey,i);
139 #endif
140         
141         /* next non-white must be a ';' */
142         do {j=getc(fp);}while(isspace(j));
143         if(j != ';'){
144                 if(read_privkey_messages) printf("\n%s: Hash Overflow.\n",__FILE__);
145                 return(0);
146         }
147
148         for(i=0;i<MAX_KEY;i++)
149                 if(fscanf(fp,"%2x",&j)==1) buffer[i]=j ;
150                 else break;
151         if(i==MAX_KEY) return(0);
152 #ifdef DEBUG
153 printf("\n%s: Size of private key read: %d\n", __FILE__,i);
154 dumphex(buffer,i);
155 #endif
156         sprintf(prompt,"\nEnter %s's password: ", filename);
157         if (DES_read_password(&pwkey, prompt, 0) == 0) {
158             if (read_privkey_messages) printf("\n%s, Error entering password.\n",__FILE__);
159             return(0);
160         }
161
162         memset(key,0,sizeof(*key));
163         if (recover_private(&pwkey,buffer,i,key)==0) {
164                if (read_privkey_messages) printf("\nError recovering key.\n");
165                return(0);
166         }
167         
168 #ifdef DEBUG
169 printf("\n%s: Recovered Key: \n",__FILE__);
170 PrintTestKey(key);
171 #endif
172
173 return(1);
174 }
175
176
This page took 0.579891 seconds and 5 git commands to generate.