You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

640 lines
25 KiB

  1. /* Copyright (c) 2014, 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 <stdint.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <openssl/bio.h>
  18. #include <openssl/bytestring.h>
  19. #include <openssl/crypto.h>
  20. #include <openssl/digest.h>
  21. #include <openssl/err.h>
  22. #include <openssl/evp.h>
  23. #include <openssl/rsa.h>
  24. #include <openssl/x509.h>
  25. /* kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
  26. * should never use this key anywhere but in an example. */
  27. static const uint8_t kExampleRSAKeyDER[] = {
  28. 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
  29. 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
  30. 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
  31. 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
  32. 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
  33. 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
  34. 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
  35. 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
  36. 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
  37. 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
  38. 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
  39. 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
  40. 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
  41. 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
  42. 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
  43. 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
  44. 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
  45. 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
  46. 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
  47. 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
  48. 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
  49. 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
  50. 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
  51. 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
  52. 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
  53. 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
  54. 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
  55. 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
  56. 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
  57. 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
  58. 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
  59. 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
  60. 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
  61. 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
  62. 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
  63. 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
  64. 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
  65. 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
  66. 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
  67. 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
  68. 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
  69. 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
  70. 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
  71. 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
  72. 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
  73. 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
  74. 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
  75. 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
  76. 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
  77. 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
  78. 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  79. };
  80. static const uint8_t kMsg[] = {1, 2, 3, 4};
  81. static const uint8_t kSignature[] = {
  82. 0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d,
  83. 0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e,
  84. 0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04,
  85. 0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09,
  86. 0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67,
  87. 0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a,
  88. 0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda,
  89. 0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48,
  90. 0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04,
  91. 0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7,
  92. 0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42,
  93. };
  94. /* kExamplePSSCert is an example self-signed certificate, signed with
  95. * kExampleRSAKeyDER using RSA-PSS with default hash functions. */
  96. static const uint8_t kExamplePSSCert[] = {
  97. 0x30, 0x82, 0x02, 0x62, 0x30, 0x82, 0x01, 0xc6, 0xa0, 0x03, 0x02, 0x01,
  98. 0x02, 0x02, 0x09, 0x00, 0x8d, 0xea, 0x53, 0x24, 0xfa, 0x48, 0x87, 0xf3,
  99. 0x30, 0x12, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  100. 0x0a, 0x30, 0x05, 0xa2, 0x03, 0x02, 0x01, 0x6a, 0x30, 0x45, 0x31, 0x0b,
  101. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31,
  102. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f,
  103. 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f,
  104. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72,
  105. 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20,
  106. 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x1e, 0x17, 0x0d, 0x31,
  107. 0x34, 0x31, 0x30, 0x30, 0x39, 0x31, 0x39, 0x30, 0x39, 0x35, 0x35, 0x5a,
  108. 0x17, 0x0d, 0x31, 0x35, 0x31, 0x30, 0x30, 0x39, 0x31, 0x39, 0x30, 0x39,
  109. 0x35, 0x35, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
  110. 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
  111. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74,
  112. 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a,
  113. 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57,
  114. 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c,
  115. 0x74, 0x64, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  116. 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00,
  117. 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4,
  118. 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d,
  119. 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83,
  120. 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c,
  121. 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf,
  122. 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33,
  123. 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7,
  124. 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77,
  125. 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52,
  126. 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c,
  127. 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64,
  128. 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x50, 0x30, 0x4e,
  129. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xd0,
  130. 0x41, 0xfb, 0x89, 0x41, 0x1e, 0xa7, 0xad, 0x5a, 0xec, 0x34, 0x5d, 0x49,
  131. 0x11, 0xf9, 0x55, 0x81, 0x78, 0x1f, 0x13, 0x30, 0x1f, 0x06, 0x03, 0x55,
  132. 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xd0, 0x41, 0xfb, 0x89,
  133. 0x41, 0x1e, 0xa7, 0xad, 0x5a, 0xec, 0x34, 0x5d, 0x49, 0x11, 0xf9, 0x55,
  134. 0x81, 0x78, 0x1f, 0x13, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04,
  135. 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x12, 0x06, 0x09, 0x2a, 0x86,
  136. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0a, 0x30, 0x05, 0xa2, 0x03, 0x02,
  137. 0x01, 0x6a, 0x03, 0x81, 0x81, 0x00, 0x49, 0x4c, 0xb6, 0x45, 0x97, 0x20,
  138. 0x35, 0xb3, 0x50, 0x64, 0x0d, 0x3f, 0xec, 0x5f, 0x95, 0xd5, 0x84, 0xcb,
  139. 0x11, 0x7c, 0x03, 0xd7, 0xa6, 0xe6, 0xfa, 0x24, 0x95, 0x9f, 0x31, 0xb0,
  140. 0xb5, 0xec, 0x66, 0x41, 0x51, 0x18, 0x21, 0x91, 0xbb, 0xe0, 0xaf, 0xf0,
  141. 0xc5, 0xb7, 0x59, 0x41, 0xd4, 0xdb, 0xa4, 0xd2, 0x64, 0xa7, 0x54, 0x0f,
  142. 0x8c, 0xf7, 0xe1, 0xd3, 0x3b, 0x1a, 0xb7, 0x0e, 0x9d, 0x9a, 0xde, 0x50,
  143. 0xa1, 0x9f, 0x0a, 0xf0, 0xda, 0x34, 0x0e, 0x34, 0x7d, 0x76, 0x07, 0xfe,
  144. 0x5a, 0xfb, 0xf9, 0x58, 0x9b, 0xc9, 0x50, 0x84, 0x01, 0xa0, 0x05, 0x4d,
  145. 0x67, 0x42, 0x0b, 0xf8, 0xe4, 0x05, 0xcf, 0xaf, 0x8b, 0x71, 0x31, 0xf1,
  146. 0x0f, 0x6e, 0xc9, 0x24, 0x27, 0x9b, 0xac, 0x04, 0xd7, 0x64, 0x0d, 0x30,
  147. 0x4e, 0x11, 0x93, 0x40, 0x39, 0xbb, 0x72, 0xb2, 0xfe, 0x6b, 0xe4, 0xae,
  148. 0x8c, 0x16,
  149. };
  150. /* kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
  151. * PrivateKeyInfo. */
  152. static const uint8_t kExampleRSAKeyPKCS8[] = {
  153. 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  154. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
  155. 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
  156. 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
  157. 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
  158. 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
  159. 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
  160. 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
  161. 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
  162. 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
  163. 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
  164. 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
  165. 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
  166. 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
  167. 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
  168. 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
  169. 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
  170. 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
  171. 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
  172. 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
  173. 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
  174. 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
  175. 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
  176. 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
  177. 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
  178. 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
  179. 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
  180. 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
  181. 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
  182. 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
  183. 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
  184. 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
  185. 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
  186. 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
  187. 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
  188. 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
  189. 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
  190. 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
  191. 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
  192. 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
  193. 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
  194. 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
  195. 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
  196. 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
  197. 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
  198. 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
  199. 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
  200. 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
  201. 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
  202. 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
  203. 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
  204. 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
  205. 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  206. };
  207. /* kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
  208. * structure. */
  209. static const uint8_t kExampleECKeyDER[] = {
  210. 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
  211. 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
  212. 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
  213. 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  214. 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
  215. 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
  216. 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
  217. 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
  218. 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
  219. 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
  220. 0xc1,
  221. };
  222. /* kExampleBadECKeyDER is a sample EC private key encoded as an ECPrivateKey
  223. * structure. The private key is equal to the order and will fail to import */
  224. static const uint8_t kExampleBadECKeyDER[] = {
  225. 0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
  226. 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
  227. 0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF,
  228. 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  229. 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3,
  230. 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00,
  231. 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  232. 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
  233. 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
  234. };
  235. static EVP_PKEY *load_example_rsa_key(void) {
  236. EVP_PKEY *ret = NULL;
  237. const uint8_t *derp = kExampleRSAKeyDER;
  238. EVP_PKEY *pkey = NULL;
  239. RSA *rsa = NULL;
  240. if (!d2i_RSAPrivateKey(&rsa, &derp, sizeof(kExampleRSAKeyDER))) {
  241. return NULL;
  242. }
  243. pkey = EVP_PKEY_new();
  244. if (pkey == NULL || !EVP_PKEY_set1_RSA(pkey, rsa)) {
  245. goto out;
  246. }
  247. ret = pkey;
  248. pkey = NULL;
  249. out:
  250. if (pkey) {
  251. EVP_PKEY_free(pkey);
  252. }
  253. if (rsa) {
  254. RSA_free(rsa);
  255. }
  256. return ret;
  257. }
  258. static int test_EVP_DigestSignInit(void) {
  259. int ret = 0;
  260. EVP_PKEY *pkey = NULL;
  261. uint8_t *sig = NULL;
  262. size_t sig_len = 0;
  263. EVP_MD_CTX md_ctx, md_ctx_verify;
  264. EVP_MD_CTX_init(&md_ctx);
  265. EVP_MD_CTX_init(&md_ctx_verify);
  266. pkey = load_example_rsa_key();
  267. if (pkey == NULL ||
  268. !EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL, pkey) ||
  269. !EVP_DigestSignUpdate(&md_ctx, kMsg, sizeof(kMsg))) {
  270. goto out;
  271. }
  272. /* Determine the size of the signature. */
  273. if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
  274. goto out;
  275. }
  276. /* Sanity check for testing. */
  277. if (sig_len != EVP_PKEY_size(pkey)) {
  278. fprintf(stderr, "sig_len mismatch\n");
  279. goto out;
  280. }
  281. sig = malloc(sig_len);
  282. if (sig == NULL || !EVP_DigestSignFinal(&md_ctx, sig, &sig_len)) {
  283. goto out;
  284. }
  285. /* Ensure that the signature round-trips. */
  286. if (!EVP_DigestVerifyInit(&md_ctx_verify, NULL, EVP_sha256(), NULL, pkey) ||
  287. !EVP_DigestVerifyUpdate(&md_ctx_verify, kMsg, sizeof(kMsg)) ||
  288. !EVP_DigestVerifyFinal(&md_ctx_verify, sig, sig_len)) {
  289. goto out;
  290. }
  291. ret = 1;
  292. out:
  293. if (!ret) {
  294. BIO_print_errors_fp(stderr);
  295. }
  296. EVP_MD_CTX_cleanup(&md_ctx);
  297. EVP_MD_CTX_cleanup(&md_ctx_verify);
  298. if (pkey) {
  299. EVP_PKEY_free(pkey);
  300. }
  301. if (sig) {
  302. free(sig);
  303. }
  304. return ret;
  305. }
  306. static int test_EVP_DigestVerifyInit(void) {
  307. int ret = 0;
  308. EVP_PKEY *pkey = NULL;
  309. EVP_MD_CTX md_ctx;
  310. EVP_MD_CTX_init(&md_ctx);
  311. pkey = load_example_rsa_key();
  312. if (pkey == NULL ||
  313. !EVP_DigestVerifyInit(&md_ctx, NULL, EVP_sha256(), NULL, pkey) ||
  314. !EVP_DigestVerifyUpdate(&md_ctx, kMsg, sizeof(kMsg)) ||
  315. !EVP_DigestVerifyFinal(&md_ctx, kSignature, sizeof(kSignature))) {
  316. goto out;
  317. }
  318. ret = 1;
  319. out:
  320. if (!ret) {
  321. BIO_print_errors_fp(stderr);
  322. }
  323. EVP_MD_CTX_cleanup(&md_ctx);
  324. if (pkey) {
  325. EVP_PKEY_free(pkey);
  326. }
  327. return ret;
  328. }
  329. /* test_algorithm_roundtrip signs a message using an already-initialized
  330. * |md_ctx|, sampling the AlgorithmIdentifier. It then uses |pkey| and the
  331. * AlgorithmIdentifier to verify the signature. */
  332. static int test_algorithm_roundtrip(EVP_MD_CTX *md_ctx, EVP_PKEY *pkey) {
  333. int ret = 0;
  334. uint8_t *sig = NULL;
  335. size_t sig_len = 0;
  336. EVP_MD_CTX md_ctx_verify;
  337. X509_ALGOR *algor = NULL;
  338. EVP_MD_CTX_init(&md_ctx_verify);
  339. if (!EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))) {
  340. goto out;
  341. }
  342. /* Save the algorithm. */
  343. algor = X509_ALGOR_new();
  344. if (algor == NULL || !EVP_DigestSignAlgorithm(md_ctx, algor)) {
  345. goto out;
  346. }
  347. /* Determine the size of the signature. */
  348. if (!EVP_DigestSignFinal(md_ctx, NULL, &sig_len)) {
  349. goto out;
  350. }
  351. /* Sanity check for testing. */
  352. if (sig_len != EVP_PKEY_size(pkey)) {
  353. fprintf(stderr, "sig_len mismatch\n");
  354. goto out;
  355. }
  356. sig = malloc(sig_len);
  357. if (sig == NULL || !EVP_DigestSignFinal(md_ctx, sig, &sig_len)) {
  358. goto out;
  359. }
  360. /* Ensure that the signature round-trips. */
  361. if (!EVP_DigestVerifyInitFromAlgorithm(&md_ctx_verify, algor, pkey) ||
  362. !EVP_DigestVerifyUpdate(&md_ctx_verify, kMsg, sizeof(kMsg)) ||
  363. !EVP_DigestVerifyFinal(&md_ctx_verify, sig, sig_len)) {
  364. goto out;
  365. }
  366. ret = 1;
  367. out:
  368. EVP_MD_CTX_cleanup(&md_ctx_verify);
  369. if (sig) {
  370. free(sig);
  371. }
  372. if (algor) {
  373. X509_ALGOR_free(algor);
  374. }
  375. return ret;
  376. }
  377. static int test_EVP_DigestSignAlgorithm(void) {
  378. int ret = 0;
  379. EVP_PKEY *pkey = NULL;
  380. EVP_MD_CTX md_ctx;
  381. EVP_PKEY_CTX *pkey_ctx;
  382. EVP_MD_CTX_init(&md_ctx);
  383. pkey = load_example_rsa_key();
  384. if (pkey == NULL) {
  385. goto out;
  386. }
  387. /* Test a simple AlgorithmIdentifier. */
  388. if (!EVP_DigestSignInit(&md_ctx, &pkey_ctx, EVP_sha256(), NULL, pkey) ||
  389. !test_algorithm_roundtrip(&md_ctx, pkey)) {
  390. fprintf(stderr, "RSA with SHA-256 failed\n");
  391. goto out;
  392. }
  393. EVP_MD_CTX_cleanup(&md_ctx);
  394. EVP_MD_CTX_init(&md_ctx);
  395. /* Test RSA-PSS with custom parameters. */
  396. if (!EVP_DigestSignInit(&md_ctx, &pkey_ctx, EVP_sha256(), NULL, pkey) ||
  397. EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING) != 1 ||
  398. EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, EVP_sha512()) != 1 ||
  399. !test_algorithm_roundtrip(&md_ctx, pkey)) {
  400. fprintf(stderr, "RSA-PSS failed\n");
  401. goto out;
  402. }
  403. ret = 1;
  404. out:
  405. if (!ret) {
  406. BIO_print_errors_fp(stderr);
  407. }
  408. EVP_MD_CTX_cleanup(&md_ctx);
  409. if (pkey) {
  410. EVP_PKEY_free(pkey);
  411. }
  412. return ret;
  413. }
  414. static int test_EVP_DigestVerifyInitFromAlgorithm(void) {
  415. int ret = 0;
  416. CBS cert, cert_body, tbs_cert, algorithm, signature;
  417. uint8_t padding;
  418. X509_ALGOR *algor = NULL;
  419. const uint8_t *derp;
  420. EVP_PKEY *pkey = NULL;
  421. EVP_MD_CTX md_ctx;
  422. EVP_MD_CTX_init(&md_ctx);
  423. CBS_init(&cert, kExamplePSSCert, sizeof(kExamplePSSCert));
  424. if (!CBS_get_asn1(&cert, &cert_body, CBS_ASN1_SEQUENCE) ||
  425. CBS_len(&cert) != 0 ||
  426. !CBS_get_any_asn1_element(&cert_body, &tbs_cert, NULL, NULL) ||
  427. !CBS_get_asn1_element(&cert_body, &algorithm, CBS_ASN1_SEQUENCE) ||
  428. !CBS_get_asn1(&cert_body, &signature, CBS_ASN1_BITSTRING) ||
  429. CBS_len(&cert_body) != 0) {
  430. fprintf(stderr, "Failed to parse certificate\n");
  431. goto out;
  432. }
  433. /* Signatures are BIT STRINGs, but they have are multiple of 8 bytes, so the
  434. leading phase byte is just a zero. */
  435. if (!CBS_get_u8(&signature, &padding) || padding != 0) {
  436. fprintf(stderr, "Invalid signature padding\n");
  437. goto out;
  438. }
  439. derp = CBS_data(&algorithm);
  440. if (!d2i_X509_ALGOR(&algor, &derp, CBS_len(&algorithm)) ||
  441. derp != CBS_data(&algorithm) + CBS_len(&algorithm)) {
  442. fprintf(stderr, "Failed to parse algorithm\n");
  443. }
  444. pkey = load_example_rsa_key();
  445. if (pkey == NULL ||
  446. !EVP_DigestVerifyInitFromAlgorithm(&md_ctx, algor, pkey) ||
  447. !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&tbs_cert),
  448. CBS_len(&tbs_cert)) ||
  449. !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
  450. CBS_len(&signature))) {
  451. goto out;
  452. }
  453. ret = 1;
  454. out:
  455. if (!ret) {
  456. BIO_print_errors_fp(stderr);
  457. }
  458. EVP_MD_CTX_cleanup(&md_ctx);
  459. if (pkey) {
  460. EVP_PKEY_free(pkey);
  461. }
  462. return ret;
  463. }
  464. static int test_d2i_AutoPrivateKey(const uint8_t *input, size_t input_len,
  465. int expected_id) {
  466. int ret = 0;
  467. const uint8_t *p;
  468. EVP_PKEY *pkey = NULL;
  469. p = input;
  470. pkey = d2i_AutoPrivateKey(NULL, &p, input_len);
  471. if (pkey == NULL || p != input + input_len) {
  472. fprintf(stderr, "d2i_AutoPrivateKey failed\n");
  473. goto done;
  474. }
  475. if (EVP_PKEY_id(pkey) != expected_id) {
  476. fprintf(stderr, "Did not decode expected type\n");
  477. goto done;
  478. }
  479. ret = 1;
  480. done:
  481. if (!ret) {
  482. BIO_print_errors_fp(stderr);
  483. }
  484. if (pkey != NULL) {
  485. EVP_PKEY_free(pkey);
  486. }
  487. return ret;
  488. }
  489. /* Tests loading a bad key in PKCS8 format */
  490. static int test_EVP_PKCS82PKEY(void) {
  491. int ret = 0;
  492. const uint8_t *derp = kExampleBadECKeyDER;
  493. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  494. EVP_PKEY *pkey = NULL;
  495. p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp, sizeof(kExampleBadECKeyDER));
  496. if (!p8inf || derp != kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER)) {
  497. fprintf(stderr, "Failed to parse key\n");
  498. goto done;
  499. }
  500. pkey = EVP_PKCS82PKEY(p8inf);
  501. if (pkey) {
  502. fprintf(stderr, "Imported invalid EC key\n");
  503. goto done;
  504. }
  505. ret = 1;
  506. done:
  507. if (p8inf != NULL) {
  508. PKCS8_PRIV_KEY_INFO_free(p8inf);
  509. }
  510. if (pkey != NULL) {
  511. EVP_PKEY_free(pkey);
  512. }
  513. return ret;
  514. }
  515. int main(void) {
  516. CRYPTO_library_init();
  517. ERR_load_crypto_strings();
  518. if (!test_EVP_DigestSignInit()) {
  519. fprintf(stderr, "EVP_DigestSignInit failed\n");
  520. return 1;
  521. }
  522. if (!test_EVP_DigestVerifyInit()) {
  523. fprintf(stderr, "EVP_DigestVerifyInit failed\n");
  524. return 1;
  525. }
  526. if (!test_EVP_DigestSignAlgorithm()) {
  527. fprintf(stderr, "EVP_DigestSignInit failed\n");
  528. return 1;
  529. }
  530. if (!test_EVP_DigestVerifyInitFromAlgorithm()) {
  531. fprintf(stderr, "EVP_DigestVerifyInitFromAlgorithm failed\n");
  532. return 1;
  533. }
  534. if (!test_d2i_AutoPrivateKey(kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER),
  535. EVP_PKEY_RSA)) {
  536. fprintf(stderr, "d2i_AutoPrivateKey(kExampleRSAKeyDER) failed\n");
  537. return 1;
  538. }
  539. if (!test_d2i_AutoPrivateKey(kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8),
  540. EVP_PKEY_RSA)) {
  541. fprintf(stderr, "d2i_AutoPrivateKey(kExampleRSAKeyPKCS8) failed\n");
  542. return 1;
  543. }
  544. if (!test_d2i_AutoPrivateKey(kExampleECKeyDER, sizeof(kExampleECKeyDER),
  545. EVP_PKEY_EC)) {
  546. fprintf(stderr, "d2i_AutoPrivateKey(kExampleECKeyDER) failed\n");
  547. return 1;
  548. }
  549. if (!test_EVP_PKCS82PKEY()) {
  550. fprintf(stderr, "test_EVP_PKCS82PKEY failed\n");
  551. return 1;
  552. }
  553. printf("PASS\n");
  554. return 0;
  555. }