6 #define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n"
10 struct rsaprivkey_if ops;
17 struct rsapubkey_if ops;
22 /* Sign data. NB data must be smaller than modulus */
24 static char *hexchars="0123456789abcdef";
26 static string_t rsa_sign(void *sst, uint8_t *data, uint32_t datalen)
28 struct rsapriv *st=sst;
37 msize=mpz_sizeinbase(&st->n, 16);
39 if (datalen*2+4>=msize) {
40 fatal("rsa_sign: message too big\n");
45 for (i=0; i<datalen; i++) {
46 buff[4+i*2]=hexchars[(data[i]&0xf0)>>4];
47 buff[5+i*2]=hexchars[data[i]&0xf];
51 for (i=datalen*2+4; i<msize; i++)
56 mpz_set_str(&a, buff, 16);
58 mpz_powm(&b, &a, &st->d, &st->n);
60 signature=write_mpstring(&b);
67 static bool_t rsa_sig_check(void *sst, uint8_t *data, uint32_t datalen,
70 struct rsapub *st=sst;
80 msize=mpz_sizeinbase(&st->n, 16);
84 for (i=0; i<datalen; i++) {
85 buff[4+i*2]=hexchars[(data[i]&0xf0)>>4];
86 buff[5+i*2]=hexchars[data[i]&0xf];
90 for (i=datalen*2+4; i<msize; i++)
95 mpz_set_str(&a, buff, 16);
97 mpz_set_str(&b, signature, 16);
99 mpz_powm(&c, &b, &st->e, &st->n);
101 ok=(mpz_cmp(&a, &c)==0);
110 static list_t *rsapub_apply(closure_t *self, struct cloc loc, dict_t *context,
117 st=safe_malloc(sizeof(*st),"rsapub_apply");
118 st->cl.description="rsapub";
119 st->cl.type=CL_RSAPUBKEY;
121 st->cl.interface=&st->ops;
123 st->ops.check=rsa_sig_check;
128 if (i->type!=t_string) {
129 cfgfatal(i->loc,"rsa-public","first argument must be a string");
132 if (mpz_init_set_str(&st->e,e,10)!=0) {
133 cfgfatal(i->loc,"rsa-public","encryption key \"%s\" is not a "
134 "decimal number string\n",e);
137 cfgfatal(loc,"rsa-public","you must provide an encryption key\n");
142 if (i->type!=t_string) {
143 cfgfatal(i->loc,"rsa-public","second argument must be a string");
146 if (mpz_init_set_str(&st->n,n,10)!=0) {
147 cfgfatal(i->loc,"rsa-public","modulus \"%s\" is not a decimal "
148 "number string\n",n);
151 cfgfatal(loc,"rsa-public","you must provide a modulus\n");
153 return new_closure(&st->cl);
156 static uint32_t keyfile_get_int(FILE *f)
166 static uint16_t keyfile_get_short(FILE *f)
174 static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context,
184 MP_INT e,sig,plain,check;
186 st=safe_malloc(sizeof(*st),"rsapriv_apply");
187 st->cl.description="rsapriv";
188 st->cl.type=CL_RSAPRIVKEY;
190 st->cl.interface=&st->ops;
192 st->ops.sign=rsa_sign;
195 /* Argument is filename pointing to SSH1 private key file */
198 if (i->type!=t_string) {
199 cfgfatal(i->loc,"rsa-public","first argument must be a string");
201 filename=i->data.string;
203 filename=""; /* Make compiler happy */
204 cfgfatal(loc,"rsa-private","you must provide a filename\n");
207 f=fopen(filename,"rb");
209 if (just_check_config) {
210 Message(M_WARNING,"rsa-private (%s:%d): cannot open keyfile "
211 "\"%s\"; assuming it's valid while we check the "
212 "rest of the configuration\n",loc.file,loc.line,filename);
215 fatal_perror("rsa-private (%s:%d): cannot open file \"%s\"",
216 loc.file,loc.line,filename);
220 /* Check that the ID string is correct */
221 length=strlen(AUTHFILE_ID_STRING)+1;
222 b=safe_malloc(length,"rsapriv_apply");
223 if (fread(b,length,1,f)!=1 || memcmp(b,AUTHFILE_ID_STRING,length)!=0) {
224 cfgfatal(loc,"rsa-private","file \"%s\" is not a "
225 "SSH1 private keyfile\n",filename);
229 cipher_type=fgetc(f);
230 keyfile_get_int(f); /* "Reserved data" */
231 if (cipher_type != 0) {
232 cfgfatal(loc,"rsa-private","we don't support encrypted keyfiles\n");
235 /* Read the public key */
236 keyfile_get_int(f); /* Not sure what this is */
237 length=(keyfile_get_short(f)+7)/8;
239 cfgfatal(loc,"rsa-private","implausible length %ld for modulus\n",
242 b=safe_malloc(length,"rsapriv_apply");
243 if (fread(b,length,1,f) != 1) {
244 cfgfatal(loc,"rsa-private","error reading modulus\n");
247 read_mpbin(&st->n,b,length);
249 length=(keyfile_get_short(f)+7)/8;
251 cfgfatal(loc,"rsa-private","implausible length %ld for e\n",length);
253 b=safe_malloc(length,"rsapriv_apply");
254 if (fread(b,length,1,f)!=1) {
255 cfgfatal(loc,"rsa-private","error reading e\n");
258 read_mpbin(&e,b,length);
261 length=keyfile_get_int(f);
263 cfgfatal(loc,"rsa-private","implausibly long (%ld) key comment\n",
266 c=safe_malloc(length+1,"rsapriv_apply");
267 if (fread(c,length,1,f)!=1) {
268 cfgfatal(loc,"rsa-private","error reading key comment\n");
272 /* Check that the next two pairs of characters are identical - the
273 keyfile is not encrypted, so they should be */
274 if (keyfile_get_short(f) != keyfile_get_short(f)) {
275 cfgfatal(loc,"rsa-private","corrupt keyfile\n");
279 length=(keyfile_get_short(f)+7)/8;
281 cfgfatal(loc,"rsa-private","implausibly long (%ld) decryption key\n",
284 b=safe_malloc(length,"rsapriv_apply");
285 if (fread(b,length,1,f)!=1) {
286 cfgfatal(loc,"rsa-private","error reading decryption key\n");
289 read_mpbin(&st->d,b,length);
293 fatal_perror("rsa-private (%s:%d): fclose",loc.file,loc.line);
296 /* Now do trial signature/check to make sure it's a real keypair:
297 sign the comment string! */
299 if (i && i->type==t_bool && i->data.bool==False) {
300 Message(M_INFO,"rsa-private (%s:%d): skipping RSA key validity "
301 "check\n",loc.file,loc.line);
306 read_mpbin(&plain,c,strlen(c));
307 mpz_powm(&sig, &plain, &st->d, &st->n);
308 mpz_powm(&check, &sig, &e, &st->n);
309 if (mpz_cmp(&plain,&check)!=0) {
310 cfgfatal(loc,"rsa-private","file \"%s\" does not contain a "
311 "valid RSA key!\n",filename);
322 return new_closure(&st->cl);
325 init_module rsa_module;
326 void rsa_module(dict_t *dict)
328 add_closure(dict,"rsa-private",rsapriv_apply);
329 add_closure(dict,"rsa-public",rsapub_apply);