Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file x509.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
| 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine |
| 5 | * |
Paul Bakker | 785a9ee | 2009-01-25 14:15:10 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org> |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 22 | #ifndef POLARSSL_X509_H |
| 23 | #define POLARSSL_X509_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 24 | |
Paul Bakker | 8e831ed | 2009-01-03 21:24:11 +0000 | [diff] [blame] | 25 | #include "polarssl/rsa.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #define POLARSSL_ERR_ASN1_OUT_OF_DATA -0x0014 |
| 28 | #define POLARSSL_ERR_ASN1_UNEXPECTED_TAG -0x0016 |
| 29 | #define POLARSSL_ERR_ASN1_INVALID_LENGTH -0x0018 |
| 30 | #define POLARSSL_ERR_ASN1_LENGTH_MISMATCH -0x001A |
| 31 | #define POLARSSL_ERR_ASN1_INVALID_DATA -0x001C |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 33 | #define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x0020 |
| 34 | #define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x0040 |
| 35 | #define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x0060 |
| 36 | #define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x0080 |
| 37 | #define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x00A0 |
| 38 | #define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x00C0 |
| 39 | #define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x00E0 |
| 40 | #define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x0100 |
| 41 | #define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x0120 |
| 42 | #define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x0140 |
| 43 | #define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x0160 |
| 44 | #define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x0180 |
| 45 | #define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x01A0 |
| 46 | #define POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG -0x01C0 |
| 47 | #define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x01E0 |
| 48 | #define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x0200 |
| 49 | #define POLARSSL_ERR_X509_KEY_INVALID_PEM -0x0220 |
| 50 | #define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x0240 |
| 51 | #define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x0260 |
| 52 | #define POLARSSL_ERR_X509_KEY_INVALID_ENC_IV -0x0280 |
| 53 | #define POLARSSL_ERR_X509_KEY_UNKNOWN_ENC_ALG -0x02A0 |
| 54 | #define POLARSSL_ERR_X509_KEY_PASSWORD_REQUIRED -0x02C0 |
| 55 | #define POLARSSL_ERR_X509_KEY_PASSWORD_MISMATCH -0x02E0 |
| 56 | #define POLARSSL_ERR_X509_POINT_ERROR -0x0300 |
| 57 | #define POLARSSL_ERR_X509_VALUE_TO_LENGTH -0x0320 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 | |
| 59 | #define BADCERT_EXPIRED 1 |
| 60 | #define BADCERT_REVOKED 2 |
| 61 | #define BADCERT_CN_MISMATCH 4 |
| 62 | #define BADCERT_NOT_TRUSTED 8 |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 63 | #define BADCRL_NOT_TRUSTED 16 |
| 64 | #define BADCRL_EXPIRED 32 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * DER constants |
| 68 | */ |
| 69 | #define ASN1_BOOLEAN 0x01 |
| 70 | #define ASN1_INTEGER 0x02 |
| 71 | #define ASN1_BIT_STRING 0x03 |
| 72 | #define ASN1_OCTET_STRING 0x04 |
| 73 | #define ASN1_NULL 0x05 |
| 74 | #define ASN1_OID 0x06 |
| 75 | #define ASN1_UTF8_STRING 0x0C |
| 76 | #define ASN1_SEQUENCE 0x10 |
| 77 | #define ASN1_SET 0x11 |
| 78 | #define ASN1_PRINTABLE_STRING 0x13 |
| 79 | #define ASN1_T61_STRING 0x14 |
| 80 | #define ASN1_IA5_STRING 0x16 |
| 81 | #define ASN1_UTC_TIME 0x17 |
| 82 | #define ASN1_UNIVERSAL_STRING 0x1C |
| 83 | #define ASN1_BMP_STRING 0x1E |
| 84 | #define ASN1_PRIMITIVE 0x00 |
| 85 | #define ASN1_CONSTRUCTED 0x20 |
| 86 | #define ASN1_CONTEXT_SPECIFIC 0x80 |
| 87 | |
| 88 | /* |
| 89 | * various object identifiers |
| 90 | */ |
| 91 | #define X520_COMMON_NAME 3 |
| 92 | #define X520_COUNTRY 6 |
| 93 | #define X520_LOCALITY 7 |
| 94 | #define X520_STATE 8 |
| 95 | #define X520_ORGANIZATION 10 |
| 96 | #define X520_ORG_UNIT 11 |
| 97 | #define PKCS9_EMAIL 1 |
| 98 | |
| 99 | #define X509_OUTPUT_DER 0x01 |
| 100 | #define X509_OUTPUT_PEM 0x02 |
| 101 | #define PEM_LINE_LENGTH 72 |
| 102 | #define X509_ISSUER 0x01 |
| 103 | #define X509_SUBJECT 0x02 |
| 104 | |
| 105 | #define OID_X520 "\x55\x04" |
| 106 | #define OID_CN "\x55\x04\x03" |
| 107 | #define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01" |
| 108 | #define OID_PKCS1_RSA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01" |
| 109 | #define OID_PKCS1_RSA_SHA "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05" |
| 110 | #define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09" |
| 111 | #define OID_PKCS9_EMAIL "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01" |
| 112 | |
| 113 | /* |
| 114 | * Structures for parsing X.509 certificates |
| 115 | */ |
| 116 | typedef struct _x509_buf |
| 117 | { |
| 118 | int tag; |
| 119 | int len; |
| 120 | unsigned char *p; |
| 121 | } |
| 122 | x509_buf; |
| 123 | |
| 124 | typedef struct _x509_name |
| 125 | { |
| 126 | x509_buf oid; |
| 127 | x509_buf val; |
| 128 | struct _x509_name *next; |
| 129 | } |
| 130 | x509_name; |
| 131 | |
| 132 | typedef struct _x509_time |
| 133 | { |
| 134 | int year, mon, day; |
| 135 | int hour, min, sec; |
| 136 | } |
| 137 | x509_time; |
| 138 | |
| 139 | typedef struct _x509_cert |
| 140 | { |
| 141 | x509_buf raw; |
| 142 | x509_buf tbs; |
| 143 | |
| 144 | int version; |
| 145 | x509_buf serial; |
| 146 | x509_buf sig_oid1; |
| 147 | |
| 148 | x509_buf issuer_raw; |
| 149 | x509_buf subject_raw; |
| 150 | |
| 151 | x509_name issuer; |
| 152 | x509_name subject; |
| 153 | |
| 154 | x509_time valid_from; |
| 155 | x509_time valid_to; |
| 156 | |
| 157 | x509_buf pk_oid; |
| 158 | rsa_context rsa; |
| 159 | |
| 160 | x509_buf issuer_id; |
| 161 | x509_buf subject_id; |
| 162 | x509_buf v3_ext; |
| 163 | |
| 164 | int ca_istrue; |
| 165 | int max_pathlen; |
| 166 | |
| 167 | x509_buf sig_oid2; |
| 168 | x509_buf sig; |
| 169 | |
| 170 | struct _x509_cert *next; |
| 171 | } |
| 172 | x509_cert; |
| 173 | |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 174 | typedef struct _x509_crl_entry |
| 175 | { |
| 176 | x509_buf raw; |
| 177 | |
| 178 | x509_buf serial; |
| 179 | |
| 180 | x509_time revocation_date; |
| 181 | |
| 182 | x509_buf entry_ext; |
| 183 | |
| 184 | struct _x509_crl_entry *next; |
| 185 | } |
| 186 | x509_crl_entry; |
| 187 | |
| 188 | typedef struct _x509_crl |
| 189 | { |
| 190 | x509_buf raw; |
| 191 | x509_buf tbs; |
| 192 | |
| 193 | int version; |
| 194 | x509_buf sig_oid1; |
| 195 | |
| 196 | x509_buf issuer_raw; |
| 197 | |
| 198 | x509_name issuer; |
| 199 | |
| 200 | x509_time this_update; |
| 201 | x509_time next_update; |
| 202 | |
| 203 | x509_crl_entry entry; |
| 204 | |
| 205 | x509_buf crl_ext; |
| 206 | |
| 207 | x509_buf sig_oid2; |
| 208 | x509_buf sig; |
| 209 | |
| 210 | struct _x509_crl *next; |
| 211 | } |
| 212 | x509_crl; |
| 213 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 214 | /* |
| 215 | * Structures for writing X.509 certificates |
| 216 | */ |
| 217 | typedef struct _x509_node |
| 218 | { |
| 219 | unsigned char *data; |
| 220 | unsigned char *p; |
| 221 | unsigned char *end; |
| 222 | |
| 223 | size_t len; |
| 224 | } |
| 225 | x509_node; |
| 226 | |
| 227 | typedef struct _x509_raw |
| 228 | { |
| 229 | x509_node raw; |
| 230 | x509_node tbs; |
| 231 | |
| 232 | x509_node version; |
| 233 | x509_node serial; |
| 234 | x509_node tbs_signalg; |
| 235 | x509_node issuer; |
| 236 | x509_node validity; |
| 237 | x509_node subject; |
| 238 | x509_node subpubkey; |
| 239 | |
| 240 | x509_node signalg; |
| 241 | x509_node sign; |
| 242 | } |
| 243 | x509_raw; |
| 244 | |
| 245 | #ifdef __cplusplus |
| 246 | extern "C" { |
| 247 | #endif |
| 248 | |
| 249 | /** |
| 250 | * \brief Parse one or more certificates and add them |
| 251 | * to the chained list |
| 252 | * |
| 253 | * \param chain points to the start of the chain |
| 254 | * \param buf buffer holding the certificate data |
| 255 | * \param buflen size of the buffer |
| 256 | * |
| 257 | * \return 0 if successful, or a specific X509 error code |
| 258 | */ |
Paul Bakker | 592457c | 2009-04-01 19:01:43 +0000 | [diff] [blame] | 259 | int x509parse_crt( x509_cert *chain, unsigned char *buf, int buflen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * \brief Load one or more certificates and add them |
| 263 | * to the chained list |
| 264 | * |
| 265 | * \param chain points to the start of the chain |
| 266 | * \param path filename to read the certificates from |
| 267 | * |
| 268 | * \return 0 if successful, or a specific X509 error code |
| 269 | */ |
Paul Bakker | 592457c | 2009-04-01 19:01:43 +0000 | [diff] [blame] | 270 | int x509parse_crtfile( x509_cert *chain, char *path ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 271 | |
| 272 | /** |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 273 | * \brief Parse one or more CRLs and add them |
| 274 | * to the chained list |
| 275 | * |
| 276 | * \param chain points to the start of the chain |
| 277 | * \param buf buffer holding the CRL data |
| 278 | * \param buflen size of the buffer |
| 279 | * |
| 280 | * \return 0 if successful, or a specific X509 error code |
| 281 | */ |
| 282 | int x509parse_crl( x509_crl *chain, unsigned char *buf, int buflen ); |
| 283 | |
| 284 | /** |
| 285 | * \brief Load one or more CRLs and add them |
| 286 | * to the chained list |
| 287 | * |
| 288 | * \param chain points to the start of the chain |
| 289 | * \param path filename to read the CRLs from |
| 290 | * |
| 291 | * \return 0 if successful, or a specific X509 error code |
| 292 | */ |
| 293 | int x509parse_crlfile( x509_crl *chain, char *path ); |
| 294 | |
| 295 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 296 | * \brief Parse a private RSA key |
| 297 | * |
| 298 | * \param rsa RSA context to be initialized |
| 299 | * \param buf input buffer |
| 300 | * \param buflen size of the buffer |
| 301 | * \param pwd password for decryption (optional) |
| 302 | * \param pwdlen size of the password |
| 303 | * |
| 304 | * \return 0 if successful, or a specific X509 error code |
| 305 | */ |
| 306 | int x509parse_key( rsa_context *rsa, |
| 307 | unsigned char *buf, int buflen, |
| 308 | unsigned char *pwd, int pwdlen ); |
| 309 | |
| 310 | /** |
| 311 | * \brief Load and parse a private RSA key |
| 312 | * |
| 313 | * \param rsa RSA context to be initialized |
| 314 | * \param path filename to read the private key from |
| 315 | * \param pwd password to decrypt the file (can be NULL) |
| 316 | * |
| 317 | * \return 0 if successful, or a specific X509 error code |
| 318 | */ |
| 319 | int x509parse_keyfile( rsa_context *rsa, char *path, char *password ); |
| 320 | |
| 321 | /** |
| 322 | * \brief Store the certificate DN in printable form into buf; |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 323 | * no more than size characters will be written. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 324 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 325 | int x509parse_dn_gets( char *buf, size_t size, x509_name *dn ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 326 | |
| 327 | /** |
| 328 | * \brief Returns an informational string about the |
| 329 | * certificate. |
| 330 | */ |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 331 | int x509parse_cert_info( char *buf, size_t size, char *prefix, x509_cert *crt ); |
| 332 | |
| 333 | /** |
| 334 | * \brief Returns an informational string about the |
| 335 | * CRL. |
| 336 | */ |
| 337 | int x509parse_crl_info( char *buf, size_t size, char *prefix, x509_crl *crl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 338 | |
| 339 | /** |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 340 | * \brief Return 0 if the x509_time is still valid, |
| 341 | * or 1 otherwise. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 342 | */ |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 343 | int x509parse_time_expired( x509_time *time ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 344 | |
| 345 | /** |
| 346 | * \brief Verify the certificate signature |
| 347 | * |
| 348 | * \param crt a certificate to be verified |
| 349 | * \param trust_ca the trusted CA chain |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 350 | * \param ca_crl the CRL chain for trusted CA's |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 351 | * \param cn expected Common Name (can be set to |
| 352 | * NULL if the CN must not be verified) |
| 353 | * \param flags result of the verification |
| 354 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 355 | * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 356 | * in which case *flags will have one or more of |
| 357 | * the following values set: |
| 358 | * BADCERT_EXPIRED -- |
| 359 | * BADCERT_REVOKED -- |
| 360 | * BADCERT_CN_MISMATCH -- |
| 361 | * BADCERT_NOT_TRUSTED |
| 362 | * |
| 363 | * \note TODO: add two arguments, depth and crl |
| 364 | */ |
| 365 | int x509parse_verify( x509_cert *crt, |
| 366 | x509_cert *trust_ca, |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 367 | x509_crl *ca_crl, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 368 | char *cn, int *flags ); |
| 369 | |
| 370 | /** |
| 371 | * \brief Unallocate all certificate data |
| 372 | */ |
| 373 | void x509_free( x509_cert *crt ); |
| 374 | |
| 375 | /** |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 376 | * \brief Unallocate all CRL data |
| 377 | */ |
| 378 | void x509_crl_free( x509_crl *crl ); |
| 379 | |
| 380 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 381 | * \brief Checkup routine |
| 382 | * |
| 383 | * \return 0 if successful, or 1 if the test failed |
| 384 | */ |
| 385 | int x509_self_test( int verbose ); |
| 386 | |
| 387 | #ifdef __cplusplus |
| 388 | } |
| 389 | #endif |
| 390 | |
| 391 | #endif /* x509.h */ |