evp_extra_test2.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. /*
  2. * Copyright 2015-2023 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. /* We need to use some deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. /*
  12. * Really these tests should be in evp_extra_test - but that doesn't
  13. * yet support testing with a non-default libctx. Once it does we should move
  14. * everything into one file. Consequently some things are duplicated between
  15. * the two files.
  16. */
  17. #include <openssl/evp.h>
  18. #include <openssl/pem.h>
  19. #include <openssl/provider.h>
  20. #include <openssl/rsa.h>
  21. #include <openssl/dh.h>
  22. #include <openssl/core_names.h>
  23. #include <openssl/ui.h>
  24. #include "testutil.h"
  25. #include "internal/nelem.h"
  26. static OSSL_LIB_CTX *mainctx = NULL;
  27. static OSSL_PROVIDER *nullprov = NULL;
  28. /*
  29. * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
  30. * should never use this key anywhere but in an example.
  31. */
  32. static const unsigned char kExampleRSAKeyDER[] = {
  33. 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
  34. 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
  35. 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
  36. 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
  37. 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
  38. 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
  39. 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
  40. 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
  41. 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
  42. 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
  43. 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
  44. 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
  45. 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
  46. 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
  47. 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
  48. 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
  49. 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
  50. 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
  51. 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
  52. 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
  53. 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
  54. 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
  55. 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
  56. 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
  57. 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
  58. 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
  59. 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
  60. 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
  61. 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
  62. 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
  63. 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
  64. 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
  65. 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
  66. 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
  67. 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
  68. 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
  69. 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
  70. 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
  71. 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
  72. 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
  73. 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
  74. 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
  75. 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
  76. 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
  77. 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
  78. 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
  79. 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
  80. 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
  81. 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
  82. 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
  83. 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  84. };
  85. /*
  86. * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
  87. * PrivateKeyInfo.
  88. */
  89. static const unsigned char kExampleRSAKeyPKCS8[] = {
  90. 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  91. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
  92. 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
  93. 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
  94. 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
  95. 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
  96. 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
  97. 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
  98. 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
  99. 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
  100. 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
  101. 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
  102. 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
  103. 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
  104. 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
  105. 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
  106. 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
  107. 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
  108. 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
  109. 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
  110. 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
  111. 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
  112. 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
  113. 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
  114. 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
  115. 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
  116. 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
  117. 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
  118. 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
  119. 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
  120. 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
  121. 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
  122. 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
  123. 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
  124. 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
  125. 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
  126. 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
  127. 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
  128. 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
  129. 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
  130. 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
  131. 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
  132. 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
  133. 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
  134. 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
  135. 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
  136. 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
  137. 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
  138. 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
  139. 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
  140. 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
  141. 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
  142. 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  143. };
  144. #ifndef OPENSSL_NO_DH
  145. static const unsigned char kExampleDHPrivateKeyDER[] = {
  146. 0x30, 0x82, 0x02, 0x26, 0x02, 0x01, 0x00, 0x30, 0x82, 0x01, 0x17, 0x06,
  147. 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x03, 0x01, 0x30, 0x82,
  148. 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xD8, 0x4B, 0x0F, 0x0E, 0x6B,
  149. 0x79, 0xE9, 0x23, 0x4E, 0xE4, 0xBE, 0x9A, 0x8F, 0x7A, 0x5C, 0xA3, 0x20,
  150. 0xD0, 0x86, 0x6B, 0x95, 0x78, 0x39, 0x59, 0x7A, 0x11, 0x2A, 0x5B, 0x87,
  151. 0xA4, 0xFB, 0x2F, 0x99, 0xD0, 0x57, 0xF5, 0xE1, 0xA3, 0xAF, 0x41, 0xD1,
  152. 0xCD, 0xA3, 0x94, 0xBB, 0xE5, 0x5A, 0x68, 0xE2, 0xEE, 0x69, 0x56, 0x51,
  153. 0xB2, 0xEE, 0xF2, 0xFE, 0x10, 0xC9, 0x55, 0xE3, 0x82, 0x3C, 0x50, 0x0D,
  154. 0xF5, 0x82, 0x73, 0xE4, 0xD6, 0x3E, 0x45, 0xB4, 0x89, 0x80, 0xE4, 0xF0,
  155. 0x99, 0x85, 0x2B, 0x4B, 0xF9, 0xB8, 0xFD, 0x2C, 0x3C, 0x49, 0x2E, 0xB3,
  156. 0x56, 0x7E, 0x99, 0x07, 0xD3, 0xF7, 0xD9, 0xE4, 0x0C, 0x64, 0xC5, 0x7D,
  157. 0x03, 0x8E, 0x05, 0x3C, 0x0A, 0x40, 0x17, 0xAD, 0xA8, 0x0F, 0x9B, 0xF4,
  158. 0x8B, 0xA7, 0xDB, 0x16, 0x4F, 0x4A, 0x57, 0x0B, 0x89, 0x80, 0x0B, 0x9F,
  159. 0x26, 0x56, 0x3F, 0x1D, 0xFA, 0x52, 0x2D, 0x1A, 0x9E, 0xDC, 0x42, 0xA3,
  160. 0x2E, 0xA9, 0x87, 0xE3, 0x8B, 0x45, 0x5E, 0xEE, 0x99, 0xB8, 0x30, 0x15,
  161. 0x58, 0xA3, 0x5F, 0xB5, 0x69, 0xD8, 0x0C, 0xE8, 0x6B, 0x36, 0xD8, 0xAB,
  162. 0xD8, 0xE4, 0x77, 0x46, 0x13, 0xA2, 0x15, 0xB3, 0x9C, 0xAD, 0x99, 0x91,
  163. 0xE5, 0xA3, 0x30, 0x7D, 0x40, 0x70, 0xB3, 0x32, 0x5E, 0xAF, 0x96, 0x8D,
  164. 0xE6, 0x3F, 0x47, 0xA3, 0x18, 0xDA, 0xE1, 0x9A, 0x20, 0x11, 0xE1, 0x49,
  165. 0x51, 0x45, 0xE3, 0x8C, 0xA5, 0x56, 0x39, 0x67, 0xCB, 0x9D, 0xCF, 0xBA,
  166. 0xF4, 0x46, 0x4E, 0x0A, 0xB6, 0x0B, 0xA9, 0xB4, 0xF6, 0xF1, 0x6A, 0xC8,
  167. 0x63, 0xE2, 0xB4, 0xB2, 0x9F, 0x44, 0xAA, 0x0A, 0xDA, 0x53, 0xF7, 0x52,
  168. 0x14, 0x57, 0xEE, 0x2C, 0x5D, 0x31, 0x9C, 0x27, 0x03, 0x64, 0x9E, 0xC0,
  169. 0x1E, 0x4B, 0x1B, 0x4F, 0xEE, 0xA6, 0x3F, 0xC1, 0x3E, 0x61, 0x93, 0x02,
  170. 0x01, 0x02, 0x04, 0x82, 0x01, 0x04, 0x02, 0x82, 0x01, 0x00, 0x7E, 0xC2,
  171. 0x04, 0xF9, 0x95, 0xC7, 0xEF, 0x96, 0xBE, 0xA0, 0x9D, 0x2D, 0xC3, 0x0C,
  172. 0x3A, 0x67, 0x02, 0x7C, 0x7D, 0x3B, 0xC9, 0xB1, 0xDE, 0x13, 0x97, 0x64,
  173. 0xEF, 0x87, 0x80, 0x4F, 0xBF, 0xA2, 0xAC, 0x18, 0x6B, 0xD5, 0xB2, 0x42,
  174. 0x0F, 0xDA, 0x28, 0x40, 0x93, 0x40, 0xB2, 0x1E, 0x80, 0xB0, 0x6C, 0xDE,
  175. 0x9C, 0x54, 0xA4, 0xB4, 0x68, 0x29, 0xE0, 0x13, 0x57, 0x1D, 0xC9, 0x87,
  176. 0xC0, 0xDE, 0x2F, 0x1D, 0x72, 0xF0, 0xC0, 0xE4, 0x4E, 0x04, 0x48, 0xF5,
  177. 0x2D, 0x8D, 0x9A, 0x1B, 0xE5, 0xEB, 0x06, 0xAB, 0x7C, 0x74, 0x10, 0x3C,
  178. 0xA8, 0x2D, 0x39, 0xBC, 0xE3, 0x15, 0x3E, 0x63, 0x37, 0x8C, 0x1B, 0xF1,
  179. 0xB3, 0x99, 0xB6, 0xAE, 0x5A, 0xEB, 0xB3, 0x3D, 0x30, 0x39, 0x69, 0xDB,
  180. 0xF2, 0x4F, 0x94, 0xB7, 0x71, 0xAF, 0xBA, 0x5C, 0x1F, 0xF8, 0x6B, 0xE5,
  181. 0xD1, 0xB1, 0x00, 0x81, 0xE2, 0x6D, 0xEC, 0x65, 0xF7, 0x7E, 0xCE, 0x03,
  182. 0x84, 0x68, 0x42, 0x6A, 0x8B, 0x47, 0x8E, 0x4A, 0x88, 0xDE, 0x82, 0xDD,
  183. 0xAF, 0xA9, 0x6F, 0x18, 0xF7, 0xC6, 0xE2, 0xB9, 0x97, 0xCE, 0x47, 0x8F,
  184. 0x85, 0x19, 0x61, 0x42, 0x67, 0x21, 0x7D, 0x13, 0x6E, 0xB5, 0x5A, 0x62,
  185. 0xF3, 0x08, 0xE2, 0x70, 0x3B, 0x0E, 0x85, 0x3C, 0xA1, 0xD3, 0xED, 0x7A,
  186. 0x43, 0xD6, 0xDE, 0x30, 0x5C, 0x48, 0xB2, 0x99, 0xAB, 0x3E, 0x65, 0xA6,
  187. 0x66, 0x80, 0x22, 0xFF, 0x92, 0xC1, 0x42, 0x1C, 0x30, 0x87, 0x74, 0x1E,
  188. 0x53, 0x57, 0x7C, 0xF8, 0x77, 0x51, 0xF1, 0x74, 0x16, 0xF4, 0x45, 0x26,
  189. 0x77, 0x0A, 0x05, 0x96, 0x13, 0x12, 0x06, 0x86, 0x2B, 0xB8, 0x49, 0x82,
  190. 0x69, 0x43, 0x0A, 0x57, 0xA7, 0x30, 0x19, 0x4C, 0xB8, 0x47, 0x82, 0x6E,
  191. 0x64, 0x7A, 0x06, 0x13, 0x5A, 0x82, 0x98, 0xD6, 0x7A, 0x09, 0xEC, 0x03,
  192. 0x8D, 0x03
  193. };
  194. #endif /* OPENSSL_NO_DH */
  195. #ifndef OPENSSL_NO_EC
  196. /*
  197. * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
  198. * structure.
  199. */
  200. static const unsigned char kExampleECKeyDER[] = {
  201. 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
  202. 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
  203. 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
  204. 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  205. 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
  206. 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
  207. 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
  208. 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
  209. 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
  210. 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
  211. 0xc1,
  212. };
  213. /* P-384 sample EC private key in PKCS8 format (no public key) */
  214. static const unsigned char kExampleECKey2DER[] = {
  215. 0x30, 0x4E, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2A, 0x86, 0x48,
  216. 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22, 0x04,
  217. 0x37, 0x30, 0x35, 0x02, 0x01, 0x01, 0x04, 0x30, 0x73, 0xE3, 0x3A, 0x05,
  218. 0xF2, 0xB6, 0x99, 0x6D, 0x0C, 0x33, 0x7F, 0x15, 0x9E, 0x10, 0xA9, 0x17,
  219. 0x4C, 0x0A, 0x82, 0x57, 0x71, 0x13, 0x7A, 0xAC, 0x46, 0xA2, 0x5E, 0x1C,
  220. 0xE0, 0xC7, 0xB2, 0xF8, 0x20, 0x40, 0xC2, 0x27, 0xC8, 0xBE, 0x02, 0x7E,
  221. 0x96, 0x69, 0xE0, 0x04, 0xCB, 0x89, 0x0B, 0x42
  222. };
  223. # ifndef OPENSSL_NO_ECX
  224. static const unsigned char kExampleECXKey2DER[] = {
  225. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e,
  226. 0x04, 0x22, 0x04, 0x20, 0xc8, 0xa9, 0xd5, 0xa9, 0x10, 0x91, 0xad, 0x85,
  227. 0x1c, 0x66, 0x8b, 0x07, 0x36, 0xc1, 0xc9, 0xa0, 0x29, 0x36, 0xc0, 0xd3,
  228. 0xad, 0x62, 0x67, 0x08, 0x58, 0x08, 0x80, 0x47, 0xba, 0x05, 0x74, 0x75
  229. };
  230. # endif
  231. #endif
  232. typedef struct APK_DATA_st {
  233. const unsigned char *kder;
  234. size_t size;
  235. int evptype;
  236. } APK_DATA;
  237. static APK_DATA keydata[] = {
  238. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA},
  239. {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA},
  240. #ifndef OPENSSL_NO_EC
  241. # ifndef OPENSSL_NO_ECX
  242. {kExampleECXKey2DER, sizeof(kExampleECXKey2DER), EVP_PKEY_X25519},
  243. # endif
  244. {kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC},
  245. {kExampleECKey2DER, sizeof(kExampleECKey2DER), EVP_PKEY_EC},
  246. #endif
  247. #ifndef OPENSSL_NO_DH
  248. {kExampleDHPrivateKeyDER, sizeof(kExampleDHPrivateKeyDER), EVP_PKEY_DH},
  249. #endif
  250. };
  251. static int pkey_has_private(EVP_PKEY *key, const char *privtag,
  252. int use_octstring)
  253. {
  254. int ret = 0;
  255. if (use_octstring) {
  256. unsigned char buf[64];
  257. ret = EVP_PKEY_get_octet_string_param(key, privtag, buf, sizeof(buf),
  258. NULL);
  259. } else {
  260. BIGNUM *bn = NULL;
  261. ret = EVP_PKEY_get_bn_param(key, privtag, &bn);
  262. BN_free(bn);
  263. }
  264. return ret;
  265. }
  266. static int do_pkey_tofrom_data_select(EVP_PKEY *key, const char *keytype)
  267. {
  268. int ret = 0;
  269. OSSL_PARAM *pub_params = NULL, *keypair_params = NULL;
  270. EVP_PKEY *fromkey = NULL, *fromkeypair = NULL;
  271. EVP_PKEY_CTX *fromctx = NULL;
  272. const char *privtag = strcmp(keytype, "RSA") == 0 ? "d" : "priv";
  273. const int use_octstring = strcmp(keytype, "X25519") == 0;
  274. /*
  275. * Select only the public key component when using EVP_PKEY_todata() and
  276. * check that the resulting param array does not contain a private key.
  277. */
  278. if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_PUBLIC_KEY, &pub_params), 1)
  279. || !TEST_ptr_null(OSSL_PARAM_locate(pub_params, privtag)))
  280. goto end;
  281. /*
  282. * Select the keypair when using EVP_PKEY_todata() and check that
  283. * the param array contains a private key.
  284. */
  285. if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_KEYPAIR, &keypair_params), 1)
  286. || !TEST_ptr(OSSL_PARAM_locate(keypair_params, privtag)))
  287. goto end;
  288. /*
  289. * Select only the public key when using EVP_PKEY_fromdata() and check that
  290. * the resulting key does not contain a private key.
  291. */
  292. if (!TEST_ptr(fromctx = EVP_PKEY_CTX_new_from_name(mainctx, keytype, NULL))
  293. || !TEST_int_eq(EVP_PKEY_fromdata_init(fromctx), 1)
  294. || !TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkey, EVP_PKEY_PUBLIC_KEY,
  295. keypair_params), 1)
  296. || !TEST_false(pkey_has_private(fromkey, privtag, use_octstring)))
  297. goto end;
  298. /*
  299. * Select the keypair when using EVP_PKEY_fromdata() and check that
  300. * the resulting key contains a private key.
  301. */
  302. if (!TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkeypair,
  303. EVP_PKEY_KEYPAIR, keypair_params), 1)
  304. || !TEST_true(pkey_has_private(fromkeypair, privtag, use_octstring)))
  305. goto end;
  306. ret = 1;
  307. end:
  308. EVP_PKEY_free(fromkeypair);
  309. EVP_PKEY_free(fromkey);
  310. EVP_PKEY_CTX_free(fromctx);
  311. OSSL_PARAM_free(keypair_params);
  312. OSSL_PARAM_free(pub_params);
  313. return ret;
  314. }
  315. #ifndef OPENSSL_NO_DH
  316. static int test_dh_tofrom_data_select(void)
  317. {
  318. int ret;
  319. OSSL_PARAM params[2];
  320. EVP_PKEY *key = NULL;
  321. EVP_PKEY_CTX *gctx = NULL;
  322. # ifndef OPENSSL_NO_DEPRECATED_3_0
  323. const DH *dhkey;
  324. const BIGNUM *privkey;
  325. # endif
  326. params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0);
  327. params[1] = OSSL_PARAM_construct_end();
  328. ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DHX", NULL))
  329. && TEST_int_gt(EVP_PKEY_keygen_init(gctx), 0)
  330. && TEST_true(EVP_PKEY_CTX_set_params(gctx, params))
  331. && TEST_int_gt(EVP_PKEY_generate(gctx, &key), 0)
  332. && TEST_true(do_pkey_tofrom_data_select(key, "DHX"));
  333. # ifndef OPENSSL_NO_DEPRECATED_3_0
  334. ret = ret && TEST_ptr(dhkey = EVP_PKEY_get0_DH(key))
  335. && TEST_ptr(privkey = DH_get0_priv_key(dhkey))
  336. && TEST_int_le(BN_num_bits(privkey), 225);
  337. # endif
  338. EVP_PKEY_free(key);
  339. EVP_PKEY_CTX_free(gctx);
  340. return ret;
  341. }
  342. static int test_dh_paramgen(void)
  343. {
  344. int ret;
  345. OSSL_PARAM params[3];
  346. EVP_PKEY *pkey = NULL;
  347. EVP_PKEY_CTX *gctx = NULL;
  348. unsigned int pbits = 512; /* minimum allowed for speed */
  349. params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
  350. params[1] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
  351. "generator", 0);
  352. params[2] = OSSL_PARAM_construct_end();
  353. ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL))
  354. && TEST_int_gt(EVP_PKEY_paramgen_init(gctx), 0)
  355. && TEST_true(EVP_PKEY_CTX_set_params(gctx, params))
  356. && TEST_true(EVP_PKEY_paramgen(gctx, &pkey))
  357. && TEST_ptr(pkey);
  358. EVP_PKEY_CTX_free(gctx);
  359. gctx = NULL;
  360. ret = ret && TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
  361. && TEST_int_eq(EVP_PKEY_param_check(gctx), 1)
  362. && TEST_int_eq(EVP_PKEY_param_check_quick(gctx), 1);
  363. EVP_PKEY_CTX_free(gctx);
  364. EVP_PKEY_free(pkey);
  365. return ret;
  366. }
  367. static int set_fromdata_string(EVP_PKEY_CTX *ctx, const char *name, char *value)
  368. {
  369. int ret;
  370. OSSL_PARAM params[2];
  371. EVP_PKEY *pkey = NULL;
  372. if (EVP_PKEY_fromdata_init(ctx) != 1)
  373. return -1;
  374. params[0] = OSSL_PARAM_construct_utf8_string(name, value, 0);
  375. params[1] = OSSL_PARAM_construct_end();
  376. ret = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params);
  377. EVP_PKEY_free(pkey);
  378. return ret;
  379. }
  380. static int set_fromdata_uint(EVP_PKEY_CTX *ctx, const char *name)
  381. {
  382. int ret;
  383. unsigned int tmp = 0;
  384. OSSL_PARAM params[2];
  385. EVP_PKEY *pkey = NULL;
  386. if (EVP_PKEY_fromdata_init(ctx) != 1)
  387. return -1;
  388. params[0] = OSSL_PARAM_construct_uint(name, &tmp);
  389. params[1] = OSSL_PARAM_construct_end();
  390. ret = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params);
  391. EVP_PKEY_free(pkey);
  392. return ret;
  393. }
  394. static int test_dh_paramfromdata(void)
  395. {
  396. EVP_PKEY_CTX *ctx = NULL;
  397. int ret = 0;
  398. /* Test failure paths for FFC - mainly due to setting the wrong param type */
  399. ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL))
  400. && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_GROUP_NAME), 0)
  401. && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_GROUP_NAME, "bad"), 0)
  402. && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_P, "bad"), 0)
  403. && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_GINDEX, "bad"), 0)
  404. && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_PCOUNTER, "bad"), 0)
  405. && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_COFACTOR, "bad"), 0)
  406. && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_H, "bad"), 0)
  407. && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_FFC_SEED), 0)
  408. && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, "bad"), 0)
  409. && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_G, "bad"), 0)
  410. && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, "bad"), 0)
  411. && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_FFC_DIGEST), 0);
  412. EVP_PKEY_CTX_free(ctx);
  413. return ret;
  414. }
  415. #endif
  416. #ifndef OPENSSL_NO_EC
  417. static int test_ec_d2i_i2d_pubkey(void)
  418. {
  419. int ret = 0;
  420. FILE *fp = NULL;
  421. EVP_PKEY *key = NULL, *outkey = NULL;
  422. static const char *filename = "pubkey.der";
  423. if (!TEST_ptr(fp = fopen(filename, "wb"))
  424. || !TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "EC", "P-256"))
  425. || !TEST_true(i2d_PUBKEY_fp(fp, key))
  426. || !TEST_int_eq(fclose(fp), 0))
  427. goto err;
  428. fp = NULL;
  429. if (!TEST_ptr(fp = fopen(filename, "rb"))
  430. || !TEST_ptr(outkey = d2i_PUBKEY_ex_fp(fp, NULL, mainctx, NULL))
  431. || !TEST_int_eq(EVP_PKEY_eq(key, outkey), 1))
  432. goto err;
  433. ret = 1;
  434. err:
  435. EVP_PKEY_free(outkey);
  436. EVP_PKEY_free(key);
  437. fclose(fp);
  438. return ret;
  439. }
  440. static int test_ec_tofrom_data_select(void)
  441. {
  442. int ret;
  443. EVP_PKEY *key = NULL;
  444. ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "EC", "P-256"))
  445. && TEST_true(do_pkey_tofrom_data_select(key, "EC"));
  446. EVP_PKEY_free(key);
  447. return ret;
  448. }
  449. # ifndef OPENSSL_NO_ECX
  450. static int test_ecx_tofrom_data_select(void)
  451. {
  452. int ret;
  453. EVP_PKEY *key = NULL;
  454. ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "X25519"))
  455. && TEST_true(do_pkey_tofrom_data_select(key, "X25519"));
  456. EVP_PKEY_free(key);
  457. return ret;
  458. }
  459. # endif
  460. #endif
  461. #ifndef OPENSSL_NO_SM2
  462. static int test_sm2_tofrom_data_select(void)
  463. {
  464. int ret;
  465. EVP_PKEY *key = NULL;
  466. ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "SM2"))
  467. && TEST_true(do_pkey_tofrom_data_select(key, "SM2"));
  468. EVP_PKEY_free(key);
  469. return ret;
  470. }
  471. #endif
  472. static int test_rsa_tofrom_data_select(void)
  473. {
  474. int ret;
  475. EVP_PKEY *key = NULL;
  476. const unsigned char *pdata = kExampleRSAKeyDER;
  477. int pdata_len = sizeof(kExampleRSAKeyDER);
  478. ret = TEST_ptr(key = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len,
  479. mainctx, NULL))
  480. && TEST_true(do_pkey_tofrom_data_select(key, "RSA"));
  481. EVP_PKEY_free(key);
  482. return ret;
  483. }
  484. /* This is the equivalent of test_d2i_AutoPrivateKey in evp_extra_test */
  485. static int test_d2i_AutoPrivateKey_ex(int i)
  486. {
  487. int ret = 0;
  488. const unsigned char *p;
  489. EVP_PKEY *pkey = NULL;
  490. const APK_DATA *ak = &keydata[i];
  491. const unsigned char *input = ak->kder;
  492. size_t input_len = ak->size;
  493. int expected_id = ak->evptype;
  494. BIGNUM *p_bn = NULL;
  495. BIGNUM *g_bn = NULL;
  496. BIGNUM *priv_bn = NULL;
  497. p = input;
  498. if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &p, input_len, mainctx,
  499. NULL))
  500. || !TEST_ptr_eq(p, input + input_len)
  501. || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
  502. goto done;
  503. if (ak->evptype == EVP_PKEY_RSA) {
  504. if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_D,
  505. &priv_bn)))
  506. goto done;
  507. } else if (ak->evptype == EVP_PKEY_X25519) {
  508. unsigned char buffer[32];
  509. size_t len;
  510. if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey,
  511. OSSL_PKEY_PARAM_PRIV_KEY,
  512. buffer, sizeof(buffer),
  513. &len)))
  514. goto done;
  515. } else {
  516. if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY,
  517. &priv_bn)))
  518. goto done;
  519. }
  520. if (ak->evptype == EVP_PKEY_DH) {
  521. if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_P, &p_bn))
  522. || !TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_G,
  523. &g_bn)))
  524. goto done;
  525. }
  526. ret = 1;
  527. done:
  528. BN_free(p_bn);
  529. BN_free(g_bn);
  530. BN_free(priv_bn);
  531. EVP_PKEY_free(pkey);
  532. return ret;
  533. }
  534. #ifndef OPENSSL_NO_DES
  535. static int test_pkcs8key_nid_bio(void)
  536. {
  537. int ret;
  538. const int nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  539. static const char pwd[] = "PASSWORD";
  540. EVP_PKEY *pkey = NULL, *pkey_dec = NULL;
  541. BIO *in = NULL, *enc_bio = NULL;
  542. char *enc_data = NULL;
  543. long enc_datalen = 0;
  544. OSSL_PROVIDER *provider = NULL;
  545. ret = TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))
  546. && TEST_ptr(enc_bio = BIO_new(BIO_s_mem()))
  547. && TEST_ptr(in = BIO_new_mem_buf(kExampleRSAKeyPKCS8,
  548. sizeof(kExampleRSAKeyPKCS8)))
  549. && TEST_ptr(pkey = d2i_PrivateKey_ex_bio(in, NULL, NULL, NULL))
  550. && TEST_int_eq(i2d_PKCS8PrivateKey_nid_bio(enc_bio, pkey, nid,
  551. pwd, sizeof(pwd) - 1,
  552. NULL, NULL), 1)
  553. && TEST_int_gt(enc_datalen = BIO_get_mem_data(enc_bio, &enc_data), 0)
  554. && TEST_ptr(pkey_dec = d2i_PKCS8PrivateKey_bio(enc_bio, NULL, NULL,
  555. (void *)pwd))
  556. && TEST_true(EVP_PKEY_eq(pkey, pkey_dec));
  557. EVP_PKEY_free(pkey_dec);
  558. EVP_PKEY_free(pkey);
  559. BIO_free(in);
  560. BIO_free(enc_bio);
  561. OSSL_PROVIDER_unload(provider);
  562. return ret;
  563. }
  564. #endif /* OPENSSL_NO_DES */
  565. static int test_alternative_default(void)
  566. {
  567. OSSL_LIB_CTX *oldctx;
  568. EVP_MD *sha256;
  569. int ok = 0;
  570. /*
  571. * setup_tests() loaded the "null" provider in the current default, so
  572. * we know this fetch should fail.
  573. */
  574. if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
  575. goto err;
  576. /*
  577. * Now we switch to our main library context, and try again. Since no
  578. * providers are loaded in this one, it should fall back to the default.
  579. */
  580. if (!TEST_ptr(oldctx = OSSL_LIB_CTX_set0_default(mainctx))
  581. || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
  582. goto err;
  583. EVP_MD_free(sha256);
  584. sha256 = NULL;
  585. /*
  586. * Switching back should give us our main library context back, and
  587. * fetching SHA2-256 should fail again.
  588. */
  589. if (!TEST_ptr_eq(OSSL_LIB_CTX_set0_default(oldctx), mainctx)
  590. || !TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
  591. goto err;
  592. ok = 1;
  593. err:
  594. EVP_MD_free(sha256);
  595. return ok;
  596. }
  597. static int test_provider_unload_effective(int testid)
  598. {
  599. EVP_MD *sha256 = NULL;
  600. OSSL_PROVIDER *provider = NULL;
  601. int ok = 0;
  602. if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))
  603. || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
  604. goto err;
  605. if (testid > 0) {
  606. OSSL_PROVIDER_unload(provider);
  607. provider = NULL;
  608. EVP_MD_free(sha256);
  609. sha256 = NULL;
  610. } else {
  611. EVP_MD_free(sha256);
  612. sha256 = NULL;
  613. OSSL_PROVIDER_unload(provider);
  614. provider = NULL;
  615. }
  616. /*
  617. * setup_tests() loaded the "null" provider in the current default, and
  618. * we unloaded it above after the load so we know this fetch should fail.
  619. */
  620. if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
  621. goto err;
  622. ok = 1;
  623. err:
  624. EVP_MD_free(sha256);
  625. OSSL_PROVIDER_unload(provider);
  626. return ok;
  627. }
  628. static int test_d2i_PrivateKey_ex(int testid)
  629. {
  630. int ok = 0;
  631. OSSL_PROVIDER *provider = NULL;
  632. BIO *key_bio = NULL;
  633. EVP_PKEY *pkey = NULL;
  634. int id = (testid == 0) ? 0 : 2;
  635. if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
  636. goto err;
  637. if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[id].kder, keydata[id].size)))
  638. goto err;
  639. if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
  640. goto err;
  641. ERR_clear_error();
  642. if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
  643. goto err;
  644. ok = TEST_ptr(pkey = d2i_PrivateKey_bio(key_bio, NULL));
  645. TEST_int_eq(ERR_peek_error(), 0);
  646. test_openssl_errors();
  647. err:
  648. EVP_PKEY_free(pkey);
  649. BIO_free(key_bio);
  650. OSSL_PROVIDER_unload(provider);
  651. return ok;
  652. }
  653. static int test_PEM_read_bio_negative(int testid)
  654. {
  655. int ok = 0;
  656. OSSL_PROVIDER *provider = NULL;
  657. BIO *key_bio = NULL;
  658. EVP_PKEY *pkey = NULL;
  659. if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[testid].kder, keydata[testid].size)))
  660. goto err;
  661. ERR_clear_error();
  662. if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
  663. goto err;
  664. if (!TEST_int_ne(ERR_peek_error(), 0))
  665. goto err;
  666. if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
  667. goto err;
  668. if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
  669. goto err;
  670. ERR_clear_error();
  671. if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
  672. goto err;
  673. if (!TEST_int_ne(ERR_peek_error(), 0))
  674. goto err;
  675. ok = 1;
  676. err:
  677. test_openssl_errors();
  678. EVP_PKEY_free(pkey);
  679. BIO_free(key_bio);
  680. OSSL_PROVIDER_unload(provider);
  681. return ok;
  682. }
  683. static int test_PEM_read_bio_negative_wrong_password(int testid)
  684. {
  685. int ok = 0;
  686. OSSL_PROVIDER *provider = OSSL_PROVIDER_load(NULL, "default");
  687. EVP_PKEY *read_pkey = NULL;
  688. EVP_PKEY *write_pkey = EVP_RSA_gen(1024);
  689. BIO *key_bio = BIO_new(BIO_s_mem());
  690. const UI_METHOD *undo_ui_method = NULL;
  691. const UI_METHOD *ui_method = NULL;
  692. if (testid > 0)
  693. ui_method = UI_null();
  694. if (!TEST_ptr(provider))
  695. goto err;
  696. if (!TEST_ptr(key_bio))
  697. goto err;
  698. if (!TEST_ptr(write_pkey))
  699. goto err;
  700. undo_ui_method = UI_get_default_method();
  701. UI_set_default_method(ui_method);
  702. if (/* Output Encrypted private key in PEM form */
  703. !TEST_true(PEM_write_bio_PrivateKey(key_bio, write_pkey, EVP_aes_256_cbc(),
  704. NULL, 0, NULL, "pass")))
  705. goto err;
  706. ERR_clear_error();
  707. read_pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL);
  708. if (!TEST_ptr_null(read_pkey))
  709. goto err;
  710. if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), PEM_R_PROBLEMS_GETTING_PASSWORD))
  711. goto err;
  712. ok = 1;
  713. err:
  714. test_openssl_errors();
  715. EVP_PKEY_free(read_pkey);
  716. EVP_PKEY_free(write_pkey);
  717. BIO_free(key_bio);
  718. OSSL_PROVIDER_unload(provider);
  719. UI_set_default_method(undo_ui_method);
  720. return ok;
  721. }
  722. static int do_fromdata_key_is_equal(const OSSL_PARAM params[],
  723. const EVP_PKEY *expected, const char *type)
  724. {
  725. EVP_PKEY_CTX *ctx = NULL;
  726. EVP_PKEY *pkey = NULL;
  727. int ret;
  728. ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, type, NULL))
  729. && TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  730. && TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey,
  731. EVP_PKEY_KEYPAIR,
  732. (OSSL_PARAM *)params), 1)
  733. && TEST_true(EVP_PKEY_eq(pkey, expected));
  734. EVP_PKEY_CTX_free(ctx);
  735. EVP_PKEY_free(pkey);
  736. return ret;
  737. }
  738. #ifndef OPENSSL_NO_DSA
  739. /*
  740. * This data was generated using:
  741. * > openssl genpkey \
  742. * -genparam -algorithm DSA -pkeyopt type:fips186_4 -text \
  743. * -pkeyopt gindex:5 -out dsa_param.pem
  744. * > openssl genpkey \
  745. * -paramfile dsa_param.pem -pkeyopt type:fips186_4 -out dsa_priv.pem
  746. */
  747. static const unsigned char dsa_key[] = {
  748. 0x30, 0x82, 0x03, 0x4e, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
  749. 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4, 0x61, 0xa0, 0x6f, 0x66,
  750. 0x17, 0xda, 0xeb, 0x90, 0xf0, 0xa3, 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21,
  751. 0x3d, 0x85, 0xa3, 0x4a, 0xf0, 0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37,
  752. 0x70, 0x06, 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1, 0xf6, 0xc4, 0x42, 0x23,
  753. 0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0, 0xc6, 0xe8,
  754. 0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e, 0x05, 0x68, 0x6d, 0x8b,
  755. 0xa8, 0xad, 0xa1, 0xc2, 0x3a, 0x4e, 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21,
  756. 0x55, 0x22, 0xce, 0xa2, 0xe9, 0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03,
  757. 0x59, 0xa0, 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a, 0x07, 0x80, 0xfc, 0x4e,
  758. 0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b, 0x22, 0x55,
  759. 0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a, 0x38, 0x2b, 0x17, 0xc2,
  760. 0x62, 0x12, 0x8b, 0x0b, 0x22, 0x8c, 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec,
  761. 0x70, 0xce, 0x0f, 0x8c, 0xff, 0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32,
  762. 0x59, 0x78, 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb, 0x28, 0xe2, 0xc1, 0x1a,
  763. 0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4, 0xaa, 0x69,
  764. 0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44,
  765. 0xfb, 0x9a, 0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12,
  766. 0x79, 0xbd, 0xa0, 0x70, 0x47, 0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07,
  767. 0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f, 0x1a, 0xd1, 0xa2, 0x96,
  768. 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c, 0x40,
  769. 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c,
  770. 0x5d, 0xbe, 0xfd, 0x1b, 0x02, 0x1d, 0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02,
  771. 0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73, 0xf3, 0x7d, 0x31,
  772. 0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31, 0x02,
  773. 0x82, 0x01, 0x01, 0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5,
  774. 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c, 0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47,
  775. 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3, 0xd4, 0xd2, 0xe3,
  776. 0xee, 0x3b, 0x46, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37,
  777. 0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c,
  778. 0x2a, 0x04, 0x7c, 0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2,
  779. 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb, 0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63,
  780. 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2, 0x5b, 0x40, 0x6d,
  781. 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d,
  782. 0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50,
  783. 0xd0, 0x44, 0x57, 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80,
  784. 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a, 0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37,
  785. 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28, 0x05, 0xb5, 0xd8,
  786. 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41,
  787. 0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97,
  788. 0xb7, 0xeb, 0x67, 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9,
  789. 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1, 0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12,
  790. 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e, 0x28, 0xa9, 0xfc,
  791. 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05,
  792. 0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01,
  793. 0x56, 0xfb, 0x78, 0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7,
  794. 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01, 0x9f, 0x34, 0x02, 0x82, 0x01, 0x00,
  795. 0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30,
  796. 0xeb, 0x8e, 0x44, 0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38,
  797. 0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8, 0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2,
  798. 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52, 0x50, 0x15, 0x9c,
  799. 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99,
  800. 0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3,
  801. 0xd9, 0xd4, 0x66, 0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f,
  802. 0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b, 0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e,
  803. 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27, 0xb7, 0x5f, 0x65,
  804. 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63,
  805. 0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41,
  806. 0xaf, 0x88, 0x23, 0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d,
  807. 0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63, 0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07,
  808. 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7, 0x86, 0x99, 0x29,
  809. 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e,
  810. 0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee,
  811. 0x6b, 0x11, 0xdd, 0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f,
  812. 0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55, 0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d,
  813. 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5, 0x47, 0xa9, 0xb5,
  814. 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58,
  815. 0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf,
  816. 0x07, 0x48, 0x45, 0xfd, 0x02, 0x1c, 0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b,
  817. 0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3, 0x43, 0xb8, 0xa4,
  818. 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21,
  819. };
  820. static const unsigned char dsa_p[] = {
  821. 0x00, 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4, 0x61, 0xa0, 0x6f, 0x66, 0x17, 0xda,
  822. 0xeb, 0x90, 0xf0, 0xa3, 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21, 0x3d, 0x85, 0xa3, 0x4a, 0xf0,
  823. 0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37, 0x70, 0x06, 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1,
  824. 0xf6, 0xc4, 0x42, 0x23, 0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0, 0xc6,
  825. 0xe8, 0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e, 0x05, 0x68, 0x6d, 0x8b, 0xa8, 0xad,
  826. 0xa1, 0xc2, 0x3a, 0x4e, 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21, 0x55, 0x22, 0xce, 0xa2, 0xe9,
  827. 0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03, 0x59, 0xa0, 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a,
  828. 0x07, 0x80, 0xfc, 0x4e, 0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b, 0x22,
  829. 0x55, 0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a, 0x38, 0x2b, 0x17, 0xc2, 0x62, 0x12,
  830. 0x8b, 0x0b, 0x22, 0x8c, 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec, 0x70, 0xce, 0x0f, 0x8c, 0xff,
  831. 0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32, 0x59, 0x78, 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb,
  832. 0x28, 0xe2, 0xc1, 0x1a, 0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4, 0xaa,
  833. 0x69, 0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44, 0xfb, 0x9a,
  834. 0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12, 0x79, 0xbd, 0xa0, 0x70, 0x47,
  835. 0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07, 0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f,
  836. 0x1a, 0xd1, 0xa2, 0x96, 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c,
  837. 0x40, 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c, 0x5d, 0xbe,
  838. 0xfd, 0x1b
  839. };
  840. static const unsigned char dsa_q[] = {
  841. 0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02, 0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73,
  842. 0xf3, 0x7d, 0x31, 0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31
  843. };
  844. static const unsigned char dsa_g[] = {
  845. 0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5, 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c,
  846. 0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47, 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3,
  847. 0xd4, 0xd2, 0xe3, 0xee, 0x3b, 0x46, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37,
  848. 0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c, 0x2a, 0x04, 0x7c,
  849. 0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2, 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb,
  850. 0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63, 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2,
  851. 0x5b, 0x40, 0x6d, 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d,
  852. 0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50, 0xd0, 0x44, 0x57,
  853. 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80, 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a,
  854. 0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37, 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28,
  855. 0x05, 0xb5, 0xd8, 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41,
  856. 0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97, 0xb7, 0xeb, 0x67,
  857. 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9, 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1,
  858. 0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12, 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e,
  859. 0x28, 0xa9, 0xfc, 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05,
  860. 0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01, 0x56, 0xfb, 0x78,
  861. 0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7, 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01,
  862. 0x9f, 0x34
  863. };
  864. static const unsigned char dsa_priv[] = {
  865. 0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b, 0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3,
  866. 0x43, 0xb8, 0xa4, 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21
  867. };
  868. static const unsigned char dsa_pub[] = {
  869. 0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30, 0xeb, 0x8e, 0x44,
  870. 0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38, 0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8,
  871. 0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2, 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52,
  872. 0x50, 0x15, 0x9c, 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99,
  873. 0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3, 0xd9, 0xd4, 0x66,
  874. 0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f, 0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b,
  875. 0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e, 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27,
  876. 0xb7, 0x5f, 0x65, 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63,
  877. 0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41, 0xaf, 0x88, 0x23,
  878. 0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d, 0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63,
  879. 0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07, 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7,
  880. 0x86, 0x99, 0x29, 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e,
  881. 0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee, 0x6b, 0x11, 0xdd,
  882. 0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f, 0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55,
  883. 0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d, 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5,
  884. 0x47, 0xa9, 0xb5, 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58,
  885. 0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf, 0x07, 0x48, 0x45,
  886. 0xfd
  887. };
  888. static int do_check_params(OSSL_PARAM key_params[], int expected)
  889. {
  890. EVP_PKEY_CTX *gen_ctx = NULL, *check_ctx = NULL;
  891. EVP_PKEY *pkey = NULL;
  892. int ret;
  893. ret = TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL))
  894. && TEST_int_eq(EVP_PKEY_fromdata_init(gen_ctx), 1)
  895. && TEST_int_eq(EVP_PKEY_fromdata(gen_ctx, &pkey,
  896. EVP_PKEY_KEYPAIR, key_params), 1)
  897. && TEST_ptr(check_ctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey,
  898. NULL))
  899. && TEST_int_eq(EVP_PKEY_param_check(check_ctx), expected);
  900. EVP_PKEY_CTX_free(check_ctx);
  901. EVP_PKEY_CTX_free(gen_ctx);
  902. EVP_PKEY_free(pkey);
  903. return ret;
  904. }
  905. static int do_check_bn(OSSL_PARAM params[], const char *key,
  906. const unsigned char *expected, size_t expected_len)
  907. {
  908. OSSL_PARAM *p;
  909. BIGNUM *bn = NULL;
  910. unsigned char buffer[256 + 1];
  911. int ret, len;
  912. ret = TEST_ptr(p = OSSL_PARAM_locate(params, key))
  913. && TEST_true(OSSL_PARAM_get_BN(p, &bn))
  914. && TEST_int_gt(len = BN_bn2binpad(bn, buffer, expected_len), 0)
  915. && TEST_mem_eq(expected, expected_len, buffer, len);
  916. BN_free(bn);
  917. return ret;
  918. }
  919. static int do_check_int(OSSL_PARAM params[], const char *key, int expected)
  920. {
  921. OSSL_PARAM *p;
  922. int val = 0;
  923. return TEST_ptr(p = OSSL_PARAM_locate(params, key))
  924. && TEST_true(OSSL_PARAM_get_int(p, &val))
  925. && TEST_int_eq(val, expected);
  926. }
  927. static int test_dsa_tofrom_data_select(void)
  928. {
  929. int ret;
  930. EVP_PKEY *key = NULL;
  931. const unsigned char *pkeydata = dsa_key;
  932. ret = TEST_ptr(key = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key),
  933. mainctx, NULL))
  934. && TEST_true(do_pkey_tofrom_data_select(key, "DSA"));
  935. EVP_PKEY_free(key);
  936. return ret;
  937. }
  938. static int test_dsa_todata(void)
  939. {
  940. EVP_PKEY *pkey = NULL;
  941. OSSL_PARAM *to_params = NULL, *all_params = NULL;
  942. OSSL_PARAM gen_params[4];
  943. int ret = 0;
  944. const unsigned char *pkeydata = dsa_key;
  945. unsigned char dsa_seed[] = {
  946. 0xbc, 0x8a, 0x81, 0x64, 0x9e, 0x9d, 0x63, 0xa7, 0xa3, 0x5d, 0x87, 0xdd,
  947. 0x32, 0xf3, 0xc1, 0x9f, 0x18, 0x22, 0xeb, 0x73, 0x63, 0xad, 0x5e, 0x7b,
  948. 0x90, 0xc1, 0xe3, 0xe0
  949. };
  950. int dsa_pcounter = 319;
  951. int dsa_gindex = 5;
  952. gen_params[0] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
  953. (void*)dsa_seed,
  954. sizeof(dsa_seed));
  955. gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX,
  956. &dsa_gindex);
  957. gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER,
  958. &dsa_pcounter);
  959. gen_params[3] = OSSL_PARAM_construct_end();
  960. if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key),
  961. mainctx, NULL))
  962. || !TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, &to_params), 1)
  963. || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_P, dsa_p, sizeof(dsa_p))
  964. || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_Q, dsa_q, sizeof(dsa_q))
  965. || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_G, dsa_g, sizeof(dsa_g))
  966. || !do_check_bn(to_params, OSSL_PKEY_PARAM_PUB_KEY, dsa_pub,
  967. sizeof(dsa_pub))
  968. || !do_check_bn(to_params, OSSL_PKEY_PARAM_PRIV_KEY, dsa_priv,
  969. sizeof(dsa_priv))
  970. || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_GINDEX, -1)
  971. || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_PCOUNTER, -1)
  972. || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_H, 0)
  973. || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, 1)
  974. || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_G, 1)
  975. || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, 0)
  976. || !TEST_ptr_null(OSSL_PARAM_locate(to_params, OSSL_PKEY_PARAM_FFC_SEED)))
  977. goto err;
  978. if (!do_fromdata_key_is_equal(to_params, pkey, "DSA"))
  979. goto err;
  980. if (!TEST_ptr(all_params = OSSL_PARAM_merge(to_params, gen_params))
  981. || !do_check_params(all_params, 1))
  982. goto err;
  983. gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX,
  984. &dsa_gindex);
  985. gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER,
  986. &dsa_pcounter);
  987. /*
  988. * Check that modifying the shallow copy values used in OSSL_PARAM_merge()
  989. * results in an invalid key. This also verifies that the fips186-4
  990. * validation code is running.
  991. */
  992. dsa_gindex++;
  993. if (!do_check_params(all_params, 0))
  994. goto err;
  995. dsa_gindex--;
  996. dsa_pcounter++;
  997. if (!do_check_params(all_params, 0))
  998. goto err;
  999. dsa_pcounter--;
  1000. dsa_seed[0] = 0xb0;
  1001. if (!do_check_params(all_params, 0))
  1002. goto err;
  1003. ret = 1;
  1004. err:
  1005. EVP_PKEY_free(pkey);
  1006. OSSL_PARAM_free(all_params);
  1007. OSSL_PARAM_free(to_params);
  1008. return ret;
  1009. }
  1010. /*
  1011. * Test that OSSL_PKEY_PARAM_FFC_DIGEST_PROPS is set properly when using fromdata
  1012. * This test:
  1013. * checks for failure when the property query is bad (tstid == 0)
  1014. * checks for success when the property query is valid (tstid == 1)
  1015. */
  1016. static int test_dsa_fromdata_digest_prop(int tstid)
  1017. {
  1018. EVP_PKEY_CTX *ctx = NULL, *gctx = NULL;
  1019. EVP_PKEY *pkey = NULL, *pkey2 = NULL;
  1020. OSSL_PARAM params[4], *p = params;
  1021. int ret = 0;
  1022. int expected = (tstid == 0 ? 0 : 1);
  1023. unsigned int pbits = 512; /* minimum allowed for speed */
  1024. *p++ = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
  1025. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, "SHA512", 0);
  1026. /* Setting a bad prop query here should fail during paramgen - when it tries to do a fetch */
  1027. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
  1028. tstid == 0 ? "provider=unknown" : "provider=default", 0);
  1029. *p++ = OSSL_PARAM_construct_end();
  1030. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL))
  1031. || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  1032. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params), 1))
  1033. goto err;
  1034. if (!TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
  1035. || !TEST_int_eq(EVP_PKEY_paramgen_init(gctx), 1)
  1036. || !TEST_int_eq(EVP_PKEY_paramgen(gctx, &pkey2), expected))
  1037. goto err;
  1038. ret = 1;
  1039. err:
  1040. EVP_PKEY_free(pkey2);
  1041. EVP_PKEY_free(pkey);
  1042. EVP_PKEY_CTX_free(ctx);
  1043. EVP_PKEY_CTX_free(gctx);
  1044. return ret;
  1045. }
  1046. #endif /* OPENSSL_NO_DSA */
  1047. static int test_pkey_todata_null(void)
  1048. {
  1049. OSSL_PARAM *params = NULL;
  1050. EVP_PKEY *pkey = NULL;
  1051. int ret = 0;
  1052. const unsigned char *pdata = keydata[0].kder;
  1053. ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
  1054. mainctx, NULL))
  1055. && TEST_int_eq(EVP_PKEY_todata(NULL, EVP_PKEY_KEYPAIR, &params), 0)
  1056. && TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, NULL), 0);
  1057. EVP_PKEY_free(pkey);
  1058. return ret;
  1059. }
  1060. static OSSL_CALLBACK test_pkey_export_cb;
  1061. static int test_pkey_export_cb(const OSSL_PARAM params[], void *arg)
  1062. {
  1063. if (arg == NULL)
  1064. return 0;
  1065. return do_fromdata_key_is_equal(params, (EVP_PKEY *)arg, "RSA");
  1066. }
  1067. static int test_pkey_export_null(void)
  1068. {
  1069. EVP_PKEY *pkey = NULL;
  1070. int ret = 0;
  1071. const unsigned char *pdata = keydata[0].kder;
  1072. ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
  1073. mainctx, NULL))
  1074. && TEST_int_eq(EVP_PKEY_export(NULL, EVP_PKEY_KEYPAIR,
  1075. test_pkey_export_cb, NULL), 0)
  1076. && TEST_int_eq(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, NULL, NULL), 0);
  1077. EVP_PKEY_free(pkey);
  1078. return ret;
  1079. }
  1080. static int test_pkey_export(void)
  1081. {
  1082. EVP_PKEY *pkey = NULL;
  1083. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1084. RSA *rsa = NULL;
  1085. #endif
  1086. int ret = 1;
  1087. const unsigned char *pdata = keydata[0].kder;
  1088. int pdata_len = keydata[0].size;
  1089. if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len,
  1090. mainctx, NULL))
  1091. || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
  1092. test_pkey_export_cb, pkey))
  1093. || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
  1094. test_pkey_export_cb, NULL)))
  1095. ret = 0;
  1096. EVP_PKEY_free(pkey);
  1097. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1098. /* Now, try with a legacy key */
  1099. pdata = keydata[0].kder;
  1100. pdata_len = keydata[0].size;
  1101. if (!TEST_ptr(rsa = d2i_RSAPrivateKey(NULL, &pdata, pdata_len))
  1102. || !TEST_ptr(pkey = EVP_PKEY_new())
  1103. || !TEST_true(EVP_PKEY_assign_RSA(pkey, rsa))
  1104. || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
  1105. test_pkey_export_cb, pkey))
  1106. || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
  1107. test_pkey_export_cb, NULL)))
  1108. ret = 0;
  1109. EVP_PKEY_free(pkey);
  1110. #endif
  1111. return ret;
  1112. }
  1113. static int test_rsa_pss_sign(void)
  1114. {
  1115. EVP_PKEY *pkey = NULL;
  1116. EVP_PKEY_CTX *pctx = NULL;
  1117. int ret = 0;
  1118. const unsigned char *pdata = keydata[0].kder;
  1119. const char *mdname = "SHA2-256";
  1120. OSSL_PARAM sig_params[3];
  1121. unsigned char mdbuf[256 / 8] = { 0 };
  1122. int padding = RSA_PKCS1_PSS_PADDING;
  1123. unsigned char *sig = NULL;
  1124. size_t sig_len = 0;
  1125. sig_params[0] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE,
  1126. &padding);
  1127. sig_params[1] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  1128. (char *)mdname, 0);
  1129. sig_params[2] = OSSL_PARAM_construct_end();
  1130. ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
  1131. mainctx, NULL))
  1132. && TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
  1133. && TEST_int_gt(EVP_PKEY_sign_init_ex(pctx, sig_params), 0)
  1134. && TEST_int_gt(EVP_PKEY_sign(pctx, NULL, &sig_len, mdbuf,
  1135. sizeof(mdbuf)), 0)
  1136. && TEST_int_gt(sig_len, 0)
  1137. && TEST_ptr(sig = OPENSSL_malloc(sig_len))
  1138. && TEST_int_gt(EVP_PKEY_sign(pctx, sig, &sig_len, mdbuf,
  1139. sizeof(mdbuf)), 0);
  1140. EVP_PKEY_CTX_free(pctx);
  1141. OPENSSL_free(sig);
  1142. EVP_PKEY_free(pkey);
  1143. return ret;
  1144. }
  1145. static int test_evp_md_ctx_dup(void)
  1146. {
  1147. EVP_MD_CTX *mdctx;
  1148. EVP_MD_CTX *copyctx = NULL;
  1149. int ret;
  1150. /* test copying freshly initialized context */
  1151. ret = TEST_ptr(mdctx = EVP_MD_CTX_new())
  1152. && TEST_ptr(copyctx = EVP_MD_CTX_dup(mdctx));
  1153. EVP_MD_CTX_free(mdctx);
  1154. EVP_MD_CTX_free(copyctx);
  1155. return ret;
  1156. }
  1157. static int test_evp_md_ctx_copy(void)
  1158. {
  1159. EVP_MD_CTX *mdctx = NULL;
  1160. EVP_MD_CTX *copyctx = NULL;
  1161. int ret;
  1162. /* test copying freshly initialized context */
  1163. ret = TEST_ptr(mdctx = EVP_MD_CTX_new())
  1164. && TEST_ptr(copyctx = EVP_MD_CTX_new())
  1165. && TEST_true(EVP_MD_CTX_copy_ex(copyctx, mdctx));
  1166. EVP_MD_CTX_free(mdctx);
  1167. EVP_MD_CTX_free(copyctx);
  1168. return ret;
  1169. }
  1170. #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5
  1171. static int test_evp_pbe_alg_add(void)
  1172. {
  1173. int ret = 0;
  1174. int cipher_nid = 0, md_nid = 0;
  1175. EVP_PBE_KEYGEN_EX *keygen_ex = NULL;
  1176. EVP_PBE_KEYGEN *keygen = NULL;
  1177. if (!TEST_true(EVP_PBE_alg_add(NID_pbeWithMD5AndDES_CBC, EVP_des_cbc(), EVP_md5(),
  1178. PKCS5_PBE_keyivgen)))
  1179. goto err;
  1180. if (!TEST_true(EVP_PBE_find_ex(EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC,
  1181. &cipher_nid, &md_nid, &keygen, &keygen_ex)))
  1182. goto err;
  1183. if (!TEST_true(keygen != NULL))
  1184. goto err;
  1185. if (!TEST_true(keygen_ex == NULL))
  1186. goto err;
  1187. ret = 1;
  1188. err:
  1189. return ret;
  1190. }
  1191. #endif
  1192. int setup_tests(void)
  1193. {
  1194. if (!test_get_libctx(&mainctx, &nullprov, NULL, NULL, NULL)) {
  1195. OSSL_LIB_CTX_free(mainctx);
  1196. mainctx = NULL;
  1197. return 0;
  1198. }
  1199. ADD_TEST(test_alternative_default);
  1200. ADD_ALL_TESTS(test_d2i_AutoPrivateKey_ex, OSSL_NELEM(keydata));
  1201. #ifndef OPENSSL_NO_EC
  1202. ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 2);
  1203. ADD_TEST(test_ec_tofrom_data_select);
  1204. # ifndef OPENSSL_NO_ECX
  1205. ADD_TEST(test_ecx_tofrom_data_select);
  1206. # endif
  1207. ADD_TEST(test_ec_d2i_i2d_pubkey);
  1208. #else
  1209. ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 1);
  1210. #endif
  1211. #ifndef OPENSSL_NO_SM2
  1212. ADD_TEST(test_sm2_tofrom_data_select);
  1213. #endif
  1214. #ifndef OPENSSL_NO_DSA
  1215. ADD_TEST(test_dsa_todata);
  1216. ADD_TEST(test_dsa_tofrom_data_select);
  1217. ADD_ALL_TESTS(test_dsa_fromdata_digest_prop, 2);
  1218. #endif
  1219. #ifndef OPENSSL_NO_DH
  1220. ADD_TEST(test_dh_tofrom_data_select);
  1221. ADD_TEST(test_dh_paramgen);
  1222. ADD_TEST(test_dh_paramfromdata);
  1223. #endif
  1224. ADD_TEST(test_rsa_tofrom_data_select);
  1225. ADD_TEST(test_pkey_todata_null);
  1226. ADD_TEST(test_pkey_export_null);
  1227. ADD_TEST(test_pkey_export);
  1228. #ifndef OPENSSL_NO_DES
  1229. ADD_TEST(test_pkcs8key_nid_bio);
  1230. #endif
  1231. ADD_ALL_TESTS(test_PEM_read_bio_negative, OSSL_NELEM(keydata));
  1232. ADD_ALL_TESTS(test_PEM_read_bio_negative_wrong_password, 2);
  1233. ADD_TEST(test_rsa_pss_sign);
  1234. ADD_TEST(test_evp_md_ctx_dup);
  1235. ADD_TEST(test_evp_md_ctx_copy);
  1236. ADD_ALL_TESTS(test_provider_unload_effective, 2);
  1237. #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5
  1238. ADD_TEST(test_evp_pbe_alg_add);
  1239. #endif
  1240. return 1;
  1241. }
  1242. void cleanup_tests(void)
  1243. {
  1244. OSSL_LIB_CTX_free(mainctx);
  1245. OSSL_PROVIDER_unload(nullprov);
  1246. }