mscode_parser.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(void *_ctx, const void *content_data, size_t data_len,
  23. size_t asn1hdrlen)
  24. {
  25. struct pefile_context *ctx = _ctx;
  26. content_data -= asn1hdrlen;
  27. data_len += asn1hdrlen;
  28. pr_devel("Data: %zu [%*ph]\n", data_len, (unsigned)(data_len),
  29. content_data);
  30. return asn1_ber_decoder(&mscode_decoder, ctx, content_data, data_len);
  31. }
  32. /*
  33. * Check the content type OID
  34. */
  35. int mscode_note_content_type(void *context, size_t hdrlen,
  36. unsigned char tag,
  37. const void *value, size_t vlen)
  38. {
  39. enum OID oid;
  40. oid = look_up_OID(value, vlen);
  41. if (oid == OID__NR) {
  42. char buffer[50];
  43. sprint_oid(value, vlen, buffer, sizeof(buffer));
  44. pr_err("Unknown OID: %s\n", buffer);
  45. return -EBADMSG;
  46. }
  47. /*
  48. * pesign utility had a bug where it was putting
  49. * OID_msIndividualSPKeyPurpose instead of OID_msPeImageDataObjId
  50. * So allow both OIDs.
  51. */
  52. if (oid != OID_msPeImageDataObjId &&
  53. oid != OID_msIndividualSPKeyPurpose) {
  54. pr_err("Unexpected content type OID %u\n", oid);
  55. return -EBADMSG;
  56. }
  57. return 0;
  58. }
  59. /*
  60. * Note the digest algorithm OID
  61. */
  62. int mscode_note_digest_algo(void *context, size_t hdrlen,
  63. unsigned char tag,
  64. const void *value, size_t vlen)
  65. {
  66. struct pefile_context *ctx = context;
  67. char buffer[50];
  68. enum OID oid;
  69. oid = look_up_OID(value, vlen);
  70. switch (oid) {
  71. case OID_md4:
  72. ctx->digest_algo = "md4";
  73. break;
  74. case OID_md5:
  75. ctx->digest_algo = "md5";
  76. break;
  77. case OID_sha1:
  78. ctx->digest_algo = "sha1";
  79. break;
  80. case OID_sha256:
  81. ctx->digest_algo = "sha256";
  82. break;
  83. case OID_sha384:
  84. ctx->digest_algo = "sha384";
  85. break;
  86. case OID_sha512:
  87. ctx->digest_algo = "sha512";
  88. break;
  89. case OID_sha224:
  90. ctx->digest_algo = "sha224";
  91. break;
  92. case OID__NR:
  93. sprint_oid(value, vlen, buffer, sizeof(buffer));
  94. pr_err("Unknown OID: %s\n", buffer);
  95. return -EBADMSG;
  96. default:
  97. pr_err("Unsupported content type: %u\n", oid);
  98. return -ENOPKG;
  99. }
  100. return 0;
  101. }
  102. /*
  103. * Note the digest we're guaranteeing with this certificate
  104. */
  105. int mscode_note_digest(void *context, size_t hdrlen,
  106. unsigned char tag,
  107. const void *value, size_t vlen)
  108. {
  109. struct pefile_context *ctx = context;
  110. ctx->digest = kmemdup(value, vlen, GFP_KERNEL);
  111. if (!ctx->digest)
  112. return -ENOMEM;
  113. ctx->digest_len = vlen;
  114. return 0;
  115. }