mscode_parser.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Parse a Microsoft Individual Code Signing blob
  2. *
  3. * Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) "MSCODE: "fmt
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/err.h>
  15. #include <linux/oid_registry.h>
  16. #include <crypto/pkcs7.h>
  17. #include "verify_pefile.h"
  18. #include "mscode-asn1.h"
  19. /*
  20. * Parse a Microsoft Individual Code Signing blob
  21. */
  22. int mscode_parse(struct pefile_context *ctx)
  23. {
  24. const void *content_data;
  25. size_t data_len;
  26. int ret;
  27. ret = pkcs7_get_content_data(ctx->pkcs7, &content_data, &data_len, 1);
  28. if (ret) {
  29. pr_debug("PKCS#7 message does not contain data\n");
  30. return ret;
  31. }
  32. pr_devel("Data: %zu [%*ph]\n", data_len, (unsigned)(data_len),
  33. content_data);
  34. return asn1_ber_decoder(&mscode_decoder, ctx, content_data, data_len);
  35. }
  36. /*
  37. * Check the content type OID
  38. */
  39. int mscode_note_content_type(void *context, size_t hdrlen,
  40. unsigned char tag,
  41. const void *value, size_t vlen)
  42. {
  43. enum OID oid;
  44. oid = look_up_OID(value, vlen);
  45. if (oid == OID__NR) {
  46. char buffer[50];
  47. sprint_oid(value, vlen, buffer, sizeof(buffer));
  48. pr_err("Unknown OID: %s\n", buffer);
  49. return -EBADMSG;
  50. }
  51. /*
  52. * pesign utility had a bug where it was putting
  53. * OID_msIndividualSPKeyPurpose instead of OID_msPeImageDataObjId
  54. * So allow both OIDs.
  55. */
  56. if (oid != OID_msPeImageDataObjId &&
  57. oid != OID_msIndividualSPKeyPurpose) {
  58. pr_err("Unexpected content type OID %u\n", oid);
  59. return -EBADMSG;
  60. }
  61. return 0;
  62. }
  63. /*
  64. * Note the digest algorithm OID
  65. */
  66. int mscode_note_digest_algo(void *context, size_t hdrlen,
  67. unsigned char tag,
  68. const void *value, size_t vlen)
  69. {
  70. struct pefile_context *ctx = context;
  71. char buffer[50];
  72. enum OID oid;
  73. oid = look_up_OID(value, vlen);
  74. switch (oid) {
  75. case OID_md4:
  76. ctx->digest_algo = HASH_ALGO_MD4;
  77. break;
  78. case OID_md5:
  79. ctx->digest_algo = HASH_ALGO_MD5;
  80. break;
  81. case OID_sha1:
  82. ctx->digest_algo = HASH_ALGO_SHA1;
  83. break;
  84. case OID_sha256:
  85. ctx->digest_algo = HASH_ALGO_SHA256;
  86. break;
  87. case OID_sha384:
  88. ctx->digest_algo = HASH_ALGO_SHA384;
  89. break;
  90. case OID_sha512:
  91. ctx->digest_algo = HASH_ALGO_SHA512;
  92. break;
  93. case OID_sha224:
  94. ctx->digest_algo = HASH_ALGO_SHA224;
  95. break;
  96. case OID__NR:
  97. sprint_oid(value, vlen, buffer, sizeof(buffer));
  98. pr_err("Unknown OID: %s\n", buffer);
  99. return -EBADMSG;
  100. default:
  101. pr_err("Unsupported content type: %u\n", oid);
  102. return -ENOPKG;
  103. }
  104. return 0;
  105. }
  106. /*
  107. * Note the digest we're guaranteeing with this certificate
  108. */
  109. int mscode_note_digest(void *context, size_t hdrlen,
  110. unsigned char tag,
  111. const void *value, size_t vlen)
  112. {
  113. struct pefile_context *ctx = context;
  114. ctx->digest = value;
  115. ctx->digest_len = vlen;
  116. return 0;
  117. }