evp_extra_test.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <openssl/bio.h>
  13. #include <openssl/conf.h>
  14. #include <openssl/crypto.h>
  15. #include <openssl/err.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/rsa.h>
  18. #include <openssl/x509.h>
  19. #include <openssl/pem.h>
  20. #include <openssl/kdf.h>
  21. #include <openssl/provider.h>
  22. #include <openssl/core_names.h>
  23. #include <openssl/dsa.h>
  24. #include "testutil.h"
  25. #include "internal/nelem.h"
  26. #include "crypto/evp.h"
  27. /*
  28. * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
  29. * should never use this key anywhere but in an example.
  30. */
  31. static const unsigned char kExampleRSAKeyDER[] = {
  32. 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
  33. 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
  34. 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
  35. 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
  36. 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
  37. 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
  38. 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
  39. 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
  40. 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
  41. 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
  42. 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
  43. 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
  44. 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
  45. 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
  46. 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
  47. 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
  48. 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
  49. 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
  50. 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
  51. 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
  52. 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
  53. 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
  54. 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
  55. 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
  56. 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
  57. 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
  58. 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
  59. 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
  60. 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
  61. 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
  62. 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
  63. 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
  64. 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
  65. 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
  66. 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
  67. 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
  68. 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
  69. 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
  70. 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
  71. 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
  72. 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
  73. 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
  74. 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
  75. 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
  76. 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
  77. 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
  78. 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
  79. 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
  80. 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
  81. 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
  82. 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  83. };
  84. /*
  85. * kExampleBadRSAKeyDER is an RSA private key in ASN.1, DER format. The private
  86. * components are not correct.
  87. */
  88. static const unsigned char kExampleBadRSAKeyDER[] = {
  89. 0x30, 0x82, 0x04, 0x27, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
  90. 0xa6, 0x1a, 0x1e, 0x6e, 0x7b, 0xee, 0xc6, 0x89, 0x66, 0xe7, 0x93, 0xef,
  91. 0x54, 0x12, 0x68, 0xea, 0xbf, 0x86, 0x2f, 0xdd, 0xd2, 0x79, 0xb8, 0xa9,
  92. 0x6e, 0x03, 0xc2, 0xa3, 0xb9, 0xa3, 0xe1, 0x4b, 0x2a, 0xb3, 0xf8, 0xb4,
  93. 0xcd, 0xea, 0xbe, 0x24, 0xa6, 0x57, 0x5b, 0x83, 0x1f, 0x0f, 0xf2, 0xd3,
  94. 0xb7, 0xac, 0x7e, 0xd6, 0x8e, 0x6e, 0x1e, 0xbf, 0xb8, 0x73, 0x8c, 0x05,
  95. 0x56, 0xe6, 0x35, 0x1f, 0xe9, 0x04, 0x0b, 0x09, 0x86, 0x7d, 0xf1, 0x26,
  96. 0x08, 0x99, 0xad, 0x7b, 0xc8, 0x4d, 0x94, 0xb0, 0x0b, 0x8b, 0x38, 0xa0,
  97. 0x5c, 0x62, 0xa0, 0xab, 0xd3, 0x8f, 0xd4, 0x09, 0x60, 0x72, 0x1e, 0x33,
  98. 0x50, 0x80, 0x6e, 0x22, 0xa6, 0x77, 0x57, 0x6b, 0x9a, 0x33, 0x21, 0x66,
  99. 0x87, 0x6e, 0x21, 0x7b, 0xc7, 0x24, 0x0e, 0xd8, 0x13, 0xdf, 0x83, 0xde,
  100. 0xcd, 0x40, 0x58, 0x1d, 0x84, 0x86, 0xeb, 0xb8, 0x12, 0x4e, 0xd2, 0xfa,
  101. 0x80, 0x1f, 0xe4, 0xe7, 0x96, 0x29, 0xb8, 0xcc, 0xce, 0x66, 0x6d, 0x53,
  102. 0xca, 0xb9, 0x5a, 0xd7, 0xf6, 0x84, 0x6c, 0x2d, 0x9a, 0x1a, 0x14, 0x1c,
  103. 0x4e, 0x93, 0x39, 0xba, 0x74, 0xed, 0xed, 0x87, 0x87, 0x5e, 0x48, 0x75,
  104. 0x36, 0xf0, 0xbc, 0x34, 0xfb, 0x29, 0xf9, 0x9f, 0x96, 0x5b, 0x0b, 0xa7,
  105. 0x54, 0x30, 0x51, 0x29, 0x18, 0x5b, 0x7d, 0xac, 0x0f, 0xd6, 0x5f, 0x7c,
  106. 0xf8, 0x98, 0x8c, 0xd8, 0x86, 0x62, 0xb3, 0xdc, 0xff, 0x0f, 0xff, 0x7a,
  107. 0xaf, 0x5c, 0x4c, 0x61, 0x49, 0x2e, 0xc8, 0x95, 0x86, 0xc4, 0x0e, 0x87,
  108. 0xfc, 0x1d, 0xcf, 0x8b, 0x7c, 0x61, 0xf6, 0xd8, 0xd0, 0x69, 0xf6, 0xcd,
  109. 0x8a, 0x8c, 0xf6, 0x62, 0xa2, 0x56, 0xa9, 0xe3, 0xd1, 0xcf, 0x4d, 0xa0,
  110. 0xf6, 0x2d, 0x20, 0x0a, 0x04, 0xb7, 0xa2, 0xf7, 0xb5, 0x99, 0x47, 0x18,
  111. 0x56, 0x85, 0x87, 0xc7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
  112. 0x01, 0x00, 0x99, 0x41, 0x38, 0x1a, 0xd0, 0x96, 0x7a, 0xf0, 0x83, 0xd5,
  113. 0xdf, 0x94, 0xce, 0x89, 0x3d, 0xec, 0x7a, 0x52, 0x21, 0x10, 0x16, 0x06,
  114. 0xe0, 0xee, 0xd2, 0xe6, 0xfd, 0x4b, 0x7b, 0x19, 0x4d, 0xe1, 0xc0, 0xc0,
  115. 0xd5, 0x14, 0x5d, 0x79, 0xdd, 0x7e, 0x8b, 0x4b, 0xc6, 0xcf, 0xb0, 0x75,
  116. 0x52, 0xa3, 0x2d, 0xb1, 0x26, 0x46, 0x68, 0x9c, 0x0a, 0x1a, 0xf2, 0xe1,
  117. 0x09, 0xac, 0x53, 0x85, 0x8c, 0x36, 0xa9, 0x14, 0x65, 0xea, 0xa0, 0x00,
  118. 0xcb, 0xe3, 0x3f, 0xc4, 0x2b, 0x61, 0x2e, 0x6b, 0x06, 0x69, 0x77, 0xfd,
  119. 0x38, 0x7e, 0x1d, 0x3f, 0x92, 0xe7, 0x77, 0x08, 0x19, 0xa7, 0x9d, 0x29,
  120. 0x2d, 0xdc, 0x42, 0xc6, 0x7c, 0xd7, 0xd3, 0xa8, 0x01, 0x2c, 0xf2, 0xd5,
  121. 0x82, 0x57, 0xcb, 0x55, 0x3d, 0xe7, 0xaa, 0xd2, 0x06, 0x30, 0x30, 0x05,
  122. 0xe6, 0xf2, 0x47, 0x86, 0xba, 0xc6, 0x61, 0x64, 0xeb, 0x4f, 0x2a, 0x5e,
  123. 0x07, 0x29, 0xe0, 0x96, 0xb2, 0x43, 0xff, 0x5f, 0x1a, 0x54, 0x16, 0xcf,
  124. 0xb5, 0x56, 0x5c, 0xa0, 0x9b, 0x0c, 0xfd, 0xb3, 0xd2, 0xe3, 0x79, 0x1d,
  125. 0x21, 0xe2, 0xd6, 0x13, 0xc4, 0x74, 0xa6, 0xf5, 0x8e, 0x8e, 0x81, 0xbb,
  126. 0xb4, 0xad, 0x8a, 0xf0, 0x93, 0x0a, 0xd8, 0x0a, 0x42, 0x36, 0xbc, 0xe5,
  127. 0x26, 0x2a, 0x0d, 0x5d, 0x57, 0x13, 0xc5, 0x4e, 0x2f, 0x12, 0x0e, 0xef,
  128. 0xa7, 0x81, 0x1e, 0xc3, 0xa5, 0xdb, 0xc9, 0x24, 0xeb, 0x1a, 0xa1, 0xf9,
  129. 0xf6, 0xa1, 0x78, 0x98, 0x93, 0x77, 0x42, 0x45, 0x03, 0xe2, 0xc9, 0xa2,
  130. 0xfe, 0x2d, 0x77, 0xc8, 0xc6, 0xac, 0x9b, 0x98, 0x89, 0x6d, 0x9a, 0xe7,
  131. 0x61, 0x63, 0xb7, 0xf2, 0xec, 0xd6, 0xb1, 0xa1, 0x6e, 0x0a, 0x1a, 0xff,
  132. 0xfd, 0x43, 0x28, 0xc3, 0x0c, 0xdc, 0xf2, 0x47, 0x4f, 0x27, 0xaa, 0x99,
  133. 0x04, 0x8e, 0xac, 0xe8, 0x7c, 0x01, 0x02, 0x04, 0x12, 0x34, 0x56, 0x78,
  134. 0x02, 0x81, 0x81, 0x00, 0xca, 0x69, 0xe5, 0xbb, 0x3a, 0x90, 0x82, 0xcb,
  135. 0x82, 0x50, 0x2f, 0x29, 0xe2, 0x76, 0x6a, 0x57, 0x55, 0x45, 0x4e, 0x35,
  136. 0x18, 0x61, 0xe0, 0x12, 0x70, 0xc0, 0xab, 0xc7, 0x80, 0xa2, 0xd4, 0x46,
  137. 0x34, 0x03, 0xa0, 0x19, 0x26, 0x23, 0x9e, 0xef, 0x1a, 0xcb, 0x75, 0xd6,
  138. 0xba, 0x81, 0xf4, 0x7e, 0x52, 0xe5, 0x2a, 0xe8, 0xf1, 0x49, 0x6c, 0x0f,
  139. 0x1a, 0xa0, 0xf9, 0xc6, 0xe7, 0xec, 0x60, 0xe4, 0xcb, 0x2a, 0xb5, 0x56,
  140. 0xe9, 0x9c, 0xcd, 0x19, 0x75, 0x92, 0xb1, 0x66, 0xce, 0xc3, 0xd9, 0x3d,
  141. 0x11, 0xcb, 0xc4, 0x09, 0xce, 0x1e, 0x30, 0xba, 0x2f, 0x60, 0x60, 0x55,
  142. 0x8d, 0x02, 0xdc, 0x5d, 0xaf, 0xf7, 0x52, 0x31, 0x17, 0x07, 0x53, 0x20,
  143. 0x33, 0xad, 0x8c, 0xd5, 0x2f, 0x5a, 0xd0, 0x57, 0xd7, 0xd1, 0x80, 0xd6,
  144. 0x3a, 0x9b, 0x04, 0x4f, 0x35, 0xbf, 0xe7, 0xd5, 0xbc, 0x8f, 0xd4, 0x81,
  145. 0x02, 0x81, 0x81, 0x00, 0xc0, 0x9f, 0xf8, 0xcd, 0xf7, 0x3f, 0x26, 0x8a,
  146. 0x3d, 0x4d, 0x2b, 0x0c, 0x01, 0xd0, 0xa2, 0xb4, 0x18, 0xfe, 0xf7, 0x5e,
  147. 0x2f, 0x06, 0x13, 0xcd, 0x63, 0xaa, 0x12, 0xa9, 0x24, 0x86, 0xe3, 0xf3,
  148. 0x7b, 0xda, 0x1a, 0x3c, 0xb1, 0x38, 0x80, 0x80, 0xef, 0x64, 0x64, 0xa1,
  149. 0x9b, 0xfe, 0x76, 0x63, 0x8e, 0x83, 0xd2, 0xd9, 0xb9, 0x86, 0xb0, 0xe6,
  150. 0xa6, 0x0c, 0x7e, 0xa8, 0x84, 0x90, 0x98, 0x0c, 0x1e, 0xf3, 0x14, 0x77,
  151. 0xe0, 0x5f, 0x81, 0x08, 0x11, 0x8f, 0xa6, 0x23, 0xc4, 0xba, 0xc0, 0x8a,
  152. 0xe4, 0xc6, 0xe3, 0x5c, 0xbe, 0xc5, 0xec, 0x2c, 0xb9, 0xd8, 0x8c, 0x4d,
  153. 0x1a, 0x9d, 0xe7, 0x7c, 0x85, 0x4c, 0x0d, 0x71, 0x4e, 0x72, 0x33, 0x1b,
  154. 0xfe, 0xa9, 0x17, 0x72, 0x76, 0x56, 0x9d, 0x74, 0x7e, 0x52, 0x67, 0x9a,
  155. 0x87, 0x9a, 0xdb, 0x30, 0xde, 0xe4, 0x49, 0x28, 0x3b, 0xd2, 0x67, 0xaf,
  156. 0x02, 0x81, 0x81, 0x00, 0x89, 0x74, 0x9a, 0x8e, 0xa7, 0xb9, 0xa5, 0x28,
  157. 0xc0, 0x68, 0xe5, 0x6e, 0x63, 0x1c, 0x99, 0x20, 0x8f, 0x86, 0x8e, 0x12,
  158. 0x9e, 0x69, 0x30, 0xfa, 0x34, 0xd9, 0x92, 0x8d, 0xdb, 0x7c, 0x37, 0xfd,
  159. 0x28, 0xab, 0x61, 0x98, 0x52, 0x7f, 0x14, 0x1a, 0x39, 0xae, 0xfb, 0x6a,
  160. 0x03, 0xa3, 0xe6, 0xbd, 0xb6, 0x5b, 0x6b, 0xe5, 0x5e, 0x9d, 0xc6, 0xa5,
  161. 0x07, 0x27, 0x54, 0x17, 0xd0, 0x3d, 0x84, 0x9b, 0x3a, 0xa0, 0xd9, 0x1e,
  162. 0x99, 0x6c, 0x63, 0x17, 0xab, 0xf1, 0x1f, 0x49, 0xba, 0x95, 0xe3, 0x3b,
  163. 0x86, 0x8f, 0x42, 0xa4, 0x89, 0xf5, 0x94, 0x8f, 0x8b, 0x46, 0xbe, 0x84,
  164. 0xba, 0x4a, 0xbc, 0x0d, 0x5f, 0x46, 0xeb, 0xe8, 0xec, 0x43, 0x8c, 0x1e,
  165. 0xad, 0x19, 0x69, 0x2f, 0x08, 0x86, 0x7a, 0x3f, 0x7d, 0x0f, 0x07, 0x97,
  166. 0xf3, 0x9a, 0x7b, 0xb5, 0xb2, 0xc1, 0x8c, 0x95, 0x68, 0x04, 0xa0, 0x81,
  167. 0x02, 0x81, 0x80, 0x4e, 0xbf, 0x7e, 0x1b, 0xcb, 0x13, 0x61, 0x75, 0x3b,
  168. 0xdb, 0x59, 0x5f, 0xb1, 0xd4, 0xb8, 0xeb, 0x9e, 0x73, 0xb5, 0xe7, 0xf6,
  169. 0x89, 0x3d, 0x1c, 0xda, 0xf0, 0x36, 0xff, 0x35, 0xbd, 0x1e, 0x0b, 0x74,
  170. 0xe3, 0x9e, 0xf0, 0xf2, 0xf7, 0xd7, 0x82, 0xb7, 0x7b, 0x6a, 0x1b, 0x0e,
  171. 0x30, 0x4a, 0x98, 0x0e, 0xb4, 0xf9, 0x81, 0x07, 0xe4, 0x75, 0x39, 0xe9,
  172. 0x53, 0xca, 0xbb, 0x5c, 0xaa, 0x93, 0x07, 0x0e, 0xa8, 0x2f, 0xba, 0x98,
  173. 0x49, 0x30, 0xa7, 0xcc, 0x1a, 0x3c, 0x68, 0x0c, 0xe1, 0xa4, 0xb1, 0x05,
  174. 0xe6, 0xe0, 0x25, 0x78, 0x58, 0x14, 0x37, 0xf5, 0x1f, 0xe3, 0x22, 0xef,
  175. 0xa8, 0x0e, 0x22, 0xa0, 0x94, 0x3a, 0xf6, 0xc9, 0x13, 0xe6, 0x06, 0xbf,
  176. 0x7f, 0x99, 0xc6, 0xcc, 0xd8, 0xc6, 0xbe, 0xd9, 0x2e, 0x24, 0xc7, 0x69,
  177. 0x8c, 0x95, 0xba, 0xf6, 0x04, 0xb3, 0x0a, 0xf4, 0xcb, 0xf0, 0xce,
  178. };
  179. static const unsigned char kMsg[] = { 1, 2, 3, 4 };
  180. static const unsigned char kSignature[] = {
  181. 0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d,
  182. 0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e,
  183. 0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04,
  184. 0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09,
  185. 0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67,
  186. 0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a,
  187. 0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda,
  188. 0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48,
  189. 0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04,
  190. 0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7,
  191. 0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42,
  192. };
  193. /*
  194. * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
  195. * PrivateKeyInfo.
  196. */
  197. static const unsigned char kExampleRSAKeyPKCS8[] = {
  198. 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  199. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
  200. 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
  201. 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
  202. 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
  203. 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
  204. 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
  205. 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
  206. 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
  207. 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
  208. 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
  209. 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
  210. 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
  211. 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
  212. 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
  213. 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
  214. 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
  215. 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
  216. 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
  217. 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
  218. 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
  219. 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
  220. 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
  221. 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
  222. 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
  223. 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
  224. 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
  225. 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
  226. 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
  227. 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
  228. 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
  229. 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
  230. 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
  231. 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
  232. 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
  233. 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
  234. 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
  235. 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
  236. 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
  237. 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
  238. 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
  239. 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
  240. 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
  241. 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
  242. 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
  243. 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
  244. 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
  245. 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
  246. 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
  247. 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
  248. 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
  249. 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
  250. 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  251. };
  252. #ifndef OPENSSL_NO_EC
  253. /*
  254. * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
  255. * structure.
  256. */
  257. static const unsigned char kExampleECKeyDER[] = {
  258. 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
  259. 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
  260. 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
  261. 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  262. 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
  263. 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
  264. 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
  265. 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
  266. 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
  267. 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
  268. 0xc1,
  269. };
  270. /*
  271. * kExampleBadECKeyDER is a sample EC private key encoded as an ECPrivateKey
  272. * structure. The private key is equal to the order and will fail to import
  273. */
  274. static const unsigned char kExampleBadECKeyDER[] = {
  275. 0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
  276. 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
  277. 0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF,
  278. 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  279. 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3,
  280. 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00,
  281. 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  282. 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
  283. 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
  284. };
  285. /* prime256v1 */
  286. static const unsigned char kExampleECPubKeyDER[] = {
  287. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  288. 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  289. 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
  290. 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
  291. 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
  292. 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
  293. 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
  294. 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
  295. };
  296. /*
  297. * kExampleBadECPubKeyDER is a sample EC public key with a wrong OID
  298. * 1.2.840.10045.2.2 instead of 1.2.840.10045.2.1 - EC Public Key
  299. */
  300. static const unsigned char kExampleBadECPubKeyDER[] = {
  301. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  302. 0x02, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  303. 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
  304. 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
  305. 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
  306. 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
  307. 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
  308. 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
  309. };
  310. static const unsigned char pExampleECParamDER[] = {
  311. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
  312. };
  313. #endif
  314. typedef struct APK_DATA_st {
  315. const unsigned char *kder;
  316. size_t size;
  317. int evptype;
  318. int check;
  319. int pub_check;
  320. int param_check;
  321. int type; /* 0 for private, 1 for public, 2 for params */
  322. } APK_DATA;
  323. static APK_DATA keydata[] = {
  324. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA},
  325. {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA},
  326. #ifndef OPENSSL_NO_EC
  327. {kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC}
  328. #endif
  329. };
  330. static APK_DATA keycheckdata[] = {
  331. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA, 1, -2, -2, 0},
  332. {kExampleBadRSAKeyDER, sizeof(kExampleBadRSAKeyDER), EVP_PKEY_RSA,
  333. 0, -2, -2, 0},
  334. #ifndef OPENSSL_NO_EC
  335. {kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC, 1, 1, 1, 0},
  336. /* group is also associated in our pub key */
  337. {kExampleECPubKeyDER, sizeof(kExampleECPubKeyDER), EVP_PKEY_EC, 0, 1, 1, 1},
  338. {pExampleECParamDER, sizeof(pExampleECParamDER), EVP_PKEY_EC, 0, 0, 1, 2}
  339. #endif
  340. };
  341. static EVP_PKEY *load_example_rsa_key(void)
  342. {
  343. EVP_PKEY *ret = NULL;
  344. const unsigned char *derp = kExampleRSAKeyDER;
  345. EVP_PKEY *pkey = NULL;
  346. RSA *rsa = NULL;
  347. if (!TEST_true(d2i_RSAPrivateKey(&rsa, &derp, sizeof(kExampleRSAKeyDER))))
  348. return NULL;
  349. if (!TEST_ptr(pkey = EVP_PKEY_new())
  350. || !TEST_true(EVP_PKEY_set1_RSA(pkey, rsa)))
  351. goto end;
  352. ret = pkey;
  353. pkey = NULL;
  354. end:
  355. EVP_PKEY_free(pkey);
  356. RSA_free(rsa);
  357. return ret;
  358. }
  359. static int test_EVP_Enveloped(void)
  360. {
  361. int ret = 0;
  362. EVP_CIPHER_CTX *ctx = NULL;
  363. EVP_PKEY *keypair = NULL;
  364. unsigned char *kek = NULL;
  365. unsigned char iv[EVP_MAX_IV_LENGTH];
  366. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  367. int len, kek_len, ciphertext_len, plaintext_len;
  368. unsigned char ciphertext[32], plaintext[16];
  369. const EVP_CIPHER *type = EVP_aes_256_cbc();
  370. if (!TEST_ptr(keypair = load_example_rsa_key())
  371. || !TEST_ptr(kek = OPENSSL_zalloc(EVP_PKEY_size(keypair)))
  372. || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  373. || !TEST_true(EVP_SealInit(ctx, type, &kek, &kek_len, iv,
  374. &keypair, 1))
  375. || !TEST_true(EVP_SealUpdate(ctx, ciphertext, &ciphertext_len,
  376. msg, sizeof(msg)))
  377. || !TEST_true(EVP_SealFinal(ctx, ciphertext + ciphertext_len,
  378. &len)))
  379. goto err;
  380. ciphertext_len += len;
  381. if (!TEST_true(EVP_OpenInit(ctx, type, kek, kek_len, iv, keypair))
  382. || !TEST_true(EVP_OpenUpdate(ctx, plaintext, &plaintext_len,
  383. ciphertext, ciphertext_len))
  384. || !TEST_true(EVP_OpenFinal(ctx, plaintext + plaintext_len, &len)))
  385. goto err;
  386. plaintext_len += len;
  387. if (!TEST_mem_eq(msg, sizeof(msg), plaintext, plaintext_len))
  388. goto err;
  389. ret = 1;
  390. err:
  391. OPENSSL_free(kek);
  392. EVP_PKEY_free(keypair);
  393. EVP_CIPHER_CTX_free(ctx);
  394. return ret;
  395. }
  396. static int test_EVP_DigestSignInit(void)
  397. {
  398. int ret = 0;
  399. EVP_PKEY *pkey = NULL;
  400. unsigned char *sig = NULL;
  401. size_t sig_len = 0;
  402. EVP_MD_CTX *md_ctx, *md_ctx_verify = NULL;
  403. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())
  404. || !TEST_ptr(md_ctx_verify = EVP_MD_CTX_new())
  405. || !TEST_ptr(pkey = load_example_rsa_key()))
  406. goto out;
  407. if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))
  408. || !TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
  409. goto out;
  410. /* Determine the size of the signature. */
  411. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len))
  412. || !TEST_size_t_eq(sig_len, (size_t)EVP_PKEY_size(pkey)))
  413. goto out;
  414. if (!TEST_ptr(sig = OPENSSL_malloc(sig_len))
  415. || !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
  416. goto out;
  417. /* Ensure that the signature round-trips. */
  418. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sha256(),
  419. NULL, pkey))
  420. || !TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify,
  421. kMsg, sizeof(kMsg)))
  422. || !TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))
  423. goto out;
  424. ret = 1;
  425. out:
  426. EVP_MD_CTX_free(md_ctx);
  427. EVP_MD_CTX_free(md_ctx_verify);
  428. EVP_PKEY_free(pkey);
  429. OPENSSL_free(sig);
  430. return ret;
  431. }
  432. static int test_EVP_DigestVerifyInit(void)
  433. {
  434. int ret = 0;
  435. EVP_PKEY *pkey = NULL;
  436. EVP_MD_CTX *md_ctx = NULL;
  437. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())
  438. || !TEST_ptr(pkey = load_example_rsa_key()))
  439. goto out;
  440. if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))
  441. || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg)))
  442. || !TEST_true(EVP_DigestVerifyFinal(md_ctx, kSignature,
  443. sizeof(kSignature))))
  444. goto out;
  445. ret = 1;
  446. out:
  447. EVP_MD_CTX_free(md_ctx);
  448. EVP_PKEY_free(pkey);
  449. return ret;
  450. }
  451. static int test_d2i_AutoPrivateKey(int i)
  452. {
  453. int ret = 0;
  454. const unsigned char *p;
  455. EVP_PKEY *pkey = NULL;
  456. const APK_DATA *ak = &keydata[i];
  457. const unsigned char *input = ak->kder;
  458. size_t input_len = ak->size;
  459. int expected_id = ak->evptype;
  460. p = input;
  461. if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len))
  462. || !TEST_ptr_eq(p, input + input_len)
  463. || !TEST_int_eq(EVP_PKEY_id(pkey), expected_id))
  464. goto done;
  465. ret = 1;
  466. done:
  467. EVP_PKEY_free(pkey);
  468. return ret;
  469. }
  470. #ifndef OPENSSL_NO_EC
  471. static const unsigned char ec_public_sect163k1_validxy[] = {
  472. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  473. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  474. 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  475. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x02, 0xd1, 0x7b,
  476. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  477. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
  478. };
  479. static const unsigned char ec_public_sect163k1_badx[] = {
  480. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  481. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  482. 0x0a, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  483. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0xb0, 0x02, 0xd1, 0x7b,
  484. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  485. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
  486. };
  487. static const unsigned char ec_public_sect163k1_bady[] = {
  488. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  489. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  490. 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  491. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x0a, 0xd1, 0x7b,
  492. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  493. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0xe6
  494. };
  495. static struct ec_der_pub_keys_st {
  496. const unsigned char *der;
  497. size_t len;
  498. int valid;
  499. } ec_der_pub_keys[] = {
  500. { ec_public_sect163k1_validxy, sizeof(ec_public_sect163k1_validxy), 1 },
  501. { ec_public_sect163k1_badx, sizeof(ec_public_sect163k1_badx), 0 },
  502. { ec_public_sect163k1_bady, sizeof(ec_public_sect163k1_bady), 0 },
  503. };
  504. /*
  505. * Tests the range of the decoded EC char2 public point.
  506. * See ec_GF2m_simple_oct2point().
  507. */
  508. static int test_invalide_ec_char2_pub_range_decode(int id)
  509. {
  510. int ret = 0;
  511. BIO *bio = NULL;
  512. EC_KEY *eckey = NULL;
  513. if (!TEST_ptr(bio = BIO_new_mem_buf(ec_der_pub_keys[id].der,
  514. ec_der_pub_keys[id].len)))
  515. goto err;
  516. eckey = d2i_EC_PUBKEY_bio(bio, NULL);
  517. ret = (ec_der_pub_keys[id].valid && TEST_ptr(eckey))
  518. || TEST_ptr_null(eckey);
  519. err:
  520. EC_KEY_free(eckey);
  521. BIO_free(bio);
  522. return ret;
  523. }
  524. /* Tests loading a bad key in PKCS8 format */
  525. static int test_EVP_PKCS82PKEY(void)
  526. {
  527. int ret = 0;
  528. const unsigned char *derp = kExampleBadECKeyDER;
  529. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  530. EVP_PKEY *pkey = NULL;
  531. if (!TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp,
  532. sizeof(kExampleBadECKeyDER))))
  533. goto done;
  534. if (!TEST_ptr_eq(derp,
  535. kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER)))
  536. goto done;
  537. if (!TEST_ptr_null(pkey = EVP_PKCS82PKEY(p8inf)))
  538. goto done;
  539. ret = 1;
  540. done:
  541. PKCS8_PRIV_KEY_INFO_free(p8inf);
  542. EVP_PKEY_free(pkey);
  543. return ret;
  544. }
  545. #endif
  546. #if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODE)
  547. static int test_EVP_SM2_verify(void)
  548. {
  549. /* From https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02#appendix-A */
  550. const char *pubkey =
  551. "-----BEGIN PUBLIC KEY-----\n"
  552. "MIIBMzCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEAhULWnkwETxjouSQ1\n"
  553. "v2/33kVyg5FcRVF9ci7biwjx38MwRAQgeHlotPoyw/0kF4Quc7v+/y88hItoMdfg\n"
  554. "7GUiizk35JgEIGPkxtOyOwyEnPhCQUhL/kj2HVmlsWugbm4S0donxSSaBEEEQh3r\n"
  555. "1hti6rZ0ZDTrw8wxXjIiCzut1QvcTE5sFH/t1D0GgFEry7QsB9RzSdIVO3DE5df9\n"
  556. "/L+jbqGoWEG55G4JogIhAIVC1p5MBE8Y6LkkNb9v990pdyBjBIVijVrnTufDLnm3\n"
  557. "AgEBA0IABArkx3mKoPEZRxvuEYJb5GICu3nipYRElel8BP9N8lSKfAJA+I8c1OFj\n"
  558. "Uqc8F7fxbwc1PlOhdtaEqf4Ma7eY6Fc=\n"
  559. "-----END PUBLIC KEY-----\n";
  560. const char *msg = "message digest";
  561. const char *id = "ALICE123@YAHOO.COM";
  562. const uint8_t signature[] = {
  563. 0x30, 0x44, 0x02, 0x20,
  564. 0x40, 0xF1, 0xEC, 0x59, 0xF7, 0x93, 0xD9, 0xF4, 0x9E, 0x09, 0xDC,
  565. 0xEF, 0x49, 0x13, 0x0D, 0x41, 0x94, 0xF7, 0x9F, 0xB1, 0xEE, 0xD2,
  566. 0xCA, 0xA5, 0x5B, 0xAC, 0xDB, 0x49, 0xC4, 0xE7, 0x55, 0xD1,
  567. 0x02, 0x20,
  568. 0x6F, 0xC6, 0xDA, 0xC3, 0x2C, 0x5D, 0x5C, 0xF1, 0x0C, 0x77, 0xDF,
  569. 0xB2, 0x0F, 0x7C, 0x2E, 0xB6, 0x67, 0xA4, 0x57, 0x87, 0x2F, 0xB0,
  570. 0x9E, 0xC5, 0x63, 0x27, 0xA6, 0x7E, 0xC7, 0xDE, 0xEB, 0xE7
  571. };
  572. int rc = 0;
  573. BIO *bio = NULL;
  574. EVP_PKEY *pkey = NULL;
  575. EVP_MD_CTX *mctx = NULL;
  576. EVP_PKEY_CTX *pctx = NULL;
  577. bio = BIO_new_mem_buf(pubkey, strlen(pubkey));
  578. if (!TEST_true(bio != NULL))
  579. goto done;
  580. pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
  581. if (!TEST_true(pkey != NULL))
  582. goto done;
  583. if (!TEST_true(EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2)))
  584. goto done;
  585. if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
  586. goto done;
  587. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new(pkey, NULL)))
  588. goto done;
  589. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, (const uint8_t *)id,
  590. strlen(id)), 0))
  591. goto done;
  592. EVP_MD_CTX_set_pkey_ctx(mctx, pctx);
  593. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, EVP_sm3(), NULL, pkey)))
  594. goto done;
  595. if (!TEST_true(EVP_DigestVerifyUpdate(mctx, msg, strlen(msg))))
  596. goto done;
  597. if (!TEST_true(EVP_DigestVerifyFinal(mctx, signature, sizeof(signature))))
  598. goto done;
  599. rc = 1;
  600. done:
  601. BIO_free(bio);
  602. EVP_PKEY_free(pkey);
  603. EVP_PKEY_CTX_free(pctx);
  604. EVP_MD_CTX_free(mctx);
  605. return rc;
  606. }
  607. static int test_EVP_SM2(void)
  608. {
  609. int ret = 0;
  610. EVP_PKEY *pkey = NULL;
  611. EVP_PKEY *params = NULL;
  612. EVP_PKEY_CTX *pctx = NULL;
  613. EVP_PKEY_CTX *kctx = NULL;
  614. EVP_PKEY_CTX *sctx = NULL;
  615. size_t sig_len = 0;
  616. unsigned char *sig = NULL;
  617. EVP_MD_CTX *md_ctx = NULL;
  618. EVP_MD_CTX *md_ctx_verify = NULL;
  619. EVP_PKEY_CTX *cctx = NULL;
  620. uint8_t ciphertext[128];
  621. size_t ctext_len = sizeof(ciphertext);
  622. uint8_t plaintext[8];
  623. size_t ptext_len = sizeof(plaintext);
  624. uint8_t sm2_id[] = {1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r'};
  625. pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
  626. if (!TEST_ptr(pctx))
  627. goto done;
  628. if (!TEST_true(EVP_PKEY_paramgen_init(pctx) == 1))
  629. goto done;
  630. if (!TEST_true(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2)))
  631. goto done;
  632. if (!TEST_true(EVP_PKEY_paramgen(pctx, &params)))
  633. goto done;
  634. kctx = EVP_PKEY_CTX_new(params, NULL);
  635. if (!TEST_ptr(kctx))
  636. goto done;
  637. if (!TEST_true(EVP_PKEY_keygen_init(kctx)))
  638. goto done;
  639. if (!TEST_true(EVP_PKEY_keygen(kctx, &pkey)))
  640. goto done;
  641. if (!TEST_true(EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2)))
  642. goto done;
  643. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
  644. goto done;
  645. if (!TEST_ptr(md_ctx_verify = EVP_MD_CTX_new()))
  646. goto done;
  647. if (!TEST_ptr(sctx = EVP_PKEY_CTX_new(pkey, NULL)))
  648. goto done;
  649. EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
  650. EVP_MD_CTX_set_pkey_ctx(md_ctx_verify, sctx);
  651. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
  652. goto done;
  653. if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, EVP_sm3(), NULL, pkey)))
  654. goto done;
  655. if(!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
  656. goto done;
  657. /* Determine the size of the signature. */
  658. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len)))
  659. goto done;
  660. if (!TEST_size_t_eq(sig_len, (size_t)EVP_PKEY_size(pkey)))
  661. goto done;
  662. if (!TEST_ptr(sig = OPENSSL_malloc(sig_len)))
  663. goto done;
  664. if (!TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
  665. goto done;
  666. /* Ensure that the signature round-trips. */
  667. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sm3(), NULL, pkey)))
  668. goto done;
  669. if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))))
  670. goto done;
  671. if (!TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))
  672. goto done;
  673. /* now check encryption/decryption */
  674. if (!TEST_ptr(cctx = EVP_PKEY_CTX_new(pkey, NULL)))
  675. goto done;
  676. if (!TEST_true(EVP_PKEY_encrypt_init(cctx)))
  677. goto done;
  678. if (!TEST_true(EVP_PKEY_encrypt(cctx, ciphertext, &ctext_len, kMsg, sizeof(kMsg))))
  679. goto done;
  680. if (!TEST_true(EVP_PKEY_decrypt_init(cctx)))
  681. goto done;
  682. if (!TEST_true(EVP_PKEY_decrypt(cctx, plaintext, &ptext_len, ciphertext, ctext_len)))
  683. goto done;
  684. if (!TEST_true(ptext_len == sizeof(kMsg)))
  685. goto done;
  686. if (!TEST_true(memcmp(plaintext, kMsg, sizeof(kMsg)) == 0))
  687. goto done;
  688. ret = 1;
  689. done:
  690. EVP_PKEY_CTX_free(pctx);
  691. EVP_PKEY_CTX_free(kctx);
  692. EVP_PKEY_CTX_free(sctx);
  693. EVP_PKEY_CTX_free(cctx);
  694. EVP_PKEY_free(pkey);
  695. EVP_PKEY_free(params);
  696. EVP_MD_CTX_free(md_ctx);
  697. EVP_MD_CTX_free(md_ctx_verify);
  698. OPENSSL_free(sig);
  699. return ret;
  700. }
  701. #endif
  702. static struct keys_st {
  703. int type;
  704. char *priv;
  705. char *pub;
  706. } keys[] = {
  707. {
  708. EVP_PKEY_HMAC, "0123456789", NULL
  709. }, {
  710. EVP_PKEY_POLY1305, "01234567890123456789012345678901", NULL
  711. }, {
  712. EVP_PKEY_SIPHASH, "0123456789012345", NULL
  713. },
  714. #ifndef OPENSSL_NO_EC
  715. {
  716. EVP_PKEY_X25519, "01234567890123456789012345678901",
  717. "abcdefghijklmnopqrstuvwxyzabcdef"
  718. }, {
  719. EVP_PKEY_ED25519, "01234567890123456789012345678901",
  720. "abcdefghijklmnopqrstuvwxyzabcdef"
  721. }, {
  722. EVP_PKEY_X448,
  723. "01234567890123456789012345678901234567890123456789012345",
  724. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd"
  725. }, {
  726. EVP_PKEY_ED448,
  727. "012345678901234567890123456789012345678901234567890123456",
  728. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde"
  729. }
  730. #endif
  731. };
  732. static int test_set_get_raw_keys_int(int tst, int pub)
  733. {
  734. int ret = 0;
  735. unsigned char buf[80];
  736. unsigned char *in;
  737. size_t inlen, len = 0;
  738. EVP_PKEY *pkey;
  739. /* Check if this algorithm supports public keys */
  740. if (keys[tst].pub == NULL)
  741. return 1;
  742. memset(buf, 0, sizeof(buf));
  743. if (pub) {
  744. inlen = strlen(keys[tst].pub);
  745. in = (unsigned char *)keys[tst].pub;
  746. pkey = EVP_PKEY_new_raw_public_key(keys[tst].type,
  747. NULL,
  748. in,
  749. inlen);
  750. } else {
  751. inlen = strlen(keys[tst].priv);
  752. in = (unsigned char *)keys[tst].priv;
  753. pkey = EVP_PKEY_new_raw_private_key(keys[tst].type,
  754. NULL,
  755. in,
  756. inlen);
  757. }
  758. if (!TEST_ptr(pkey)
  759. || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, NULL, &len)))
  760. || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, NULL, &len)))
  761. || !TEST_true(len == inlen)
  762. || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, buf, &len)))
  763. || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, buf, &len)))
  764. || !TEST_mem_eq(in, inlen, buf, len))
  765. goto done;
  766. ret = 1;
  767. done:
  768. EVP_PKEY_free(pkey);
  769. return ret;
  770. }
  771. static int test_set_get_raw_keys(int tst)
  772. {
  773. return test_set_get_raw_keys_int(tst, 0)
  774. && test_set_get_raw_keys_int(tst, 1);
  775. }
  776. static int pkey_custom_check(EVP_PKEY *pkey)
  777. {
  778. return 0xbeef;
  779. }
  780. static int pkey_custom_pub_check(EVP_PKEY *pkey)
  781. {
  782. return 0xbeef;
  783. }
  784. static int pkey_custom_param_check(EVP_PKEY *pkey)
  785. {
  786. return 0xbeef;
  787. }
  788. static EVP_PKEY_METHOD *custom_pmeth;
  789. static int test_EVP_PKEY_check(int i)
  790. {
  791. int ret = 0;
  792. const unsigned char *p;
  793. EVP_PKEY *pkey = NULL;
  794. #ifndef OPENSSL_NO_EC
  795. EC_KEY *eckey = NULL;
  796. #endif
  797. EVP_PKEY_CTX *ctx = NULL;
  798. EVP_PKEY_CTX *ctx2 = NULL;
  799. const APK_DATA *ak = &keycheckdata[i];
  800. const unsigned char *input = ak->kder;
  801. size_t input_len = ak->size;
  802. int expected_id = ak->evptype;
  803. int expected_check = ak->check;
  804. int expected_pub_check = ak->pub_check;
  805. int expected_param_check = ak->param_check;
  806. int type = ak->type;
  807. BIO *pubkey = NULL;
  808. p = input;
  809. switch (type) {
  810. case 0:
  811. if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len))
  812. || !TEST_ptr_eq(p, input + input_len)
  813. || !TEST_int_eq(EVP_PKEY_id(pkey), expected_id))
  814. goto done;
  815. break;
  816. #ifndef OPENSSL_NO_EC
  817. case 1:
  818. if (!TEST_ptr(pubkey = BIO_new_mem_buf(input, input_len))
  819. || !TEST_ptr(eckey = d2i_EC_PUBKEY_bio(pubkey, NULL))
  820. || !TEST_ptr(pkey = EVP_PKEY_new())
  821. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
  822. goto done;
  823. break;
  824. case 2:
  825. if (!TEST_ptr(eckey = d2i_ECParameters(NULL, &p, input_len))
  826. || !TEST_ptr_eq(p, input + input_len)
  827. || !TEST_ptr(pkey = EVP_PKEY_new())
  828. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
  829. goto done;
  830. break;
  831. #endif
  832. default:
  833. return 0;
  834. }
  835. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
  836. goto done;
  837. if (!TEST_int_eq(EVP_PKEY_check(ctx), expected_check))
  838. goto done;
  839. if (!TEST_int_eq(EVP_PKEY_public_check(ctx), expected_pub_check))
  840. goto done;
  841. if (!TEST_int_eq(EVP_PKEY_param_check(ctx), expected_param_check))
  842. goto done;
  843. ctx2 = EVP_PKEY_CTX_new_id(0xdefaced, NULL);
  844. /* assign the pkey directly, as an internal test */
  845. EVP_PKEY_up_ref(pkey);
  846. ctx2->pkey = pkey;
  847. if (!TEST_int_eq(EVP_PKEY_check(ctx2), 0xbeef))
  848. goto done;
  849. if (!TEST_int_eq(EVP_PKEY_public_check(ctx2), 0xbeef))
  850. goto done;
  851. if (!TEST_int_eq(EVP_PKEY_param_check(ctx2), 0xbeef))
  852. goto done;
  853. ret = 1;
  854. done:
  855. EVP_PKEY_CTX_free(ctx);
  856. EVP_PKEY_CTX_free(ctx2);
  857. EVP_PKEY_free(pkey);
  858. BIO_free(pubkey);
  859. return ret;
  860. }
  861. static int test_HKDF(void)
  862. {
  863. EVP_PKEY_CTX *pctx;
  864. unsigned char out[20];
  865. size_t outlen;
  866. int i, ret = 0;
  867. unsigned char salt[] = "0123456789";
  868. unsigned char key[] = "012345678901234567890123456789";
  869. unsigned char info[] = "infostring";
  870. const unsigned char expected[] = {
  871. 0xe5, 0x07, 0x70, 0x7f, 0xc6, 0x78, 0xd6, 0x54, 0x32, 0x5f, 0x7e, 0xc5,
  872. 0x7b, 0x59, 0x3e, 0xd8, 0x03, 0x6b, 0xed, 0xca
  873. };
  874. size_t expectedlen = sizeof(expected);
  875. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)))
  876. goto done;
  877. /* We do this twice to test reuse of the EVP_PKEY_CTX */
  878. for (i = 0; i < 2; i++) {
  879. outlen = sizeof(out);
  880. memset(out, 0, outlen);
  881. if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
  882. || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
  883. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
  884. sizeof(salt) - 1), 0)
  885. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
  886. sizeof(key) - 1), 0)
  887. || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
  888. sizeof(info) - 1), 0)
  889. || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
  890. || !TEST_mem_eq(out, outlen, expected, expectedlen))
  891. goto done;
  892. }
  893. ret = 1;
  894. done:
  895. EVP_PKEY_CTX_free(pctx);
  896. return ret;
  897. }
  898. #ifndef OPENSSL_NO_EC
  899. static int test_X509_PUBKEY_inplace(void)
  900. {
  901. int ret = 0;
  902. X509_PUBKEY *xp = NULL;
  903. const unsigned char *p = kExampleECPubKeyDER;
  904. size_t input_len = sizeof(kExampleECPubKeyDER);
  905. if (!TEST_ptr(xp = d2i_X509_PUBKEY(NULL, &p, input_len)))
  906. goto done;
  907. if (!TEST_ptr(X509_PUBKEY_get0(xp)))
  908. goto done;
  909. p = kExampleBadECPubKeyDER;
  910. input_len = sizeof(kExampleBadECPubKeyDER);
  911. if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len)))
  912. goto done;
  913. if (!TEST_true(X509_PUBKEY_get0(xp) == NULL))
  914. goto done;
  915. ret = 1;
  916. done:
  917. X509_PUBKEY_free(xp);
  918. return ret;
  919. }
  920. #endif /* OPENSSL_NO_EC */
  921. #ifndef OPENSSL_NO_DSA
  922. /* Test getting and setting parameters on an EVP_PKEY_CTX */
  923. static int test_EVP_PKEY_CTX_get_set_params(void)
  924. {
  925. EVP_MD_CTX *mdctx = NULL;
  926. EVP_PKEY_CTX *ctx = NULL;
  927. EVP_SIGNATURE *dsaimpl = NULL;
  928. const OSSL_PARAM *params;
  929. OSSL_PARAM ourparams[2], *param = ourparams;
  930. DSA *dsa = NULL;
  931. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  932. EVP_PKEY *pkey = NULL;
  933. int ret = 0;
  934. const EVP_MD *md;
  935. size_t mdsize = SHA512_DIGEST_LENGTH;
  936. char ssl3ms[48];
  937. /*
  938. * Setup the parameters for our DSA object. For our purposes they don't have
  939. * to actually be *valid* parameters. We just need to set something. We
  940. * don't even need a pub_key/priv_key.
  941. */
  942. dsa = DSA_new();
  943. p = BN_new();
  944. q = BN_new();
  945. g = BN_new();
  946. if (!TEST_ptr(dsa)
  947. || !TEST_ptr(p)
  948. || !TEST_ptr(q)
  949. || !TEST_ptr(g)
  950. || !DSA_set0_pqg(dsa, p, q, g))
  951. goto err;
  952. p = q = g = NULL;
  953. pkey = EVP_PKEY_new();
  954. if (!TEST_ptr(pkey)
  955. || !TEST_true(EVP_PKEY_assign_DSA(pkey, dsa)))
  956. goto err;
  957. dsa = NULL;
  958. /* Initialise a sign operation */
  959. ctx = EVP_PKEY_CTX_new(pkey, NULL);
  960. dsaimpl = EVP_SIGNATURE_fetch(NULL, "DSA", NULL);
  961. if (!TEST_ptr(ctx)
  962. || !TEST_ptr(dsaimpl)
  963. || !TEST_int_gt(EVP_PKEY_sign_init_ex(ctx, dsaimpl), 0))
  964. goto err;
  965. /*
  966. * We should be able to query the parameters now. The default DSA
  967. * implementation supports exactly one parameter - so we expect to see that
  968. * returned and no more.
  969. */
  970. params = EVP_PKEY_CTX_settable_params(ctx);
  971. if (!TEST_ptr(params)
  972. || !TEST_int_eq(strcmp(params[0].key,
  973. OSSL_SIGNATURE_PARAM_DIGEST_SIZE), 0)
  974. || !TEST_int_eq(strcmp(params[1].key, OSSL_SIGNATURE_PARAM_DIGEST),
  975. 0)
  976. /* The final key should be NULL */
  977. || !TEST_ptr_null(params[2].key))
  978. goto err;
  979. /* Gettable params are the same as the settable ones */
  980. params = EVP_PKEY_CTX_gettable_params(ctx);
  981. if (!TEST_ptr(params)
  982. || !TEST_int_eq(strcmp(params[0].key,
  983. OSSL_SIGNATURE_PARAM_DIGEST_SIZE), 0)
  984. || !TEST_int_eq(strcmp(params[1].key, OSSL_SIGNATURE_PARAM_DIGEST),
  985. 0)
  986. /* The final key should be NULL */
  987. || !TEST_ptr_null(params[2].key))
  988. goto err;
  989. /*
  990. * Test getting and setting params via EVP_PKEY_CTX_set_params() and
  991. * EVP_PKEY_CTX_get_params()
  992. */
  993. *param++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
  994. &mdsize);
  995. *param++ = OSSL_PARAM_construct_end();
  996. if (!TEST_true(EVP_PKEY_CTX_set_params(ctx, ourparams)))
  997. goto err;
  998. mdsize = 0;
  999. if (!TEST_true(EVP_PKEY_CTX_get_params(ctx, ourparams))
  1000. || !TEST_size_t_eq(mdsize, SHA512_DIGEST_LENGTH))
  1001. goto err;
  1002. /*
  1003. * Test the TEST_PKEY_CTX_set_signature_md() and
  1004. * TEST_PKEY_CTX_get_signature_md() functions
  1005. */
  1006. if (!TEST_int_gt(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()), 0)
  1007. || !TEST_int_gt(EVP_PKEY_CTX_get_signature_md(ctx, &md), 0)
  1008. || !TEST_ptr_eq(md, EVP_sha256()))
  1009. goto err;
  1010. /*
  1011. * Test getting MD parameters via an associated EVP_PKEY_CTX
  1012. */
  1013. mdctx = EVP_MD_CTX_new();
  1014. if (!TEST_ptr(mdctx)
  1015. || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, "SHA1", NULL,
  1016. pkey, dsaimpl)))
  1017. goto err;
  1018. /*
  1019. * We now have an EVP_MD_CTX with an EVP_PKEY_CTX inside it. We should be
  1020. * able to obtain the digest's settable parameters from the provider.
  1021. */
  1022. params = EVP_MD_CTX_settable_params(mdctx);
  1023. if (!TEST_ptr(params)
  1024. || !TEST_int_eq(strcmp(params[0].key, OSSL_DIGEST_PARAM_SSL3_MS), 0)
  1025. /* The final key should be NULL */
  1026. || !TEST_ptr_null(params[1].key))
  1027. goto err;
  1028. param = ourparams;
  1029. memset(ssl3ms, 0, sizeof(ssl3ms));
  1030. *param++ = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
  1031. ssl3ms, sizeof(ssl3ms));
  1032. *param++ = OSSL_PARAM_construct_end();
  1033. if (!TEST_true(EVP_MD_CTX_set_params(mdctx, ourparams)))
  1034. goto err;
  1035. ret = 1;
  1036. err:
  1037. EVP_MD_CTX_free(mdctx);
  1038. EVP_PKEY_CTX_free(ctx);
  1039. EVP_SIGNATURE_free(dsaimpl);
  1040. EVP_PKEY_free(pkey);
  1041. DSA_free(dsa);
  1042. BN_free(p);
  1043. BN_free(q);
  1044. BN_free(g);
  1045. return ret;
  1046. }
  1047. #endif
  1048. int setup_tests(void)
  1049. {
  1050. ADD_TEST(test_EVP_DigestSignInit);
  1051. ADD_TEST(test_EVP_DigestVerifyInit);
  1052. ADD_TEST(test_EVP_Enveloped);
  1053. ADD_ALL_TESTS(test_d2i_AutoPrivateKey, OSSL_NELEM(keydata));
  1054. #ifndef OPENSSL_NO_EC
  1055. ADD_TEST(test_EVP_PKCS82PKEY);
  1056. #endif
  1057. #if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODE)
  1058. ADD_TEST(test_EVP_SM2);
  1059. ADD_TEST(test_EVP_SM2_verify);
  1060. #endif
  1061. ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys));
  1062. custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0);
  1063. if (!TEST_ptr(custom_pmeth))
  1064. return 0;
  1065. EVP_PKEY_meth_set_check(custom_pmeth, pkey_custom_check);
  1066. EVP_PKEY_meth_set_public_check(custom_pmeth, pkey_custom_pub_check);
  1067. EVP_PKEY_meth_set_param_check(custom_pmeth, pkey_custom_param_check);
  1068. if (!TEST_int_eq(EVP_PKEY_meth_add0(custom_pmeth), 1))
  1069. return 0;
  1070. ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata));
  1071. ADD_TEST(test_HKDF);
  1072. #ifndef OPENSSL_NO_EC
  1073. ADD_TEST(test_X509_PUBKEY_inplace);
  1074. ADD_ALL_TESTS(test_invalide_ec_char2_pub_range_decode,
  1075. OSSL_NELEM(ec_der_pub_keys));
  1076. #endif
  1077. #ifndef OPENSSL_NO_DSA
  1078. ADD_TEST(test_EVP_PKEY_CTX_get_set_params);
  1079. #endif
  1080. return 1;
  1081. }