[pkg-opensc-commit] [opensc] 67/295: libopensc: generic parse of certificate extensions

Eric Dorland eric at moszumanska.debian.org
Sat Jun 24 21:11:16 UTC 2017


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository opensc.

commit e4f5f845129e3d0311e8e2994ae7e471b337f7f5
Author: Jakub Jelen <jjelen at redhat.com>
Date:   Mon Aug 15 13:38:29 2016 +0200

    libopensc: generic parse of certificate extensions
    
    The code attempted to handle extensions assuming extensions were ordered. The
    only extension it handled was crl's, but the handling was wrong and I didn't
    find any actual use of the crl code. I've changed it to cache all the extensions
    and then provided accessors functions to read a specific extension. I needed this
    to read the key Usage, but the extension fetching code can work with any extension
    (though the caller will need to parse the result. I also added code that parses DN
    and returns a specifically requested DN component. I needed this to get the Common
    Name for the certificate Subject. This gives the token a 'unique' name rather than
    some generic name (like CAC-I or CAC-II). Both of these can be used to enhance the
    piv support as well.
    
    rebased by VTA
    Closes #852
---
 src/libopensc/opensc.h      |   2 +-
 src/libopensc/pkcs15-cert.c | 209 +++++++++++++++++++++++++++++++++++++++++---
 src/libopensc/pkcs15.h      |  18 +++-
 3 files changed, 213 insertions(+), 16 deletions(-)

diff --git a/src/libopensc/opensc.h b/src/libopensc/opensc.h
index 8af0227..2c4d776 100644
--- a/src/libopensc/opensc.h
+++ b/src/libopensc/opensc.h
@@ -1265,7 +1265,7 @@ int sc_format_oid(struct sc_object_id *oid, const char *in);
  * Compares two sc_object_id objects
  * @param  oid1  the first sc_object_id object
  * @param  oid2  the second sc_object_id object
- * @return 1 if the oids are equal and a non-zero value otherwise
+ * @return 1 if the oids are equal and a zero value otherwise
  */
 int sc_compare_oid(const struct sc_object_id *oid1, const struct sc_object_id *oid2);
 /**
diff --git a/src/libopensc/pkcs15-cert.c b/src/libopensc/pkcs15-cert.c
index 80051b5..390cd50 100644
--- a/src/libopensc/pkcs15-cert.c
+++ b/src/libopensc/pkcs15-cert.c
@@ -47,16 +47,8 @@ parse_x509_cert(sc_context_t *ctx, struct sc_pkcs15_der *der, struct sc_pkcs15_c
 		{ "version", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, 0, &cert->version, NULL },
 		{ NULL, 0, 0, 0, NULL, NULL }
 	};
-	struct sc_asn1_entry asn1_x509v3[] = {
-		{ "certificatePolicies",	SC_ASN1_OCTET_STRING, SC_ASN1_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
-		{ "subjectKeyIdentifier",	SC_ASN1_OCTET_STRING, SC_ASN1_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
-		{ "crlDistributionPoints",	SC_ASN1_OCTET_STRING, SC_ASN1_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL | SC_ASN1_ALLOC, &cert->crl, &cert->crl_len },
-		{ "authorityKeyIdentifier",	SC_ASN1_OCTET_STRING, SC_ASN1_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
-		{ "keyUsage",			SC_ASN1_BOOLEAN, SC_ASN1_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
-		{ NULL, 0, 0, 0, NULL, NULL }
-	};
 	struct sc_asn1_entry asn1_extensions[] = {
-		{ "x509v3",		SC_ASN1_STRUCT,    SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, asn1_x509v3, NULL },
+		{ "x509v3",		SC_ASN1_OCTET_STRING,    SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL| SC_ASN1_ALLOC, &cert->extensions, &cert->extensions_len },
 		{ NULL, 0, 0, 0, NULL, NULL }
 	};
 	struct sc_asn1_entry asn1_tbscert[] = {
@@ -141,6 +133,197 @@ parse_x509_cert(sc_context_t *ctx, struct sc_pkcs15_der *der, struct sc_pkcs15_c
 }
 
 
+/* Get a component of Distinguished Name (e.i. subject or issuer) USING the oid tag.
+ * dn can be either cert->subject or cert->issuer.
+ * dn_len would be cert->subject_len or cert->issuer_len.
+ *
+ * Common types:
+ *   CN:      struct sc_object_id type = {{85, 4, 3, -1}};
+ *   Country: struct sc_object_id type = {{85, 4, 6, -1}};
+ *   L:       struct sc_object_id type = {{85, 4, 7, -1}};
+ *   S:       struct sc_object_id type = {{85, 4, 8, -1}};
+ *   O:       struct sc_object_id type = {{85, 4, 10, -1}};
+ *   OU:      struct sc_object_id type = {{85, 4, 11, -1}};
+ *
+ * if *name is NULL, sc_pkcs15_get_name_from_dn will allocate space for name.
+ */
+int
+sc_pkcs15_get_name_from_dn(struct sc_context *ctx, const u8 *dn, size_t dn_len,
+	const struct sc_object_id *type, u8 **name, size_t *name_len)
+{
+	const u8 *rdn = NULL;
+	const u8 *next_ava = NULL;
+	size_t rdn_len = 0;
+	size_t next_ava_len = 0;
+	int rv;
+
+	rdn = sc_asn1_skip_tag(ctx, &dn, &dn_len, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, &rdn_len);
+	if (rdn == NULL)
+		LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of Distiguished Name");
+
+	for (next_ava = rdn, next_ava_len = rdn_len; next_ava_len; ) {
+		const u8 *ava, *dummy, *oidp;
+		struct sc_object_id oid;
+		size_t ava_len, dummy_len, oid_len;
+
+		/* unwrap the set and point to the next ava */
+		ava = sc_asn1_skip_tag(ctx, &next_ava, &next_ava_len, SC_ASN1_TAG_SET | SC_ASN1_CONS, &ava_len);
+		if (ava == NULL)
+			LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of AVA");
+
+		/* It would be nice to use sc_asn1_decode here to parse the entire AVA, but we are missing 1 critical
+		 *	function in the templates: the ability to accept any tag for value. This prevents us from just
+		 *	grabbing the value as is out of the template. AVA's can have tags of PRINTABLE_STRING,
+		 *	TELETEXSTRING, T61STRING or UTF8_STRING with PRINTABLE_STRING and UTF8_STRING being the most common.
+		 * The other feature that would be nice is returning a pointer to our requested data using the space
+		 *	of the parent (basically what this code is doing here), rather than allocating and copying.
+		 */
+
+		/* unwrap the sequence */
+		dummy = ava; dummy_len = ava_len;
+		ava = sc_asn1_skip_tag(ctx, &dummy, &dummy_len, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, &ava_len);
+		if (ava == NULL)
+			LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of AVA");
+
+		/* unwrap the oid */
+		oidp = sc_asn1_skip_tag(ctx, &ava, &ava_len, SC_ASN1_TAG_OBJECT, &oid_len);
+		if (ava == NULL)
+			LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of AVA OID");
+
+		/* Convert to OID */
+		rv = sc_asn1_decode_object_id(oidp, oid_len, &oid);
+		if (rv != SC_SUCCESS)
+			LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of AVA OID");
+
+		if (sc_compare_oid(&oid, type) == 0)
+			continue;
+
+		/* Yes, then return the name */
+		dummy = sc_asn1_skip_tag(ctx, &ava, &ava_len, ava[0] & SC_ASN1_TAG_PRIMITIVE, &dummy_len);
+		if (*name == NULL) {
+			*name = malloc(dummy_len);
+			if (*name == NULL)
+				LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
+			*name_len = dummy_len;
+		}
+
+		*name_len = MIN(dummy_len, *name_len);
+		memcpy(*name, dummy, *name_len);
+		LOG_FUNC_RETURN(ctx, SC_SUCCESS);
+	}
+
+	LOG_FUNC_RETURN(ctx, SC_ERROR_ASN1_OBJECT_NOT_FOUND);
+}
+
+
+/* Get a specific extension from the cert.
+ * The extension is identified by it's oid value.
+ * NOTE: extensions can occur in any number or any order, which is why we
+ *	can't parse them with a single pass of the asn1 decoder.
+ * If is_critical is supplied, then it is set to 1 if the extention is critical
+ * and 0 if it is not.
+ * The data in the extension is extension specific.
+ * The following are common extension values:
+ *   Subject Key ID:		struct sc_object_id type = {{85, 29, 14, -1}};
+ *   Key Usage:			struct sc_object_id type = {{85, 29, 15, -1}};
+ *   Subject Alt Name:		struct sc_object_id type = {{85, 29, 17, -1}};
+ *   Basic Constraints:		struct sc_object_id type = {{85, 29, 19, -1}};
+ *   CRL Distribution Points:	struct sc_object_id type = {{85, 29, 31, -1}};
+ *   Certificate Policies:	struct sc_object_id type = {{85, 29, 32, -1}};
+ *   Extended Key Usage:	struct sc_object_id type = {{85, 29, 37, -1}};
+ *
+ * if *ext_val is NULL, sc_pkcs15_get_extension will allocate space for ext_val.
+ */
+int
+sc_pkcs15_get_extension(struct sc_context *ctx, struct sc_pkcs15_cert *cert,
+	const struct sc_object_id *type, u8 **ext_val,
+	size_t *ext_val_len, int *is_critical)
+{
+	const u8 *ext = NULL;
+	const u8 *next_ext = NULL;
+	size_t ext_len = 0;
+	size_t next_ext_len = 0;
+	struct sc_object_id oid;
+	u8 *val;
+	size_t val_len;
+	int critical;
+	int r;
+	struct sc_asn1_entry asn1_cert_ext[] = {
+		{ "x509v3 entry OID", SC_ASN1_OBJECT, SC_ASN1_TAG_OBJECT, 0, &oid, 0 },
+		{ "criticalFlag",  SC_ASN1_BOOLEAN, SC_ASN1_TAG_BOOLEAN, SC_ASN1_OPTIONAL, &critical, NULL },
+		{ "extensionValue",SC_ASN1_OCTET_STRING, SC_ASN1_TAG_OCTET_STRING, SC_ASN1_ALLOC, &val, &val_len },
+		{ NULL, 0, 0, 0, NULL, NULL }
+	};
+
+	for (next_ext = cert->extensions, next_ext_len = cert->extensions_len; next_ext_len; ) {
+		/* unwrap the set and point to the next ava */
+		ext = sc_asn1_skip_tag(ctx, &next_ext, &next_ext_len,
+			SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, &ext_len);
+		if (ext == NULL)
+			LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of AVA");
+
+		/*
+		 * use the sc_asn1_decoder for clarity. NOTE it would be more efficient to do this by hand
+		 * so we avoid the man malloc/frees here, but one hopes that one day the asn1_decode will allow
+		 * a 'static pointer' flag that returns a const pointer to the actual asn1 space so we only need
+		 * to make a final copy of the extension value before we return */
+		critical = 0;
+		r = sc_asn1_decode(ctx, asn1_cert_ext, ext, ext_len, NULL, NULL);
+		if (r < 0)
+			LOG_FUNC_RETURN(ctx, r);
+
+		/* is it the RN we are looking for */
+		if(sc_compare_oid(&oid, type) != 0) {
+			if (*ext_val == NULL) {
+				*ext_val= val;
+				val = NULL;
+				*ext_val_len = val_len;
+			}
+			else {
+				*ext_val_len = MIN(*ext_val_len, val_len);
+				memcpy(*ext_val, val, *ext_val_len);
+			}
+
+			if (is_critical)
+				*is_critical = critical;
+
+			r = val_len;
+			free(val);
+			LOG_FUNC_RETURN(ctx, r);
+		}
+		free(val);
+	}
+
+	LOG_FUNC_RETURN(ctx, SC_ERROR_ASN1_OBJECT_NOT_FOUND);
+}
+
+/*
+ *  Get an extension whose value is a bit string. These include keyUsage and extendedKeyUsage.
+ *  See above for the other parameters.
+ */
+int
+sc_pkcs15_get_bitstring_extension(struct sc_context *ctx,
+	struct sc_pkcs15_cert *cert, const struct sc_object_id *type,
+	unsigned long long *value, int *is_critical)
+{
+	int r;
+	u8 *bit_string = NULL;
+	size_t bit_string_len=0, val_len = sizeof(*value);
+	struct sc_asn1_entry asn1_bit_string[] = {
+		{ "bitString", SC_ASN1_BIT_STRING, SC_ASN1_TAG_BIT_STRING, 0, value, &val_len },
+		{ NULL, 0, 0, 0, NULL, NULL }
+	};
+
+	r = sc_pkcs15_get_extension(ctx, cert, type, &bit_string, &bit_string_len, is_critical);
+	LOG_TEST_RET(ctx, r, "Get extension error");
+
+	r = sc_asn1_decode(ctx, asn1_bit_string, bit_string, bit_string_len, NULL, NULL);
+	LOG_TEST_RET(ctx, r, "Decoding extension bit string");
+
+	LOG_FUNC_RETURN(ctx, SC_SUCCESS);
+}
+
+
 int
 sc_pkcs15_pubkey_from_cert(struct sc_context *ctx,
 		struct sc_pkcs15_der *cert_blob, struct sc_pkcs15_pubkey **out)
@@ -210,9 +393,9 @@ static const struct sc_asn1_entry c_asn1_cred_ident[] = {
 	{ NULL, 0, 0, 0, NULL, NULL }
 };
 static const struct sc_asn1_entry c_asn1_com_cert_attr[] = {
-	{ "iD",         SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL },
-	{ "authority",  SC_ASN1_BOOLEAN,   SC_ASN1_TAG_BOOLEAN, SC_ASN1_OPTIONAL, NULL, NULL },
-	{ "identifier", SC_ASN1_STRUCT,    SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
+	{ "iD",		SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL },
+	{ "authority",	SC_ASN1_BOOLEAN,   SC_ASN1_TAG_BOOLEAN, SC_ASN1_OPTIONAL, NULL, NULL },
+	{ "identifier",	SC_ASN1_STRUCT,    SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
 	/* FIXME: Add rest of the optional fields */
 	{ NULL, 0, 0, 0, NULL, NULL }
 };
@@ -350,7 +533,7 @@ sc_pkcs15_free_certificate(struct sc_pkcs15_cert *cert)
 	free(cert->issuer);
 	free(cert->serial);
 	free(cert->data.value);
-	free(cert->crl);
+	free(cert->extensions);
 	free(cert);
 }
 
diff --git a/src/libopensc/pkcs15.h b/src/libopensc/pkcs15.h
index 09ca2ea..84f1305 100644
--- a/src/libopensc/pkcs15.h
+++ b/src/libopensc/pkcs15.h
@@ -276,8 +276,8 @@ struct sc_pkcs15_cert {
 	size_t issuer_len;
 	u8 *subject;
 	size_t subject_len;
-	u8 *crl;
-	size_t crl_len;
+	u8 *extensions;
+	size_t extensions_len;
 
 	struct sc_pkcs15_pubkey * key;
 
@@ -713,6 +713,20 @@ void sc_pkcs15_free_certificate(struct sc_pkcs15_cert *cert);
 int sc_pkcs15_find_cert_by_id(struct sc_pkcs15_card *card,
 			      const struct sc_pkcs15_id *id,
 			      struct sc_pkcs15_object **out);
+int sc_pkcs15_get_name_from_dn(struct sc_context *ctx,
+                              const u8 *dn, size_t dn_len,
+                              const struct sc_object_id *type,
+                              u8 **name, size_t *name_len);
+int sc_pkcs15_get_extension(struct sc_context *ctx,
+                            struct sc_pkcs15_cert *cert,
+                            const struct sc_object_id *type,
+                            u8 **ext_val, size_t *ext_val_len,
+                            int *is_critical);
+int sc_pkcs15_get_bitstring_extension(struct sc_context *ctx,
+                                      struct sc_pkcs15_cert *cert,
+                                      const struct sc_object_id *type,
+                                      unsigned long long *value,
+                                      int *is_critical);
 /* sc_pkcs15_create_cdf:  Creates a new certificate DF on a card pointed
  * by <card>.  Information about the file, such as the file ID, is read
  * from <file>.  <certs> has to be NULL-terminated. */

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-opensc/opensc.git



More information about the pkg-opensc-commit mailing list