Skip to content

Commit 2213f30

Browse files
cameronrichigrr
authored andcommitted
* X509 State, country and location are now used for verification and display.
* SNI hostname memory is now managed by the calling application * X509 version number is checked before processing v3 extensions. git-svn-id: svn://svn.code.sf.net/p/axtls/code/trunk@272 9a5d90b5-6617-0410-8a86-bb477d3ed2e3
1 parent 425067a commit 2213f30

File tree

6 files changed

+107
-41
lines changed

6 files changed

+107
-41
lines changed

samples/c/axssl.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,6 @@ static void do_client(int argc, char *argv[])
650650
}
651651
652652
ssl_free(ssl);
653-
ssl_ext_free(extensions);
654653
exit(1);
655654
}
656655
@@ -660,7 +659,6 @@ static void do_client(int argc, char *argv[])
660659
if (reconnect)
661660
{
662661
ssl_free(ssl);
663-
ssl_ext_free(extensions);
664662
SOCKET_CLOSE(client_fd);
665663
666664
client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
@@ -687,13 +685,6 @@ static void do_client(int argc, char *argv[])
687685
688686
if (!quiet)
689687
{
690-
const char *common_name = ssl_get_cert_dn(ssl,
691-
SSL_X509_CERT_COMMON_NAME);
692-
if (common_name)
693-
{
694-
printf("Common Name:\t\t\t%s\n", common_name);
695-
}
696-
697688
display_session_id(ssl);
698689
display_cipher(ssl);
699690
}
@@ -766,7 +757,6 @@ static void do_client(int argc, char *argv[])
766757
}
767758
768759
ssl_ctx_free(ssl_ctx);
769-
ssl_ext_free(extensions);
770760
SOCKET_CLOSE(client_fd);
771761
#else
772762
print_client_options(argv[1]);

ssl/asn1.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007-2015, Cameron Rich
2+
* Copyright (c) 2007-2016, Cameron Rich
33
*
44
* All rights reserved.
55
*
@@ -80,8 +80,8 @@ static const uint8_t sig_subject_alt_name[] =
8080
0x55, 0x1d, 0x11
8181
};
8282

83-
/* CN, O, OU */
84-
static const uint8_t g_dn_types[] = { 3, 10, 11 };
83+
/* CN, O, OU, L, C, ST */
84+
static const uint8_t g_dn_types[] = { 3, 10, 11, 7, 6, 8 };
8585

8686
uint32_t get_asn1_length(const uint8_t *buf, int *offset)
8787
{
@@ -300,13 +300,19 @@ static int asn1_get_utc_time(const uint8_t *buf, int *offset, time_t *t)
300300
int asn1_version(const uint8_t *cert, int *offset, X509_CTX *x509_ctx)
301301
{
302302
int ret = X509_NOT_OK;
303+
int len;
303304

304305
(*offset) += 2; /* get past explicit tag */
305-
if (asn1_skip_obj(cert, offset, ASN1_INTEGER))
306-
goto end_version;
306+
if (cert[(*offset)++] != ASN1_INTEGER)
307+
return X509_NOT_OK;
307308

308-
ret = X509_OK;
309-
end_version:
309+
len = get_asn1_length(cert, offset);
310+
if (len == 1)
311+
{
312+
ret = cert[*offset];
313+
}
314+
315+
*offset += len;
310316
return ret;
311317
}
312318

ssl/crypto_misc.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ extern "C" {
6060
/*
6161
* The Distinguished Name
6262
*/
63-
#define X509_NUM_DN_TYPES 3
63+
#define X509_NUM_DN_TYPES 6
6464
#define X509_COMMON_NAME 0
6565
#define X509_ORGANIZATION 1
6666
#define X509_ORGANIZATIONAL_UNIT 2
67+
#define X509_LOCATION 3
68+
#define X509_COUNTRY 4
69+
#define X509_STATE 5
6770

6871
struct _x509_ctx
6972
{

ssl/ssl.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,15 @@ extern "C" {
159159
#define SSL_X509_CERT_COMMON_NAME 0
160160
#define SSL_X509_CERT_ORGANIZATION 1
161161
#define SSL_X509_CERT_ORGANIZATIONAL_NAME 2
162-
#define SSL_X509_CA_CERT_COMMON_NAME 3
163-
#define SSL_X509_CA_CERT_ORGANIZATION 4
164-
#define SSL_X509_CA_CERT_ORGANIZATIONAL_NAME 5
162+
#define SSL_X509_CERT_LOCATION 3
163+
#define SSL_X509_CERT_COUNTRY 4
164+
#define SSL_X509_CERT_STATE 5
165+
#define SSL_X509_CA_CERT_COMMON_NAME 6
166+
#define SSL_X509_CA_CERT_ORGANIZATION 7
167+
#define SSL_X509_CA_CERT_ORGANIZATIONAL_NAME 8
168+
#define SSL_X509_CA_CERT_LOCATION 9
169+
#define SSL_X509_CA_CERT_COUNTRY 10
170+
#define SSL_X509_CA_CERT_STATE 11
165171

166172
/* SSL object loader types */
167173
#define SSL_OBJ_X509_CERT 1
@@ -454,9 +460,15 @@ EXP_FUNC int STDCALL ssl_match_spki_sha256(const SSL *ssl, const uint8_t* hash);
454460
* - SSL_X509_CERT_COMMON_NAME
455461
* - SSL_X509_CERT_ORGANIZATION
456462
* - SSL_X509_CERT_ORGANIZATIONAL_NAME
463+
* - SSL_X509_CERT_LOCATION
464+
* - SSL_X509_CERT_COUNTRY
465+
* - SSL_X509_CERT_STATE
457466
* - SSL_X509_CA_CERT_COMMON_NAME
458467
* - SSL_X509_CA_CERT_ORGANIZATION
459468
* - SSL_X509_CA_CERT_ORGANIZATIONAL_NAME
469+
* - SSL_X509_CA_CERT_LOCATION
470+
* - SSL_X509_CA_CERT_COUNTRY
471+
* - SSL_X509_CA_CERT_STATE
460472
* @return The appropriate string (or null if not defined)
461473
* @note Verification build mode must be enabled.
462474
*/

ssl/tls1.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,7 @@ void DISPLAY_BYTES(SSL *ssl, const char *format,
145145
*/
146146
EXP_FUNC SSL_EXTENSIONS * STDCALL ssl_ext_new()
147147
{
148-
SSL_EXTENSIONS *ssl_ext = (SSL_EXTENSIONS *)malloc(sizeof(SSL_EXTENSIONS));
149-
ssl_ext->max_fragment_size = 0;
150-
ssl_ext->host_name = NULL;
151-
152-
return ssl_ext;
148+
return (SSL_EXTENSIONS *)calloc(1, sizeof(SSL_EXTENSIONS));
153149
}
154150

155151
/**
@@ -163,10 +159,6 @@ EXP_FUNC void STDCALL ssl_ext_free(SSL_EXTENSIONS *ssl_ext)
163159
return;
164160
}
165161

166-
if (ssl_ext->host_name != NULL)
167-
{
168-
free(ssl_ext->host_name);
169-
}
170162
free(ssl_ext);
171163
}
172164

@@ -530,6 +522,15 @@ EXP_FUNC const char * STDCALL ssl_get_cert_dn(const SSL *ssl, int component)
530522
case SSL_X509_CERT_ORGANIZATIONAL_NAME:
531523
return ssl->x509_ctx->cert_dn[X509_ORGANIZATIONAL_UNIT];
532524

525+
case SSL_X509_CERT_LOCATION:
526+
return ssl->x509_ctx->cert_dn[X509_LOCATION];
527+
528+
case SSL_X509_CERT_COUNTRY:
529+
return ssl->x509_ctx->cert_dn[X509_COUNTRY];
530+
531+
case SSL_X509_CERT_STATE:
532+
return ssl->x509_ctx->cert_dn[X509_STATE];
533+
533534
case SSL_X509_CA_CERT_COMMON_NAME:
534535
return ssl->x509_ctx->ca_cert_dn[X509_COMMON_NAME];
535536

@@ -539,6 +540,15 @@ EXP_FUNC const char * STDCALL ssl_get_cert_dn(const SSL *ssl, int component)
539540
case SSL_X509_CA_CERT_ORGANIZATIONAL_NAME:
540541
return ssl->x509_ctx->ca_cert_dn[X509_ORGANIZATIONAL_UNIT];
541542

543+
case SSL_X509_CA_CERT_LOCATION:
544+
return ssl->x509_ctx->ca_cert_dn[X509_LOCATION];
545+
546+
case SSL_X509_CA_CERT_COUNTRY:
547+
return ssl->x509_ctx->ca_cert_dn[X509_COUNTRY];
548+
549+
case SSL_X509_CA_CERT_STATE:
550+
return ssl->x509_ctx->ca_cert_dn[X509_STATE];
551+
542552
default:
543553
return NULL;
544554
}
@@ -1393,7 +1403,7 @@ int basic_read(SSL *ssl, uint8_t **in_data)
13931403
if (IS_SET_SSL_FLAG(SSL_NEED_RECORD))
13941404
{
13951405
/* check for sslv2 "client hello" */
1396-
if (buf[0] & 0x80 && buf[2] == 1)
1406+
if ((buf[0] & 0x80) && buf[2] == 1)
13971407
{
13981408
#ifdef CONFIG_SSL_FULL_MODE
13991409
printf("Error: no SSLv23 handshaking allowed\n");
@@ -2149,6 +2159,10 @@ int process_certificate(SSL *ssl, X509_CTX **x509_ctx)
21492159
goto error;
21502160
}
21512161

2162+
#if defined (CONFIG_SSL_FULL_MODE)
2163+
if (ssl->ssl_ctx->options & SSL_DISPLAY_CERTS)
2164+
x509_print(certs[num_certs], NULL);
2165+
#endif
21522166
num_certs++;
21532167
offset += cert_size;
21542168
}
@@ -2168,6 +2182,7 @@ int process_certificate(SSL *ssl, X509_CTX **x509_ctx)
21682182
{
21692183
if (certs[i] == chain)
21702184
continue;
2185+
21712186
if (cert_used[i])
21722187
continue; // don't allow loops
21732188

ssl/x509.c

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007-2015, Cameron Rich
2+
* Copyright (c) 2007-2016, Cameron Rich
33
*
44
* All rights reserved.
55
*
@@ -73,6 +73,7 @@ int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx)
7373
{
7474
int begin_tbs, end_tbs, begin_spki, end_spki;
7575
int ret = X509_NOT_OK, offset = 0, cert_size = 0;
76+
int version = 0;
7677
X509_CTX *x509_ctx;
7778
#ifdef CONFIG_SSL_CERT_VERIFICATION /* only care if doing verification */
7879
BI_CTX *bi_ctx;
@@ -96,7 +97,7 @@ int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx)
9697

9798
if (cert[offset] == ASN1_EXPLICIT_TAG) /* optional version */
9899
{
99-
if (asn1_version(cert, &offset, x509_ctx))
100+
if ((version = asn1_version(cert, &offset, x509_ctx)) == X509_NOT_OK)
100101
goto end_cert;
101102
}
102103

@@ -122,7 +123,6 @@ int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx)
122123
goto end_cert;
123124
end_spki = offset;
124125

125-
126126
x509_ctx->fingerprint = malloc(SHA1_SIZE);
127127
SHA1_CTX sha_fp_ctx;
128128
SHA1_Init(&sha_fp_ctx);
@@ -197,7 +197,7 @@ int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx)
197197
break;
198198
}
199199

200-
if (cert[offset] == ASN1_V3_DATA)
200+
if (version == 2 && cert[offset] == ASN1_V3_DATA)
201201
{
202202
int suboffset;
203203

@@ -518,9 +518,29 @@ void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx)
518518
printf("%s\n", cert->cert_dn[X509_ORGANIZATION] ?
519519
cert->cert_dn[X509_ORGANIZATION] : not_part_of_cert);
520520

521-
printf("Organizational Unit (OU):\t");
522-
printf("%s\n", cert->cert_dn[X509_ORGANIZATIONAL_UNIT] ?
523-
cert->cert_dn[X509_ORGANIZATIONAL_UNIT] : not_part_of_cert);
521+
if (cert->cert_dn[X509_ORGANIZATIONAL_UNIT])
522+
{
523+
printf("Organizational Unit (OU):\t");
524+
printf("%s\n", cert->cert_dn[X509_ORGANIZATIONAL_UNIT]);
525+
}
526+
527+
if (cert->cert_dn[X509_LOCATION])
528+
{
529+
printf("Location (L):\t\t\t");
530+
printf("%s\n", cert->cert_dn[X509_LOCATION]);
531+
}
532+
533+
if (cert->cert_dn[X509_COUNTRY])
534+
{
535+
printf("Country (C):\t\t\t");
536+
printf("%s\n", cert->cert_dn[X509_COUNTRY]);
537+
}
538+
539+
if (cert->cert_dn[X509_STATE])
540+
{
541+
printf("State (ST):\t\t\t");
542+
printf("%s\n", cert->cert_dn[X509_STATE]);
543+
}
524544

525545
printf("=== CERTIFICATE ISSUED BY ===\n");
526546
printf("Common Name (CN):\t\t");
@@ -531,9 +551,29 @@ void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx)
531551
printf("%s\n", cert->ca_cert_dn[X509_ORGANIZATION] ?
532552
cert->ca_cert_dn[X509_ORGANIZATION] : not_part_of_cert);
533553

534-
printf("Organizational Unit (OU):\t");
535-
printf("%s\n", cert->ca_cert_dn[X509_ORGANIZATIONAL_UNIT] ?
536-
cert->ca_cert_dn[X509_ORGANIZATIONAL_UNIT] : not_part_of_cert);
554+
if (cert->ca_cert_dn[X509_ORGANIZATIONAL_UNIT])
555+
{
556+
printf("Organizational Unit (OU):\t");
557+
printf("%s\n", cert->ca_cert_dn[X509_ORGANIZATIONAL_UNIT]);
558+
}
559+
560+
if (cert->ca_cert_dn[X509_LOCATION])
561+
{
562+
printf("Location (L):\t\t\t");
563+
printf("%s\n", cert->ca_cert_dn[X509_LOCATION]);
564+
}
565+
566+
if (cert->ca_cert_dn[X509_COUNTRY])
567+
{
568+
printf("Country (C):\t\t\t");
569+
printf("%s\n", cert->ca_cert_dn[X509_COUNTRY]);
570+
}
571+
572+
if (cert->ca_cert_dn[X509_STATE])
573+
{
574+
printf("State (ST):\t\t\t");
575+
printf("%s\n", cert->ca_cert_dn[X509_STATE]);
576+
}
537577

538578
printf("Not Before:\t\t\t%s", ctime(&cert->not_before));
539579
printf("Not After:\t\t\t%s", ctime(&cert->not_after));

0 commit comments

Comments
 (0)