Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /* Copyright (c) 2016, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <functional>
  15. #include <string>
  16. #include <type_traits>
  17. #include <vector>
  18. #include <assert.h>
  19. #include <openssl/bytestring.h>
  20. #include <openssl/err.h>
  21. #include <openssl/evp.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/ssl.h>
  24. #include <openssl/stack.h>
  25. #include <openssl/x509.h>
  26. #include "../crypto/internal.h"
  27. static const uint8_t kCertificateDER[] = {
  28. 0x30, 0x82, 0x02, 0xff, 0x30, 0x82, 0x01, 0xe7, 0xa0, 0x03, 0x02, 0x01,
  29. 0x02, 0x02, 0x11, 0x00, 0xb1, 0x84, 0xee, 0x34, 0x99, 0x98, 0x76, 0xfb,
  30. 0x6f, 0xb2, 0x15, 0xc8, 0x47, 0x79, 0x05, 0x9b, 0x30, 0x0d, 0x06, 0x09,
  31. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30,
  32. 0x12, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x07,
  33. 0x41, 0x63, 0x6d, 0x65, 0x20, 0x43, 0x6f, 0x30, 0x1e, 0x17, 0x0d, 0x31,
  34. 0x35, 0x31, 0x31, 0x30, 0x37, 0x30, 0x30, 0x32, 0x34, 0x35, 0x36, 0x5a,
  35. 0x17, 0x0d, 0x31, 0x36, 0x31, 0x31, 0x30, 0x36, 0x30, 0x30, 0x32, 0x34,
  36. 0x35, 0x36, 0x5a, 0x30, 0x12, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  37. 0x04, 0x0a, 0x13, 0x07, 0x41, 0x63, 0x6d, 0x65, 0x20, 0x43, 0x6f, 0x30,
  38. 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  39. 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30,
  40. 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xce, 0x47, 0xcb, 0x11,
  41. 0xbb, 0xd2, 0x9d, 0x8e, 0x9e, 0xd2, 0x1e, 0x14, 0xaf, 0xc7, 0xea, 0xb6,
  42. 0xc9, 0x38, 0x2a, 0x6f, 0xb3, 0x7e, 0xfb, 0xbc, 0xfc, 0x59, 0x42, 0xb9,
  43. 0x56, 0xf0, 0x4c, 0x3f, 0xf7, 0x31, 0x84, 0xbe, 0xac, 0x03, 0x9e, 0x71,
  44. 0x91, 0x85, 0xd8, 0x32, 0xbd, 0x00, 0xea, 0xac, 0x65, 0xf6, 0x03, 0xc8,
  45. 0x0f, 0x8b, 0xfd, 0x6e, 0x58, 0x88, 0x04, 0x41, 0x92, 0x74, 0xa6, 0x57,
  46. 0x2e, 0x8e, 0x88, 0xd5, 0x3d, 0xda, 0x14, 0x3e, 0x63, 0x88, 0x22, 0xe3,
  47. 0x53, 0xe9, 0xba, 0x39, 0x09, 0xac, 0xfb, 0xd0, 0x4c, 0xf2, 0x3c, 0x20,
  48. 0xd6, 0x97, 0xe6, 0xed, 0xf1, 0x62, 0x1e, 0xe5, 0xc9, 0x48, 0xa0, 0xca,
  49. 0x2e, 0x3c, 0x14, 0x5a, 0x82, 0xd4, 0xed, 0xb1, 0xe3, 0x43, 0xc1, 0x2a,
  50. 0x59, 0xa5, 0xb9, 0xc8, 0x48, 0xa7, 0x39, 0x23, 0x74, 0xa7, 0x37, 0xb0,
  51. 0x6f, 0xc3, 0x64, 0x99, 0x6c, 0xa2, 0x82, 0xc8, 0xf6, 0xdb, 0x86, 0x40,
  52. 0xce, 0xd1, 0x85, 0x9f, 0xce, 0x69, 0xf4, 0x15, 0x2a, 0x23, 0xca, 0xea,
  53. 0xb7, 0x7b, 0xdf, 0xfb, 0x43, 0x5f, 0xff, 0x7a, 0x49, 0x49, 0x0e, 0xe7,
  54. 0x02, 0x51, 0x45, 0x13, 0xe8, 0x90, 0x64, 0x21, 0x0c, 0x26, 0x2b, 0x5d,
  55. 0xfc, 0xe4, 0xb5, 0x86, 0x89, 0x43, 0x22, 0x4c, 0xf3, 0x3b, 0xf3, 0x09,
  56. 0xc4, 0xa4, 0x10, 0x80, 0xf2, 0x46, 0xe2, 0x46, 0x8f, 0x76, 0x50, 0xbf,
  57. 0xaf, 0x2b, 0x90, 0x1b, 0x78, 0xc7, 0xcf, 0xc1, 0x77, 0xd0, 0xfb, 0xa9,
  58. 0xfb, 0xc9, 0x66, 0x5a, 0xc5, 0x9b, 0x31, 0x41, 0x67, 0x01, 0xbe, 0x33,
  59. 0x10, 0xba, 0x05, 0x58, 0xed, 0x76, 0x53, 0xde, 0x5d, 0xc1, 0xe8, 0xbb,
  60. 0x9f, 0xf1, 0xcd, 0xfb, 0xdf, 0x64, 0x7f, 0xd7, 0x18, 0xab, 0x0f, 0x94,
  61. 0x28, 0x95, 0x4a, 0xcc, 0x6a, 0xa9, 0x50, 0xc7, 0x05, 0x47, 0x10, 0x41,
  62. 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x50, 0x30, 0x4e, 0x30, 0x0e, 0x06,
  63. 0x03, 0x55, 0x1d, 0x0f, 0x01, 0x01, 0xff, 0x04, 0x04, 0x03, 0x02, 0x05,
  64. 0xa0, 0x30, 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a,
  65. 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x30, 0x0c,
  66. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00,
  67. 0x30, 0x19, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x12, 0x30, 0x10, 0x82,
  68. 0x0e, 0x66, 0x75, 0x7a, 0x7a, 0x2e, 0x62, 0x6f, 0x72, 0x69, 0x6e, 0x67,
  69. 0x73, 0x73, 0x6c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  70. 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x92,
  71. 0xde, 0xef, 0x96, 0x06, 0x7b, 0xff, 0x71, 0x7d, 0x4e, 0xa0, 0x7d, 0xae,
  72. 0xb8, 0x22, 0xb4, 0x2c, 0xf7, 0x96, 0x9c, 0x37, 0x1d, 0x8f, 0xe7, 0xd9,
  73. 0x47, 0xff, 0x3f, 0xe9, 0x35, 0x95, 0x0e, 0xdd, 0xdc, 0x7f, 0xc8, 0x8a,
  74. 0x1e, 0x36, 0x1d, 0x38, 0x47, 0xfc, 0x76, 0xd2, 0x1f, 0x98, 0xa1, 0x36,
  75. 0xac, 0xc8, 0x70, 0x38, 0x0a, 0x3d, 0x51, 0x8d, 0x0f, 0x03, 0x1b, 0xef,
  76. 0x62, 0xa1, 0xcb, 0x2b, 0x4a, 0x8c, 0x12, 0x2b, 0x54, 0x50, 0x9a, 0x6b,
  77. 0xfe, 0xaf, 0xd9, 0xf6, 0xbf, 0x58, 0x11, 0x58, 0x5e, 0xe5, 0x86, 0x1e,
  78. 0x3b, 0x6b, 0x30, 0x7e, 0x72, 0x89, 0xe8, 0x6b, 0x7b, 0xb7, 0xaf, 0xef,
  79. 0x8b, 0xa9, 0x3e, 0xb0, 0xcd, 0x0b, 0xef, 0xb0, 0x0c, 0x96, 0x2b, 0xc5,
  80. 0x3b, 0xd5, 0xf1, 0xc2, 0xae, 0x3a, 0x60, 0xd9, 0x0f, 0x75, 0x37, 0x55,
  81. 0x4d, 0x62, 0xd2, 0xed, 0x96, 0xac, 0x30, 0x6b, 0xda, 0xa1, 0x48, 0x17,
  82. 0x96, 0x23, 0x85, 0x9a, 0x57, 0x77, 0xe9, 0x22, 0xa2, 0x37, 0x03, 0xba,
  83. 0x49, 0x77, 0x40, 0x3b, 0x76, 0x4b, 0xda, 0xc1, 0x04, 0x57, 0x55, 0x34,
  84. 0x22, 0x83, 0x45, 0x29, 0xab, 0x2e, 0x11, 0xff, 0x0d, 0xab, 0x55, 0xb1,
  85. 0xa7, 0x58, 0x59, 0x05, 0x25, 0xf9, 0x1e, 0x3d, 0xb7, 0xac, 0x04, 0x39,
  86. 0x2c, 0xf9, 0xaf, 0xb8, 0x68, 0xfb, 0x8e, 0x35, 0x71, 0x32, 0xff, 0x70,
  87. 0xe9, 0x46, 0x6d, 0x5c, 0x06, 0x90, 0x88, 0x23, 0x48, 0x0c, 0x50, 0xeb,
  88. 0x0a, 0xa9, 0xae, 0xe8, 0xfc, 0xbe, 0xa5, 0x76, 0x94, 0xd7, 0x64, 0x22,
  89. 0x38, 0x98, 0x17, 0xa4, 0x3a, 0xa7, 0x59, 0x9f, 0x1d, 0x3b, 0x75, 0x90,
  90. 0x1a, 0x81, 0xef, 0x19, 0xfb, 0x2b, 0xb7, 0xa7, 0x64, 0x61, 0x22, 0xa4,
  91. 0x6f, 0x7b, 0xfa, 0x58, 0xbb, 0x8c, 0x4e, 0x77, 0x67, 0xd0, 0x5d, 0x58,
  92. 0x76, 0x8a, 0xbb,
  93. };
  94. static const uint8_t kRSAPrivateKeyDER[] = {
  95. 0x30, 0x82, 0x04, 0xa5, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
  96. 0xce, 0x47, 0xcb, 0x11, 0xbb, 0xd2, 0x9d, 0x8e, 0x9e, 0xd2, 0x1e, 0x14,
  97. 0xaf, 0xc7, 0xea, 0xb6, 0xc9, 0x38, 0x2a, 0x6f, 0xb3, 0x7e, 0xfb, 0xbc,
  98. 0xfc, 0x59, 0x42, 0xb9, 0x56, 0xf0, 0x4c, 0x3f, 0xf7, 0x31, 0x84, 0xbe,
  99. 0xac, 0x03, 0x9e, 0x71, 0x91, 0x85, 0xd8, 0x32, 0xbd, 0x00, 0xea, 0xac,
  100. 0x65, 0xf6, 0x03, 0xc8, 0x0f, 0x8b, 0xfd, 0x6e, 0x58, 0x88, 0x04, 0x41,
  101. 0x92, 0x74, 0xa6, 0x57, 0x2e, 0x8e, 0x88, 0xd5, 0x3d, 0xda, 0x14, 0x3e,
  102. 0x63, 0x88, 0x22, 0xe3, 0x53, 0xe9, 0xba, 0x39, 0x09, 0xac, 0xfb, 0xd0,
  103. 0x4c, 0xf2, 0x3c, 0x20, 0xd6, 0x97, 0xe6, 0xed, 0xf1, 0x62, 0x1e, 0xe5,
  104. 0xc9, 0x48, 0xa0, 0xca, 0x2e, 0x3c, 0x14, 0x5a, 0x82, 0xd4, 0xed, 0xb1,
  105. 0xe3, 0x43, 0xc1, 0x2a, 0x59, 0xa5, 0xb9, 0xc8, 0x48, 0xa7, 0x39, 0x23,
  106. 0x74, 0xa7, 0x37, 0xb0, 0x6f, 0xc3, 0x64, 0x99, 0x6c, 0xa2, 0x82, 0xc8,
  107. 0xf6, 0xdb, 0x86, 0x40, 0xce, 0xd1, 0x85, 0x9f, 0xce, 0x69, 0xf4, 0x15,
  108. 0x2a, 0x23, 0xca, 0xea, 0xb7, 0x7b, 0xdf, 0xfb, 0x43, 0x5f, 0xff, 0x7a,
  109. 0x49, 0x49, 0x0e, 0xe7, 0x02, 0x51, 0x45, 0x13, 0xe8, 0x90, 0x64, 0x21,
  110. 0x0c, 0x26, 0x2b, 0x5d, 0xfc, 0xe4, 0xb5, 0x86, 0x89, 0x43, 0x22, 0x4c,
  111. 0xf3, 0x3b, 0xf3, 0x09, 0xc4, 0xa4, 0x10, 0x80, 0xf2, 0x46, 0xe2, 0x46,
  112. 0x8f, 0x76, 0x50, 0xbf, 0xaf, 0x2b, 0x90, 0x1b, 0x78, 0xc7, 0xcf, 0xc1,
  113. 0x77, 0xd0, 0xfb, 0xa9, 0xfb, 0xc9, 0x66, 0x5a, 0xc5, 0x9b, 0x31, 0x41,
  114. 0x67, 0x01, 0xbe, 0x33, 0x10, 0xba, 0x05, 0x58, 0xed, 0x76, 0x53, 0xde,
  115. 0x5d, 0xc1, 0xe8, 0xbb, 0x9f, 0xf1, 0xcd, 0xfb, 0xdf, 0x64, 0x7f, 0xd7,
  116. 0x18, 0xab, 0x0f, 0x94, 0x28, 0x95, 0x4a, 0xcc, 0x6a, 0xa9, 0x50, 0xc7,
  117. 0x05, 0x47, 0x10, 0x41, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
  118. 0x01, 0x00, 0xa8, 0x47, 0xb9, 0x4a, 0x06, 0x47, 0x93, 0x71, 0x3d, 0xef,
  119. 0x7b, 0xca, 0xb4, 0x7c, 0x0a, 0xe6, 0x82, 0xd0, 0xe7, 0x0d, 0xa9, 0x08,
  120. 0xf6, 0xa4, 0xfd, 0xd8, 0x73, 0xae, 0x6f, 0x56, 0x29, 0x5e, 0x25, 0x72,
  121. 0xa8, 0x30, 0x44, 0x73, 0xcf, 0x56, 0x26, 0xb9, 0x61, 0xde, 0x42, 0x81,
  122. 0xf4, 0xf0, 0x1f, 0x5d, 0xcb, 0x47, 0xf2, 0x26, 0xe9, 0xe0, 0x93, 0x28,
  123. 0xa3, 0x10, 0x3b, 0x42, 0x1e, 0x51, 0x11, 0x12, 0x06, 0x5e, 0xaf, 0xce,
  124. 0xb0, 0xa5, 0x14, 0xdd, 0x82, 0x58, 0xa1, 0xa4, 0x12, 0xdf, 0x65, 0x1d,
  125. 0x51, 0x70, 0x64, 0xd5, 0x58, 0x68, 0x11, 0xa8, 0x6a, 0x23, 0xc2, 0xbf,
  126. 0xa1, 0x25, 0x24, 0x47, 0xb3, 0xa4, 0x3c, 0x83, 0x96, 0xb7, 0x1f, 0xf4,
  127. 0x44, 0xd4, 0xd1, 0xe9, 0xfc, 0x33, 0x68, 0x5e, 0xe2, 0x68, 0x99, 0x9c,
  128. 0x91, 0xe8, 0x72, 0xc9, 0xd7, 0x8c, 0x80, 0x20, 0x8e, 0x77, 0x83, 0x4d,
  129. 0xe4, 0xab, 0xf9, 0x74, 0xa1, 0xdf, 0xd3, 0xc0, 0x0d, 0x5b, 0x05, 0x51,
  130. 0xc2, 0x6f, 0xb2, 0x91, 0x02, 0xec, 0xc0, 0x02, 0x1a, 0x5c, 0x91, 0x05,
  131. 0xf1, 0xe3, 0xfa, 0x65, 0xc2, 0xad, 0x24, 0xe6, 0xe5, 0x3c, 0xb6, 0x16,
  132. 0xf1, 0xa1, 0x67, 0x1a, 0x9d, 0x37, 0x56, 0xbf, 0x01, 0xd7, 0x3b, 0x35,
  133. 0x30, 0x57, 0x73, 0xf4, 0xf0, 0x5e, 0xa7, 0xe8, 0x0a, 0xc1, 0x94, 0x17,
  134. 0xcf, 0x0a, 0xbd, 0xf5, 0x31, 0xa7, 0x2d, 0xf7, 0xf5, 0xd9, 0x8c, 0xc2,
  135. 0x01, 0xbd, 0xda, 0x16, 0x8e, 0xb9, 0x30, 0x40, 0xa6, 0x6e, 0xbd, 0xcd,
  136. 0x4d, 0x84, 0x67, 0x4e, 0x0b, 0xce, 0xd5, 0xef, 0xf8, 0x08, 0x63, 0x02,
  137. 0xc6, 0xc7, 0xf7, 0x67, 0x92, 0xe2, 0x23, 0x9d, 0x27, 0x22, 0x1d, 0xc6,
  138. 0x67, 0x5e, 0x66, 0xbf, 0x03, 0xb8, 0xa9, 0x67, 0xd4, 0x39, 0xd8, 0x75,
  139. 0xfa, 0xe8, 0xed, 0x56, 0xb8, 0x81, 0x02, 0x81, 0x81, 0x00, 0xf7, 0x46,
  140. 0x68, 0xc6, 0x13, 0xf8, 0xba, 0x0f, 0x83, 0xdb, 0x05, 0xa8, 0x25, 0x00,
  141. 0x70, 0x9c, 0x9e, 0x8b, 0x12, 0x34, 0x0d, 0x96, 0xcf, 0x0d, 0x98, 0x9b,
  142. 0x8d, 0x9c, 0x96, 0x78, 0xd1, 0x3c, 0x01, 0x8c, 0xb9, 0x35, 0x5c, 0x20,
  143. 0x42, 0xb4, 0x38, 0xe3, 0xd6, 0x54, 0xe7, 0x55, 0xd6, 0x26, 0x8a, 0x0c,
  144. 0xf6, 0x1f, 0xe0, 0x04, 0xc1, 0x22, 0x42, 0x19, 0x61, 0xc4, 0x94, 0x7c,
  145. 0x07, 0x2e, 0x80, 0x52, 0xfe, 0x8d, 0xe6, 0x92, 0x3a, 0x91, 0xfe, 0x72,
  146. 0x99, 0xe1, 0x2a, 0x73, 0x76, 0xb1, 0x24, 0x20, 0x67, 0xde, 0x28, 0xcb,
  147. 0x0e, 0xe6, 0x52, 0xb5, 0xfa, 0xfb, 0x8b, 0x1e, 0x6a, 0x1d, 0x09, 0x26,
  148. 0xb9, 0xa7, 0x61, 0xba, 0xf8, 0x79, 0xd2, 0x66, 0x57, 0x28, 0xd7, 0x31,
  149. 0xb5, 0x0b, 0x27, 0x19, 0x1e, 0x6f, 0x46, 0xfc, 0x54, 0x95, 0xeb, 0x78,
  150. 0x01, 0xb6, 0xd9, 0x79, 0x5a, 0x4d, 0x02, 0x81, 0x81, 0x00, 0xd5, 0x8f,
  151. 0x16, 0x53, 0x2f, 0x57, 0x93, 0xbf, 0x09, 0x75, 0xbf, 0x63, 0x40, 0x3d,
  152. 0x27, 0xfd, 0x23, 0x21, 0xde, 0x9b, 0xe9, 0x73, 0x3f, 0x49, 0x02, 0xd2,
  153. 0x38, 0x96, 0xcf, 0xc3, 0xba, 0x92, 0x07, 0x87, 0x52, 0xa9, 0x35, 0xe3,
  154. 0x0c, 0xe4, 0x2f, 0x05, 0x7b, 0x37, 0xa5, 0x40, 0x9c, 0x3b, 0x94, 0xf7,
  155. 0xad, 0xa0, 0xee, 0x3a, 0xa8, 0xfb, 0x1f, 0x11, 0x1f, 0xd8, 0x9a, 0x80,
  156. 0x42, 0x3d, 0x7f, 0xa4, 0xb8, 0x9a, 0xaa, 0xea, 0x72, 0xc1, 0xe3, 0xed,
  157. 0x06, 0x60, 0x92, 0x37, 0xf9, 0xba, 0xfb, 0x9e, 0xed, 0x05, 0xa6, 0xd4,
  158. 0x72, 0x68, 0x4f, 0x63, 0xfe, 0xd6, 0x10, 0x0d, 0x4f, 0x0a, 0x93, 0xc6,
  159. 0xb9, 0xd7, 0xaf, 0xfd, 0xd9, 0x57, 0x7d, 0xcb, 0x75, 0xe8, 0x93, 0x2b,
  160. 0xae, 0x4f, 0xea, 0xd7, 0x30, 0x0b, 0x58, 0x44, 0x82, 0x0f, 0x84, 0x5d,
  161. 0x62, 0x11, 0x78, 0xea, 0x5f, 0xc5, 0x02, 0x81, 0x81, 0x00, 0x82, 0x0c,
  162. 0xc1, 0xe6, 0x0b, 0x72, 0xf1, 0x48, 0x5f, 0xac, 0xbd, 0x98, 0xe5, 0x7d,
  163. 0x09, 0xbd, 0x15, 0x95, 0x47, 0x09, 0xa1, 0x6c, 0x03, 0x91, 0xbf, 0x05,
  164. 0x70, 0xc1, 0x3e, 0x52, 0x64, 0x99, 0x0e, 0xa7, 0x98, 0x70, 0xfb, 0xf6,
  165. 0xeb, 0x9e, 0x25, 0x9d, 0x8e, 0x88, 0x30, 0xf2, 0xf0, 0x22, 0x6c, 0xd0,
  166. 0xcc, 0x51, 0x8f, 0x5c, 0x70, 0xc7, 0x37, 0xc4, 0x69, 0xab, 0x1d, 0xfc,
  167. 0xed, 0x3a, 0x03, 0xbb, 0xa2, 0xad, 0xb6, 0xea, 0x89, 0x6b, 0x67, 0x4b,
  168. 0x96, 0xaa, 0xd9, 0xcc, 0xc8, 0x4b, 0xfa, 0x18, 0x21, 0x08, 0xb2, 0xa3,
  169. 0xb9, 0x3e, 0x61, 0x99, 0xdc, 0x5a, 0x97, 0x9c, 0x73, 0x6a, 0xb9, 0xf9,
  170. 0x68, 0x03, 0x24, 0x5f, 0x55, 0x77, 0x9c, 0xb4, 0xbe, 0x7a, 0x78, 0x53,
  171. 0x68, 0x48, 0x69, 0x53, 0xc8, 0xb1, 0xf5, 0xbf, 0x98, 0x2d, 0x11, 0x1e,
  172. 0x98, 0xa8, 0x36, 0x50, 0xa0, 0xb1, 0x02, 0x81, 0x81, 0x00, 0x90, 0x88,
  173. 0x30, 0x71, 0xc7, 0xfe, 0x9b, 0x6d, 0x95, 0x37, 0x6d, 0x79, 0xfc, 0x85,
  174. 0xe7, 0x44, 0x78, 0xbc, 0x79, 0x6e, 0x47, 0x86, 0xc9, 0xf3, 0xdd, 0xc6,
  175. 0xec, 0xa9, 0x94, 0x9f, 0x40, 0xeb, 0x87, 0xd0, 0xdb, 0xee, 0xcd, 0x1b,
  176. 0x87, 0x23, 0xff, 0x76, 0xd4, 0x37, 0x8a, 0xcd, 0xb9, 0x6e, 0xd1, 0x98,
  177. 0xf6, 0x97, 0x8d, 0xe3, 0x81, 0x6d, 0xc3, 0x4e, 0xd1, 0xa0, 0xc4, 0x9f,
  178. 0xbd, 0x34, 0xe5, 0xe8, 0x53, 0x4f, 0xca, 0x10, 0xb5, 0xed, 0xe7, 0x16,
  179. 0x09, 0x54, 0xde, 0x60, 0xa7, 0xd1, 0x16, 0x6e, 0x2e, 0xb7, 0xbe, 0x7a,
  180. 0xd5, 0x9b, 0x26, 0xef, 0xe4, 0x0e, 0x77, 0xfa, 0xa9, 0xdd, 0xdc, 0xb9,
  181. 0x88, 0x19, 0x23, 0x70, 0xc7, 0xe1, 0x60, 0xaf, 0x8c, 0x73, 0x04, 0xf7,
  182. 0x71, 0x17, 0x81, 0x36, 0x75, 0xbb, 0x97, 0xd7, 0x75, 0xb6, 0x8e, 0xbc,
  183. 0xac, 0x9c, 0x6a, 0x9b, 0x24, 0x89, 0x02, 0x81, 0x80, 0x5a, 0x2b, 0xc7,
  184. 0x6b, 0x8c, 0x65, 0xdb, 0x04, 0x73, 0xab, 0x25, 0xe1, 0x5b, 0xbc, 0x3c,
  185. 0xcf, 0x5a, 0x3c, 0x04, 0xae, 0x97, 0x2e, 0xfd, 0xa4, 0x97, 0x1f, 0x05,
  186. 0x17, 0x27, 0xac, 0x7c, 0x30, 0x85, 0xb4, 0x82, 0x3f, 0x5b, 0xb7, 0x94,
  187. 0x3b, 0x7f, 0x6c, 0x0c, 0xc7, 0x16, 0xc6, 0xa0, 0xbd, 0x80, 0xb0, 0x81,
  188. 0xde, 0xa0, 0x23, 0xa6, 0xf6, 0x75, 0x33, 0x51, 0x35, 0xa2, 0x75, 0x55,
  189. 0x70, 0x4d, 0x42, 0xbb, 0xcf, 0x54, 0xe4, 0xdb, 0x2d, 0x88, 0xa0, 0x7a,
  190. 0xf2, 0x17, 0xa7, 0xdd, 0x13, 0x44, 0x9f, 0x5f, 0x6b, 0x2c, 0x42, 0x42,
  191. 0x8b, 0x13, 0x4d, 0xf9, 0x5b, 0xf8, 0x33, 0x42, 0xd9, 0x9e, 0x50, 0x1c,
  192. 0x7c, 0xbc, 0xfa, 0x62, 0x85, 0x0b, 0xcf, 0x99, 0xda, 0x9e, 0x04, 0x90,
  193. 0xb2, 0xc6, 0xb2, 0x0a, 0x2a, 0x7c, 0x6d, 0x6a, 0x40, 0xfc, 0xf5, 0x50,
  194. 0x98, 0x46, 0x89, 0x82, 0x40,
  195. };
  196. struct GlobalState {
  197. GlobalState() {
  198. const uint8_t *bufp = kRSAPrivateKeyDER;
  199. RSA *privkey = d2i_RSAPrivateKey(NULL, &bufp, sizeof(kRSAPrivateKeyDER));
  200. assert(privkey != nullptr);
  201. pkey_.reset(EVP_PKEY_new());
  202. EVP_PKEY_assign_RSA(pkey_.get(), privkey);
  203. bufp = kCertificateDER;
  204. cert_.reset(d2i_X509(NULL, &bufp, sizeof(kCertificateDER)));
  205. assert(cert_.get() != nullptr);
  206. certs_.reset(sk_X509_new_null());
  207. bssl::PushToStack(certs_.get(), bssl::UpRef(cert_));
  208. }
  209. bssl::UniquePtr<EVP_PKEY> pkey_;
  210. bssl::UniquePtr<X509> cert_;
  211. bssl::UniquePtr<STACK_OF(X509)> certs_;
  212. };
  213. static GlobalState g_state;
  214. static bool GetString(std::string *out, CBS *cbs) {
  215. CBS str;
  216. if (!CBS_get_u8_length_prefixed(cbs, &str)) {
  217. return false;
  218. }
  219. out->assign(reinterpret_cast<const char *>(CBS_data(&str)), CBS_len(&str));
  220. return true;
  221. }
  222. template <typename T>
  223. static bool GetVector(std::vector<T> *out, CBS *cbs) {
  224. static_assert(std::is_pod<T>::value,
  225. "GetVector may only be called on POD types");
  226. CBS child;
  227. if (!CBS_get_u8_length_prefixed(cbs, &child)) {
  228. return false;
  229. }
  230. size_t num = CBS_len(&child) / sizeof(T);
  231. out->resize(num);
  232. out->shrink_to_fit(); // Ensure ASan notices out-of-bounds reads.
  233. OPENSSL_memcpy(out->data(), CBS_data(&child), num * sizeof(T));
  234. return true;
  235. }
  236. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
  237. constexpr size_t kMaxExpensiveAPIs = 100;
  238. unsigned expensive_api_count = 0;
  239. const std::function<void(SSL_CTX *, CBS *)> kAPIs[] = {
  240. [](SSL_CTX *ctx, CBS *cbs) {
  241. uint8_t b;
  242. if (!CBS_get_u8(cbs, &b)) {
  243. return;
  244. }
  245. SSL_CTX_set_quiet_shutdown(ctx, b);
  246. },
  247. [](SSL_CTX *ctx, CBS *cbs) { SSL_CTX_get_quiet_shutdown(ctx); },
  248. [](SSL_CTX *ctx, CBS *cbs) {
  249. uint16_t version;
  250. if (!CBS_get_u16(cbs, &version)) {
  251. return;
  252. }
  253. SSL_CTX_set_min_proto_version(ctx, version);
  254. },
  255. [](SSL_CTX *ctx, CBS *cbs) {
  256. uint16_t version;
  257. if (!CBS_get_u16(cbs, &version)) {
  258. return;
  259. }
  260. SSL_CTX_set_max_proto_version(ctx, version);
  261. },
  262. [](SSL_CTX *ctx, CBS *cbs) {
  263. uint32_t options;
  264. if (!CBS_get_u32(cbs, &options)) {
  265. return;
  266. }
  267. SSL_CTX_set_options(ctx, options);
  268. },
  269. [](SSL_CTX *ctx, CBS *cbs) {
  270. uint32_t options;
  271. if (!CBS_get_u32(cbs, &options)) {
  272. return;
  273. }
  274. SSL_CTX_clear_options(ctx, options);
  275. },
  276. [](SSL_CTX *ctx, CBS *cbs) { SSL_CTX_get_options(ctx); },
  277. [](SSL_CTX *ctx, CBS *cbs) {
  278. uint32_t mode;
  279. if (!CBS_get_u32(cbs, &mode)) {
  280. return;
  281. }
  282. SSL_CTX_set_mode(ctx, mode);
  283. },
  284. [](SSL_CTX *ctx, CBS *cbs) {
  285. uint32_t mode;
  286. if (!CBS_get_u32(cbs, &mode)) {
  287. return;
  288. }
  289. SSL_CTX_clear_mode(ctx, mode);
  290. },
  291. [](SSL_CTX *ctx, CBS *cbs) { SSL_CTX_get_mode(ctx); },
  292. [](SSL_CTX *ctx, CBS *cbs) {
  293. SSL_CTX_use_certificate(ctx, g_state.cert_.get());
  294. },
  295. [](SSL_CTX *ctx, CBS *cbs) {
  296. SSL_CTX_use_PrivateKey(ctx, g_state.pkey_.get());
  297. },
  298. [](SSL_CTX *ctx, CBS *cbs) {
  299. SSL_CTX_set1_chain(ctx, g_state.certs_.get());
  300. },
  301. [&](SSL_CTX *ctx, CBS *cbs) {
  302. // Avoid an unbounded certificate chain.
  303. if (++expensive_api_count >= kMaxExpensiveAPIs) {
  304. return;
  305. }
  306. SSL_CTX_add1_chain_cert(ctx, g_state.cert_.get());
  307. },
  308. [](SSL_CTX *ctx, CBS *cbs) { SSL_CTX_clear_chain_certs(ctx); },
  309. [](SSL_CTX *ctx, CBS *cbs) { SSL_CTX_clear_extra_chain_certs(ctx); },
  310. [](SSL_CTX *ctx, CBS *cbs) { SSL_CTX_check_private_key(ctx); },
  311. [](SSL_CTX *ctx, CBS *cbs) { SSL_CTX_get0_certificate(ctx); },
  312. [](SSL_CTX *ctx, CBS *cbs) { SSL_CTX_get0_privatekey(ctx); },
  313. [](SSL_CTX *ctx, CBS *cbs) {
  314. STACK_OF(X509) * chains;
  315. SSL_CTX_get0_chain_certs(ctx, &chains);
  316. },
  317. [](SSL_CTX *ctx, CBS *cbs) {
  318. std::vector<uint8_t> sct_data;
  319. if (!GetVector(&sct_data, cbs)) {
  320. return;
  321. }
  322. SSL_CTX_set_signed_cert_timestamp_list(ctx, sct_data.data(),
  323. sct_data.size());
  324. },
  325. [](SSL_CTX *ctx, CBS *cbs) {
  326. std::vector<uint8_t> ocsp_data;
  327. if (!GetVector(&ocsp_data, cbs)) {
  328. return;
  329. }
  330. SSL_CTX_set_ocsp_response(ctx, ocsp_data.data(), ocsp_data.size());
  331. },
  332. [](SSL_CTX *ctx, CBS *cbs) {
  333. std::vector<uint16_t> algs;
  334. if (!GetVector(&algs, cbs)) {
  335. return;
  336. }
  337. SSL_CTX_set_signing_algorithm_prefs(ctx, algs.data(), algs.size());
  338. },
  339. [](SSL_CTX *ctx, CBS *cbs) {
  340. std::string ciphers;
  341. if (!GetString(&ciphers, cbs)) {
  342. return;
  343. }
  344. SSL_CTX_set_strict_cipher_list(ctx, ciphers.c_str());
  345. },
  346. [](SSL_CTX *ctx, CBS *cbs) {
  347. std::string ciphers;
  348. if (!GetString(&ciphers, cbs)) {
  349. return;
  350. }
  351. SSL_CTX_set_cipher_list(ctx, ciphers.c_str());
  352. },
  353. [](SSL_CTX *ctx, CBS *cbs) {
  354. std::vector<uint16_t> algs;
  355. if (!GetVector(&algs, cbs)) {
  356. return;
  357. }
  358. SSL_CTX_set_verify_algorithm_prefs(ctx, algs.data(), algs.size());
  359. },
  360. [](SSL_CTX *ctx, CBS *cbs) {
  361. std::vector<uint8_t> id_ctx;
  362. if (!GetVector(&id_ctx, cbs)) {
  363. return;
  364. }
  365. SSL_CTX_set_session_id_context(ctx, id_ctx.data(), id_ctx.size());
  366. },
  367. [](SSL_CTX *ctx, CBS *cbs) {
  368. uint32_t size;
  369. if (!CBS_get_u32(cbs, &size)) {
  370. return;
  371. }
  372. SSL_CTX_sess_set_cache_size(ctx, size);
  373. },
  374. [](SSL_CTX *ctx, CBS *cbs) { SSL_CTX_sess_get_cache_size(ctx); },
  375. [](SSL_CTX *ctx, CBS *cbs) { SSL_CTX_sess_number(ctx); },
  376. [](SSL_CTX *ctx, CBS *cbs) {
  377. uint32_t time;
  378. if (!CBS_get_u32(cbs, &time)) {
  379. return;
  380. }
  381. SSL_CTX_flush_sessions(ctx, time);
  382. },
  383. [](SSL_CTX *ctx, CBS *cbs) {
  384. std::vector<uint8_t> keys;
  385. if (!GetVector(&keys, cbs)) {
  386. return;
  387. }
  388. SSL_CTX_set_tlsext_ticket_keys(ctx, keys.data(), keys.size());
  389. },
  390. [](SSL_CTX *ctx, CBS *cbs) {
  391. std::vector<int> curves;
  392. if (!GetVector(&curves, cbs)) {
  393. return;
  394. }
  395. SSL_CTX_set1_curves(ctx, curves.data(), curves.size());
  396. },
  397. [](SSL_CTX *ctx, CBS *cbs) {
  398. std::string curves;
  399. if (!GetString(&curves, cbs)) {
  400. return;
  401. }
  402. SSL_CTX_set1_curves_list(ctx, curves.c_str());
  403. },
  404. [](SSL_CTX *ctx, CBS *cbs) {
  405. SSL_CTX_enable_signed_cert_timestamps(ctx);
  406. },
  407. [](SSL_CTX *ctx, CBS *cbs) { SSL_CTX_enable_ocsp_stapling(ctx); },
  408. [&](SSL_CTX *ctx, CBS *cbs) {
  409. // Avoid an unbounded client CA list.
  410. if (++expensive_api_count >= kMaxExpensiveAPIs) {
  411. return;
  412. }
  413. SSL_CTX_add_client_CA(ctx, g_state.cert_.get());
  414. },
  415. [](SSL_CTX *ctx, CBS *cbs) {
  416. std::vector<uint8_t> protos;
  417. if (!GetVector(&protos, cbs)) {
  418. return;
  419. }
  420. SSL_CTX_set_alpn_protos(ctx, protos.data(), protos.size());
  421. },
  422. [](SSL_CTX *ctx, CBS *cbs) {
  423. std::string profiles;
  424. if (!GetString(&profiles, cbs)) {
  425. return;
  426. }
  427. SSL_CTX_set_srtp_profiles(ctx, profiles.c_str());
  428. },
  429. [](SSL_CTX *ctx, CBS *cbs) { SSL_CTX_get_max_cert_list(ctx); },
  430. [](SSL_CTX *ctx, CBS *cbs) {
  431. uint32_t size;
  432. if (!CBS_get_u32(cbs, &size)) {
  433. return;
  434. }
  435. SSL_CTX_set_max_cert_list(ctx, size);
  436. },
  437. [](SSL_CTX *ctx, CBS *cbs) {
  438. uint32_t size;
  439. if (!CBS_get_u32(cbs, &size)) {
  440. return;
  441. }
  442. SSL_CTX_set_max_send_fragment(ctx, size);
  443. },
  444. [](SSL_CTX *ctx, CBS *cbs) {
  445. uint8_t b;
  446. if (!CBS_get_u8(cbs, &b)) {
  447. return;
  448. }
  449. SSL_CTX_set_retain_only_sha256_of_client_certs(ctx, b);
  450. },
  451. [](SSL_CTX *ctx, CBS *cbs) {
  452. uint8_t b;
  453. if (!CBS_get_u8(cbs, &b)) {
  454. return;
  455. }
  456. SSL_CTX_set_grease_enabled(ctx, b);
  457. },
  458. [](SSL_CTX *ctx, CBS *cbs) {
  459. std::vector<int> sigalgs;
  460. if (!GetVector(&sigalgs, cbs)) {
  461. return;
  462. }
  463. SSL_CTX_set1_sigalgs(ctx, sigalgs.data(), sigalgs.size());
  464. },
  465. [](SSL_CTX *ctx, CBS *cbs) {
  466. std::string sigalgs;
  467. if (!GetString(&sigalgs, cbs)) {
  468. return;
  469. }
  470. SSL_CTX_set1_sigalgs_list(ctx, sigalgs.c_str());
  471. },
  472. };
  473. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
  474. // If the number of functions exceeds this limit then the code needs to do
  475. // more than sample a single uint8_t to pick the function.
  476. static_assert(OPENSSL_ARRAY_SIZE(kAPIs) < 256, "kAPIs too large");
  477. CBS cbs;
  478. CBS_init(&cbs, buf, len);
  479. for (;;) {
  480. uint8_t index;
  481. if (!CBS_get_u8(&cbs, &index)) {
  482. break;
  483. }
  484. kAPIs[index % OPENSSL_ARRAY_SIZE(kAPIs)](ctx.get(), &cbs);
  485. }
  486. bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get()));
  487. ERR_clear_error();
  488. return 0;
  489. }