more pdkim wip
[users/jgh/exim.git] / src / src / pdkim / rsa.c
1 /* $Cambridge: exim/src/src/pdkim/rsa.c,v 1.1.2.3 2009/03/17 21:11:56 tom Exp $ */
2 /*
3  *  The RSA public-key cryptosystem
4  *
5  *  Based on XySSL: Copyright (C) 2006-2008  Christophe Devine
6  *
7  *  Copyright (C) 2009  Paul Bakker <polarssl_maintainer at polarssl dot org>
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 /*
24  *  RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
25  *
26  *  http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
27  *  http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
28  */
29
30 #include "rsa.h"
31 #include "base64.h"
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36
37
38 /*
39  * ASN.1 DER decoding routines
40  */
41 static int asn1_get_len( unsigned char **p,
42                          unsigned char *end,
43                          int *len )
44 {
45     if( ( end - *p ) < 1 )
46         return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
47
48     if( ( **p & 0x80 ) == 0 )
49         *len = *(*p)++;
50     else
51     {
52         switch( **p & 0x7F )
53         {
54         case 1:
55             if( ( end - *p ) < 2 )
56                 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
57
58             *len = (*p)[1];
59             (*p) += 2;
60             break;
61
62         case 2:
63             if( ( end - *p ) < 3 )
64                 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
65
66             *len = ( (*p)[1] << 8 ) | (*p)[2];
67             (*p) += 3;
68             break;
69
70         default:
71             return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
72             break;
73         }
74     }
75
76     if( *len > (int) ( end - *p ) )
77         return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
78
79     return( 0 );
80 }
81
82 static int asn1_get_tag( unsigned char **p,
83                          unsigned char *end,
84                          int *len, int tag )
85 {
86     if( ( end - *p ) < 1 )
87         return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
88
89     if( **p != tag )
90         return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
91
92     (*p)++;
93
94     return( asn1_get_len( p, end, len ) );
95 }
96
97 static int asn1_get_int( unsigned char **p,
98                          unsigned char *end,
99                          int *val )
100 {
101     int ret, len;
102
103     if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
104         return( ret );
105
106     if( len > (int) sizeof( int ) || ( **p & 0x80 ) != 0 )
107         return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
108
109     *val = 0;
110
111     while( len-- > 0 )
112     {
113         *val = ( *val << 8 ) | **p;
114         (*p)++;
115     }
116
117     return( 0 );
118 }
119
120 static int asn1_get_mpi( unsigned char **p,
121                          unsigned char *end,
122                          mpi *X )
123 {
124     int ret, len;
125
126     if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
127         return( ret );
128
129     ret = mpi_read_binary( X, *p, len );
130
131     *p += len;
132
133     return( ret );
134 }
135
136
137 /*
138  * Initialize an RSA context
139  */
140 void rsa_init( rsa_context *ctx,
141                int padding,
142                int hash_id,
143                int (*f_rng)(void *),
144                void *p_rng )
145 {
146     memset( ctx, 0, sizeof( rsa_context ) );
147
148     ctx->padding = padding;
149     ctx->hash_id = hash_id;
150
151     ctx->f_rng = f_rng;
152     ctx->p_rng = p_rng;
153 }
154
155
156 /*
157  * Check a public RSA key
158  */
159 int rsa_check_pubkey( rsa_context *ctx )
160 {
161     if( ( ctx->N.p[0] & 1 ) == 0 ||
162         ( ctx->E.p[0] & 1 ) == 0 )
163         return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
164
165     if( mpi_msb( &ctx->N ) < 128 ||
166         mpi_msb( &ctx->N ) > 4096 )
167         return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
168
169     if( mpi_msb( &ctx->E ) < 2 ||
170         mpi_msb( &ctx->E ) > 64 )
171         return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
172
173     return( 0 );
174 }
175
176 /*
177  * Check a private RSA key
178  */
179 int rsa_check_privkey( rsa_context *ctx )
180 {
181     int ret;
182     mpi PQ, DE, P1, Q1, H, I, G;
183
184     if( ( ret = rsa_check_pubkey( ctx ) ) != 0 )
185         return( ret );
186
187     mpi_init( &PQ, &DE, &P1, &Q1, &H, &I, &G, NULL );
188
189     MPI_CHK( mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
190     MPI_CHK( mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
191     MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
192     MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
193     MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
194     MPI_CHK( mpi_mod_mpi( &I, &DE, &H  ) );
195     MPI_CHK( mpi_gcd( &G, &ctx->E, &H  ) );
196
197     if( mpi_cmp_mpi( &PQ, &ctx->N ) == 0 &&
198         mpi_cmp_int( &I, 1 ) == 0 &&
199         mpi_cmp_int( &G, 1 ) == 0 )
200     {
201         mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, NULL );
202         return( 0 );
203     }
204
205 cleanup:
206
207     mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, NULL );
208     return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED | ret );
209 }
210
211 /*
212  * Do an RSA public key operation
213  */
214 int rsa_public( rsa_context *ctx,
215                 unsigned char *input,
216                 unsigned char *output )
217 {
218     int ret, olen;
219     mpi T;
220
221     mpi_init( &T, NULL );
222
223     MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
224
225     if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
226     {
227         mpi_free( &T, NULL );
228         return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
229     }
230
231     olen = ctx->len;
232     MPI_CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
233     MPI_CHK( mpi_write_binary( &T, output, olen ) );
234
235 cleanup:
236
237     mpi_free( &T, NULL );
238
239     if( ret != 0 )
240         return( POLARSSL_ERR_RSA_PUBLIC_FAILED | ret );
241
242     return( 0 );
243 }
244
245 /*
246  * Do an RSA private key operation
247  */
248 int rsa_private( rsa_context *ctx,
249                  unsigned char *input,
250                  unsigned char *output )
251 {
252     int ret, olen;
253     mpi T, T1, T2;
254
255     mpi_init( &T, &T1, &T2, NULL );
256
257     MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
258
259     if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
260     {
261         mpi_free( &T, NULL );
262         return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
263     }
264
265 #if 0
266     MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
267 #else
268     /*
269      * faster decryption using the CRT
270      *
271      * T1 = input ^ dP mod P
272      * T2 = input ^ dQ mod Q
273      */
274     MPI_CHK( mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
275     MPI_CHK( mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
276
277     /*
278      * T = (T1 - T2) * (Q^-1 mod P) mod P
279      */
280     MPI_CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
281     MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
282     MPI_CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
283
284     /*
285      * output = T2 + T * Q
286      */
287     MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
288     MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) );
289 #endif
290
291     olen = ctx->len;
292     MPI_CHK( mpi_write_binary( &T, output, olen ) );
293
294 cleanup:
295
296     mpi_free( &T, &T1, &T2, NULL );
297
298     if( ret != 0 )
299         return( POLARSSL_ERR_RSA_PRIVATE_FAILED | ret );
300
301     return( 0 );
302 }
303
304 /*
305  * Add the message padding, then do an RSA operation
306  */
307 int rsa_pkcs1_encrypt( rsa_context *ctx,
308                        int mode, int  ilen,
309                        unsigned char *input,
310                        unsigned char *output )
311 {
312     int nb_pad, olen;
313     unsigned char *p = output;
314
315     olen = ctx->len;
316
317     switch( ctx->padding )
318     {
319         case RSA_PKCS_V15:
320
321             if( ilen < 0 || olen < ilen + 11 )
322                 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
323
324             nb_pad = olen - 3 - ilen;
325
326             *p++ = 0;
327             *p++ = RSA_CRYPT;
328
329             while( nb_pad-- > 0 )
330             {
331                 do {
332                     *p = (unsigned char) rand();
333                 } while( *p == 0 );
334                 p++;
335             }
336             *p++ = 0;
337             memcpy( p, input, ilen );
338             break;
339
340         default:
341
342             return( POLARSSL_ERR_RSA_INVALID_PADDING );
343     }
344
345     return( ( mode == RSA_PUBLIC )
346             ? rsa_public(  ctx, output, output )
347             : rsa_private( ctx, output, output ) );
348 }
349
350 /*
351  * Do an RSA operation, then remove the message padding
352  */
353 int rsa_pkcs1_decrypt( rsa_context *ctx,
354                        int mode, int *olen,
355                        unsigned char *input,
356                        unsigned char *output,
357                int output_max_len)
358 {
359     int ret, ilen;
360     unsigned char *p;
361     unsigned char buf[512];
362
363     ilen = ctx->len;
364
365     if( ilen < 16 || ilen > (int) sizeof( buf ) )
366         return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
367
368     ret = ( mode == RSA_PUBLIC )
369           ? rsa_public(  ctx, input, buf )
370           : rsa_private( ctx, input, buf );
371
372     if( ret != 0 )
373         return( ret );
374
375     p = buf;
376
377     switch( ctx->padding )
378     {
379         case RSA_PKCS_V15:
380
381             if( *p++ != 0 || *p++ != RSA_CRYPT )
382                 return( POLARSSL_ERR_RSA_INVALID_PADDING );
383
384             while( *p != 0 )
385             {
386                 if( p >= buf + ilen - 1 )
387                     return( POLARSSL_ERR_RSA_INVALID_PADDING );
388                 p++;
389             }
390             p++;
391             break;
392
393         default:
394
395             return( POLARSSL_ERR_RSA_INVALID_PADDING );
396     }
397
398     if (ilen - (int)(p - buf) > output_max_len)
399         return( POLARSSL_ERR_RSA_OUTPUT_TO_LARGE );
400
401     *olen = ilen - (int)(p - buf);
402     memcpy( output, p, *olen );
403
404     return( 0 );
405 }
406
407 /*
408  * Do an RSA operation to sign the message digest
409  */
410 int rsa_pkcs1_sign( rsa_context *ctx,
411                     int mode,
412                     int hash_id,
413                     int hashlen,
414                     unsigned char *hash,
415                     unsigned char *sig )
416 {
417     int nb_pad, olen;
418     unsigned char *p = sig;
419
420     olen = ctx->len;
421
422     switch( ctx->padding )
423     {
424         case RSA_PKCS_V15:
425
426             switch( hash_id )
427             {
428                 case RSA_RAW:
429                     nb_pad = olen - 3 - hashlen;
430                     break;
431
432                 case RSA_MD2:
433                 case RSA_MD4:
434                 case RSA_MD5:
435                     nb_pad = olen - 3 - 16 - 18;
436                     break;
437
438                 case RSA_SHA1:
439                     nb_pad = olen - 3 - 20 - 15;
440                     break;
441
442                 case RSA_SHA256:
443                     nb_pad = olen - 3 - 32 - 19;
444                     break;
445
446                 default:
447                     return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
448             }
449
450             if( nb_pad < 8 )
451                 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
452
453             *p++ = 0;
454             *p++ = RSA_SIGN;
455             memset( p, 0xFF, nb_pad );
456             p += nb_pad;
457             *p++ = 0;
458             break;
459
460         default:
461
462             return( POLARSSL_ERR_RSA_INVALID_PADDING );
463     }
464
465     switch( hash_id )
466     {
467         case RSA_RAW:
468             memcpy( p, hash, hashlen );
469             break;
470
471         case RSA_MD2:
472             memcpy( p, ASN1_HASH_MDX, 18 );
473             memcpy( p + 18, hash, 16 );
474             p[13] = 2; break;
475
476         case RSA_MD4:
477             memcpy( p, ASN1_HASH_MDX, 18 );
478             memcpy( p + 18, hash, 16 );
479             p[13] = 4; break;
480
481         case RSA_MD5:
482             memcpy( p, ASN1_HASH_MDX, 18 );
483             memcpy( p + 18, hash, 16 );
484             p[13] = 5; break;
485
486         case RSA_SHA1:
487             memcpy( p, ASN1_HASH_SHA1, 15 );
488             memcpy( p + 15, hash, 20 );
489             break;
490
491         case RSA_SHA256:
492             memcpy( p, ASN1_HASH_SHA256, 19 );
493             memcpy( p + 19, hash, 32 );
494             break;
495
496         default:
497             return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
498     }
499
500     return( ( mode == RSA_PUBLIC )
501             ? rsa_public(  ctx, sig, sig )
502             : rsa_private( ctx, sig, sig ) );
503 }
504
505 /*
506  * Do an RSA operation and check the message digest
507  */
508 int rsa_pkcs1_verify( rsa_context *ctx,
509                       int mode,
510                       int hash_id,
511                       int hashlen,
512                       unsigned char *hash,
513                       unsigned char *sig )
514 {
515     int ret, len, siglen;
516     unsigned char *p, c;
517     unsigned char buf[512];
518
519     siglen = ctx->len;
520
521     if( siglen < 16 || siglen > (int) sizeof( buf ) )
522         return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
523
524     ret = ( mode == RSA_PUBLIC )
525           ? rsa_public(  ctx, sig, buf )
526           : rsa_private( ctx, sig, buf );
527
528     if( ret != 0 )
529         return( ret );
530
531     p = buf;
532
533     switch( ctx->padding )
534     {
535         case RSA_PKCS_V15:
536
537             if( *p++ != 0 || *p++ != RSA_SIGN )
538                 return( POLARSSL_ERR_RSA_INVALID_PADDING );
539
540             while( *p != 0 )
541             {
542                 if( p >= buf + siglen - 1 || *p != 0xFF )
543                     return( POLARSSL_ERR_RSA_INVALID_PADDING );
544                 p++;
545             }
546             p++;
547             break;
548
549         default:
550
551             return( POLARSSL_ERR_RSA_INVALID_PADDING );
552     }
553
554     len = siglen - (int)( p - buf );
555
556     if( len == 34 )
557     {
558         c = p[13];
559         p[13] = 0;
560
561         if( memcmp( p, ASN1_HASH_MDX, 18 ) != 0 )
562             return( POLARSSL_ERR_RSA_VERIFY_FAILED );
563
564         if( ( c == 2 && hash_id == RSA_MD2 ) ||
565             ( c == 4 && hash_id == RSA_MD4 ) ||
566             ( c == 5 && hash_id == RSA_MD5 ) )
567         {
568             if( memcmp( p + 18, hash, 16 ) == 0 )
569                 return( 0 );
570             else
571                 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
572         }
573     }
574
575     if( len == 35 && hash_id == RSA_SHA1 )
576     {
577         if( memcmp( p, ASN1_HASH_SHA1, 15 ) == 0 &&
578             memcmp( p + 15, hash, 20 ) == 0 )
579             return( 0 );
580         else
581             return( POLARSSL_ERR_RSA_VERIFY_FAILED );
582     }
583
584     if( len == 51 && hash_id == RSA_SHA256 )
585     {
586         if( memcmp( p, ASN1_HASH_SHA256, 19 ) == 0 &&
587             memcmp( p + 19, hash, 32 ) == 0 )
588             return( 0 );
589         else
590             return( POLARSSL_ERR_RSA_VERIFY_FAILED );
591     }
592
593     if( len == hashlen && hash_id == RSA_RAW )
594     {
595         if( memcmp( p, hash, hashlen ) == 0 )
596             return( 0 );
597         else
598             return( POLARSSL_ERR_RSA_VERIFY_FAILED );
599     }
600
601     return( POLARSSL_ERR_RSA_INVALID_PADDING );
602 }
603
604 /*
605  * Free the components of an RSA key
606  */
607 void rsa_free( rsa_context *ctx )
608 {
609     mpi_free( &ctx->RQ, &ctx->RP, &ctx->RN,
610               &ctx->QP, &ctx->DQ, &ctx->DP,
611               &ctx->Q,  &ctx->P,  &ctx->D,
612               &ctx->E,  &ctx->N,  NULL );
613 }
614
615
616 /*
617  * Parse a public RSA key
618
619 OpenSSL RSA public key ASN1 container
620   0:d=0  hl=3 l= 159 cons: SEQUENCE
621   3:d=1  hl=2 l=  13 cons: SEQUENCE
622   5:d=2  hl=2 l=   9 prim: OBJECT:rsaEncryption
623  16:d=2  hl=2 l=   0 prim: NULL
624  18:d=1  hl=3 l= 141 prim: BIT STRING:RSAPublicKey (below)
625
626 RSAPublicKey ASN1 container
627   0:d=0  hl=3 l= 137 cons: SEQUENCE
628   3:d=1  hl=3 l= 129 prim: INTEGER:Public modulus
629 135:d=1  hl=2 l=   3 prim: INTEGER:Public exponent
630 */
631
632 int rsa_parse_public_key( rsa_context *rsa, unsigned char *buf, int buflen )
633 {
634     unsigned char *p, *end;
635     int ret, len;
636
637     p = buf;
638     end = buf+buflen;
639
640     if( ( ret = asn1_get_tag( &p, end, &len,
641             ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) {
642         return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
643     }
644
645     if( ( ret = asn1_get_tag( &p, end, &len,
646             ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) == 0 ) {
647         /* Skip over embedded rsaEncryption Object */
648         p+=len;
649
650         /* The RSAPublicKey ASN1 container is wrapped in a BIT STRING */
651         if( ( ret = asn1_get_tag( &p, end, &len,
652                 ASN1_BIT_STRING ) ) != 0 ) {
653             return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
654         }
655
656         /* Limit range to that BIT STRING */
657         end = p + len;
658         p++;
659
660         if( ( ret = asn1_get_tag( &p, end, &len,
661                 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) {
662             return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
663         }
664     }
665
666     if ( ( ( ret = asn1_get_mpi( &p, end, &(rsa->N)  ) ) == 0 ) &&
667          ( ( ret = asn1_get_mpi( &p, end, &(rsa->E)  ) ) == 0 ) ) {
668         rsa->len = mpi_size( &rsa->N );
669         return 0;
670     }
671
672     return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
673 }
674
675 /*
676  * Parse a private RSA key
677  */
678 int rsa_parse_key( rsa_context *rsa, unsigned char *buf, int buflen,
679                                      unsigned char *pwd, int pwdlen )
680 {
681     int ret, len, enc;
682     unsigned char *s1, *s2;
683     unsigned char *p, *end;
684
685     s1 = (unsigned char *) strstr( (char *) buf,
686         "-----BEGIN RSA PRIVATE KEY-----" );
687
688     if( s1 != NULL )
689     {
690         s2 = (unsigned char *) strstr( (char *) buf,
691             "-----END RSA PRIVATE KEY-----" );
692
693         if( s2 == NULL || s2 <= s1 )
694             return( POLARSSL_ERR_X509_KEY_INVALID_PEM );
695
696         s1 += 31;
697         if( *s1 == '\r' ) s1++;
698         if( *s1 == '\n' ) s1++;
699             else return( POLARSSL_ERR_X509_KEY_INVALID_PEM );
700
701         enc = 0;
702
703         if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
704         {
705             return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
706         }
707
708         len = 0;
709         ret = base64_decode( NULL, &len, s1, s2 - s1 );
710
711         if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
712             return( ret | POLARSSL_ERR_X509_KEY_INVALID_PEM );
713
714         if( ( buf = (unsigned char *) malloc( len ) ) == NULL )
715             return( 1 );
716
717         if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 )
718         {
719             free( buf );
720             return( ret | POLARSSL_ERR_X509_KEY_INVALID_PEM );
721         }
722
723         buflen = len;
724
725         if( enc != 0 )
726         {
727             return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
728         }
729     }
730
731     memset( rsa, 0, sizeof( rsa_context ) );
732
733     p = buf;
734     end = buf + buflen;
735
736     /*
737      *  RSAPrivateKey ::= SEQUENCE {
738      *      version           Version,
739      *      modulus           INTEGER,  -- n
740      *      publicExponent    INTEGER,  -- e
741      *      privateExponent   INTEGER,  -- d
742      *      prime1            INTEGER,  -- p
743      *      prime2            INTEGER,  -- q
744      *      exponent1         INTEGER,  -- d mod (p-1)
745      *      exponent2         INTEGER,  -- d mod (q-1)
746      *      coefficient       INTEGER,  -- (inverse of q) mod p
747      *      otherPrimeInfos   OtherPrimeInfos OPTIONAL
748      *  }
749      */
750     if( ( ret = asn1_get_tag( &p, end, &len,
751             ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
752     {
753         if( s1 != NULL )
754             free( buf );
755
756         rsa_free( rsa );
757         return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
758     }
759
760     end = p + len;
761
762     if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
763     {
764         if( s1 != NULL )
765             free( buf );
766
767         rsa_free( rsa );
768         return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
769     }
770
771     if( rsa->ver != 0 )
772     {
773         if( s1 != NULL )
774             free( buf );
775
776         rsa_free( rsa );
777         return( ret | POLARSSL_ERR_X509_KEY_INVALID_VERSION );
778     }
779
780     if( ( ret = asn1_get_mpi( &p, end, &rsa->N  ) ) != 0 ||
781         ( ret = asn1_get_mpi( &p, end, &rsa->E  ) ) != 0 ||
782         ( ret = asn1_get_mpi( &p, end, &rsa->D  ) ) != 0 ||
783         ( ret = asn1_get_mpi( &p, end, &rsa->P  ) ) != 0 ||
784         ( ret = asn1_get_mpi( &p, end, &rsa->Q  ) ) != 0 ||
785         ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
786         ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
787         ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
788     {
789         if( s1 != NULL )
790             free( buf );
791
792         rsa_free( rsa );
793         return( ret | POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
794     }
795
796     rsa->len = mpi_size( &rsa->N );
797
798     if( p != end )
799     {
800         if( s1 != NULL )
801             free( buf );
802
803         rsa_free( rsa );
804         return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT |
805                 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
806     }
807
808     if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
809     {
810         if( s1 != NULL )
811             free( buf );
812
813         rsa_free( rsa );
814         return( ret );
815     }
816
817     if( s1 != NULL )
818         free( buf );
819
820     return( 0 );
821 }