evp_extra_test2.c 52 KB

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