Skip to contentSkip to conversation

tls: Pass full Subject DN contents to "ready" callback

PATCH Applied Andrew Zaborowski 2 participants 11 messages Aug 6, 2019 – Aug 8, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 tls: Pass full Subject DN contents to "ready" callback

Until now the ready callback would receive a displayable string as peer_identity (if peer was authenticated) which was either the Common Name or the Organization Name extracted from the subject DN in the peer's certificate.

In order to implement functionality similat to wpa_supplicant's substring_match= option for EAP-TLS, we need to look specifically at Common Name although if we have the full contents of the Subject DN that's even better as wpa_supplicant allows configurations to match against any element in the Subject DN, so if we do that iwd and wpa_s config files may be easier to convert in either direction.

We could also just pass the whole certificate struct to the callback but then we'd need to also have utilities for parsing the DN so instead we parse it in tls.c and convert it into the same string format that is used by wpa_s, openssl, etc. Parsing that string in user code is easier than parsing the original DER struct.

---
 ell/asn1-private.h |  1 +
 ell/tls.c          | 76 ++++++++++++++++++++++++++++++++--------------
 2 files changed, 55 insertions(+), 22 deletions(-)
ell/asn1-private.h +1 -0
index 53bba4c..71a4ae6 100644
@@ -30,6 +30,7 @@
 #define ASN1_ID_OID		ASN1_ID(ASN1_CLASS_UNIVERSAL, 0, 0x06)
 #define ASN1_ID_UTF8STRING	ASN1_ID(ASN1_CLASS_UNIVERSAL, 0, 0x0c)
 #define ASN1_ID_PRINTABLESTRING	ASN1_ID(ASN1_CLASS_UNIVERSAL, 0, 0x13)
+#define ASN1_ID_IA5STRING	ASN1_ID(ASN1_CLASS_UNIVERSAL, 0, 0x16)
 
 struct asn1_oid {
 	uint8_t asn1_len;
ell/tls.c +54 -22

122 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/4 unit: Update expected peer identity strings in TLS

---
 unit/test-tls.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)
unit/test-tls.c +22 -11
index 03c5370..d701f42 100644
@@ -326,7 +326,8 @@ static const struct tls_conn_test tls_conn_test_server_auth = {
 	.server_key_path = CERTDIR "cert-server-key-pkcs8.pem",
 	.server_expect_identity = NULL,
 	.client_ca_cert_path = CERTDIR "cert-ca.pem",
-	.client_expect_identity = "Foo Example Organization",
+	.client_expect_identity = "/O=Foo Example Organization"
+		"/CN=Foo Example Organization/emailAddress=foo(a)mail.example",
 };
 
 static const struct tls_conn_test tls_conn_test_client_auth_attempt = {
@@ -341,7 +342,8 @@ static const struct tls_conn_test tls_conn_test_client_auth = {
 	.server_cert_path = CERTDIR "cert-server.pem",
 	.server_key_path = CERTDIR "cert-server-key-pkcs8.pem",
 	.server_ca_cert_path = CERTDIR "cert-ca.pem",
-	.server_expect_identity = "Bar Example Organization",
+	.server_expect_identity = "/O=Bar Example Organization"
+		"/CN=Bar Example Organization/emailAddress=bar(a)mail.example",
 	.client_cert_path = CERTDIR "cert-client.pem",
 	.client_key_path = CERTDIR "cert-client-key-pkcs8.pem",
 	.client_expect_identity = NULL,
@@ -353,29 +355,34 @@ static const struct tls_conn_test tls_conn_test_full_auth_attempt = {
 	.server_ca_cert_path = CERTDIR "cert-ca.pem",
 	.server_expect_identity = NULL,
 	.client_ca_cert_path = CERTDIR "cert-ca.pem",
-	.client_expect_identity = "Foo Example Organization",
+	.client_expect_identity = "/O=Foo Example Organization"
+		"/CN=Foo Example Organization/emailAddress=foo(a)mail.example",
 };
 
 static const struct tls_conn_test tls_conn_test_full_auth = {
 	.server_cert_path = CERTDIR "cert-server.pem",
 	.server_key_path = CERTDIR "cert-server-key-pkcs8.pem",
 	.server_ca_cert_path = CERTDIR "cert-ca.pem",
-	.server_expect_identity = "Bar Example Organization",
+	.server_expect_identity = "/O=Bar Example Organization"
+		"/CN=Bar Example Organization/emailAddress=bar(a)mail.example",
 	.client_cert_path = CERTDIR "cert-client.pem",
 	.client_key_path = CERTDIR "cert-client-key-pkcs8.pem",
 	.client_ca_cert_path = CERTDIR "cert-ca.pem",
-	.client_expect_identity = "Foo Example Organization",
+	.client_expect_identity = "/O=Foo Example Organization"
+		"/CN=Foo Example Organization/emailAddress=foo(a)mail.example",
 };
 
 static const struct tls_conn_test tls_conn_test_bad_client_suite = {
 	.server_cert_path = CERTDIR "cert-server.pem",
 	.server_key_path = CERTDIR "cert-server-key-pkcs8.pem",
 	.server_ca_cert_path = CERTDIR "cert-ca.pem",
-	.server_expect_identity = "Bar Example Organization",
+	.server_expect_identity = "/O=Bar Example Organization"
+		"/CN=Bar Example Organization/emailAddress=bar(a)mail.example",
 	.client_cert_path = CERTDIR "cert-client.pem",
 	.client_key_path = CERTDIR "cert-client-key-pkcs8.pem",
 	.client_ca_cert_path = CERTDIR "cert-ca.pem",
-	.client_expect_identity = "Foo Example Organization",
+	.client_expect_identity = "/O=Foo Example Organization"
+		"/CN=Foo Example Organization/emailAddress=foo(a)mail.example",
 	.client_cipher_suites = (const char *[]) { "UNKNOWN", NULL },
 	.expect_client_start_fail = true,
 };
@@ -384,13 +391,15 @@ static const struct tls_conn_test tls_conn_test_suite_mismatch = {
 	.server_cert_path = CERTDIR "cert-server.pem",
 	.server_key_path = CERTDIR "cert-server-key-pkcs8.pem",
 	.server_ca_cert_path = CERTDIR "cert-ca.pem",
-	.server_expect_identity = "Bar Example Organization",
+	.server_expect_identity = "/O=Bar Example Organization"
+		"/CN=Bar Example Organization/emailAddress=bar(a)mail.example",
 	.server_cipher_suites =
 		(const char *[]) { "TLS_RSA_WITH_AES_128_CBC_SHA256", NULL },
 	.client_cert_path = CERTDIR "cert-client.pem",
 	.client_key_path = CERTDIR "cert-client-key-pkcs8.pem",
 	.client_ca_cert_path = CERTDIR "cert-ca.pem",
-	.client_expect_identity = "Foo Example Organization",
+	.client_expect_identity = "/O=Foo Example Organization"
+		"/CN=Foo Example Organization/emailAddress=foo(a)mail.example",
 	.client_cipher_suites =
 		(const char *[]) { "TLS_RSA_WITH_AES_256_CBC_SHA256", NULL },
 	.expect_alert = true,
@@ -401,11 +410,13 @@ static const struct tls_conn_test tls_conn_test_version_mismatch = {
 	.server_cert_path = CERTDIR "cert-server.pem",
 	.server_key_path = CERTDIR "cert-server-key-pkcs8.pem",
 	.server_ca_cert_path = CERTDIR "cert-ca.pem",
-	.server_expect_identity = "Bar Example Organization",
+	.server_expect_identity = "/O=Bar Example Organization"
+		"/CN=Bar Example Organization/emailAddress=bar(a)mail.example",
 	.client_cert_path = CERTDIR "cert-client.pem",
 	.client_key_path = CERTDIR "cert-client-key-pkcs8.pem",
 	.client_ca_cert_path = CERTDIR "cert-ca.pem",
-	.client_expect_identity = "Foo Example Organization",
+	.client_expect_identity = "/O=Foo Example Organization"
+		"/CN=Foo Example Organization/emailAddress=foo(a)mail.example",
 	.expect_alert = true,
 	.alert_desc = TLS_ALERT_PROTOCOL_VERSION,
 };
Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH 2/4] unit: Update expected peer identity strings in TLS

Hi Andrew,

Quoted 4 lines · Andrew Zaborowski
---
  unit/test-tls.c | 33 ++++++++++++++++++++++-----------
  1 file changed, 22 insertions(+), 11 deletions(-)

Applied, but any chance we can get some spoofed certs for negative testing?

unit/test-tls.c static const struct tls_conn_test tls_conn_test_server_auth = { · line 330

Quoted 11 lines
unit/test-tls.c +2 -1
index 03c5370..d701f42 100644
@@ -326,7 +326,8 @@ static const struct tls_conn_test tls_conn_test_server_auth = {
  	.server_key_path = CERTDIR "cert-server-key-pkcs8.pem",
  	.server_expect_identity = NULL,
  	.client_ca_cert_path = CERTDIR "cert-ca.pem",
-	.client_expect_identity = "Foo Example Organization",
+	.client_expect_identity = "/O=Foo Example Organization"
+		"/CN=Foo Example Organization/emailAddress=foo(a)mail.example",

Also, what happens if the strings contain a '/' character? Or is that not possible?

unit/test-tls.c static const struct tls_conn_test tls_conn_test_server_auth = { · line 333

Quoted 3 lines
unit/test-tls.c +0 -0
  };
 
  static const struct tls_conn_test tls_conn_test_client_auth_attempt = {

Regards,
-Denis

#3 lore raw

Re: [PATCH 2/4] unit: Update expected peer identity strings in TLS

Hi Denis,

Quoted 10 lines · Denis Kenzior

Hi Andrew,

Quoted 4 lines · Andrew Zaborowski
---
  unit/test-tls.c | 33 ++++++++++++++++++++++-----------
  1 file changed, 22 insertions(+), 11 deletions(-)

Applied, but any chance we can get some spoofed certs for negative testing?

We've got one negative test for the subject matching in the iwd patches (which I still need to respin I think) but we don't need a special cert for that we just use a wrong subject_match string. And I think we've got some test cases in ell where the actual RSA crypto is supposed to fail if we're verifying against a wrong cert.

Quoted 15 lines

unit/test-tls.c static const struct tls_conn_test tls_conn_test_server_auth = { · line 330

Quoted 12 lines
unit/test-tls.c +2 -1
index 03c5370..d701f42 100644
@@ -326,7 +326,8 @@ static const struct tls_conn_test tls_conn_test_server_auth = {
      .server_key_path = CERTDIR "cert-server-key-pkcs8.pem",
      .server_expect_identity = NULL,
      .client_ca_cert_path = CERTDIR "cert-ca.pem",
-     .client_expect_identity = "Foo Example Organization",
+     .client_expect_identity = "/O=Foo Example Organization"
+             "/CN=Foo Example Organization/emailAddress=foo(a)mail.example",

Also, what happens if the strings contain a '/' character? Or is that not possible?

So apparently openssl has some \ unescaping support for these strings when you generate the certificate and probably the characters are escaped again when you extract this string from a cert. We could replicate this but in terms of security I think it doesn't add anything, users will just need to remember to not escape the characters in the iwd configs.

Best regards

#4 lore raw

Re: [PATCH 2/4] unit: Update expected peer identity strings in TLS

Hi Andrew,

Quoted 10 lines
Quoted 2 lines

Also, what happens if the strings contain a '/' character? Or is that not possible?

So apparently openssl has some \ unescaping support for these strings when you generate the certificate and probably the characters are escaped again when you extract this string from a cert. We could replicate this but in terms of security I think it doesn't add anything, users will just need to remember to not escape the characters in the iwd configs.

This response doesn't fill me with a lot of confidence :) How is domain name suffix matching code (for example) going to deal with random '/' characters if they serve as delimiters?

Regards,
-Denis

#5 lore raw

Re: [PATCH 2/4] unit: Update expected peer identity strings in TLS

Quoted 11 lines · Denis Kenzior
Quoted 10 lines
Quoted 2 lines

Also, what happens if the strings contain a '/' character? Or is that not possible?

So apparently openssl has some \ unescaping support for these strings when you generate the certificate and probably the characters are escaped again when you extract this string from a cert. We could replicate this but in terms of security I think it doesn't add anything, users will just need to remember to not escape the characters in the iwd configs.

Quoted 3 lines

This response doesn't fill me with a lot of confidence :) How is domain name suffix matching code (for example) going to deal with random '/' characters if they serve as delimiters?

So with the simple fnmatch or substring matching this wasn't really a problem as without a regex you had no way to distinguish a '/' from an escaped "\/". I mean, it was just as bad in both cases :)

With a proper parser for this string in the client code though, and with proper escaping, we may be able to prevent some attacks where someone convinces the CA to sell them a certificate with e.g. "/CN=example.com" contained inside another field in the subject DN, so yeah, perhaps it's worth adding, I'll do that.

Best regards

#6 lore raw

PATCH 3/4 tls: Make l_tls_free reentrant

In reply to Andrew Zaborowski

Handle l_tls_free being called from inside user callbacks that our code may have called. Use flags because in this case it's not possible to move all of the callbacks to the end of our functions.

---
 ell/tls-private.h |  3 +++
 ell/tls.c         | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+)
ell/tls-private.h +3 -0
index bf9e2ec..c1de257 100644
@@ -215,6 +215,9 @@ struct l_tls {
 
 	struct tls_cipher_suite **cipher_suite_pref_list;
 
+	bool in_callback;
+	bool pending_destroy;
+
 	/* Record layer */
 
 	uint8_t *record_buf;
ell/tls.c +19 -0
index c3591a8..4b23076 100644
@@ -2098,7 +2098,9 @@ static void tls_finished(struct l_tls *tls)
 	TLS_SET_STATE(TLS_HANDSHAKE_DONE);
 	tls->ready = true;
 
+	tls->in_callback = true;
 	tls->ready_handle(peer_identity, tls->user_data);
+	tls->in_callback = false;
 	l_free(peer_identity);
 
 	tls_cleanup_handshake(tls);
@@ -2389,6 +2391,11 @@ LIB_EXPORT void l_tls_free(struct l_tls *tls)
 	if (unlikely(!tls))
 		return;
 
+	if (tls->in_callback) {
+		tls->pending_destroy = true;
+		return;
+	}
+
 	l_tls_set_cacert(tls, NULL);
 	l_tls_set_auth_data(tls, NULL, NULL, NULL);
 
@@ -2537,6 +2544,11 @@ bool tls_handle_message(struct l_tls *tls, const uint8_t *message,
 					message + TLS_HANDSHAKE_HEADER_SIZE,
 					len - TLS_HANDSHAKE_HEADER_SIZE);
 
+		if (tls->pending_destroy) {
+			l_tls_free(tls);
+			return false;
+		}
+
 		return true;
 
 	case TLS_CT_APPLICATION_DATA:
@@ -2551,7 +2563,14 @@ bool tls_handle_message(struct l_tls *tls, const uint8_t *message,
 		if (!len)
 			return true;
 
+		tls->in_callback = true;
 		tls->rx(message, len, tls->user_data);
+		tls->in_callback = false;
+
+		if (tls->pending_destroy) {
+			l_tls_free(tls);
+			return false;
+		}
 
 		return true;
 	}
Signature · 1 line
2.20.1
#7 lore raw

Re: [PATCH 3/4] tls: Make l_tls_free reentrant

Hi Andrew,

Quoted 8 lines · Andrew Zaborowski

Handle l_tls_free being called from inside user callbacks that our code may have called. Use flags because in this case it's not possible to move all of the callbacks to the end of our functions.

---
  ell/tls-private.h |  3 +++
  ell/tls.c         | 19 +++++++++++++++++++
  2 files changed, 22 insertions(+)

Applied, thanks.

Quoted 5 lines
ell/tls-private.h +0 -0
index bf9e2ec..c1de257 100644

ell/tls-private.h struct l_tls { · line 223

Quoted 9 lines
ell/tls-private.h +3 -0
@@ -215,6 +215,9 @@ struct l_tls {
 
  	struct tls_cipher_suite **cipher_suite_pref_list;
 
+	bool in_callback;
+	bool pending_destroy;
+
  	/* Record layer */
 
  	uint8_t *record_buf;

By the way, do you want to make all these bools here a bitfield instead?

Regards,
-Denis

#8 lore raw

PATCH 4/4 tls: Fix an error check

In reply to Andrew Zaborowski
---
 ell/tls.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ell/tls.c +1 -1
index 4b23076..7405098 100644
@@ -2058,7 +2058,7 @@ static char *tls_get_peer_identity_str(struct l_cert *cert)
 			goto error;
 
 		name = asn1_der_find_elem(seq, len, 1, &tag, &name_len);
-		if (!oid || (tag != ASN1_ID_PRINTABLESTRING &&
+		if (!name || (tag != ASN1_ID_PRINTABLESTRING &&
 					tag != ASN1_ID_UTF8STRING &&
 					tag != ASN1_ID_IA5STRING))
 			continue;
Signature · 1 line
2.20.1
#9 lore raw

Re: [PATCH 4/4] tls: Fix an error check

Hi Andrew,

Quoted 4 lines · Andrew Zaborowski
---
  ell/tls.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

Guess my question from patch 1 is answered ;)

Applied, thanks.

Regards,
-Denis

#10 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 17 lines · Andrew Zaborowski

Until now the ready callback would receive a displayable string as peer_identity (if peer was authenticated) which was either the Common Name or the Organization Name extracted from the subject DN in the peer's certificate.

In order to implement functionality similat to wpa_supplicant's substring_match= option for EAP-TLS, we need to look specifically at Common Name although if we have the full contents of the Subject DN that's even better as wpa_supplicant allows configurations to match against any element in the Subject DN, so if we do that iwd and wpa_s config files may be easier to convert in either direction.

We could also just pass the whole certificate struct to the callback but then we'd need to also have utilities for parsing the DN so instead we parse it in tls.c and convert it into the same string format that is used by wpa_s, openssl, etc. Parsing that string in user code is easier than parsing the original DER struct.

Quoted 5 lines
---
  ell/asn1-private.h |  1 +
  ell/tls.c          | 76 ++++++++++++++++++++++++++++++++--------------
  2 files changed, 55 insertions(+), 22 deletions(-)

Applied, but..

Quoted 22 lines
ell/tls.c +4 -1
@@ -2009,45 +2034,52 @@ static char *tls_get_peer_identity_str(struct l_cert *cert)
  	if (!dn)
  		return NULL;
 
+	id_str = l_string_new(200);
+
  	end = dn + dn_size;
  	while (dn < end) {
  		const uint8_t *set, *seq, *oid, *name;
  		uint8_t tag;
  		size_t len, oid_len, name_len;
+		const struct dn_element_info *info;
 
  		set = asn1_der_find_elem(dn, end - dn, 0, &tag, &len);
  		if (!set || tag != ASN1_ID_SET)
-			return NULL;
+			goto error;
 
  		dn = set + len;
 
  		seq = asn1_der_find_elem(set, len, 0, &tag, &len);
  		if (!seq || tag != ASN1_ID_SEQUENCE)

ell/tls.c static char *tls_get_peer_identity_str(struct l_cert *cert) · line 2061

Quoted 10 lines
ell/tls.c +2 -2
-			return NULL;
+			goto error;
 
  		oid = asn1_der_find_elem(seq, len, 0, &tag, &oid_len);
  		if (!oid || tag != ASN1_ID_OID)
-			return NULL;
+			goto error;
 
  		name = asn1_der_find_elem(seq, len, 1, &tag, &name_len);
  		if (!oid || (tag != ASN1_ID_PRINTABLESTRING &&

Should this be !name and not !oid?

Quoted 28 lines
ell/tls.c +12 -11
-					tag != ASN1_ID_UTF8STRING))
+					tag != ASN1_ID_UTF8STRING &&
+					tag != ASN1_ID_IA5STRING))
  			continue;
 
-		/* organizationName takes priority, commonName is second */
-		if (asn1_oid_eq(&dn_organization_name_oid, oid_len, oid) ||
-				(!printable_str &&
-				 asn1_oid_eq(&dn_common_name_oid,
-						oid_len, oid))) {
-			printable_str = name;
-			printable_str_len = name_len;
-		}
+		for (info = dn_elements; info->str; info++)
+			if (asn1_oid_eq(&info->oid, oid_len, oid))
+				break;
+		if (!info->str)
+			continue;
+
+		l_string_append_c(id_str, '/');
+		l_string_append(id_str, info->str);
+		l_string_append_c(id_str, '=');
+		l_string_append_fixed(id_str, (char *) name, name_len);
  	}
 
-	if (printable_str)
-		return l_strndup((char *) printable_str, printable_str_len);

ell/tls.c static char *tls_get_peer_identity_str(struct l_cert *cert) · line 2085

Quoted 10 lines
ell/tls.c +5 -2
-	else
-		return NULL;
+	return l_string_unwrap(id_str);
+
+error:
+	l_string_free(id_str);
+	return NULL;
  }
 
  static void tls_finished(struct l_tls *tls)

Regards,
-Denis