0002-ASN.1-Validate-DigestAlgorithmIdentifier-parameters.patch 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. From a0541334a6394f8237a4393b7372693cd7e96f15 Mon Sep 17 00:00:00 2001
  2. From: Jouni Malinen <j@w1.fi>
  3. Date: Sat, 13 Mar 2021 18:19:31 +0200
  4. Subject: [PATCH] ASN.1: Validate DigestAlgorithmIdentifier parameters
  5. The supported hash algorithms do not use AlgorithmIdentifier parameters.
  6. However, there are implementations that include NULL parameters in
  7. addition to ones that omit the parameters. Previous implementation did
  8. not check the parameters value at all which supported both these cases,
  9. but did not reject any other unexpected information.
  10. Use strict validation of digest algorithm parameters and reject any
  11. unexpected value when validating a signature. This is needed to prevent
  12. potential forging attacks.
  13. Signed-off-by: Jouni Malinen <j@w1.fi>
  14. Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
  15. ---
  16. src/tls/pkcs1.c | 21 +++++++++++++++++++++
  17. src/tls/x509v3.c | 20 ++++++++++++++++++++
  18. 2 files changed, 41 insertions(+)
  19. diff --git a/src/tls/pkcs1.c b/src/tls/pkcs1.c
  20. index bbdb0d72d..5761dfed0 100644
  21. --- a/src/tls/pkcs1.c
  22. +++ b/src/tls/pkcs1.c
  23. @@ -244,6 +244,8 @@ int pkcs1_v15_sig_ver(struct crypto_public_key *pk,
  24. os_free(decrypted);
  25. return -1;
  26. }
  27. + wpa_hexdump(MSG_MSGDUMP, "PKCS #1: DigestInfo",
  28. + hdr.payload, hdr.length);
  29. pos = hdr.payload;
  30. end = pos + hdr.length;
  31. @@ -265,6 +267,8 @@ int pkcs1_v15_sig_ver(struct crypto_public_key *pk,
  32. os_free(decrypted);
  33. return -1;
  34. }
  35. + wpa_hexdump(MSG_MSGDUMP, "PKCS #1: DigestAlgorithmIdentifier",
  36. + hdr.payload, hdr.length);
  37. da_end = hdr.payload + hdr.length;
  38. if (asn1_get_oid(hdr.payload, hdr.length, &oid, &next)) {
  39. @@ -273,6 +277,23 @@ int pkcs1_v15_sig_ver(struct crypto_public_key *pk,
  40. os_free(decrypted);
  41. return -1;
  42. }
  43. + wpa_hexdump(MSG_MSGDUMP, "PKCS #1: Digest algorithm parameters",
  44. + next, da_end - next);
  45. +
  46. + /*
  47. + * RFC 5754: The correct encoding for the SHA2 algorithms would be to
  48. + * omit the parameters, but there are implementation that encode these
  49. + * as a NULL element. Allow these two cases and reject anything else.
  50. + */
  51. + if (da_end > next &&
  52. + (asn1_get_next(next, da_end - next, &hdr) < 0 ||
  53. + !asn1_is_null(&hdr) ||
  54. + hdr.payload + hdr.length != da_end)) {
  55. + wpa_printf(MSG_DEBUG,
  56. + "PKCS #1: Unexpected digest algorithm parameters");
  57. + os_free(decrypted);
  58. + return -1;
  59. + }
  60. if (!asn1_oid_equal(&oid, hash_alg)) {
  61. char txt[100], txt2[100];
  62. diff --git a/src/tls/x509v3.c b/src/tls/x509v3.c
  63. index a8944dd2f..df337ec4d 100644
  64. --- a/src/tls/x509v3.c
  65. +++ b/src/tls/x509v3.c
  66. @@ -1964,6 +1964,7 @@ int x509_check_signature(struct x509_certificate *issuer,
  67. os_free(data);
  68. return -1;
  69. }
  70. + wpa_hexdump(MSG_MSGDUMP, "X509: DigestInfo", hdr.payload, hdr.length);
  71. pos = hdr.payload;
  72. end = pos + hdr.length;
  73. @@ -1985,6 +1986,8 @@ int x509_check_signature(struct x509_certificate *issuer,
  74. os_free(data);
  75. return -1;
  76. }
  77. + wpa_hexdump(MSG_MSGDUMP, "X509: DigestAlgorithmIdentifier",
  78. + hdr.payload, hdr.length);
  79. da_end = hdr.payload + hdr.length;
  80. if (asn1_get_oid(hdr.payload, hdr.length, &oid, &next)) {
  81. @@ -1992,6 +1995,23 @@ int x509_check_signature(struct x509_certificate *issuer,
  82. os_free(data);
  83. return -1;
  84. }
  85. + wpa_hexdump(MSG_MSGDUMP, "X509: Digest algorithm parameters",
  86. + next, da_end - next);
  87. +
  88. + /*
  89. + * RFC 5754: The correct encoding for the SHA2 algorithms would be to
  90. + * omit the parameters, but there are implementation that encode these
  91. + * as a NULL element. Allow these two cases and reject anything else.
  92. + */
  93. + if (da_end > next &&
  94. + (asn1_get_next(next, da_end - next, &hdr) < 0 ||
  95. + !asn1_is_null(&hdr) ||
  96. + hdr.payload + hdr.length != da_end)) {
  97. + wpa_printf(MSG_DEBUG,
  98. + "X509: Unexpected digest algorithm parameters");
  99. + os_free(data);
  100. + return -1;
  101. + }
  102. if (x509_sha1_oid(&oid)) {
  103. if (signature->oid.oid[6] != 5 /* sha-1WithRSAEncryption */) {
  104. --
  105. 2.20.1