39 #if defined(POLARSSL_X509_CRT_PARSE_C)
43 #if defined(POLARSSL_PEM_PARSE_C)
47 #if defined(POLARSSL_MEMORY_C)
50 #define polarssl_malloc malloc
51 #define polarssl_free free
56 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
62 #if defined(EFIX64) || defined(EFI32)
66 #if defined(POLARSSL_FS_IO)
69 #include <sys/types.h>
78 static int x509_get_version(
unsigned char **p,
79 const unsigned char *end,
114 static int x509_get_dates(
unsigned char **p,
115 const unsigned char *end,
144 static int x509_get_uid(
unsigned char **p,
145 const unsigned char *end,
170 static int x509_get_basic_constraints(
unsigned char **p,
171 const unsigned char *end,
201 if( *ca_istrue != 0 )
208 if( ( ret =
asn1_get_int( p, end, max_pathlen ) ) != 0 )
220 static int x509_get_ns_cert_type(
unsigned char **p,
221 const unsigned char *end,
222 unsigned char *ns_cert_type)
235 *ns_cert_type = *bs.
p;
239 static int x509_get_key_usage(
unsigned char **p,
240 const unsigned char *end,
241 unsigned char *key_usage)
263 static int x509_get_ext_key_usage(
unsigned char **p,
264 const unsigned char *end,
273 if( ext_key_usage->
buf.
p == NULL )
306 static int x509_get_subject_alt_name(
unsigned char **p,
307 const unsigned char *end,
321 if( *p + len != end )
327 if( ( end - *p ) < 1 )
340 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
358 if( cur->
next == NULL )
384 static int x509_get_crt_ext(
unsigned char **p,
385 const unsigned char *end,
390 unsigned char *end_ext_data, *end_ext_octet;
416 end_ext_data = *p + len;
427 if( ( end - *p ) < 1 )
432 if( ( ret =
asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
441 end_ext_octet = *p + len;
443 if( end_ext_octet != end_ext_data )
457 #if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
474 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
481 if( ( ret = x509_get_key_usage( p, end_ext_octet,
488 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
495 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
502 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
522 static int x509_crt_parse_der_core(
x509_crt *crt,
const unsigned char *buf,
527 unsigned char *p, *end, *crt_end;
532 if( crt == NULL || buf == NULL )
540 memcpy( p, buf, buflen );
561 if( len > (
size_t) ( end - p ) )
591 if( ( ret = x509_get_version( &p, end, &crt->
version ) ) != 0 ||
640 if( ( ret = x509_get_dates( &p, end, &crt->
valid_from,
686 ret = x509_get_uid( &p, end, &crt->
issuer_id, 1 );
696 ret = x509_get_uid( &p, end, &crt->
subject_id, 2 );
704 #if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
708 ret = x509_get_crt_ext( &p, end, crt);
714 #if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
771 x509_crt *crt = chain, *prev = NULL;
776 if( crt == NULL || buf == NULL )
792 if( crt->
next == NULL )
800 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
819 int success = 0, first_error = 0, total_failed = 0;
825 if( chain == NULL || buf == NULL )
832 #if defined(POLARSSL_PEM_PARSE_C)
833 if( strstr( (
const char *) buf,
"-----BEGIN CERTIFICATE-----" ) != NULL )
840 #if defined(POLARSSL_PEM_PARSE_C)
851 ret = pem_read_buffer( &pem,
852 "-----BEGIN CERTIFICATE-----",
853 "-----END CERTIFICATE-----",
854 buf, NULL, 0, &use_len );
878 if( first_error == 0 )
898 if( first_error == 0 )
911 return( total_failed );
912 else if( first_error )
913 return( first_error );
918 #if defined(POLARSSL_FS_IO)
933 memset( buf, 0, n + 1 );
942 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
944 WCHAR szDir[MAX_PATH];
945 char filename[MAX_PATH];
947 int len = (int) strlen( path );
949 WIN32_FIND_DATAW file_data;
952 if( len > MAX_PATH - 3 )
955 memset( szDir, 0,
sizeof(szDir) );
956 memset( filename, 0, MAX_PATH );
957 memcpy( filename, path, len );
958 filename[len++] =
'\\';
960 filename[len++] =
'*';
962 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
964 hFind = FindFirstFileW( szDir, &file_data );
965 if (hFind == INVALID_HANDLE_VALUE)
968 len = MAX_PATH - len;
973 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
976 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
977 lstrlenW(file_data.cFileName),
987 while( FindNextFileW( hFind, &file_data ) != 0 );
989 if (GetLastError() != ERROR_NO_MORE_FILES)
994 #if defined(POLARSSL_HAVE_READDIR_R)
997 struct dirent entry, *result = NULL;
998 char entry_name[255];
999 DIR *dir = opendir( path );
1004 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
1006 if( result == NULL )
1009 snprintf( entry_name,
sizeof(entry_name),
"%s/%s", path, entry.d_name );
1011 i = stat( entry_name, &sb );
1019 if( !S_ISREG( sb.st_mode ) )
1042 #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1046 #if !defined vsnprintf
1047 #define vsnprintf _vsnprintf
1057 static int compat_snprintf(
char *str,
size_t size,
const char *format, ...)
1062 va_start( ap, format );
1064 res = vsnprintf( str, size, format, ap );
1070 return( (
int) size + 20 );
1075 #define snprintf compat_snprintf
1078 #define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1080 #define SAFE_SNPRINTF() \
1085 if ( (unsigned int) ret > n ) { \
1087 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1090 n -= (unsigned int) ret; \
1091 p += (unsigned int) ret; \
1097 #define BEFORE_COLON 14
1099 int x509_crt_info(
char *buf,
size_t size,
const char *prefix,
1105 const char *desc = NULL;
1106 char key_size_str[BEFORE_COLON];
1111 ret = snprintf( p, n,
"%scert. version : %d\n",
1114 ret = snprintf( p, n,
"%sserial number : ",
1121 ret = snprintf( p, n,
"\n%sissuer name : ", prefix );
1126 ret = snprintf( p, n,
"\n%ssubject name : ", prefix );
1131 ret = snprintf( p, n,
"\n%sissued on : " \
1132 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1138 ret = snprintf( p, n,
"\n%sexpires on : " \
1139 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1145 ret = snprintf( p, n,
"\n%ssigned using : ", prefix );
1150 ret = snprintf( p, n,
"???" );
1152 ret = snprintf( p, n,
"%s", desc );
1161 ret = snprintf( p, n,
"\n%s%-" BC
"s: %d bits\n", prefix, key_size_str,
1165 return( (
int) ( size - n ) );
1168 #if defined(POLARSSL_X509_CRL_PARSE_C)
1176 while( cur != NULL && cur->
serial.
len != 0 )
1210 while( crl_list != NULL )
1217 crl_list = crl_list->
next;
1225 if( md_info == NULL )
1234 md( md_info, crl_list->
tbs.
p, crl_list->
tbs.
len, hash );
1259 crl_list = crl_list->
next;
1266 static int x509_name_cmp(
const void *s1,
const void *s2,
size_t len )
1270 const unsigned char *n1 = s1, *n2 = s2;
1272 for( i = 0; i < len; i++ )
1274 diff = n1[i] ^ n2[i];
1276 if( ( n1[i] >=
'a' || n1[i] <=
'z' ) && ( diff == 0 || diff == 32 ) )
1279 if( ( n1[i] >=
'A' || n1[i] <=
'Z' ) && ( diff == 0 || diff == 32 ) )
1288 static int x509_wildcard_verify(
const char *cn,
x509_buf *name )
1293 if( name->
len < 3 || name->
p[0] !=
'*' || name->
p[1] !=
'.' )
1296 for( i = 0; i < strlen( cn ); ++i )
1308 if( strlen( cn ) - cn_idx == name->
len - 1 &&
1309 x509_name_cmp( name->
p + 1, cn + cn_idx, name->
len - 1 ) == 0 )
1317 static int x509_crt_verify_top(
1319 x509_crl *ca_crl,
int path_cnt,
int *flags,
1320 int (*f_vrfy)(
void *,
x509_crt *,
int,
int *),
1324 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1337 if( md_info == NULL )
1347 while( trust_ca != NULL )
1354 trust_ca = trust_ca->
next;
1372 trust_ca = trust_ca->
next;
1380 trust_ca = trust_ca->
next;
1396 if( trust_ca != NULL &&
1401 #if defined(POLARSSL_X509_CRL_PARSE_C)
1403 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
1411 if( NULL != f_vrfy )
1413 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
1419 if( NULL != f_vrfy )
1421 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1430 static int x509_crt_verify_child(
1432 x509_crl *ca_crl,
int path_cnt,
int *flags,
1433 int (*f_vrfy)(
void *,
x509_crt *,
int,
int *),
1437 int parent_flags = 0;
1446 if( md_info == NULL )
1465 #if defined(POLARSSL_X509_CRL_PARSE_C)
1467 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
1470 grandparent = parent->
next;
1472 while( grandparent != NULL )
1474 if( grandparent->
version == 0 ||
1480 grandparent = grandparent->
next;
1486 if( grandparent != NULL )
1491 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
1497 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
1503 if( NULL != f_vrfy )
1504 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1507 *flags |= parent_flags;
1518 const char *cn,
int *flags,
1519 int (*f_vrfy)(
void *,
x509_crt *,
int,
int *),
1534 cn_len = strlen( cn );
1540 while( cur != NULL )
1542 if( cur->
buf.
len == cn_len &&
1543 x509_name_cmp( cn, cur->
buf.
p, cn_len ) == 0 )
1547 memcmp( cur->
buf.
p,
"*.", 2 ) == 0 &&
1548 x509_wildcard_verify( cn, &cur->
buf ) )
1559 while( name != NULL )
1563 if( name->
val.
len == cn_len &&
1564 x509_name_cmp( name->
val.
p, cn, cn_len ) == 0 )
1568 memcmp( name->
val.
p,
"*.", 2 ) == 0 &&
1569 x509_wildcard_verify( cn, &name->
val ) )
1587 while( parent != NULL && parent->
version != 0 )
1594 parent = parent->
next;
1600 if( parent != NULL )
1605 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
1611 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
1627 memset( crt, 0,
sizeof(
x509_crt) );
1650 while( name_cur != NULL )
1652 name_prv = name_cur;
1653 name_cur = name_cur->
next;
1654 memset( name_prv, 0,
sizeof(
x509_name ) );
1659 while( name_cur != NULL )
1661 name_prv = name_cur;
1662 name_cur = name_cur->
next;
1663 memset( name_prv, 0,
sizeof(
x509_name ) );
1668 while( seq_cur != NULL )
1671 seq_cur = seq_cur->
next;
1677 while( seq_cur != NULL )
1680 seq_cur = seq_cur->
next;
1685 if( cert_cur->
raw.
p != NULL )
1687 memset( cert_cur->
raw.
p, 0, cert_cur->
raw.
len );
1691 cert_cur = cert_cur->
next;
1693 while( cert_cur != NULL );
1698 cert_prv = cert_cur;
1699 cert_cur = cert_cur->
next;
1701 memset( cert_prv, 0,
sizeof(
x509_crt ) );
1702 if( cert_prv != crt )
1705 while( cert_cur != NULL );
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
int x509_time_expired(const x509_time *time)
Check a given x509_time against the system time and check if it is valid.
int asn1_get_sequence_of(unsigned char **p, const unsigned char *end, asn1_sequence *cur, int tag)
Parses and splits an ASN.1 "SEQUENCE OF <tag>" Updated the pointer to immediately behind the full seq...
x509_sequence subject_alt_names
Optional list of Subject Alternative Names (Only dNSName supported).
void *(* polarssl_malloc)(size_t len)
int x509_get_name(unsigned char **p, const unsigned char *end, x509_name *cur)
#define POLARSSL_ERR_X509_INVALID_DATE
The date tag or value is invalid.
asn1_buf buf
Buffer containing the given ASN.1 item.
int x509_get_serial(unsigned char **p, const unsigned char *end, x509_buf *serial)
x509_buf raw
The raw certificate data (DER).
#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH
Actual length differs from expected length.
int ext_types
Bit string containing detected and parsed extensions.
Certificate revocation list entry.
size_t pk_get_size(const pk_context *ctx)
Get the size in bits of the underlying key.
#define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT
Format not recognized as DER or PEM.
#define EXT_BASIC_CONSTRAINTS
unsigned char ns_cert_type
Optional Netscape certificate type extension value: See the values below.
x509_buf issuer_raw
The raw issuer data (DER).
#define POLARSSL_ERR_X509_INVALID_FORMAT
The CRT/CRL/CSR format is invalid, e.g.
int x509_key_size_helper(char *buf, size_t size, const char *name)
int asn1_get_int(unsigned char **p, const unsigned char *end, int *val)
Retrieve an integer ASN.1 tag and its value.
size_t len
ASN1 length, e.g.
int x509_get_alg_null(unsigned char **p, const unsigned char *end, x509_buf *alg)
Container for date and time (precision in seconds).
int x509_crt_parse(x509_crt *chain, const unsigned char *buf, size_t buflen)
Parse one or more certificates and add them to the chained list.
x509_buf sig_oid2
Signature algorithm.
int oid_get_x509_ext_type(const asn1_buf *oid, int *ext_type)
Translate an X.509 extension OID into local values.
void x509_crt_free(x509_crt *crt)
Unallocate all certificate data.
Configuration options (set of defines)
#define OID_CMP(oid_str, oid_buf)
Compares two asn1_buf structures for the same OID.
x509_buf tbs
The raw certificate body (DER).
x509_buf serial
Unique id for certificate issued by a specific CA.
md_type_t sig_md
Internal representation of the MD algorithm of the signature algorithm, e.g.
int ca_istrue
Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise...
int x509_crt_parse_der(x509_crt *chain, const unsigned char *buf, size_t buflen)
Parse a single DER formatted certificate and add it to the chained list.
int max_pathlen
Optional Basic Constraint extension value: The maximum path length to the root certificate.
int x509_get_sig(unsigned char **p, const unsigned char *end, x509_buf *sig)
const char * pk_get_name(const pk_context *ctx)
Access the type name.
#define BADCRL_NOT_TRUSTED
CRL is not correctly signed by the trusted CA.
#define POLARSSL_ERR_ASN1_INVALID_LENGTH
Error when trying to determine the length or invalid length.
Container for ASN1 bit strings.
#define POLARSSL_ERR_X509_UNKNOWN_VERSION
CRT/CRL/CSR has an unsupported version number.
Object Identifier (OID) database.
#define OID_AT_CN
id-at-commonName AttributeType:= {id-at 3}
struct _x509_crt * next
Next certificate in the CA-chain.
x509_crl_entry entry
The CRL entries containing the certificate revocation times for this CA.
asn1_buf val
The named value.
Container for a sequence of ASN.1 items.
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
int x509_get_time(unsigned char **p, const unsigned char *end, x509_time *time)
#define BADCERT_EXPIRED
The certificate validity has expired.
unsigned char * p
Raw ASN1 data for the bit string.
#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED
Certificate verification failed, e.g.
Container for an X.509 certificate.
Privacy Enhanced Mail (PEM) decoding.
x509_time valid_from
Start time of certificate validity.
int x509_dn_gets(char *buf, size_t size, const x509_name *dn)
Store the certificate DN in printable form into buf; no more than size characters will be written...
asn1_buf oid
The object identifier.
int pk_verify(pk_context *ctx, md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature.
x509_sequence ext_key_usage
Optional list of extended key usage OIDs.
void x509_crt_init(x509_crt *crt)
Initialize a certificate (chain)
void(* polarssl_free)(void *ptr)
unsigned char * p
ASN1 data, e.g.
int oid_get_sig_alg_desc(const asn1_buf *oid, const char **desc)
Translate SignatureAlgorithm OID into description.
x509_name subject
The parsed subject data (named information object).
int x509_crt_verify(x509_crt *crt, x509_crt *trust_ca, x509_crl *ca_crl, const char *cn, int *flags, int(*f_vrfy)(void *, x509_crt *, int, int *), void *p_vrfy)
Verify the certificate signature.
int pk_can_do(pk_context *ctx, pk_type_t type)
Tell if a context can do the operation given by type.
x509_buf tbs
The raw certificate body (DER).
int asn1_get_bool(unsigned char **p, const unsigned char *end, int *val)
Retrieve a boolean ASN.1 tag and its value.
x509_time valid_to
End time of certificate validity.
struct _x509_crl_entry * next
md_type_t sig_md
Internal representation of the MD algorithm of the signature algorithm, e.g.
X.509 certificate parsing and writing.
int x509_get_sig_alg(const x509_buf *sig_oid, md_type_t *md_alg, pk_type_t *pk_alg)
x509_buf sig_oid1
Signature algorithm, e.g.
#define POLARSSL_ERR_ASN1_OUT_OF_DATA
Out of data when parsing an ASN1 data structure.
#define EXT_EXTENDED_KEY_USAGE
int pk_parse_subpubkey(unsigned char **p, const unsigned char *end, pk_context *pk)
Parse a SubjectPublicKeyInfo DER structure.
int x509_load_file(const char *path, unsigned char **buf, size_t *n)
#define POLARSSL_ERR_ASN1_MALLOC_FAILED
Memory allocation failed.
#define ASN1_CONTEXT_SPECIFIC
#define BADCERT_NOT_TRUSTED
The certificate is not correctly signed by the trusted CA.
#define POLARSSL_ERR_X509_FILE_IO_ERROR
Read/write of file failed.
int x509_crt_revoked(const x509_crt *crt, const x509_crl *crl)
Verify the certificate revocation status.
Container for a sequence or list of 'named' ASN.1 data items.
Type-length-value structure that allows for ASN1 using DER.
pk_type_t sig_pk
< Internal representation of the Public Key algorithm of the signature algorithm, e...
size_t len
ASN1 length, e.g.
x509_name issuer
The parsed issuer data (named information object).
void pk_free(pk_context *ctx)
Free a pk_context.
#define POLARSSL_MD_MAX_SIZE
#define BADCERT_REVOKED
The certificate has been revoked (is on a CRL).
#define BADCRL_EXPIRED
CRL is expired.
int asn1_get_len(unsigned char **p, const unsigned char *end, size_t *len)
Get the length of an ASN.1 element.
#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE
Unavailable feature, e.g.
int asn1_get_tag(unsigned char **p, const unsigned char *end, size_t *len, int tag)
Get the tag and length of the tag.
int x509_get_ext(unsigned char **p, const unsigned char *end, x509_buf *ext, int tag)
#define POLARSSL_ERR_X509_INVALID_VERSION
The CRT/CRL/CSR version element is invalid.
Certificate revocation list structure.
int asn1_get_bitstring(unsigned char **p, const unsigned char *end, asn1_bitstring *bs)
Retrieve a bitstring ASN.1 tag and its value.
pk_context pk
Container for the public key context.
struct _asn1_named_data * next
The next entry in the sequence.
int size
Output length of the digest function.
x509_buf issuer_id
Optional X.509 v2/v3 issuer unique identifier.
#define POLARSSL_ERR_X509_INVALID_EXTENSIONS
The extension tag or value is invalid.
#define ASN1_OCTET_STRING
int x509_crt_parse_path(x509_crt *chain, const char *path)
Load one or more certificate files from a path and add them to the chained list.
x509_buf v3_ext
Optional X.509 v3 extensions.
#define POLARSSL_ERR_X509_BAD_INPUT_DATA
Input invalid.
x509_buf subject_id
Optional X.509 v2/v3 subject unique identifier.
#define BADCERT_CN_MISMATCH
The certificate Common Name (CN) does not match with the expected CN.
int version
The X.509 version.
#define POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT
No PEM header or footer found.
pk_type_t sig_pk
< Internal representation of the Public Key algorithm of the signature algorithm, e...
x509_time revocation_date
x509_buf issuer_raw
The raw issuer data (DER).
int x509_crt_info(char *buf, size_t size, const char *prefix, const x509_crt *crt)
Returns an informational string about the certificate.
#define POLARSSL_ERR_X509_MALLOC_FAILED
Allocation of memory failed.
#define POLARSSL_ERR_PEM_BAD_INPUT_DATA
Bad input parameters to function.
unsigned char key_usage
Optional key usage extension value: See the values below.
x509_buf subject_raw
The raw subject data (DER).
Message digest information.
#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG
ASN1 tag was of an unexpected value.
int x509_serial_gets(char *buf, size_t size, const x509_buf *serial)
Store the certificate serial in printable form into buf; no more than size characters will be written...
#define POLARSSL_ERR_X509_SIG_MISMATCH
Signature algorithms do not match.
struct _asn1_sequence * next
The next entry in the sequence.
int x509_crt_parse_file(x509_crt *chain, const char *path)
Load one or more certificates and add them to the chained list.
x509_buf sig
Signature: hash of the tbs part signed with the private key.
#define EXT_SUBJECT_ALT_NAME