Blame SOURCES/0015-MS-cert-template-add-template-extension-to-CSR.patch

1495bf
From 616bc539e7054f7e561ca66672019f7990759811 Mon Sep 17 00:00:00 2001
1495bf
From: Fraser Tweedale <ftweedal@redhat.com>
1495bf
Date: Thu, 17 Aug 2017 18:10:37 +1000
1495bf
Subject: [PATCH] MS cert template: add template extension to CSR
1495bf
1495bf
Add the MS V2 certificate template extension to the CSR, when the
1495bf
attribute is set.  Failure to parse the value (as stored) merely
1495bf
causes the extension to be skipped.
1495bf
1495bf
Part of: https://pagure.io/certmonger/issue/78
1495bf
---
1495bf
 src/certext.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1495bf
 1 file changed, 84 insertions(+), 2 deletions(-)
1495bf
1495bf
diff --git a/src/certext.c b/src/certext.c
1495bf
index 64ae05a..5f8a743 100644
1495bf
--- a/src/certext.c
1495bf
+++ b/src/certext.c
1495bf
@@ -69,7 +69,7 @@ struct kerberos_principal_name {
1495bf
 struct ms_template {
1495bf
 	SECItem id;
1495bf
 	SECItem major;
1495bf
-	SECItem *minor;
1495bf
+	SECItem minor;
1495bf
 };
1495bf
 
1495bf
 /* KerberosString: RFC 4120, 5.2.1 */
1495bf
@@ -180,7 +180,7 @@ cm_ms_template_template[] = {
1495bf
 	.kind = SEC_ASN1_SEQUENCE,
1495bf
 	.offset = 0,
1495bf
 	.sub = NULL,
1495bf
-	.size = sizeof(struct kerberos_principal_name),
1495bf
+	.size = sizeof(struct ms_template),
1495bf
 	},
1495bf
 	{
1495bf
 	.kind = SEC_ASN1_OBJECT_ID,
1495bf
@@ -1593,6 +1593,76 @@ cm_certext_build_profile(struct cm_store_entry *entry,
1495bf
 	return item;
1495bf
 }
1495bf
 
1495bf
+/* Build a Microsoft certificate template extension value. */
1495bf
+static SECItem *
1495bf
+cm_certext_build_certificate_template(
1495bf
+	PLArenaPool *arena,
1495bf
+	char *template_spec)
1495bf
+{
1495bf
+	struct ms_template template_data;
1495bf
+	memset(&template_data, 0, sizeof(struct ms_template));
1495bf
+
1495bf
+	if (NULL == template_spec || *template_spec == '\0')
1495bf
+		return NULL;
1495bf
+
1495bf
+	/* strtok overwrites delimiters with null bytes;
1495bf
+	 * therefore duplicate the input string */
1495bf
+	char *template_spec_dup = PORT_ArenaStrdup(arena, template_spec);
1495bf
+	if (NULL == template_spec_dup)
1495bf
+		return NULL;
1495bf
+
1495bf
+	int i = 0;
1495bf
+	char *saveptr, *endptr;
1495bf
+	for (
1495bf
+		char *part = strtok_r(template_spec_dup, ":", &saveptr);
1495bf
+		part != NULL;
1495bf
+		part = strtok_r(NULL, ":", &saveptr)
1495bf
+	) {
1495bf
+		if (i == 0) {
1495bf
+			// parse OID
1495bf
+			if (SECSuccess != SEC_StringToOID(arena, &template_data.id, part, 0))
1495bf
+				return NULL;
1495bf
+		}
1495bf
+		else if (i == 1) {
1495bf
+			// parse major version
1495bf
+			long x = strtol(part, &endptr, 10);
1495bf
+			if (*part == '\0' || *endptr != '\0') {
1495bf
+				// string was empty or contained non-digits
1495bf
+				return NULL;
1495bf
+			}
1495bf
+			if (SEC_ASN1EncodeInteger(arena, &template_data.major, x)
1495bf
+					!= &template_data.major)
1495bf
+				return NULL;
1495bf
+		}
1495bf
+		else if (i == 2) {
1495bf
+			// parse minor version
1495bf
+			long x = strtol(part, &endptr, 10);
1495bf
+			if (*part == '\0' || *endptr != '\0') {
1495bf
+				// string was empty or contained non-digits
1495bf
+				return NULL;
1495bf
+			}
1495bf
+			if (SEC_ASN1EncodeInteger(arena, &template_data.minor, x)
1495bf
+					!= &template_data.minor)
1495bf
+				return NULL;
1495bf
+		}
1495bf
+		else {
1495bf
+			// there are too many parts!
1495bf
+			return NULL;
1495bf
+		}
1495bf
+		i++;
1495bf
+	}
1495bf
+	if (i < 2) {
1495bf
+		// there are too few parts! (OID and major version are required)
1495bf
+		return NULL;
1495bf
+	}
1495bf
+
1495bf
+	SECItem encoded;
1495bf
+	if (SEC_ASN1EncodeItem(arena, &encoded, &template_data,
1495bf
+			       cm_ms_template_template) != &encoded)
1495bf
+		return NULL;
1495bf
+	return SECITEM_ArenaDupItem(arena, &encoded);
1495bf
+}
1495bf
+
1495bf
 /* Build a Netscape certtype extension value. */
1495bf
 static SECItem *
1495bf
 cm_certext_build_ns_certtype(struct cm_store_entry *entry,
1495bf
@@ -1840,6 +1910,18 @@ cm_certext_build_csr_extensions(struct cm_store_entry *entry,
1495bf
 			i++;
1495bf
 		}
1495bf
 	}
1495bf
+	if (entry->cm_template_certificate_template != NULL) {
1495bf
+		oid = (SECOidData *) &oid_microsoft_certificate_template;
1495bf
+		item = cm_certext_build_certificate_template(
1495bf
+			arena, entry->cm_template_certificate_template);
1495bf
+		if ((item != NULL) && (oid != NULL)) {
1495bf
+			ext[i].id = oid->oid;
1495bf
+			ext[i].critical = der_false;
1495bf
+			ext[i].value = *item;
1495bf
+			exts[i] = &ext[i];
1495bf
+			i++;
1495bf
+		}
1495bf
+	}
1495bf
 	if (entry->cm_template_ns_certtype != NULL) {
1495bf
 		oid = SECOID_FindOIDByTag(SEC_OID_NS_CERT_EXT_CERT_TYPE);
1495bf
 		item = cm_certext_build_ns_certtype(entry, arena,
1495bf
-- 
1495bf
2.14.4
1495bf