chiark / gitweb /
3ce72b86fcdbbb3b609cf092943a7eb38f3614c3
[secnet.git] / rsa.c
1 /*
2  * rsa.c: implementation of RSA with PKCS#1 padding
3  */
4 /*
5  * This file is Free Software.  It was originally written for secnet.
6  *
7  * Copyright 1995-2003 Stephen Early
8  * Copyright 2002-2014 Ian Jackson
9  * Copyright 2001      Simon Tatham
10  * Copyright 2013      Mark Wooding
11  *
12  * You may redistribute secnet as a whole and/or modify it under the
13  * terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3, or (at your option) any
15  * later version.
16  *
17  * You may redistribute this file and/or modify it under the terms of
18  * the GNU General Public License as published by the Free Software
19  * Foundation; either version 2, or (at your option) any later
20  * version.
21  *
22  * This software is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this software; if not, see
29  * https://www.gnu.org/licenses/gpl.html.
30  */
31
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <gmp.h>
36 #include "secnet.h"
37 #include "util.h"
38 #include "unaligned.h"
39
40 #define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n"
41
42 #define mpp(s,n) do { char *p = mpz_get_str(NULL,16,n); printf("%s 0x%sL\n", s, p); free(p); } while (0)
43
44 struct rsacommon {
45     uint8_t *hashbuf;
46 };
47
48 struct load_ctx {
49     void (*verror)(struct load_ctx *l,
50                    FILE *maybe_f, bool_t unsup,
51                    const char *message, va_list args);
52     bool_t (*postreadcheck)(struct load_ctx *l, FILE *f);
53     const char *what;
54     struct cloc *loc;
55     union {
56         struct {
57             struct log_if *log;
58         } tryload;
59     } u;
60 };
61
62 FORMAT(printf,4,0)
63 static void verror_tryload(struct load_ctx *l,
64                            FILE *maybe_f, bool_t unsup,
65                            const char *message, va_list args)
66 {
67     int class=unsup ? M_DEBUG : M_ERR;
68     slilog_part(l->u.tryload.log,class,"%s: ",l->what);
69     vslilog(l->u.tryload.log,class,message,args);
70 }
71
72 static void verror_cfgfatal(struct load_ctx *l,
73                             FILE *maybe_f, bool_t unsup,
74                             const char *message, va_list args)
75 {
76     vcfgfatal_maybefile(maybe_f,*l->loc,l->what,message,args);
77 }
78
79 struct rsapriv {
80     closure_t cl;
81     struct sigprivkey_if ops;
82     struct cloc loc;
83     struct rsacommon common;
84     MP_INT n;
85     MP_INT p, dp;
86     MP_INT q, dq;
87     MP_INT w;
88 };
89 struct rsapub {
90     closure_t cl;
91     struct sigpubkey_if ops;
92     struct cloc loc;
93     struct rsacommon common;
94     MP_INT e;
95     MP_INT n;
96 };
97 /* Sign data. NB data must be smaller than modulus */
98
99 #define RSA_MAX_MODBYTES 2048
100 /* The largest modulus I've seen is 15360 bits, which works out at 1920
101  * bytes.  Using keys this big is quite implausible, but it doesn't cost us
102  * much to support them.
103  */
104
105 static const char *hexchars="0123456789abcdef";
106
107 static void rsa_sethash(struct rsacommon *c, struct hash_if *hash,
108                         const struct hash_if **in_ops)
109 {
110     free(c->hashbuf);
111     c->hashbuf=safe_malloc(hash->hlen, "generate_msg");
112     *in_ops=hash;
113 }
114 static void rsa_pub_sethash(void *sst, struct hash_if *hash)
115 {
116     struct rsapub *st=sst;
117     rsa_sethash(&st->common, hash, &st->ops.hash);
118 }
119 static void rsa_priv_sethash(void *sst, struct hash_if *hash)
120 {
121     struct rsapriv *st=sst;
122     rsa_sethash(&st->common, hash, &st->ops.hash);
123 }
124 static void rsacommon_dispose(struct rsacommon *c)
125 {
126     free(c->hashbuf);
127 }
128
129 static void emsa_pkcs1(MP_INT *n, MP_INT *m,
130                        const uint8_t *data, int32_t datalen)
131 {
132     char buff[2*RSA_MAX_MODBYTES + 1];
133     int msize, i;
134
135     /* RSA PKCS#1 v1.5 signature padding:
136      *
137      * <------------ msize hex digits ---------->
138      *
139      * 00 01 ff ff .... ff ff 00 vv vv vv .... vv
140      *
141      *                           <--- datalen -->
142      *                                 bytes
143      *                         = datalen*2 hex digits
144      *
145      * NB that according to PKCS#1 v1.5 we're supposed to include a
146      * hash function OID in the data.  We don't do that (because we
147      * don't have the hash function OID to hand here), thus violating
148      * the spec in a way that affects interop but not security.
149      *
150      * -iwj 17.9.2002
151      */
152
153     msize=mpz_sizeinbase(n, 16);
154
155     if (datalen*2+6>=msize) {
156         fatal("rsa: message too big");
157     }
158
159     strcpy(buff,"0001");
160
161     for (i=0; i<datalen; i++) {
162         buff[msize+(-datalen+i)*2]=hexchars[(data[i]&0xf0)>>4];
163         buff[msize+(-datalen+i)*2+1]=hexchars[data[i]&0xf];
164     }
165     
166     buff[msize-datalen*2-2]= '0';
167     buff[msize-datalen*2-1]= '0';
168  
169     for (i=4; i<msize-datalen*2-2; i++)
170        buff[i]='f';
171
172     buff[msize]=0;
173
174     mpz_set_str(m, buff, 16);
175 }
176
177 static bool_t rsa_sign(void *sst, uint8_t *data, int32_t datalen,
178                        struct buffer_if *msg)
179 {
180     struct rsapriv *st=sst;
181     MP_INT a, b, u, v, tmp, tmp2;
182     string_t signature = 0;
183     bool_t ok;
184
185     mpz_init(&a);
186     mpz_init(&b);
187
188     hash_hash(st->ops.hash,data,datalen,st->common.hashbuf);
189     /* Construct the message representative. */
190     emsa_pkcs1(&st->n, &a, st->common.hashbuf, st->ops.hash->hlen);
191
192     /*
193      * Produce an RSA signature (a^d mod n) using the Chinese
194      * Remainder Theorem. We compute:
195      * 
196      *   u = a^dp mod p    (== a^d mod p, since dp == d mod (p-1))
197      *   v = a^dq mod q    (== a^d mod q, similarly)
198      * 
199      * We also know w == iqmp * q, which has the property that w ==
200      * 0 mod q and w == 1 mod p. So (1-w) has the reverse property
201      * (congruent to 0 mod p and to 1 mod q). Hence we now compute
202      * 
203      *   b = w * u + (1-w) * v
204      *     = w * (u-v) + v
205      * 
206      * so that b is congruent to a^d both mod p and mod q. Hence b,
207      * reduced mod n, is the required signature.
208      */
209     mpz_init(&tmp);
210     mpz_init(&tmp2);
211     mpz_init(&u);
212     mpz_init(&v);
213
214     mpz_powm_sec(&u, &a, &st->dp, &st->p);
215     mpz_powm_sec(&v, &a, &st->dq, &st->q);
216     mpz_sub(&tmp, &u, &v);
217     mpz_mul(&tmp2, &tmp, &st->w);
218     mpz_add(&tmp, &tmp2, &v);
219     mpz_mod(&b, &tmp, &st->n);
220
221     mpz_clear(&tmp);
222     mpz_clear(&tmp2);
223     mpz_clear(&u);
224     mpz_clear(&v);
225
226     signature=write_mpstring(&b);
227
228     uint8_t *op = buf_append(msg,2);
229     if (!op) { ok=False; goto out; }
230     size_t l = strlen(signature);
231     assert(l < 65536);
232     put_uint16(op, l);
233     op = buf_append(msg,l);
234     if (!op) { ok=False; goto out; }
235     memcpy(op, signature, l);
236
237     ok = True;
238
239  out:
240     free(signature);
241     mpz_clear(&b);
242     mpz_clear(&a);
243     return ok;
244 }
245
246 static bool_t rsa_sig_unpick(void *sst, struct buffer_if *msg,
247                              struct alg_msg_data *sig)
248 {
249     uint8_t *lp = buf_unprepend(msg, 2);
250     if (!lp) return False;
251     sig->len = get_uint16(lp);
252     sig->start = buf_unprepend(msg, sig->len);
253     if (!sig->start) return False;
254
255     /* In `rsa_sig_check' below, we assume that we can write a nul
256      * terminator following the signature.  Make sure there's enough space.
257      */
258     if (msg->start >= msg->base + msg->alloclen)
259         return False;
260
261     return True;
262 }
263
264 static sig_checksig_fn rsa_sig_check;
265 static bool_t rsa_sig_check(void *sst, uint8_t *data, int32_t datalen,
266                             const struct alg_msg_data *sig)
267 {
268     struct rsapub *st=sst;
269     MP_INT a, b, c;
270     bool_t ok;
271
272     mpz_init(&a);
273     mpz_init(&b);
274     mpz_init(&c);
275
276     hash_hash(st->ops.hash,data,datalen,st->common.hashbuf);
277     emsa_pkcs1(&st->n, &a, st->common.hashbuf, st->ops.hash->hlen);
278
279     /* Terminate signature with a '0' - already checked that this will fit */
280     int save = sig->start[sig->len];
281     sig->start[sig->len] = 0;
282     mpz_set_str(&b, sig->start, 16);
283     sig->start[sig->len] = save;
284
285     mpz_powm(&c, &b, &st->e, &st->n);
286
287     ok=(mpz_cmp(&a, &c)==0);
288
289     mpz_clear(&c);
290     mpz_clear(&b);
291     mpz_clear(&a);
292
293     return ok;
294 }
295
296 static void rsapub_dispose(void *sst) {
297     struct rsapub *st=sst;
298
299     mpz_clear(&st->e);
300     mpz_clear(&st->n);
301     rsacommon_dispose(&st->common);
302     free(st);
303 }
304
305 static list_t *rsapub_apply(closure_t *self, struct cloc loc, dict_t *context,
306                             list_t *args)
307 {
308     struct rsapub *st;
309     item_t *i;
310     string_t e,n;
311
312     NEW(st);
313     st->cl.description="rsapub";
314     st->cl.type=CL_SIGPUBKEY;
315     st->cl.apply=NULL;
316     st->cl.interface=&st->ops;
317     st->ops.st=st;
318     st->ops.sethash=rsa_pub_sethash;
319     st->common.hashbuf=NULL;
320     st->ops.unpick=rsa_sig_unpick;
321     st->ops.check=rsa_sig_check;
322     st->ops.hash=0;
323     st->ops.dispose=rsapub_dispose;
324     st->loc=loc;
325
326     i=list_elem(args,0);
327     if (i) {
328         if (i->type!=t_string) {
329             cfgfatal(i->loc,"rsa-public","first argument must be a string\n");
330         }
331         e=i->data.string;
332         if (mpz_init_set_str(&st->e,e,10)!=0) {
333             cfgfatal(i->loc,"rsa-public","encryption key \"%s\" is not a "
334                      "decimal number string\n",e);
335         }
336     } else {
337         cfgfatal(loc,"rsa-public","you must provide an encryption key\n");
338     }
339     if (mpz_sizeinbase(&st->e, 256) > RSA_MAX_MODBYTES) {
340         cfgfatal(loc, "rsa-public", "implausibly large public exponent\n");
341     }
342     
343     i=list_elem(args,1);
344     if (i) {
345         if (i->type!=t_string) {
346             cfgfatal(i->loc,"rsa-public","second argument must be a string\n");
347         }
348         n=i->data.string;
349         if (mpz_init_set_str(&st->n,n,10)!=0) {
350             cfgfatal(i->loc,"rsa-public","modulus \"%s\" is not a decimal "
351                      "number string\n",n);
352         }
353     } else {
354         cfgfatal(loc,"rsa-public","you must provide a modulus\n");
355     }
356     if (mpz_sizeinbase(&st->n, 256) > RSA_MAX_MODBYTES) {
357         cfgfatal(loc, "rsa-public", "implausibly large modulus\n");
358     }
359     return new_closure(&st->cl);
360 }
361
362 static void load_error(struct load_ctx *l, FILE *maybe_f,
363                        bool_t unsup, const char *fmt, ...)
364 {
365     va_list al;
366     va_start(al,fmt);
367     l->verror(l,maybe_f,unsup,fmt,al);
368     va_end(al);
369 }
370
371 #define LDFATAL(...)      ({ load_error(l,0,0,__VA_ARGS__); goto error_out; })
372 #define LDUNSUP(...)      ({ load_error(l,0,1,__VA_ARGS__); goto error_out; })
373 #define LDFATAL_FILE(...) ({ load_error(l,f,0,__VA_ARGS__); goto error_out; })
374 #define LDUNSUP_FILE(...) ({ load_error(l,f,1,__VA_ARGS__); goto error_out; })
375 #define FREE(b)                ({ free((b)); (b)=0; })
376 #define KEYFILE_GET(is)   ({                                    \
377         uint##is##_t keyfile_get_tmp=keyfile_get_##is(l,f);     \
378         if (!l->postreadcheck(l,f)) goto error_out;             \
379         keyfile_get_tmp;                                        \
380     })
381
382 static uint32_t keyfile_get_32(struct load_ctx *l, FILE *f)
383 {
384     uint32_t r;
385     r=fgetc(f)<<24;
386     r|=fgetc(f)<<16;
387     r|=fgetc(f)<<8;
388     r|=fgetc(f);
389     return r;
390 }
391
392 static uint16_t keyfile_get_16(struct load_ctx *l, FILE *f)
393 {
394     uint16_t r;
395     r=fgetc(f)<<8;
396     r|=fgetc(f);
397     return r;
398 }
399
400 static void rsapriv_dispose(void *sst)
401 {
402     struct rsapriv *st=sst;
403     mpz_clear(&st->n);
404     mpz_clear(&st->p); mpz_clear(&st->dp);
405     mpz_clear(&st->q); mpz_clear(&st->dq);
406     mpz_clear(&st->w);
407     rsacommon_dispose(&st->common);
408     free(st);
409 }
410
411 static struct rsapriv *rsa_loadpriv_core(struct load_ctx *l,
412                                          FILE *f, struct cloc loc,
413                                          bool_t do_validity_check)
414 {
415     struct rsapriv *st=0;
416     long length;
417     uint8_t *b=0, *c=0;
418     int cipher_type;
419     MP_INT e,d,iqmp,tmp,tmp2,tmp3;
420     bool_t valid;
421
422     mpz_init(&e);
423     mpz_init(&d);
424     mpz_init(&iqmp);
425     mpz_init(&tmp);
426     mpz_init(&tmp2);
427     mpz_init(&tmp3);
428
429     NEW(st);
430     st->cl.description="rsapriv";
431     st->cl.type=CL_SIGPRIVKEY;
432     st->cl.apply=NULL;
433     st->cl.interface=&st->ops;
434     st->ops.st=st;
435     st->ops.sethash=rsa_priv_sethash;
436     st->common.hashbuf=NULL;
437     st->ops.sign=rsa_sign;
438     st->ops.hash=0;
439     st->ops.dispose=rsapriv_dispose;
440     st->loc=loc;
441     mpz_init(&st->n);
442     mpz_init(&st->q);
443     mpz_init(&st->p);
444     mpz_init(&st->dp);
445     mpz_init(&st->dq);
446     mpz_init(&st->w);
447
448     if (!f) {
449         assert(just_check_config);
450         goto assume_valid;
451     }
452
453     /* Check that the ID string is correct */
454     length=strlen(AUTHFILE_ID_STRING)+1;
455     b=safe_malloc(length,"rsapriv_apply");
456     if (fread(b,length,1,f)!=1 || memcmp(b,AUTHFILE_ID_STRING,length)!=0) {
457         LDUNSUP_FILE("failed to read magic ID"
458                      " string from SSH1 private keyfile\n");
459     }
460     FREE(b);
461
462     cipher_type=fgetc(f);
463     KEYFILE_GET(32); /* "Reserved data" */
464     if (cipher_type != 0) {
465         LDUNSUP("we don't support encrypted keyfiles\n");
466     }
467
468     /* Read the public key */
469     KEYFILE_GET(32); /* Not sure what this is */
470     length=(KEYFILE_GET(16)+7)/8;
471     if (length>RSA_MAX_MODBYTES) {
472         LDFATAL("implausible length %ld for modulus\n",
473                  length);
474     }
475     b=safe_malloc(length,"rsapriv_apply");
476     if (fread(b,length,1,f) != 1) {
477         LDFATAL_FILE("error reading modulus\n");
478     }
479     read_mpbin(&st->n,b,length);
480     FREE(b);
481     length=(KEYFILE_GET(16)+7)/8;
482     if (length>RSA_MAX_MODBYTES) {
483         LDFATAL("implausible length %ld for e\n",length);
484     }
485     b=safe_malloc(length,"rsapriv_apply");
486     if (fread(b,length,1,f)!=1) {
487         LDFATAL_FILE("error reading e\n");
488     }
489     read_mpbin(&e,b,length);
490     FREE(b);
491     
492     length=KEYFILE_GET(32);
493     if (length>1024) {
494         LDFATAL("implausibly long (%ld) key comment\n",
495                  length);
496     }
497     c=safe_malloc(length+1,"rsapriv_apply");
498     if (fread(c,length,1,f)!=1) {
499         LDFATAL_FILE("error reading key comment\n");
500     }
501     c[length]=0;
502
503     /* Check that the next two pairs of characters are identical - the
504        keyfile is not encrypted, so they should be */
505
506     if (KEYFILE_GET(16) != KEYFILE_GET(16)) {
507         LDFATAL("corrupt keyfile\n");
508     }
509
510     /* Read d */
511     length=(KEYFILE_GET(16)+7)/8;
512     if (length>RSA_MAX_MODBYTES) {
513         LDFATAL("implausibly long (%ld) decryption key\n",
514                  length);
515     }
516     b=safe_malloc(length,"rsapriv_apply");
517     if (fread(b,length,1,f)!=1) {
518         LDFATAL_FILE("error reading decryption key\n");
519     }
520     read_mpbin(&d,b,length);
521     FREE(b);
522     /* Read iqmp (inverse of q mod p) */
523     length=(KEYFILE_GET(16)+7)/8;
524     if (length>RSA_MAX_MODBYTES) {
525         LDFATAL("implausibly long (%ld)"
526                  " iqmp auxiliary value\n", length);
527     }
528     b=safe_malloc(length,"rsapriv_apply");
529     if (fread(b,length,1,f)!=1) {
530         LDFATAL_FILE("error reading decryption key\n");
531     }
532     read_mpbin(&iqmp,b,length);
533     FREE(b);
534     /* Read q (the smaller of the two primes) */
535     length=(KEYFILE_GET(16)+7)/8;
536     if (length>RSA_MAX_MODBYTES) {
537         LDFATAL("implausibly long (%ld) q value\n",
538                  length);
539     }
540     b=safe_malloc(length,"rsapriv_apply");
541     if (fread(b,length,1,f)!=1) {
542         LDFATAL_FILE("error reading q value\n");
543     }
544     read_mpbin(&st->q,b,length);
545     FREE(b);
546     /* Read p (the larger of the two primes) */
547     length=(KEYFILE_GET(16)+7)/8;
548     if (length>RSA_MAX_MODBYTES) {
549         LDFATAL("implausibly long (%ld) p value\n",
550                  length);
551     }
552     b=safe_malloc(length,"rsapriv_apply");
553     if (fread(b,length,1,f)!=1) {
554         LDFATAL_FILE("error reading p value\n");
555     }
556     read_mpbin(&st->p,b,length);
557     FREE(b);
558     
559     if (ferror(f)) {
560         fatal_perror("rsa-private (%s:%d): ferror",loc.file,loc.line);
561     }
562
563     /*
564      * Now verify the validity of the key, and set up the auxiliary
565      * values for fast CRT signing.
566      */
567     valid=False;
568     if (do_validity_check) {
569         /* Verify that p*q is equal to n. */
570         mpz_mul(&tmp, &st->p, &st->q);
571         if (mpz_cmp(&tmp, &st->n) != 0)
572             goto done_checks;
573
574         /*
575          * Verify that d*e is congruent to 1 mod (p-1), and mod
576          * (q-1). This is equivalent to it being congruent to 1 mod
577          * lambda(n) = lcm(p-1,q-1).  The usual `textbook' condition,
578          * that d e == 1 (mod (p-1)(q-1)) is sufficient, but not
579          * actually necessary.
580          */
581         mpz_mul(&tmp, &d, &e);
582         mpz_sub_ui(&tmp2, &st->p, 1);
583         mpz_mod(&tmp3, &tmp, &tmp2);
584         if (mpz_cmp_si(&tmp3, 1) != 0)
585             goto done_checks;
586         mpz_sub_ui(&tmp2, &st->q, 1);
587         mpz_mod(&tmp3, &tmp, &tmp2);
588         if (mpz_cmp_si(&tmp3, 1) != 0)
589             goto done_checks;
590
591         /* Verify that q*iqmp is congruent to 1 mod p. */
592         mpz_mul(&tmp, &st->q, &iqmp);
593         mpz_mod(&tmp2, &tmp, &st->p);
594         if (mpz_cmp_si(&tmp2, 1) != 0)
595             goto done_checks;
596     }
597     /* Now we know the key is valid (or we don't care). */
598     valid = True;
599     
600     /*
601      * Now we compute auxiliary values dp, dq and w to allow us
602      * to use the CRT optimisation when signing.
603      * 
604      *   dp == d mod (p-1)      so that a^dp == a^d mod p, for all a
605      *   dq == d mod (q-1)      similarly mod q
606      *   w == iqmp * q          so that w == 0 mod q, and w == 1 mod p
607      */
608     mpz_sub_ui(&tmp, &st->p, 1);
609     mpz_mod(&st->dp, &d, &tmp);
610     mpz_sub_ui(&tmp, &st->q, 1);
611     mpz_mod(&st->dq, &d, &tmp);
612     mpz_mul(&st->w, &iqmp, &st->q);
613     
614 done_checks:
615     if (!valid) {
616         LDFATAL("file does not contain a "
617                  "valid RSA key!\n");
618     }
619
620 assume_valid:
621 out:
622     mpz_clear(&tmp);
623     mpz_clear(&tmp2);
624     mpz_clear(&tmp3);
625
626     FREE(b);
627     FREE(c);
628     mpz_clear(&e);
629     mpz_clear(&d);
630     mpz_clear(&iqmp);
631
632     return st;
633
634 error_out:
635     if (st) rsapriv_dispose(st);
636     st=0;
637     goto out;
638 }
639
640 static bool_t postreadcheck_tryload(struct load_ctx *l, FILE *f)
641 {
642     assert(!ferror(f));
643     if (feof(f)) { load_error(l,0,0,"eof mid-integer"); return False; }
644     return True;
645 }
646
647 bool_t rsa1_loadpriv(const struct sigscheme_info *algo,
648                      struct buffer_if *privkeydata,
649                      struct sigprivkey_if **sigpriv_r,
650                      struct log_if *log, struct cloc loc)
651 {
652     FILE *f=0;
653     struct rsapriv *st=0;
654
655     f=fmemopen(privkeydata->start,privkeydata->size,"r");
656     if (!f) {
657         slilog(log,M_ERR,"failed to fmemopen private key file\n");
658         goto error_out;
659     }
660
661     struct load_ctx l[1];
662     l->what="rsa1priv load";
663     l->verror=verror_tryload;
664     l->postreadcheck=postreadcheck_tryload;
665     l->loc=&loc;
666     l->u.tryload.log=log;
667
668     st=rsa_loadpriv_core(l,f,loc,False);
669     if (!st) goto error_out;
670     goto out;
671
672  error_out:
673     if (st) { free(st); st=0; }
674  out:
675     if (f) fclose(f);
676     if (!st) return False;
677     *sigpriv_r=&st->ops;
678     return True;
679 }
680
681 static bool_t postreadcheck_apply(struct load_ctx *l, FILE *f)
682 {
683     cfgfile_postreadcheck(*l->loc,f);
684     return True;
685 }
686
687 static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context,
688                              list_t *args)
689 {
690     struct rsapriv *st;
691     item_t *i;
692     cstring_t filename;
693     FILE *f;
694     struct load_ctx l[1];
695
696     l->what="rsa-private";
697     l->verror=verror_cfgfatal;
698     l->postreadcheck=postreadcheck_apply;
699     l->loc=&loc;
700
701     /* Argument is filename pointing to SSH1 private key file */
702     i=list_elem(args,0);
703     if (i) {
704         if (i->type!=t_string) {
705             cfgfatal(i->loc,"rsa-private","first argument must be a string\n");
706         }
707         filename=i->data.string;
708     } else {
709         filename=NULL; /* Make compiler happy */
710         cfgfatal(i->loc,"rsa-private","you must provide a filename\n");
711     }
712
713     f=fopen(filename,"rb");
714     if (!f) {
715         if (just_check_config) {
716             Message(M_WARNING,"rsa-private (%s:%d): cannot open keyfile "
717                     "\"%s\"; assuming it's valid while we check the "
718                     "rest of the configuration\n",loc.file,loc.line,filename);
719         } else {
720             fatal_perror("rsa-private (%s:%d): cannot open file \"%s\"",
721                          loc.file,loc.line,filename);
722         }
723     }
724
725     bool_t do_validity_check=True;
726     i=list_elem(args,1);
727     if (i && i->type==t_bool && i->data.bool==False) {
728         Message(M_INFO,"rsa-private (%s:%d): skipping RSA key validity "
729                 "check\n",loc.file,loc.line);
730         do_validity_check=False;
731     }
732
733     st=rsa_loadpriv_core(l,f,loc,do_validity_check);
734     fclose(f);
735     return new_closure(&st->cl);
736 }
737
738 void rsa_module(dict_t *dict)
739 {
740     add_closure(dict,"rsa-private",rsapriv_apply);
741     add_closure(dict,"rsa-public",rsapub_apply);
742 }