Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * X.509 base functions for creating certificates / CSRs |
| 3 | * |
| 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
| 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | #include "polarssl/config.h" |
| 27 | |
| 28 | #if defined(POLARSSL_X509_CREATE_C) |
| 29 | |
| 30 | #include "polarssl/x509.h" |
| 31 | #include "polarssl/asn1write.h" |
| 32 | #include "polarssl/oid.h" |
| 33 | |
Paul Bakker | 6edcd41 | 2013-10-29 15:22:54 +0100 | [diff] [blame] | 34 | #if defined(_MSC_VER) && !defined strncasecmp && !defined(EFIX64) && \ |
| 35 | !defined(EFI32) |
Paul Bakker | 7b0be68 | 2013-10-29 14:24:37 +0100 | [diff] [blame] | 36 | #define strncasecmp _strnicmp |
| 37 | #endif |
| 38 | |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 39 | int x509_string_to_names( asn1_named_data **head, const char *name ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 40 | { |
| 41 | int ret = 0; |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 42 | const char *s = name, *c = s; |
| 43 | const char *end = s + strlen( s ); |
Paul Bakker | fcc1721 | 2013-10-11 09:36:52 +0200 | [diff] [blame] | 44 | const char *oid = NULL; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 45 | int in_tag = 1; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 46 | |
| 47 | /* Clear existing chain if present */ |
| 48 | asn1_free_named_data_list( head ); |
| 49 | |
| 50 | while( c <= end ) |
| 51 | { |
| 52 | if( in_tag && *c == '=' ) |
| 53 | { |
Paul Bakker | 7b0be68 | 2013-10-29 14:24:37 +0100 | [diff] [blame] | 54 | if( c - s == 2 && strncasecmp( s, "CN", 2 ) == 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 55 | oid = OID_AT_CN; |
Paul Bakker | 6384440 | 2014-04-30 15:34:12 +0200 | [diff] [blame^] | 56 | else if( c - s == 10 && strncasecmp( s, "commonName", 10 ) == 0 ) |
| 57 | oid = OID_AT_CN; |
Paul Bakker | 7b0be68 | 2013-10-29 14:24:37 +0100 | [diff] [blame] | 58 | else if( c - s == 1 && strncasecmp( s, "C", 1 ) == 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 59 | oid = OID_AT_COUNTRY; |
Paul Bakker | 6384440 | 2014-04-30 15:34:12 +0200 | [diff] [blame^] | 60 | else if( c - s == 11 && strncasecmp( s, "countryName", 11 ) == 0 ) |
| 61 | oid = OID_AT_COUNTRY; |
Paul Bakker | 7b0be68 | 2013-10-29 14:24:37 +0100 | [diff] [blame] | 62 | else if( c - s == 1 && strncasecmp( s, "O", 1 ) == 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 63 | oid = OID_AT_ORGANIZATION; |
Paul Bakker | 6384440 | 2014-04-30 15:34:12 +0200 | [diff] [blame^] | 64 | else if( c - s == 16 && strncasecmp( s, "organizationName", 16 ) == 0 ) |
| 65 | oid = OID_AT_ORGANIZATION; |
Paul Bakker | 7b0be68 | 2013-10-29 14:24:37 +0100 | [diff] [blame] | 66 | else if( c - s == 1 && strncasecmp( s, "L", 1 ) == 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 67 | oid = OID_AT_LOCALITY; |
Paul Bakker | 6384440 | 2014-04-30 15:34:12 +0200 | [diff] [blame^] | 68 | else if( c - s == 8 && strncasecmp( s, "locality", 8 ) == 0 ) |
| 69 | oid = OID_AT_LOCALITY; |
Paul Bakker | 7b0be68 | 2013-10-29 14:24:37 +0100 | [diff] [blame] | 70 | else if( c - s == 1 && strncasecmp( s, "R", 1 ) == 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 71 | oid = OID_PKCS9_EMAIL; |
Paul Bakker | 7b0be68 | 2013-10-29 14:24:37 +0100 | [diff] [blame] | 72 | else if( c - s == 2 && strncasecmp( s, "OU", 2 ) == 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 73 | oid = OID_AT_ORG_UNIT; |
Paul Bakker | 6384440 | 2014-04-30 15:34:12 +0200 | [diff] [blame^] | 74 | else if( c - s == 22 && strncasecmp( s, "organizationalUnitName", 22 ) == 0 ) |
| 75 | oid = OID_AT_ORG_UNIT; |
Paul Bakker | 7b0be68 | 2013-10-29 14:24:37 +0100 | [diff] [blame] | 76 | else if( c - s == 2 && strncasecmp( s, "ST", 2 ) == 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 77 | oid = OID_AT_STATE; |
Paul Bakker | 6384440 | 2014-04-30 15:34:12 +0200 | [diff] [blame^] | 78 | else if( c - s == 19 && strncasecmp( s, "stateOrProvinceName", 19 ) == 0 ) |
| 79 | oid = OID_AT_STATE; |
Paul Bakker | 0767e67 | 2014-04-18 14:11:37 +0200 | [diff] [blame] | 80 | else if( c - s == 12 && strncasecmp( s, "emailAddress", 12 ) == 0 ) |
| 81 | oid = OID_PKCS9_EMAIL; |
Paul Bakker | 7b0be68 | 2013-10-29 14:24:37 +0100 | [diff] [blame] | 82 | else if( c - s == 12 && strncasecmp( s, "serialNumber", 12 ) == 0 ) |
| 83 | oid = OID_AT_SERIAL_NUMBER; |
| 84 | else if( c - s == 13 && strncasecmp( s, "postalAddress", 13 ) == 0 ) |
| 85 | oid = OID_AT_POSTAL_ADDRESS; |
| 86 | else if( c - s == 10 && strncasecmp( s, "postalCode", 10 ) == 0 ) |
| 87 | oid = OID_AT_POSTAL_CODE; |
Paul Bakker | 6384440 | 2014-04-30 15:34:12 +0200 | [diff] [blame^] | 88 | else if( c - s == 11 && strncasecmp( s, "dnQualifier", 11 ) == 0 ) |
| 89 | oid = OID_AT_DN_QUALIFIER; |
| 90 | else if( c - s == 5 && strncasecmp( s, "title", 5 ) == 0 ) |
| 91 | oid = OID_AT_TITLE; |
| 92 | else if( c - s == 7 && strncasecmp( s, "surName", 7 ) == 0 ) |
| 93 | oid = OID_AT_SUR_NAME; |
| 94 | else if( c - s == 2 && strncasecmp( s, "SN", 2 ) == 0 ) |
| 95 | oid = OID_AT_SUR_NAME; |
| 96 | else if( c - s == 9 && strncasecmp( s, "givenName", 9 ) == 0 ) |
| 97 | oid = OID_AT_GIVEN_NAME; |
| 98 | else if( c - s == 2 && strncasecmp( s, "GN", 2 ) == 0 ) |
| 99 | oid = OID_AT_GIVEN_NAME; |
| 100 | else if( c - s == 8 && strncasecmp( s, "initials", 8 ) == 0 ) |
| 101 | oid = OID_AT_INITIALS; |
| 102 | else if( c - s == 9 && strncasecmp( s, "pseudonym", 9 ) == 0 ) |
| 103 | oid = OID_AT_PSEUDONYM; |
| 104 | else if( c - s == 19 && strncasecmp( s, "generationQualifier", 19 ) == 0 ) |
| 105 | oid = OID_AT_GENERATION_QUALIFIER; |
| 106 | else if( c - s == 15 && strncasecmp( s, "domainComponent", 15 ) == 0 ) |
| 107 | oid = OID_DOMAIN_COMPONENT; |
| 108 | else if( c - s == 2 && strncasecmp( s, "DC", 2 ) == 0 ) |
| 109 | oid = OID_DOMAIN_COMPONENT; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 110 | else |
| 111 | { |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 112 | ret = POLARSSL_ERR_X509_UNKNOWN_OID; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 113 | goto exit; |
| 114 | } |
| 115 | |
| 116 | s = c + 1; |
| 117 | in_tag = 0; |
| 118 | } |
| 119 | |
| 120 | if( !in_tag && ( *c == ',' || c == end ) ) |
| 121 | { |
Paul Bakker | a9c16d2 | 2014-04-17 12:42:18 +0200 | [diff] [blame] | 122 | if( asn1_store_named_data( head, oid, strlen( oid ), |
| 123 | (unsigned char *) s, |
| 124 | c - s ) == NULL ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 125 | { |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 126 | return( POLARSSL_ERR_X509_MALLOC_FAILED ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | while( c < end && *(c + 1) == ' ' ) |
| 130 | c++; |
| 131 | |
| 132 | s = c + 1; |
| 133 | in_tag = 1; |
| 134 | } |
| 135 | c++; |
| 136 | } |
| 137 | |
| 138 | exit: |
| 139 | |
| 140 | return( ret ); |
| 141 | } |
| 142 | |
| 143 | /* The first byte of the value in the asn1_named_data structure is reserved |
| 144 | * to store the critical boolean for us |
| 145 | */ |
| 146 | int x509_set_extension( asn1_named_data **head, const char *oid, size_t oid_len, |
| 147 | int critical, const unsigned char *val, size_t val_len ) |
| 148 | { |
| 149 | asn1_named_data *cur; |
| 150 | |
| 151 | if( ( cur = asn1_store_named_data( head, oid, oid_len, |
| 152 | NULL, val_len + 1 ) ) == NULL ) |
| 153 | { |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 154 | return( POLARSSL_ERR_X509_MALLOC_FAILED ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | cur->val.p[0] = critical; |
| 158 | memcpy( cur->val.p + 1, val, val_len ); |
| 159 | |
| 160 | return( 0 ); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * RelativeDistinguishedName ::= |
| 165 | * SET OF AttributeTypeAndValue |
| 166 | * |
| 167 | * AttributeTypeAndValue ::= SEQUENCE { |
| 168 | * type AttributeType, |
| 169 | * value AttributeValue } |
| 170 | * |
| 171 | * AttributeType ::= OBJECT IDENTIFIER |
| 172 | * |
| 173 | * AttributeValue ::= ANY DEFINED BY AttributeType |
| 174 | */ |
| 175 | static int x509_write_name( unsigned char **p, unsigned char *start, |
| 176 | const char *oid, size_t oid_len, |
| 177 | const unsigned char *name, size_t name_len ) |
| 178 | { |
| 179 | int ret; |
| 180 | size_t len = 0; |
| 181 | |
| 182 | // Write PrintableString for all except OID_PKCS9_EMAIL |
| 183 | // |
| 184 | if( OID_SIZE( OID_PKCS9_EMAIL ) == oid_len && |
| 185 | memcmp( oid, OID_PKCS9_EMAIL, oid_len ) == 0 ) |
| 186 | { |
| 187 | ASN1_CHK_ADD( len, asn1_write_ia5_string( p, start, |
| 188 | (const char *) name, |
| 189 | name_len ) ); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | ASN1_CHK_ADD( len, asn1_write_printable_string( p, start, |
| 194 | (const char *) name, |
| 195 | name_len ) ); |
| 196 | } |
| 197 | |
| 198 | // Write OID |
| 199 | // |
| 200 | ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) ); |
| 201 | |
| 202 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 203 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 204 | |
| 205 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 206 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SET ) ); |
| 207 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 208 | return( (int) len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | int x509_write_names( unsigned char **p, unsigned char *start, |
| 212 | asn1_named_data *first ) |
| 213 | { |
| 214 | int ret; |
| 215 | size_t len = 0; |
| 216 | asn1_named_data *cur = first; |
| 217 | |
| 218 | while( cur != NULL ) |
| 219 | { |
| 220 | ASN1_CHK_ADD( len, x509_write_name( p, start, (char *) cur->oid.p, |
| 221 | cur->oid.len, |
| 222 | cur->val.p, cur->val.len ) ); |
| 223 | cur = cur->next; |
| 224 | } |
| 225 | |
| 226 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 227 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 228 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 229 | return( (int) len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | int x509_write_sig( unsigned char **p, unsigned char *start, |
| 233 | const char *oid, size_t oid_len, |
| 234 | unsigned char *sig, size_t size ) |
| 235 | { |
| 236 | int ret; |
| 237 | size_t len = 0; |
| 238 | |
| 239 | if( *p - start < (int) size + 1 ) |
| 240 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 241 | |
| 242 | len = size; |
| 243 | (*p) -= len; |
| 244 | memcpy( *p, sig, len ); |
| 245 | |
| 246 | *--(*p) = 0; |
| 247 | len += 1; |
| 248 | |
| 249 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 250 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) ); |
| 251 | |
| 252 | // Write OID |
| 253 | // |
| 254 | ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid, |
| 255 | oid_len, 0 ) ); |
| 256 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 257 | return( (int) len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static int x509_write_extension( unsigned char **p, unsigned char *start, |
| 261 | asn1_named_data *ext ) |
| 262 | { |
| 263 | int ret; |
| 264 | size_t len = 0; |
| 265 | |
| 266 | ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, ext->val.p + 1, |
| 267 | ext->val.len - 1 ) ); |
| 268 | ASN1_CHK_ADD( len, asn1_write_len( p, start, ext->val.len - 1 ) ); |
| 269 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) ); |
| 270 | |
| 271 | if( ext->val.p[0] != 0 ) |
| 272 | { |
| 273 | ASN1_CHK_ADD( len, asn1_write_bool( p, start, 1 ) ); |
| 274 | } |
| 275 | |
| 276 | ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, ext->oid.p, |
| 277 | ext->oid.len ) ); |
| 278 | ASN1_CHK_ADD( len, asn1_write_len( p, start, ext->oid.len ) ); |
| 279 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OID ) ); |
| 280 | |
| 281 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 282 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 283 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 284 | return( (int) len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Extension ::= SEQUENCE { |
| 289 | * extnID OBJECT IDENTIFIER, |
| 290 | * critical BOOLEAN DEFAULT FALSE, |
| 291 | * extnValue OCTET STRING |
| 292 | * -- contains the DER encoding of an ASN.1 value |
| 293 | * -- corresponding to the extension type identified |
| 294 | * -- by extnID |
| 295 | * } |
| 296 | */ |
| 297 | int x509_write_extensions( unsigned char **p, unsigned char *start, |
| 298 | asn1_named_data *first ) |
| 299 | { |
| 300 | int ret; |
| 301 | size_t len = 0; |
| 302 | asn1_named_data *cur_ext = first; |
| 303 | |
| 304 | while( cur_ext != NULL ) |
| 305 | { |
| 306 | ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) ); |
| 307 | cur_ext = cur_ext->next; |
| 308 | } |
| 309 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 310 | return( (int) len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | #endif /* POLARSSL_X509_CREATE_C */ |