~cpick/mongrel2/release

« back to all changes in this revision

Viewing changes to src/polarssl/x509.h

  • Committer: Chris Pick
  • Date: 2013-06-30 16:39:57 UTC
  • mfrom: (1106.1.15)
  • Revision ID: git-v1:ec39967acb6bc9867ed9b9dc3774304ca6b9c294
Merge tag 'v1.8.1' into debian

Hotfix for github issue 148

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
2
 * \file x509.h
3
3
 *
4
 
 *  Copyright (C) 2006-2010, Brainspark B.V.
 
4
 * \brief X.509 certificate and private key decoding
 
5
 *
 
6
 *  Copyright (C) 2006-2011, Brainspark B.V.
5
7
 *
6
8
 *  This file is part of PolarSSL (http://www.polarssl.org)
7
9
 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
25
27
#ifndef POLARSSL_X509_H
26
28
#define POLARSSL_X509_H
27
29
 
28
 
#include "polarssl/rsa.h"
29
 
 
30
 
/*
31
 
 * ASN1 Error codes
32
 
 *
33
 
 * These error codes will be OR'ed to X509 error codes for
34
 
 * higher error granularity.
35
 
 */
36
 
#define POLARSSL_ERR_ASN1_OUT_OF_DATA                      0x0014
37
 
#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG                   0x0016
38
 
#define POLARSSL_ERR_ASN1_INVALID_LENGTH                   0x0018
39
 
#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH                  0x001A
40
 
#define POLARSSL_ERR_ASN1_INVALID_DATA                     0x001C
41
 
 
42
 
/*
43
 
 * X509 Error codes
44
 
 */
45
 
#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE              -0x0020
46
 
#define POLARSSL_ERR_X509_CERT_INVALID_PEM                 -0x0040
47
 
#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT              -0x0060
48
 
#define POLARSSL_ERR_X509_CERT_INVALID_VERSION             -0x0080
49
 
#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL              -0x00A0
50
 
#define POLARSSL_ERR_X509_CERT_INVALID_ALG                 -0x00C0
51
 
#define POLARSSL_ERR_X509_CERT_INVALID_NAME                -0x00E0
52
 
#define POLARSSL_ERR_X509_CERT_INVALID_DATE                -0x0100
53
 
#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY              -0x0120
54
 
#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE           -0x0140
55
 
#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS          -0x0160
56
 
#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION             -0x0180
57
 
#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG             -0x01A0
58
 
#define POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG              -0x01C0
59
 
#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH                -0x01E0
60
 
#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED               -0x0200
61
 
#define POLARSSL_ERR_X509_KEY_INVALID_PEM                  -0x0220
62
 
#define POLARSSL_ERR_X509_KEY_INVALID_VERSION              -0x0240
63
 
#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT               -0x0260
64
 
#define POLARSSL_ERR_X509_KEY_INVALID_ENC_IV               -0x0280
65
 
#define POLARSSL_ERR_X509_KEY_UNKNOWN_ENC_ALG              -0x02A0
66
 
#define POLARSSL_ERR_X509_KEY_PASSWORD_REQUIRED            -0x02C0
67
 
#define POLARSSL_ERR_X509_KEY_PASSWORD_MISMATCH            -0x02E0
68
 
#define POLARSSL_ERR_X509_POINT_ERROR                      -0x0300
69
 
#define POLARSSL_ERR_X509_VALUE_TO_LENGTH                  -0x0320
70
 
 
71
 
/*
72
 
 * X509 Verify codes
73
 
 */
74
 
#define BADCERT_EXPIRED                 1
75
 
#define BADCERT_REVOKED                 2
76
 
#define BADCERT_CN_MISMATCH             4
77
 
#define BADCERT_NOT_TRUSTED             8
78
 
#define BADCRL_NOT_TRUSTED             16
79
 
#define BADCRL_EXPIRED                 32
80
 
 
81
 
/*
82
 
 * DER constants
83
 
 */
84
 
#define ASN1_BOOLEAN                 0x01
85
 
#define ASN1_INTEGER                 0x02
86
 
#define ASN1_BIT_STRING              0x03
87
 
#define ASN1_OCTET_STRING            0x04
88
 
#define ASN1_NULL                    0x05
89
 
#define ASN1_OID                     0x06
90
 
#define ASN1_UTF8_STRING             0x0C
91
 
#define ASN1_SEQUENCE                0x10
92
 
#define ASN1_SET                     0x11
93
 
#define ASN1_PRINTABLE_STRING        0x13
94
 
#define ASN1_T61_STRING              0x14
95
 
#define ASN1_IA5_STRING              0x16
96
 
#define ASN1_UTC_TIME                0x17
97
 
#define ASN1_GENERALIZED_TIME        0x18
98
 
#define ASN1_UNIVERSAL_STRING        0x1C
99
 
#define ASN1_BMP_STRING              0x1E
100
 
#define ASN1_PRIMITIVE               0x00
101
 
#define ASN1_CONSTRUCTED             0x20
102
 
#define ASN1_CONTEXT_SPECIFIC        0x80
 
30
#include "asn1.h"
 
31
#include "rsa.h"
 
32
#include "dhm.h"
 
33
 
 
34
/** 
 
35
 * \addtogroup x509_module
 
36
 * \{ 
 
37
 */
 
38
 
 
39
/** 
 
40
 * \name X509 Error codes
 
41
 * \{
 
42
 */
 
43
#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE              -0x2080  /**< Unavailable feature, e.g. RSA hashing/encryption combination. */
 
44
#define POLARSSL_ERR_X509_CERT_INVALID_PEM                 -0x2100  /**< The PEM-encoded certificate contains invalid elements, e.g. invalid character. */ 
 
45
#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT              -0x2180  /**< The certificate format is invalid, e.g. different type expected. */
 
46
#define POLARSSL_ERR_X509_CERT_INVALID_VERSION             -0x2200  /**< The certificate version element is invalid. */
 
47
#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL              -0x2280  /**< The serial tag or value is invalid. */
 
48
#define POLARSSL_ERR_X509_CERT_INVALID_ALG                 -0x2300  /**< The algorithm tag or value is invalid. */
 
49
#define POLARSSL_ERR_X509_CERT_INVALID_NAME                -0x2380  /**< The name tag or value is invalid. */
 
50
#define POLARSSL_ERR_X509_CERT_INVALID_DATE                -0x2400  /**< The date tag or value is invalid. */
 
51
#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY              -0x2480  /**< The pubkey tag or value is invalid (only RSA is supported). */
 
52
#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE           -0x2500  /**< The signature tag or value invalid. */
 
53
#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS          -0x2580  /**< The extension tag or value is invalid. */
 
54
#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION             -0x2600  /**< Certificate or CRL has an unsupported version number. */
 
55
#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG             -0x2680  /**< Signature algorithm (oid) is unsupported. */
 
56
#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG                   -0x2700  /**< Key algorithm is unsupported (only RSA is supported). */
 
57
#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH                -0x2780  /**< Certificate signature algorithms do not match. (see \c ::x509_cert sig_oid) */
 
58
#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED               -0x2800  /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */
 
59
#define POLARSSL_ERR_X509_KEY_INVALID_VERSION              -0x2880  /**< Unsupported RSA key version */
 
60
#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT               -0x2900  /**< Invalid RSA key tag or value. */
 
61
#define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT              -0x2980  /**< Format not recognized as DER or PEM. */
 
62
#define POLARSSL_ERR_X509_INVALID_INPUT                    -0x2A00  /**< Input invalid. */
 
63
#define POLARSSL_ERR_X509_MALLOC_FAILED                    -0x2A80  /**< Allocation of memory failed. */
 
64
#define POLARSSL_ERR_X509_FILE_IO_ERROR                    -0x2B00  /**< Read/write of file failed. */
 
65
/* \} name */
 
66
 
 
67
 
 
68
/**
 
69
 * \name X509 Verify codes
 
70
 * \{
 
71
 */
 
72
#define BADCERT_EXPIRED             0x01  /**< The certificate validity has expired. */
 
73
#define BADCERT_REVOKED             0x02  /**< The certificate has been revoked (is on a CRL). */
 
74
#define BADCERT_CN_MISMATCH         0x04  /**< The certificate Common Name (CN) does not match with the expected CN. */
 
75
#define BADCERT_NOT_TRUSTED         0x08  /**< The certificate is not correctly signed by the trusted CA. */
 
76
#define BADCRL_NOT_TRUSTED          0x10  /**< CRL is not correctly signed by the trusted CA. */
 
77
#define BADCRL_EXPIRED              0x20  /**< CRL is expired. */
 
78
#define BADCERT_MISSING             0x40  /**< Certificate was missing. */
 
79
#define BADCERT_SKIP_VERIFY         0x80  /**< Certificate verification was skipped. */
 
80
/* \} name */
 
81
/* \} addtogroup x509_module */
103
82
 
104
83
/*
105
84
 * various object identifiers
119
98
#define X509_SUBJECT                 0x02
120
99
 
121
100
#define OID_X520                "\x55\x04"
122
 
#define OID_CN                  "\x55\x04\x03"
 
101
#define OID_CN                  OID_X520 "\x03"
 
102
 
123
103
#define OID_PKCS1               "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
124
 
#define OID_PKCS1_RSA           "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"
 
104
#define OID_PKCS1_RSA           OID_PKCS1 "\x01"
 
105
 
125
106
#define OID_RSA_SHA_OBS         "\x2B\x0E\x03\x02\x1D"
 
107
 
126
108
#define OID_PKCS9               "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
127
 
#define OID_PKCS9_EMAIL         "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01"
128
 
 
129
 
/*
130
 
 * Structures for parsing X.509 certificates
131
 
 */
132
 
typedef struct _x509_buf
133
 
{
134
 
    int tag;
135
 
    int len;
136
 
    unsigned char *p;
137
 
}
138
 
x509_buf;
139
 
 
 
109
#define OID_PKCS9_EMAIL         OID_PKCS9 "\x01"
 
110
 
 
111
/** ISO arc for standard certificate and CRL extensions */
 
112
#define OID_ID_CE               "\x55\x1D" /**< id-ce OBJECT IDENTIFIER  ::=  {joint-iso-ccitt(2) ds(5) 29} */
 
113
 
 
114
/**
 
115
 * Private Internet Extensions
 
116
 * { iso(1) identified-organization(3) dod(6) internet(1)
 
117
 *                      security(5) mechanisms(5) pkix(7) }
 
118
 */
 
119
#define OID_PKIX                "\x2B\x06\x01\x05\x05\x07"
 
120
 
 
121
/*
 
122
 * OIDs for standard certificate extensions
 
123
 */
 
124
#define OID_AUTHORITY_KEY_IDENTIFIER    OID_ID_CE "\x23" /**< id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::=  { id-ce 35 } */
 
125
#define OID_SUBJECT_KEY_IDENTIFIER      OID_ID_CE "\x0E" /**< id-ce-subjectKeyIdentifier OBJECT IDENTIFIER ::=  { id-ce 14 } */
 
126
#define OID_KEY_USAGE                   OID_ID_CE "\x0F" /**< id-ce-keyUsage OBJECT IDENTIFIER ::=  { id-ce 15 } */
 
127
#define OID_CERTIFICATE_POLICIES        OID_ID_CE "\x20" /**< id-ce-certificatePolicies OBJECT IDENTIFIER ::=  { id-ce 32 } */
 
128
#define OID_POLICY_MAPPINGS             OID_ID_CE "\x21" /**< id-ce-policyMappings OBJECT IDENTIFIER ::=  { id-ce 33 } */
 
129
#define OID_SUBJECT_ALT_NAME            OID_ID_CE "\x11" /**< id-ce-subjectAltName OBJECT IDENTIFIER ::=  { id-ce 17 } */
 
130
#define OID_ISSUER_ALT_NAME             OID_ID_CE "\x12" /**< id-ce-issuerAltName OBJECT IDENTIFIER ::=  { id-ce 18 } */
 
131
#define OID_SUBJECT_DIRECTORY_ATTRS     OID_ID_CE "\x09" /**< id-ce-subjectDirectoryAttributes OBJECT IDENTIFIER ::=  { id-ce 9 } */
 
132
#define OID_BASIC_CONSTRAINTS           OID_ID_CE "\x13" /**< id-ce-basicConstraints OBJECT IDENTIFIER ::=  { id-ce 19 } */
 
133
#define OID_NAME_CONSTRAINTS            OID_ID_CE "\x1E" /**< id-ce-nameConstraints OBJECT IDENTIFIER ::=  { id-ce 30 } */
 
134
#define OID_POLICY_CONSTRAINTS          OID_ID_CE "\x24" /**< id-ce-policyConstraints OBJECT IDENTIFIER ::=  { id-ce 36 } */
 
135
#define OID_EXTENDED_KEY_USAGE          OID_ID_CE "\x25" /**< id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 } */
 
136
#define OID_CRL_DISTRIBUTION_POINTS     OID_ID_CE "\x1F" /**< id-ce-cRLDistributionPoints OBJECT IDENTIFIER ::=  { id-ce 31 } */
 
137
#define OID_INIHIBIT_ANYPOLICY          OID_ID_CE "\x36" /**< id-ce-inhibitAnyPolicy OBJECT IDENTIFIER ::=  { id-ce 54 } */
 
138
#define OID_FRESHEST_CRL                OID_ID_CE "\x2E" /**< id-ce-freshestCRL OBJECT IDENTIFIER ::=  { id-ce 46 } */
 
139
 
 
140
/*
 
141
 * X.509 v3 Key Usage Extension flags
 
142
 */
 
143
#define KU_DIGITAL_SIGNATURE            (0x80)  /* bit 0 */
 
144
#define KU_NON_REPUDIATION              (0x40)  /* bit 1 */
 
145
#define KU_KEY_ENCIPHERMENT             (0x20)  /* bit 2 */
 
146
#define KU_DATA_ENCIPHERMENT            (0x10)  /* bit 3 */
 
147
#define KU_KEY_AGREEMENT                (0x08)  /* bit 4 */
 
148
#define KU_KEY_CERT_SIGN                (0x04)  /* bit 5 */
 
149
#define KU_CRL_SIGN                     (0x02)  /* bit 6 */
 
150
 
 
151
/*
 
152
 * X.509 v3 Extended key usage OIDs
 
153
 */
 
154
#define OID_ANY_EXTENDED_KEY_USAGE      OID_EXTENDED_KEY_USAGE "\x00" /**< anyExtendedKeyUsage OBJECT IDENTIFIER ::= { id-ce-extKeyUsage 0 } */
 
155
 
 
156
#define OID_KP                          OID_PKIX "\x03" /**< id-kp OBJECT IDENTIFIER ::= { id-pkix 3 } */
 
157
#define OID_SERVER_AUTH                 OID_KP "\x01" /**< id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 } */
 
158
#define OID_CLIENT_AUTH                 OID_KP "\x02" /**< id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 } */
 
159
#define OID_CODE_SIGNING                OID_KP "\x03" /**< id-kp-codeSigning OBJECT IDENTIFIER ::= { id-kp 3 } */
 
160
#define OID_EMAIL_PROTECTION            OID_KP "\x04" /**< id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 } */
 
161
#define OID_TIME_STAMPING               OID_KP "\x08" /**< id-kp-timeStamping OBJECT IDENTIFIER ::= { id-kp 8 } */
 
162
#define OID_OCSP_SIGNING                OID_KP "\x09" /**< id-kp-OCSPSigning OBJECT IDENTIFIER ::= { id-kp 9 } */
 
163
 
 
164
#define STRING_SERVER_AUTH              "TLS Web Server Authentication"
 
165
#define STRING_CLIENT_AUTH              "TLS Web Client Authentication"
 
166
#define STRING_CODE_SIGNING             "Code Signing"
 
167
#define STRING_EMAIL_PROTECTION         "E-mail Protection"
 
168
#define STRING_TIME_STAMPING            "Time Stamping"
 
169
#define STRING_OCSP_SIGNING             "OCSP Signing"
 
170
 
 
171
/*
 
172
 * OIDs for CRL extensions
 
173
 */
 
174
#define OID_PRIVATE_KEY_USAGE_PERIOD    OID_ID_CE "\x10"
 
175
#define OID_CRL_NUMBER                  OID_ID_CE "\x14" /**< id-ce-cRLNumber OBJECT IDENTIFIER ::= { id-ce 20 } */
 
176
 
 
177
/*
 
178
 * Netscape certificate extensions
 
179
 */
 
180
#define OID_NETSCAPE                "\x60\x86\x48\x01\x86\xF8\x42" /**< Netscape OID */
 
181
#define OID_NS_CERT                 OID_NETSCAPE "\x01"
 
182
#define OID_NS_CERT_TYPE            OID_NS_CERT  "\x01"
 
183
#define OID_NS_BASE_URL             OID_NS_CERT  "\x02"
 
184
#define OID_NS_REVOCATION_URL       OID_NS_CERT  "\x03"
 
185
#define OID_NS_CA_REVOCATION_URL    OID_NS_CERT  "\x04"
 
186
#define OID_NS_RENEWAL_URL          OID_NS_CERT  "\x07"
 
187
#define OID_NS_CA_POLICY_URL        OID_NS_CERT  "\x08"
 
188
#define OID_NS_SSL_SERVER_NAME      OID_NS_CERT  "\x0C"
 
189
#define OID_NS_COMMENT              OID_NS_CERT  "\x0D"
 
190
#define OID_NS_DATA_TYPE            OID_NETSCAPE "\x02"
 
191
#define OID_NS_CERT_SEQUENCE        OID_NS_DATA_TYPE "\x05"
 
192
 
 
193
/*
 
194
 * Netscape certificate types
 
195
 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
 
196
 */
 
197
 
 
198
#define NS_CERT_TYPE_SSL_CLIENT         (0x80)  /* bit 0 */
 
199
#define NS_CERT_TYPE_SSL_SERVER         (0x40)  /* bit 1 */
 
200
#define NS_CERT_TYPE_EMAIL              (0x20)  /* bit 2 */
 
201
#define NS_CERT_TYPE_OBJECT_SIGNING     (0x10)  /* bit 3 */
 
202
#define NS_CERT_TYPE_RESERVED           (0x08)  /* bit 4 */
 
203
#define NS_CERT_TYPE_SSL_CA             (0x04)  /* bit 5 */
 
204
#define NS_CERT_TYPE_EMAIL_CA           (0x02)  /* bit 6 */
 
205
#define NS_CERT_TYPE_OBJECT_SIGNING_CA  (0x01)  /* bit 7 */
 
206
 
 
207
#define EXT_AUTHORITY_KEY_IDENTIFIER    (1 << 0)
 
208
#define EXT_SUBJECT_KEY_IDENTIFIER      (1 << 1)
 
209
#define EXT_KEY_USAGE                   (1 << 2)
 
210
#define EXT_CERTIFICATE_POLICIES        (1 << 3)
 
211
#define EXT_POLICY_MAPPINGS             (1 << 4)
 
212
#define EXT_SUBJECT_ALT_NAME            (1 << 5)
 
213
#define EXT_ISSUER_ALT_NAME             (1 << 6)
 
214
#define EXT_SUBJECT_DIRECTORY_ATTRS     (1 << 7)
 
215
#define EXT_BASIC_CONSTRAINTS           (1 << 8)
 
216
#define EXT_NAME_CONSTRAINTS            (1 << 9)
 
217
#define EXT_POLICY_CONSTRAINTS          (1 << 10)
 
218
#define EXT_EXTENDED_KEY_USAGE          (1 << 11)
 
219
#define EXT_CRL_DISTRIBUTION_POINTS     (1 << 12)
 
220
#define EXT_INIHIBIT_ANYPOLICY          (1 << 13)
 
221
#define EXT_FRESHEST_CRL                (1 << 14)
 
222
 
 
223
#define EXT_NS_CERT_TYPE                (1 << 16)
 
224
 
 
225
/*
 
226
 * Storage format identifiers
 
227
 * Recognized formats: PEM and DER
 
228
 */
 
229
#define X509_FORMAT_DER                 1
 
230
#define X509_FORMAT_PEM                 2
 
231
 
 
232
/** 
 
233
 * \addtogroup x509_module
 
234
 * \{ */
 
235
 
 
236
/**
 
237
 * \name Structures for parsing X.509 certificates and CRLs
 
238
 * \{
 
239
 */
 
240
 
 
241
/** 
 
242
 * Type-length-value structure that allows for ASN1 using DER.
 
243
 */
 
244
typedef asn1_buf x509_buf;
 
245
 
 
246
/**
 
247
 * Container for ASN1 bit strings.
 
248
 */
 
249
typedef asn1_bitstring x509_bitstring;
 
250
 
 
251
/**
 
252
 * Container for ASN1 named information objects. 
 
253
 * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
 
254
 */
140
255
typedef struct _x509_name
141
256
{
142
 
    x509_buf oid;
143
 
    x509_buf val;
144
 
    struct _x509_name *next;
 
257
    x509_buf oid;               /**< The object identifier. */
 
258
    x509_buf val;               /**< The named value. */
 
259
    struct _x509_name *next;    /**< The next named information object. */
145
260
}
146
261
x509_name;
147
262
 
 
263
/**
 
264
 * Container for a sequence of ASN.1 items
 
265
 */
 
266
typedef asn1_sequence x509_sequence;
 
267
 
 
268
/** Container for date and time (precision in seconds). */
148
269
typedef struct _x509_time
149
270
{
150
 
    int year, mon, day;
151
 
    int hour, min, sec;
 
271
    int year, mon, day;         /**< Date. */
 
272
    int hour, min, sec;         /**< Time. */
152
273
}
153
274
x509_time;
154
275
 
 
276
/** 
 
277
 * Container for an X.509 certificate. The certificate may be chained.
 
278
 */
155
279
typedef struct _x509_cert
156
280
{
157
 
    x509_buf raw;
158
 
    x509_buf tbs;
159
 
 
160
 
    int version;
161
 
    x509_buf serial;
162
 
    x509_buf sig_oid1;
163
 
 
164
 
    x509_buf issuer_raw;
165
 
    x509_buf subject_raw;
166
 
 
167
 
    x509_name issuer;
168
 
    x509_name subject;
169
 
 
170
 
    x509_time valid_from;
171
 
    x509_time valid_to;
172
 
 
173
 
    x509_buf pk_oid;
174
 
    rsa_context rsa;
175
 
 
176
 
    x509_buf issuer_id;
177
 
    x509_buf subject_id;
178
 
    x509_buf v3_ext;
179
 
 
180
 
    int ca_istrue;
181
 
    int max_pathlen;
182
 
 
183
 
    x509_buf sig_oid2;
184
 
    x509_buf sig;
185
 
    int sig_alg;
186
 
 
187
 
    struct _x509_cert *next; 
 
281
    x509_buf raw;               /**< The raw certificate data (DER). */
 
282
    x509_buf tbs;               /**< The raw certificate body (DER). The part that is To Be Signed. */
 
283
 
 
284
    int version;                /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
 
285
    x509_buf serial;            /**< Unique id for certificate issued by a specific CA. */
 
286
    x509_buf sig_oid1;          /**< Signature algorithm, e.g. sha1RSA */
 
287
 
 
288
    x509_buf issuer_raw;        /**< The raw issuer data (DER). Used for quick comparison. */
 
289
    x509_buf subject_raw;       /**< The raw subject data (DER). Used for quick comparison. */
 
290
 
 
291
    x509_name issuer;           /**< The parsed issuer data (named information object). */
 
292
    x509_name subject;          /**< The parsed subject data (named information object). */
 
293
 
 
294
    x509_time valid_from;       /**< Start time of certificate validity. */
 
295
    x509_time valid_to;         /**< End time of certificate validity. */
 
296
 
 
297
    x509_buf pk_oid;            /**< Subject public key info. Includes the public key algorithm and the key itself. */
 
298
    rsa_context rsa;            /**< Container for the RSA context. Only RSA is supported for public keys at this time. */
 
299
 
 
300
    x509_buf issuer_id;         /**< Optional X.509 v2/v3 issuer unique identifier. */
 
301
    x509_buf subject_id;        /**< Optional X.509 v2/v3 subject unique identifier. */
 
302
    x509_buf v3_ext;            /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
 
303
 
 
304
    int ext_types;              /**< Bit string containing detected and parsed extensions */
 
305
    int ca_istrue;              /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
 
306
    int max_pathlen;            /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. */
 
307
 
 
308
    unsigned char key_usage;    /**< Optional key usage extension value: See the values below */
 
309
 
 
310
    x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
 
311
 
 
312
    unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
 
313
 
 
314
    x509_buf sig_oid2;          /**< Signature algorithm. Must match sig_oid1. */
 
315
    x509_buf sig;               /**< Signature: hash of the tbs part signed with the private key. */
 
316
    int sig_alg;                /**< Internal representation of the signature algorithm, e.g. SIG_RSA_MD2 */
 
317
 
 
318
    struct _x509_cert *next;    /**< Next certificate in the CA-chain. */ 
188
319
}
189
320
x509_cert;
190
321
 
 
322
/** 
 
323
 * Certificate revocation list entry. 
 
324
 * Contains the CA-specific serial numbers and revocation dates.
 
325
 */
191
326
typedef struct _x509_crl_entry
192
327
{
193
328
    x509_buf raw;
202
337
}
203
338
x509_crl_entry;
204
339
 
 
340
/** 
 
341
 * Certificate revocation list structure. 
 
342
 * Every CRL may have multiple entries.
 
343
 */
205
344
typedef struct _x509_crl
206
345
{
207
 
    x509_buf raw;
208
 
    x509_buf tbs;
 
346
    x509_buf raw;           /**< The raw certificate data (DER). */
 
347
    x509_buf tbs;           /**< The raw certificate body (DER). The part that is To Be Signed. */
209
348
 
210
349
    int version;
211
350
    x509_buf sig_oid1;
212
351
 
213
 
    x509_buf issuer_raw;
214
 
 
215
 
    x509_name issuer;
216
 
 
217
 
    x509_time this_update;
 
352
    x509_buf issuer_raw;    /**< The raw issuer data (DER). */
 
353
 
 
354
    x509_name issuer;       /**< The parsed issuer data (named information object). */
 
355
 
 
356
    x509_time this_update;  
218
357
    x509_time next_update;
219
358
 
220
 
    x509_crl_entry entry;
 
359
    x509_crl_entry entry;   /**< The CRL entries containing the certificate revocation times for this CA. */
221
360
 
222
361
    x509_buf crl_ext;
223
362
 
228
367
    struct _x509_crl *next; 
229
368
}
230
369
x509_crl;
 
370
/** \} name Structures for parsing X.509 certificates and CRLs */
 
371
/** \} addtogroup x509_module */
231
372
 
 
373
/**
 
374
 * \name Structures for writing X.509 certificates.
 
375
 * XvP: commented out as they are not used.
 
376
 * - <tt>typedef struct _x509_node x509_node;</tt>
 
377
 * - <tt>typedef struct _x509_raw x509_raw;</tt>
 
378
 */
232
379
/*
233
 
 * Structures for writing X.509 certificates
234
 
 */
235
380
typedef struct _x509_node
236
381
{
237
382
    unsigned char *data;
259
404
    x509_node sign;
260
405
}
261
406
x509_raw;
 
407
*/
262
408
 
263
409
#ifdef __cplusplus
264
410
extern "C" {
265
411
#endif
266
412
 
267
413
/**
 
414
 * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key
 
415
 * \{
 
416
 */
 
417
 
 
418
/** \ingroup x509_module */
 
419
/**
268
420
 * \brief          Parse one or more certificates and add them
269
 
 *                 to the chained list
 
421
 *                 to the chained list. Parses permissively. If some
 
422
 *                 certificates can be parsed, the result is the number
 
423
 *                 of failed certificates it encountered. If none complete
 
424
 *                 correctly, the first error is returned.
270
425
 *
271
426
 * \param chain    points to the start of the chain
272
427
 * \param buf      buffer holding the certificate data
273
428
 * \param buflen   size of the buffer
274
429
 *
275
 
 * \return         0 if successful, or a specific X509 error code
 
430
 * \return         0 if all certificates parsed successfully, a positive number
 
431
 *                 if partly successful or a specific X509 or PEM error code
276
432
 */
277
 
int x509parse_crt( x509_cert *chain, const unsigned char *buf, int buflen );
 
433
int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen );
278
434
 
 
435
/** \ingroup x509_module */
279
436
/**
280
437
 * \brief          Load one or more certificates and add them
281
 
 *                 to the chained list
 
438
 *                 to the chained list. Parses permissively. If some
 
439
 *                 certificates can be parsed, the result is the number
 
440
 *                 of failed certificates it encountered. If none complete
 
441
 *                 correctly, the first error is returned.
282
442
 *
283
443
 * \param chain    points to the start of the chain
284
444
 * \param path     filename to read the certificates from
285
445
 *
286
 
 * \return         0 if successful, or a specific X509 error code
 
446
 * \return         0 if all certificates parsed successfully, a positive number
 
447
 *                 if partly successful or a specific X509 or PEM error code
287
448
 */
288
449
int x509parse_crtfile( x509_cert *chain, const char *path );
289
450
 
 
451
/** \ingroup x509_module */
290
452
/**
291
453
 * \brief          Parse one or more CRLs and add them
292
454
 *                 to the chained list
295
457
 * \param buf      buffer holding the CRL data
296
458
 * \param buflen   size of the buffer
297
459
 *
298
 
 * \return         0 if successful, or a specific X509 error code
 
460
 * \return         0 if successful, or a specific X509 or PEM error code
299
461
 */
300
 
int x509parse_crl( x509_crl *chain, const unsigned char *buf, int buflen );
 
462
int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen );
301
463
 
 
464
/** \ingroup x509_module */
302
465
/**
303
466
 * \brief          Load one or more CRLs and add them
304
467
 *                 to the chained list
306
469
 * \param chain    points to the start of the chain
307
470
 * \param path     filename to read the CRLs from
308
471
 *
309
 
 * \return         0 if successful, or a specific X509 error code
 
472
 * \return         0 if successful, or a specific X509 or PEM error code
310
473
 */
311
474
int x509parse_crlfile( x509_crl *chain, const char *path );
312
475
 
 
476
/** \ingroup x509_module */
313
477
/**
314
478
 * \brief          Parse a private RSA key
315
479
 *
319
483
 * \param pwd      password for decryption (optional)
320
484
 * \param pwdlen   size of the password
321
485
 *
322
 
 * \return         0 if successful, or a specific X509 error code
 
486
 * \return         0 if successful, or a specific X509 or PEM error code
323
487
 */
324
488
int x509parse_key( rsa_context *rsa,
325
 
                   const unsigned char *key, int keylen,
326
 
                   const unsigned char *pwd, int pwdlen );
 
489
                   const unsigned char *key, size_t keylen,
 
490
                   const unsigned char *pwd, size_t pwdlen );
327
491
 
 
492
/** \ingroup x509_module */
328
493
/**
329
494
 * \brief          Load and parse a private RSA key
330
495
 *
331
496
 * \param rsa      RSA context to be initialized
332
497
 * \param path     filename to read the private key from
333
 
 * \param pwd      password to decrypt the file (can be NULL)
 
498
 * \param password password to decrypt the file (can be NULL)
334
499
 *
335
 
 * \return         0 if successful, or a specific X509 error code
 
500
 * \return         0 if successful, or a specific X509 or PEM error code
336
501
 */
337
502
int x509parse_keyfile( rsa_context *rsa, const char *path,
338
503
                       const char *password );
339
504
 
 
505
/** \ingroup x509_module */
 
506
/**
 
507
 * \brief          Parse a public RSA key
 
508
 *
 
509
 * \param rsa      RSA context to be initialized
 
510
 * \param key      input buffer
 
511
 * \param keylen   size of the buffer
 
512
 *
 
513
 * \return         0 if successful, or a specific X509 or PEM error code
 
514
 */
 
515
int x509parse_public_key( rsa_context *rsa,
 
516
                   const unsigned char *key, size_t keylen );
 
517
 
 
518
/** \ingroup x509_module */
 
519
/**
 
520
 * \brief          Load and parse a public RSA key
 
521
 *
 
522
 * \param rsa      RSA context to be initialized
 
523
 * \param path     filename to read the private key from
 
524
 *
 
525
 * \return         0 if successful, or a specific X509 or PEM error code
 
526
 */
 
527
int x509parse_public_keyfile( rsa_context *rsa, const char *path );
 
528
 
 
529
/** \ingroup x509_module */
 
530
/**
 
531
 * \brief          Parse DHM parameters
 
532
 *
 
533
 * \param dhm      DHM context to be initialized
 
534
 * \param dhmin    input buffer
 
535
 * \param dhminlen size of the buffer
 
536
 *
 
537
 * \return         0 if successful, or a specific X509 or PEM error code
 
538
 */
 
539
int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen );
 
540
 
 
541
/** \ingroup x509_module */
 
542
/**
 
543
 * \brief          Load and parse DHM parameters
 
544
 *
 
545
 * \param dhm      DHM context to be initialized
 
546
 * \param path     filename to read the DHM Parameters from
 
547
 *
 
548
 * \return         0 if successful, or a specific X509 or PEM error code
 
549
 */
 
550
int x509parse_dhmfile( dhm_context *dhm, const char *path );
 
551
 
 
552
/** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
 
553
 
340
554
/**
341
555
 * \brief          Store the certificate DN in printable form into buf;
342
556
 *                 no more than size characters will be written.
351
565
int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
352
566
 
353
567
/**
 
568
 * \brief          Store the certificate serial in printable form into buf;
 
569
 *                 no more than size characters will be written.
 
570
 *
 
571
 * \param buf      Buffer to write to
 
572
 * \param size     Maximum size of buffer
 
573
 * \param serial   The X509 serial to represent
 
574
 *
 
575
 * \return         The amount of data written to the buffer, or -1 in
 
576
 *                 case of an error.
 
577
 */
 
578
int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
 
579
 
 
580
/**
354
581
 * \brief          Returns an informational string about the
355
582
 *                 certificate.
356
583
 *
372
599
 * \param buf      Buffer to write to
373
600
 * \param size     Maximum size of buffer
374
601
 * \param prefix   A line prefix
375
 
 * \param crt      The X509 CRL to represent
 
602
 * \param crl      The X509 CRL to represent
376
603
 *
377
604
 * \return         The amount of data written to the buffer, or -1 in
378
605
 *                 case of an error.
381
608
                        const x509_crl *crl );
382
609
 
383
610
/**
 
611
 * \brief          Give an known OID, return its descriptive string.
 
612
 *
 
613
 * \param oid      buffer containing the oid
 
614
 *
 
615
 * \return         Return a string if the OID is known,
 
616
 *                 or NULL otherwise.
 
617
 */
 
618
const char *x509_oid_get_description( x509_buf *oid );
 
619
 
 
620
/*
 
621
 * \brief          Give an OID, return a string version of its OID number.
 
622
 *
 
623
 * \param buf      Buffer to write to
 
624
 * \param size     Maximum size of buffer
 
625
 * \param oid      Buffer containing the OID
 
626
 *
 
627
 * \return         The amount of data written to the buffer, or -1 in
 
628
 *                 case of an error.
 
629
 */
 
630
int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
 
631
 
 
632
/**
384
633
 * \brief          Check a given x509_time against the system time and check
385
634
 *                 if it is valid.
386
635
 *
392
641
int x509parse_time_expired( const x509_time *time );
393
642
 
394
643
/**
 
644
 * \name Functions to verify a certificate
 
645
 * \{
 
646
 */
 
647
/** \ingroup x509_module */
 
648
/**
395
649
 * \brief          Verify the certificate signature
396
650
 *
397
651
 * \param crt      a certificate to be verified
400
654
 * \param cn       expected Common Name (can be set to
401
655
 *                 NULL if the CN must not be verified)
402
656
 * \param flags    result of the verification
 
657
 * \param f_vrfy   verification function
 
658
 * \param p_vrfy   verification parameter
403
659
 *
404
660
 * \return         0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
405
661
 *                 in which case *flags will have one or more of
414
670
int x509parse_verify( x509_cert *crt,
415
671
                      x509_cert *trust_ca,
416
672
                      x509_crl *ca_crl,
417
 
                      const char *cn, int *flags );
418
 
 
 
673
                      const char *cn, int *flags,
 
674
                      int (*f_vrfy)(void *, x509_cert *, int, int),
 
675
                      void *p_vrfy );
 
676
 
 
677
/**
 
678
 * \brief          Verify the certificate signature
 
679
 *
 
680
 * \param crt      a certificate to be verified
 
681
 * \param crl      the CRL to verify against
 
682
 *
 
683
 * \return         1 if the certificate is revoked, 0 otherwise
 
684
 *
 
685
 */
 
686
int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
 
687
 
 
688
/** \} name Functions to verify a certificate */
 
689
 
 
690
 
 
691
 
 
692
/**
 
693
 * \name Functions to clear a certificate, CRL or private RSA key 
 
694
 * \{
 
695
 */
 
696
/** \ingroup x509_module */
419
697
/**
420
698
 * \brief          Unallocate all certificate data
421
699
 *
423
701
 */
424
702
void x509_free( x509_cert *crt );
425
703
 
 
704
/** \ingroup x509_module */
426
705
/**
427
706
 * \brief          Unallocate all CRL data
428
707
 *
429
 
 * \param crt      CRL chain to free
 
708
 * \param crl      CRL chain to free
430
709
 */
431
710
void x509_crl_free( x509_crl *crl );
432
711
 
 
712
/** \} name Functions to clear a certificate, CRL or private RSA key */
 
713
 
 
714
 
433
715
/**
434
716
 * \brief          Checkup routine
435
717
 *