DKIM: $dkim_key_length visibility variable. Bug 1311
[exim.git] / src / src / pdkim / part-x509parse.c
1 #include "polarssl/bignum.h"
2 #include "polarssl/part-x509.h"
3 #include "polarssl/private-x509parse_c.h"
4
5 #ifndef POLARSSL_PRIVATE_X509_PARSE_C_H
6 #define POLARSSL_PRIVATE_X509_PARSE_C_H
7 /* *************** begin copy from x509parse.c ********************/
8 /*
9 * X.509 certificate and private key decoding
10 *
11 * Copyright (C) 2006-2010, Brainspark B.V.
12 *
13 * This file is part of PolarSSL (http://www.polarssl.org)
14 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
15 *
16 * All rights reserved.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License along
29 * with this program; if not, write to the Free Software Foundation, Inc.,
30 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 */
32 /*
33 * The ITU-T X.509 standard defines a certificat format for PKI.
34 *
35 * http://www.ietf.org/rfc/rfc2459.txt
36 * http://www.ietf.org/rfc/rfc3279.txt
37 *
38 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
39 *
40 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
41 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
42 */
43
44
45 /*
46 * ASN.1 DER decoding routines
47 */
48 static int asn1_get_len( unsigned char **p,
49 const unsigned char *end,
50 int *len )
51 {
52 if( ( end - *p ) < 1 )
53 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
54
55 if( ( **p & 0x80 ) == 0 )
56 *len = *(*p)++;
57 else
58 {
59 switch( **p & 0x7F )
60 {
61 case 1:
62 if( ( end - *p ) < 2 )
63 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
64
65 *len = (*p)[1];
66 (*p) += 2;
67 break;
68
69 case 2:
70 if( ( end - *p ) < 3 )
71 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
72
73 *len = ( (*p)[1] << 8 ) | (*p)[2];
74 (*p) += 3;
75 break;
76
77 default:
78 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
79 break;
80 }
81 }
82
83 if( *len > (int) ( end - *p ) )
84 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
85
86 return( 0 );
87 }
88
89 /* This function is not exported by PolarSSL 0.14.2
90 * static */
91 int asn1_get_tag( unsigned char **p,
92 const unsigned char *end,
93 int *len, int tag )
94 {
95 if( ( end - *p ) < 1 )
96 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
97
98 if( **p != tag )
99 return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
100
101 (*p)++;
102
103 return( asn1_get_len( p, end, len ) );
104 }
105
106 /* This function is not exported by PolarSSL 0.14.2
107 * static */
108 int asn1_get_int( unsigned char **p,
109 const unsigned char *end,
110 int *val )
111 {
112 int ret, len;
113
114 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
115 return( ret );
116
117 if( len > (int) sizeof( int ) || ( **p & 0x80 ) != 0 )
118 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
119
120 *val = 0;
121
122 while( len-- > 0 )
123 {
124 *val = ( *val << 8 ) | **p;
125 (*p)++;
126 }
127
128 return( 0 );
129 }
130
131 /* This function is not exported by PolarSSL 0.14.2
132 * static */
133 int asn1_get_mpi( unsigned char **p,
134 const unsigned char *end,
135 mpi *X )
136 {
137 int ret, len;
138
139 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
140 return( ret );
141
142 ret = mpi_read_binary( X, *p, len );
143
144 *p += len;
145
146 return( ret );
147 }
148 /* *************** end copy from x509parse.c ********************/
149 #endif /* private-x509parse_c.h */