]> andersk Git - moira.git/blob - util/gdss/lib/crypto/read_pubkey.c
initial import of gdss from the Athena source tree
[moira.git] / util / gdss / lib / crypto / read_pubkey.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 "BigNum.h"
36 #include "BigRSA.h"
37
38 #define MAX_NAME 80
39 #define MAX_UID 80
40 #define MAX_KEY 2048
41
42 #ifdef DEBUG
43 #undef DEBUG
44 #endif
45
46 int read_pubkey_messages = 0;
47
48 int read_pubkey_verbose (user,name,uid,uid_len,key)
49 RSAKeyStorage *key;
50 char *name;
51 unsigned char *uid;
52 int *uid_len;
53 {
54 int save = read_pubkey_messages, x;
55     read_pubkey_messages = 1;
56     x = read_pubkey (user,name,uid,uid_len,key);
57     read_pubkey_messages = save;
58     return(x);
59 }
60
61 int read_pubkey (user,name,uid,uid_len,key)
62 RSAKeyStorage *key;
63 char *user, *name;
64 unsigned char *uid;
65 int *uid_len;
66 {
67         char tempname[80];
68         static unsigned char buffer [MAX_KEY];
69         char *ptr = name;
70         unsigned char *uptr = uid;
71         int i,j,c;
72         FILE *fp;
73
74         strcpy(tempname,user);
75         strcat(tempname,"_pubkey");
76         if((fp = fopen(tempname,"r")) == NULL) {
77               if (read_pubkey_messages)
78                 printf("\n%s: can't open file %s.\n", __FILE__, tempname);
79               return(0);
80         }
81
82         ptr=name;
83         for(i=0,j=0;i<MAX_NAME;i++) {
84         if((c=getc(fp))==EOF) {
85               if (read_pubkey_messages)
86                 printf("\nUnexpected end of file %s.\n",tempname);
87               return(0);
88            }
89         switch (*ptr++ = (char)c){
90            case '{': j++;break;
91            case '}': j--; if (j==0) goto next; break;
92            case '\n': if(j==0){ptr--;goto next;} break;
93            }
94         }       
95         next:
96         if(i>=MAX_NAME-1) {
97                 if (read_pubkey_messages)
98                      printf("\n%sIssuer name too long.\n", __FILE__);
99                 return(0);
100         }
101         *ptr='\0';
102
103 #ifdef DEBUG
104 printf("\n%s: name is %s",__FILE__,name);
105 #endif
106
107         uptr=uid;
108         for(i=0;i<MAX_UID;i++)
109                 if(fscanf(fp,"%2x",&j)==1) *uptr++ =j; else break;
110         if(i==MAX_UID)return(0);
111
112         *uid_len = i;
113         
114 #ifdef DEBUG
115 printf("\n%s: uid is ", __FILE__);
116 dumphex(uid,i);
117 #endif
118
119         while(getc(fp)!=';');
120                 for(i=0;i<MAX_KEY;i++)
121                         if(fscanf(fp,"%2x",&j)==1) buffer[i]=j ;
122                         else break;
123         if(i==MAX_KEY) {
124               if (read_pubkey_messages)
125                  printf("\n%s: Length error reading public key buffer.\n",__FILE__);
126               return(0);
127         }
128 #ifdef DEBUG
129 printf("\n%s: Size of public key read is %d\n", __FILE__,i);
130 dumphex(buffer,i);
131 #endif
132
133         if(i=DecodePublic(buffer,key)) {
134 #ifdef DEBUG
135 printf("\n%s: Recovered Key is\n",__FILE__);
136 PrintTestKey(key);
137 #endif
138                 return(1);
139         }
140         else {
141               if (read_pubkey_messages) printf("\n%s: Key decode failed.\n",__FILE__);
142               return(0);
143         }                
144 }
145
146
This page took 0.046537 seconds and 5 git commands to generate.