]> andersk Git - moira.git/blob - util/gdss/lib/crypto/testEncodeP.c
initial import of gdss from the Athena source tree
[moira.git] / util / gdss / lib / crypto / testEncodeP.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 #include "bigkeygen.h"
43 #include "bigrsacode.h"
44
45 #define MAX_NAME 80
46 #define MAX_UID 80
47 #define MAX_KEY 2048
48 #define MAX_HASH 16
49
50 #ifdef DEBUG
51 #undef DEBUG
52 #endif
53
54 static RSAKeyStorage keys ;
55 static RSAKeyStorage public_key ;
56 static RSAKeyStorage private_key ;
57
58 main(argc,argv)
59 int     argc;
60 char    **argv;
61 {
62     unsigned char *encodedP ;
63     char usernameBuf [50], uidBuf [50], x500NameBuf[50], hashkey[50];
64     int uid_len;
65     FILE *publ, *priv;
66
67     if (argc < 2)
68     {
69 badargs:
70         printf("usage: %s name \n", argv[0]);
71         exit(1);
72     }
73
74     strcpy(usernameBuf, argv[1]);
75
76     memset(&keys,0,sizeof(keys));
77     
78     if (read_privkey (usernameBuf, x500NameBuf, uidBuf, &uid_len, hashkey, &private_key)) {
79         printf("\nPrivate key read.");
80         PrintTestKey(&private_key);
81     }
82     else {
83         printf("\nPrivate key read failed.\n");
84         exit(0);
85         }
86         
87     if (read_pubkey (usernameBuf, x500NameBuf, uidBuf, &uid_len, &public_key)){
88         printf("\nPublic key read.");
89         PrintTestKey(&public_key);
90     }
91
92     if ((encodedP=EncodePrivateP(&private_key))==0) {
93         printf("\nEncode private key failed.\n");
94         exit(0);
95         }
96
97     printf("\nEncoded private key (prime P only):\n");
98     dumphex(encodedP,DecodeTotalLength(encodedP));
99
100     printf("\nDecoding...\n");
101     
102     if ((DecodePrivate(encodedP, &public_key))==0) {
103         printf("\nDecode of private key failed.\n");
104         exit(0);
105         }
106     else {
107         printf("\nRecovered Private Key:\n");
108         PrintTestKey (&public_key);
109         }
110
111 BnnClose();
112 exit(0);
113 }
114
115
116
117
118 int read_privkey (filename,name,uid,uid_len,hashkey,key)
119 char *filename, *name, *hashkey;
120 unsigned char *uid;
121 int *uid_len;
122 RSAKeyStorage *key;
123 {
124         static unsigned char buffer [MAX_KEY];
125         DESblock pwkey ;
126         char *ptr;
127         unsigned char *uptr;
128         int i,j,c;
129         FILE *fp;
130         char tempname[80];
131
132         strcpy(tempname,filename);
133         strcat(tempname,"_privkey");
134
135         if((fp=fopen(tempname,"r"))==NULL) {
136                 printf("\nCan't open file %s.\n", tempname);
137                 return(0);
138         }
139
140         ptr=name;
141         for(i=0,j=0;i<MAX_NAME;i++)
142         switch (*ptr++ =getc(fp)){
143            case '{': j++;break;
144            case '}': j--;if(j==0)goto next;break;
145            case EOF : {
146                 printf("\nUnexpected end of file %s.\n",tempname);
147                 return(0);
148            }
149         }       
150         next:
151         if(i>=MAX_NAME-1) {
152                 printf("\nIssuer name too long.\n");
153                 return(0);
154         }
155         *ptr='\0';
156
157 #ifdef DEBUG
158 printf("\nissuer name= %s",name);
159 #endif
160
161         uptr=uid;
162         for(i=0;i<MAX_UID;i++)
163                 if(fscanf(fp,"%2x",&j)==1) *uptr++ =j; else break;
164         if(i==MAX_UID)return(0);
165
166         *uid_len = i;
167         
168 #ifdef DEBUG
169 printf("\nuid=");
170 dumphex(uid,i);
171 #endif
172
173         while(getc(fp)!=';');
174                 for(i=0;i<MAX_HASH;i++)
175                         if(fscanf(fp,"%2x",&j)==1) hashkey[i]=j ;
176                         else break;
177         if(i==MAX_HASH) return(0);
178 #ifdef DEBUG
179 printf("\nSize of hash: %d\n", i);
180 dumphex(hashkey,i);
181 #endif
182
183         while(getc(fp)!=';');
184                 for(i=0;i<MAX_KEY;i++)
185                         if(fscanf(fp,"%2x",&j)==1) buffer[i]=j ;
186                         else break;
187         if(i==MAX_KEY) return(0);
188 #ifdef DEBUG
189 printf("\nSize of private key read: %d\n", i);
190 dumphex(buffer,i);
191 #endif
192
193         if (DES_read_password(&pwkey, "\nEnter Password: ", 0) == 0) {
194             printf("\nError entering password.\n");
195             return(0);
196         }
197
198         memset(key,0,sizeof(*key));
199         if (recover_private(&pwkey,buffer,i,key)==0) {
200                printf("\nError recovering key.\n");
201                return(0);
202         }
203         
204 #ifdef DEBUG
205 printf("\nRecovered Key: \n");
206 PrintTestKey(key);
207 #endif
208
209
210 return(1);
211 }
212
213
This page took 0.106562 seconds and 5 git commands to generate.