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