]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
pefile: Parse the "Microsoft individual code signing" data blob
authorDavid Howells <dhowells@redhat.com>
Tue, 1 Jul 2014 15:02:52 +0000 (16:02 +0100)
committerDavid Howells <dhowells@redhat.com>
Wed, 9 Jul 2014 13:58:37 +0000 (14:58 +0100)
The PKCS#7 certificate should contain a "Microsoft individual code signing"
data blob as its signed content.  This blob contains a digest of the signed
content of the PE binary and the OID of the digest algorithm used (typically
SHA256).

Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Vivek Goyal <vgoyal@redhat.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
crypto/asymmetric_keys/Makefile
crypto/asymmetric_keys/mscode.asn1 [new file with mode: 0644]
crypto/asymmetric_keys/mscode_parser.c [new file with mode: 0644]
crypto/asymmetric_keys/verify_pefile.c
crypto/asymmetric_keys/verify_pefile.h
include/linux/oid_registry.h

index 3e4de52970159e2dac85a1246e4d43af44fbf565..e47fcd9ac5e86f56b85b88c8179d68a41ade078c 100644 (file)
@@ -54,4 +54,11 @@ pkcs7_test_key-y := \
 obj-$(CONFIG_SIGNED_PE_FILE_VERIFICATION) += verify_signed_pefile.o
 
 verify_signed_pefile-y := \
-       verify_pefile.o
+       verify_pefile.o \
+       mscode_parser.o \
+       mscode-asn1.o
+
+$(obj)/mscode_parser.o: $(obj)/mscode-asn1.h $(obj)/mscode-asn1.h
+$(obj)/mscode-asn1.o: $(obj)/mscode-asn1.c $(obj)/mscode-asn1.h
+
+clean-files    += mscode-asn1.c mscode-asn1.h
diff --git a/crypto/asymmetric_keys/mscode.asn1 b/crypto/asymmetric_keys/mscode.asn1
new file mode 100644 (file)
index 0000000..6d09ba4
--- /dev/null
@@ -0,0 +1,28 @@
+--- Microsoft individual code signing data blob parser
+---
+--- Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
+--- Written by David Howells (dhowells@redhat.com)
+---
+--- This program is free software; you can redistribute it and/or
+--- modify it under the terms of the GNU General Public Licence
+--- as published by the Free Software Foundation; either version
+--- 2 of the Licence, or (at your option) any later version.
+---
+
+MSCode ::= SEQUENCE {
+       type                    SEQUENCE {
+               contentType     ContentType,
+               parameters      ANY
+       },
+       content                 SEQUENCE {
+               digestAlgorithm DigestAlgorithmIdentifier,
+               digest          OCTET STRING ({ mscode_note_digest })
+       }
+}
+
+ContentType ::= OBJECT IDENTIFIER ({ mscode_note_content_type })
+
+DigestAlgorithmIdentifier ::= SEQUENCE {
+       algorithm   OBJECT IDENTIFIER ({ mscode_note_digest_algo }),
+       parameters  ANY OPTIONAL
+}
diff --git a/crypto/asymmetric_keys/mscode_parser.c b/crypto/asymmetric_keys/mscode_parser.c
new file mode 100644 (file)
index 0000000..09336c3
--- /dev/null
@@ -0,0 +1,120 @@
+/* Parse a Microsoft Individual Code Signing blob
+ *
+ * Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public Licence
+ * as published by the Free Software Foundation; either version
+ * 2 of the Licence, or (at your option) any later version.
+ */
+
+#define pr_fmt(fmt) "MSCODE: "fmt
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/oid_registry.h>
+#include <crypto/pkcs7.h>
+#include "verify_pefile.h"
+#include "mscode-asn1.h"
+
+/*
+ * Parse a Microsoft Individual Code Signing blob
+ */
+int mscode_parse(struct pefile_context *ctx)
+{
+       const void *content_data;
+       size_t data_len;
+       int ret;
+
+       ret = pkcs7_get_content_data(ctx->pkcs7, &content_data, &data_len, 1);
+
+       if (ret) {
+               pr_debug("PKCS#7 message does not contain data\n");
+               return ret;
+       }
+
+       pr_devel("Data: %zu [%*ph]\n", data_len, (unsigned)(data_len),
+                content_data);
+
+       return asn1_ber_decoder(&mscode_decoder, ctx, content_data, data_len);
+}
+
+/*
+ * Check the content type OID
+ */
+int mscode_note_content_type(void *context, size_t hdrlen,
+                            unsigned char tag,
+                            const void *value, size_t vlen)
+{
+       enum OID oid;
+
+       oid = look_up_OID(value, vlen);
+       if (oid == OID__NR) {
+               char buffer[50];
+
+               sprint_oid(value, vlen, buffer, sizeof(buffer));
+               pr_err("Unknown OID: %s\n", buffer);
+               return -EBADMSG;
+       }
+
+       if (oid != OID_msIndividualSPKeyPurpose) {
+               pr_err("Unexpected content type OID %u\n", oid);
+               return -EBADMSG;
+       }
+
+       return 0;
+}
+
+/*
+ * Note the digest algorithm OID
+ */
+int mscode_note_digest_algo(void *context, size_t hdrlen,
+                           unsigned char tag,
+                           const void *value, size_t vlen)
+{
+       struct pefile_context *ctx = context;
+       char buffer[50];
+       enum OID oid;
+
+       oid = look_up_OID(value, vlen);
+       switch (oid) {
+       case OID_md4:
+               ctx->digest_algo = HASH_ALGO_MD4;
+               break;
+       case OID_md5:
+               ctx->digest_algo = HASH_ALGO_MD5;
+               break;
+       case OID_sha1:
+               ctx->digest_algo = HASH_ALGO_SHA1;
+               break;
+       case OID_sha256:
+               ctx->digest_algo = HASH_ALGO_SHA256;
+               break;
+
+       case OID__NR:
+               sprint_oid(value, vlen, buffer, sizeof(buffer));
+               pr_err("Unknown OID: %s\n", buffer);
+               return -EBADMSG;
+
+       default:
+               pr_err("Unsupported content type: %u\n", oid);
+               return -ENOPKG;
+       }
+
+       return 0;
+}
+
+/*
+ * Note the digest we're guaranteeing with this certificate
+ */
+int mscode_note_digest(void *context, size_t hdrlen,
+                      unsigned char tag,
+                      const void *value, size_t vlen)
+{
+       struct pefile_context *ctx = context;
+
+       ctx->digest = value;
+       ctx->digest_len = vlen;
+       return 0;
+}
index 13f3b44b5046f2d9257087b48d80b4dd162d0a51..b975918e82d28f0e43cc1a0a15baa914a79e190a 100644 (file)
@@ -245,6 +245,13 @@ int verify_pefile_signature(const void *pebuf, unsigned pelen,
                goto error;
        }
 
+       ret = mscode_parse(&ctx);
+       if (ret < 0)
+               goto error;
+
+       pr_debug("Digest: %u [%*ph]\n",
+                ctx.digest_len, ctx.digest_len, ctx.digest);
+
        ret = -ENOANO; // Not yet complete
 
 error:
index e165d23458d453a4d5e5a350d0a60535e47b1c71..55d5f7ebc45a1e9bad0321e99750e736e64b298c 100644 (file)
@@ -35,3 +35,8 @@ struct pefile_context {
        pr_devel("==> %s("FMT")\n", __func__, ##__VA_ARGS__)
 #define kleave(FMT, ...) \
        pr_devel("<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
+
+/*
+ * mscode_parser.c
+ */
+extern int mscode_parse(struct pefile_context *ctx);
index edeff85cb1e81a6c74ad396c9008927c1604bb02..c2bbf672b84eb58e767bc82882c49761b07abee6 100644 (file)
@@ -52,8 +52,13 @@ enum OID {
        OID_md4,                        /* 1.2.840.113549.2.4 */
        OID_md5,                        /* 1.2.840.113549.2.5 */
 
-       OID_certAuthInfoAccess,         /* 1.3.6.1.5.5.7.1.1 */
+       /* Microsoft Authenticode & Software Publishing */
+       OID_msIndirectData,             /* 1.3.6.1.4.1.311.2.1.4 */
+       OID_msPeImageDataObjId,         /* 1.3.6.1.4.1.311.2.1.15 */
+       OID_msIndividualSPKeyPurpose,   /* 1.3.6.1.4.1.311.2.1.21 */
        OID_msOutlookExpress,           /* 1.3.6.1.4.1.311.16.4 */
+
+       OID_certAuthInfoAccess,         /* 1.3.6.1.5.5.7.1.1 */
        OID_sha1,                       /* 1.3.14.3.2.26 */
        OID_sha256,                     /* 2.16.840.1.101.3.4.2.1 */