{"id":80,"date":"2012-06-07T16:04:31","date_gmt":"2012-06-08T00:04:31","guid":{"rendered":"http:\/\/rmhrisk.wpengine.com\/?p=80"},"modified":"2012-06-08T07:06:08","modified_gmt":"2012-06-08T15:06:08","slug":"how-one-typically-verifies-code-comes-from-microsoft","status":"publish","type":"post","link":"https:\/\/unmitigatedrisk.com\/?p=80","title":{"rendered":"How one typically verifies code comes from Microsoft"},"content":{"rendered":"<p>Microsoft signs all code that ships from it (that is except for the case when it\u00a0doesn&#8217;t); so how does one verify that the code came from them vs. anyone with a fraudulent certificate claiming to be them?<\/p>\n<p>Well the answer is pinning, but the pinning for the most part does not happen at the signing certificate level as those keys change to frequently to be pinned. The pinning happens at the certificate issuer level, much like has been proposed by <a href=\"http:\/\/tools.ietf.org\/id\/draft-hallambaker-donotissue-02.html\">CAA<\/a>.<\/p>\n<p>So to understand how this happens in code you need to understand <a href=\"http:\/\/msdn.microsoft.com\/en-us\/windows\/hardware\/gg487309\">code signing in Windows<\/a> a little. There are two types of signatures those that are \u201cembedded\u201d and those that are \u201cattached\u201d.<\/p>\n<p><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/hardware\/gg463180.aspx\">Authenticode<\/a> would be an example of an embedded signature; <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/hardware\/ff537872(v=vs.85).aspx\">Catalog Files<\/a> on the other hand are detached signatures. There are other examples of both of these but in general these other formats have the same abstract properties.<\/p>\n<p>So when do you use one vs. the other? Well if a user will download your binary directly (ActiveX, Setup, etc.) you would typically embed a signature.<\/p>\n<p>If that\u2019s not the case you would use a detached signature because a single signature can be placed on a list of binaries without increasing the package size (it\u2019s one signature on n hashes). You produce these signatures with <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa387734(v=vs.85).aspx\">SignerSignEx<\/a>.<\/p>\n<p>For the two signature formats I mentioned you do your signature verifications using an API called<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa388208(v=vs.85).aspx\"> WinVerifyTrust<\/a>. You can also use this API to <a href=\"http:\/\/support.microsoft.com\/kb\/323809\">get information out of a signature<\/a> (who signed it, when, etc.).<\/p>\n<p>With that information you do a certificate validation that is accomplished with the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa376078(v=vs.85).aspx\">CertGetCertificateChain<\/a> API, that gives you a CERT_CHAIN_CONTEXT which you can use to call <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa377163(v=vs.85).aspx\">CertVerifyCertificateChainPolicy<\/a>.<\/p>\n<p>This API is used to do application specific verifications, for example in the case of TLS the CERT_CHAIN_POLICY_SSL provider is used \u2013 this is where the name and EKU verification logic for Windows exists.<\/p>\n<p>The providers we care about for our story today are:<\/p>\n<ul>\n<li>CERT_CHAIN_POLICY_AUTHENTICODE \u2013 Is this certificate appropriate for code-signing?<\/li>\n<li>CERT_CHAIN_POLICY_AUTHENTICODE_TS \u2013 Is this certificate appropriate for time stamping code?<\/li>\n<li>CERT_CHAIN_POLICY_MICROSOFT_ROOT \u2013 Is this certificate ultimately issued by a Microsoft Product Root?<\/li>\n<\/ul>\n<p>The last one is the one in has an array of hashes in it, I don\u2019t recall if they are key hashes or thumbprints of the certificate. In essence what it does is look at the top of the chain to see if the Root matches one of these hashes if it does it passes, if it doesn\u2019t it fails.<\/p>\n<p>Applications may choose to do additional \u201cpinning\u201d also, for example in the case of <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cc251937(v=prot.13).aspx\">Windows Update Services<\/a> a manifest is exchanged between the client and server, that manifest comes down over SSL they may choose to also do pinning to a set of certificates for the SSL session or apply digital signatures to the manifest and do pinning for the keys that are used to do signing.<\/p>\n<p><span style=\"text-decoration: line-through;\">I do not know personally for a fact that any such pinning takes place but I have been told by several people I respect that it does.<\/span><\/p>\n<p>It looks as if <a href=\"http:\/\/blogs.technet.com\/b\/mu\/archive\/2012\/06\/06\/update-to-windows-update-wsus-coming-this-week.aspx\">Windows Update was not doing pinning<\/a>\u00a0but as of this week it will, this is of course \u00a0a best practice and I am glad to see them doing it now.<\/p>\n<p>So now you know how to validate that code is coming from Microsoft, or at least how it\u2019s supposed to work.<\/p>\n<p>Issues like represented in MSRC 2718704 \/ Flame put some of these assumptions in question, it\u2019s clear that pinning is an important concept though and one that should be supported more broadly.<\/p>\n<p>Ryan<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Microsoft signs all code that ships from it (that is except for the case when it\u00a0doesn&#8217;t); so how does one verify that the code came from them vs. anyone with a fraudulent certificate claiming to be them? Well the answer is pinning, but the pinning for the most part does not happen at the signing [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12,3],"tags":[17,168,19],"class_list":["post-80","post","type-post","status-publish","format-standard","hentry","category-programming","category-security","tag-microsoft","tag-security","tag-x509"],"_links":{"self":[{"href":"https:\/\/unmitigatedrisk.com\/index.php?rest_route=\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unmitigatedrisk.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unmitigatedrisk.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unmitigatedrisk.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/unmitigatedrisk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=80"}],"version-history":[{"count":0,"href":"https:\/\/unmitigatedrisk.com\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions"}],"wp:attachment":[{"href":"https:\/\/unmitigatedrisk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unmitigatedrisk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unmitigatedrisk.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}