evp_extra_test.c 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019
  1. /*
  2. * Copyright 2015-2021 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. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <openssl/bio.h>
  15. #include <openssl/conf.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/err.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/x509.h>
  20. #include <openssl/pem.h>
  21. #include <openssl/kdf.h>
  22. #include <openssl/provider.h>
  23. #include <openssl/core_names.h>
  24. #include <openssl/params.h>
  25. #include <openssl/param_build.h>
  26. #include <openssl/dsa.h>
  27. #include <openssl/dh.h>
  28. #include <openssl/aes.h>
  29. #include <openssl/decoder.h>
  30. #include <openssl/rsa.h>
  31. #include "testutil.h"
  32. #include "internal/nelem.h"
  33. #include "internal/sizes.h"
  34. #include "crypto/evp.h"
  35. #include "../e_os.h" /* strcasecmp */
  36. static OSSL_LIB_CTX *testctx = NULL;
  37. static char *testpropq = NULL;
  38. static OSSL_PROVIDER *nullprov = NULL;
  39. static OSSL_PROVIDER *deflprov = NULL;
  40. static OSSL_PROVIDER *lgcyprov = NULL;
  41. /*
  42. * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
  43. * should never use this key anywhere but in an example.
  44. */
  45. static const unsigned char kExampleRSAKeyDER[] = {
  46. 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
  47. 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
  48. 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
  49. 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
  50. 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
  51. 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
  52. 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
  53. 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
  54. 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
  55. 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
  56. 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
  57. 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
  58. 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
  59. 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
  60. 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
  61. 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
  62. 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
  63. 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
  64. 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
  65. 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
  66. 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
  67. 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
  68. 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
  69. 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
  70. 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
  71. 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
  72. 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
  73. 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
  74. 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
  75. 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
  76. 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
  77. 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
  78. 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
  79. 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
  80. 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
  81. 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
  82. 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
  83. 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
  84. 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
  85. 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
  86. 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
  87. 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
  88. 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
  89. 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
  90. 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
  91. 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
  92. 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
  93. 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
  94. 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
  95. 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
  96. 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  97. };
  98. /*
  99. * kExampleDSAKeyDER is a DSA private key in ASN.1, DER format. Of course, you
  100. * should never use this key anywhere but in an example.
  101. */
  102. #ifndef OPENSSL_NO_DSA
  103. static const unsigned char kExampleDSAKeyDER[] = {
  104. 0x30, 0x82, 0x01, 0xba, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0x9a,
  105. 0x05, 0x6d, 0x33, 0xcd, 0x5d, 0x78, 0xa1, 0xbb, 0xcb, 0x7d, 0x5b, 0x8d,
  106. 0xb4, 0xcc, 0xbf, 0x03, 0x99, 0x64, 0xde, 0x38, 0x78, 0x06, 0x15, 0x2f,
  107. 0x86, 0x26, 0x77, 0xf3, 0xb1, 0x85, 0x00, 0xed, 0xfc, 0x28, 0x3a, 0x42,
  108. 0x4d, 0xab, 0xab, 0xdf, 0xbc, 0x9c, 0x16, 0xd0, 0x22, 0x50, 0xd1, 0x38,
  109. 0xdd, 0x3f, 0x64, 0x05, 0x9e, 0x68, 0x7a, 0x1e, 0xf1, 0x56, 0xbf, 0x1e,
  110. 0x2c, 0xc5, 0x97, 0x2a, 0xfe, 0x7a, 0x22, 0xdc, 0x6c, 0x68, 0xb8, 0x2e,
  111. 0x06, 0xdb, 0x41, 0xca, 0x98, 0xd8, 0x54, 0xc7, 0x64, 0x48, 0x24, 0x04,
  112. 0x20, 0xbc, 0x59, 0xe3, 0x6b, 0xea, 0x7e, 0xfc, 0x7e, 0xc5, 0x4e, 0xd4,
  113. 0xd8, 0x3a, 0xed, 0xcd, 0x5d, 0x99, 0xb8, 0x5c, 0xa2, 0x8b, 0xbb, 0x0b,
  114. 0xac, 0xe6, 0x8e, 0x25, 0x56, 0x22, 0x3a, 0x2d, 0x3a, 0x56, 0x41, 0x14,
  115. 0x1f, 0x1c, 0x8f, 0x53, 0x46, 0x13, 0x85, 0x02, 0x15, 0x00, 0x98, 0x7e,
  116. 0x92, 0x81, 0x88, 0xc7, 0x3f, 0x70, 0x49, 0x54, 0xf6, 0x76, 0xb4, 0xa3,
  117. 0x9e, 0x1d, 0x45, 0x98, 0x32, 0x7f, 0x02, 0x81, 0x80, 0x69, 0x4d, 0xef,
  118. 0x55, 0xff, 0x4d, 0x59, 0x2c, 0x01, 0xfa, 0x6a, 0x38, 0xe0, 0x70, 0x9f,
  119. 0x9e, 0x66, 0x8e, 0x3e, 0x8c, 0x52, 0x22, 0x9d, 0x15, 0x7e, 0x3c, 0xef,
  120. 0x4c, 0x7a, 0x61, 0x26, 0xe0, 0x2b, 0x81, 0x3f, 0xeb, 0xaf, 0x35, 0x38,
  121. 0x8d, 0xfe, 0xed, 0x46, 0xff, 0x5f, 0x03, 0x9b, 0x81, 0x92, 0xe7, 0x6f,
  122. 0x76, 0x4f, 0x1d, 0xd9, 0xbb, 0x89, 0xc9, 0x3e, 0xd9, 0x0b, 0xf9, 0xf4,
  123. 0x78, 0x11, 0x59, 0xc0, 0x1d, 0xcd, 0x0e, 0xa1, 0x6f, 0x15, 0xf1, 0x4d,
  124. 0xc1, 0xc9, 0x22, 0xed, 0x8d, 0xad, 0x67, 0xc5, 0x4b, 0x95, 0x93, 0x86,
  125. 0xa6, 0xaf, 0x8a, 0xee, 0x06, 0x89, 0x2f, 0x37, 0x7e, 0x64, 0xaa, 0xf6,
  126. 0xe7, 0xb1, 0x5a, 0x0a, 0x93, 0x95, 0x5d, 0x3e, 0x53, 0x9a, 0xde, 0x8a,
  127. 0xc2, 0x95, 0x45, 0x81, 0xbe, 0x5c, 0x2f, 0xc2, 0xb2, 0x92, 0x58, 0x19,
  128. 0x72, 0x80, 0xe9, 0x79, 0xa1, 0x02, 0x81, 0x80, 0x07, 0xd7, 0x62, 0xff,
  129. 0xdf, 0x1a, 0x3f, 0xed, 0x32, 0xd4, 0xd4, 0x88, 0x7b, 0x2c, 0x63, 0x7f,
  130. 0x97, 0xdc, 0x44, 0xd4, 0x84, 0xa2, 0xdd, 0x17, 0x16, 0x85, 0x13, 0xe0,
  131. 0xac, 0x51, 0x8d, 0x29, 0x1b, 0x75, 0x9a, 0xe4, 0xe3, 0x8a, 0x92, 0x69,
  132. 0x09, 0x03, 0xc5, 0x68, 0xae, 0x5e, 0x94, 0xfe, 0xc9, 0x92, 0x6c, 0x07,
  133. 0xb4, 0x1e, 0x64, 0x62, 0x87, 0xc6, 0xa4, 0xfd, 0x0d, 0x5f, 0xe5, 0xf9,
  134. 0x1b, 0x4f, 0x85, 0x5f, 0xae, 0xf3, 0x11, 0xe5, 0x18, 0xd4, 0x4d, 0x79,
  135. 0x9f, 0xc4, 0x79, 0x26, 0x04, 0x27, 0xf0, 0x0b, 0xee, 0x2b, 0x86, 0x9f,
  136. 0x86, 0x61, 0xe6, 0x51, 0xce, 0x04, 0x9b, 0x5d, 0x6b, 0x34, 0x43, 0x8c,
  137. 0x85, 0x3c, 0xf1, 0x51, 0x9b, 0x08, 0x23, 0x1b, 0xf5, 0x7e, 0x33, 0x12,
  138. 0xea, 0xab, 0x1f, 0xb7, 0x2d, 0xe2, 0x5f, 0xe6, 0x97, 0x99, 0xb5, 0x45,
  139. 0x16, 0x5b, 0xc3, 0x41, 0x02, 0x14, 0x61, 0xbf, 0x51, 0x60, 0xcf, 0xc8,
  140. 0xf1, 0x8c, 0x82, 0x97, 0xf2, 0xf4, 0x19, 0xba, 0x2b, 0xf3, 0x16, 0xbe,
  141. 0x40, 0x48
  142. };
  143. #endif
  144. /*
  145. * kExampleBadRSAKeyDER is an RSA private key in ASN.1, DER format. The private
  146. * components are not correct.
  147. */
  148. static const unsigned char kExampleBadRSAKeyDER[] = {
  149. 0x30, 0x82, 0x04, 0x27, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
  150. 0xa6, 0x1a, 0x1e, 0x6e, 0x7b, 0xee, 0xc6, 0x89, 0x66, 0xe7, 0x93, 0xef,
  151. 0x54, 0x12, 0x68, 0xea, 0xbf, 0x86, 0x2f, 0xdd, 0xd2, 0x79, 0xb8, 0xa9,
  152. 0x6e, 0x03, 0xc2, 0xa3, 0xb9, 0xa3, 0xe1, 0x4b, 0x2a, 0xb3, 0xf8, 0xb4,
  153. 0xcd, 0xea, 0xbe, 0x24, 0xa6, 0x57, 0x5b, 0x83, 0x1f, 0x0f, 0xf2, 0xd3,
  154. 0xb7, 0xac, 0x7e, 0xd6, 0x8e, 0x6e, 0x1e, 0xbf, 0xb8, 0x73, 0x8c, 0x05,
  155. 0x56, 0xe6, 0x35, 0x1f, 0xe9, 0x04, 0x0b, 0x09, 0x86, 0x7d, 0xf1, 0x26,
  156. 0x08, 0x99, 0xad, 0x7b, 0xc8, 0x4d, 0x94, 0xb0, 0x0b, 0x8b, 0x38, 0xa0,
  157. 0x5c, 0x62, 0xa0, 0xab, 0xd3, 0x8f, 0xd4, 0x09, 0x60, 0x72, 0x1e, 0x33,
  158. 0x50, 0x80, 0x6e, 0x22, 0xa6, 0x77, 0x57, 0x6b, 0x9a, 0x33, 0x21, 0x66,
  159. 0x87, 0x6e, 0x21, 0x7b, 0xc7, 0x24, 0x0e, 0xd8, 0x13, 0xdf, 0x83, 0xde,
  160. 0xcd, 0x40, 0x58, 0x1d, 0x84, 0x86, 0xeb, 0xb8, 0x12, 0x4e, 0xd2, 0xfa,
  161. 0x80, 0x1f, 0xe4, 0xe7, 0x96, 0x29, 0xb8, 0xcc, 0xce, 0x66, 0x6d, 0x53,
  162. 0xca, 0xb9, 0x5a, 0xd7, 0xf6, 0x84, 0x6c, 0x2d, 0x9a, 0x1a, 0x14, 0x1c,
  163. 0x4e, 0x93, 0x39, 0xba, 0x74, 0xed, 0xed, 0x87, 0x87, 0x5e, 0x48, 0x75,
  164. 0x36, 0xf0, 0xbc, 0x34, 0xfb, 0x29, 0xf9, 0x9f, 0x96, 0x5b, 0x0b, 0xa7,
  165. 0x54, 0x30, 0x51, 0x29, 0x18, 0x5b, 0x7d, 0xac, 0x0f, 0xd6, 0x5f, 0x7c,
  166. 0xf8, 0x98, 0x8c, 0xd8, 0x86, 0x62, 0xb3, 0xdc, 0xff, 0x0f, 0xff, 0x7a,
  167. 0xaf, 0x5c, 0x4c, 0x61, 0x49, 0x2e, 0xc8, 0x95, 0x86, 0xc4, 0x0e, 0x87,
  168. 0xfc, 0x1d, 0xcf, 0x8b, 0x7c, 0x61, 0xf6, 0xd8, 0xd0, 0x69, 0xf6, 0xcd,
  169. 0x8a, 0x8c, 0xf6, 0x62, 0xa2, 0x56, 0xa9, 0xe3, 0xd1, 0xcf, 0x4d, 0xa0,
  170. 0xf6, 0x2d, 0x20, 0x0a, 0x04, 0xb7, 0xa2, 0xf7, 0xb5, 0x99, 0x47, 0x18,
  171. 0x56, 0x85, 0x87, 0xc7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
  172. 0x01, 0x00, 0x99, 0x41, 0x38, 0x1a, 0xd0, 0x96, 0x7a, 0xf0, 0x83, 0xd5,
  173. 0xdf, 0x94, 0xce, 0x89, 0x3d, 0xec, 0x7a, 0x52, 0x21, 0x10, 0x16, 0x06,
  174. 0xe0, 0xee, 0xd2, 0xe6, 0xfd, 0x4b, 0x7b, 0x19, 0x4d, 0xe1, 0xc0, 0xc0,
  175. 0xd5, 0x14, 0x5d, 0x79, 0xdd, 0x7e, 0x8b, 0x4b, 0xc6, 0xcf, 0xb0, 0x75,
  176. 0x52, 0xa3, 0x2d, 0xb1, 0x26, 0x46, 0x68, 0x9c, 0x0a, 0x1a, 0xf2, 0xe1,
  177. 0x09, 0xac, 0x53, 0x85, 0x8c, 0x36, 0xa9, 0x14, 0x65, 0xea, 0xa0, 0x00,
  178. 0xcb, 0xe3, 0x3f, 0xc4, 0x2b, 0x61, 0x2e, 0x6b, 0x06, 0x69, 0x77, 0xfd,
  179. 0x38, 0x7e, 0x1d, 0x3f, 0x92, 0xe7, 0x77, 0x08, 0x19, 0xa7, 0x9d, 0x29,
  180. 0x2d, 0xdc, 0x42, 0xc6, 0x7c, 0xd7, 0xd3, 0xa8, 0x01, 0x2c, 0xf2, 0xd5,
  181. 0x82, 0x57, 0xcb, 0x55, 0x3d, 0xe7, 0xaa, 0xd2, 0x06, 0x30, 0x30, 0x05,
  182. 0xe6, 0xf2, 0x47, 0x86, 0xba, 0xc6, 0x61, 0x64, 0xeb, 0x4f, 0x2a, 0x5e,
  183. 0x07, 0x29, 0xe0, 0x96, 0xb2, 0x43, 0xff, 0x5f, 0x1a, 0x54, 0x16, 0xcf,
  184. 0xb5, 0x56, 0x5c, 0xa0, 0x9b, 0x0c, 0xfd, 0xb3, 0xd2, 0xe3, 0x79, 0x1d,
  185. 0x21, 0xe2, 0xd6, 0x13, 0xc4, 0x74, 0xa6, 0xf5, 0x8e, 0x8e, 0x81, 0xbb,
  186. 0xb4, 0xad, 0x8a, 0xf0, 0x93, 0x0a, 0xd8, 0x0a, 0x42, 0x36, 0xbc, 0xe5,
  187. 0x26, 0x2a, 0x0d, 0x5d, 0x57, 0x13, 0xc5, 0x4e, 0x2f, 0x12, 0x0e, 0xef,
  188. 0xa7, 0x81, 0x1e, 0xc3, 0xa5, 0xdb, 0xc9, 0x24, 0xeb, 0x1a, 0xa1, 0xf9,
  189. 0xf6, 0xa1, 0x78, 0x98, 0x93, 0x77, 0x42, 0x45, 0x03, 0xe2, 0xc9, 0xa2,
  190. 0xfe, 0x2d, 0x77, 0xc8, 0xc6, 0xac, 0x9b, 0x98, 0x89, 0x6d, 0x9a, 0xe7,
  191. 0x61, 0x63, 0xb7, 0xf2, 0xec, 0xd6, 0xb1, 0xa1, 0x6e, 0x0a, 0x1a, 0xff,
  192. 0xfd, 0x43, 0x28, 0xc3, 0x0c, 0xdc, 0xf2, 0x47, 0x4f, 0x27, 0xaa, 0x99,
  193. 0x04, 0x8e, 0xac, 0xe8, 0x7c, 0x01, 0x02, 0x04, 0x12, 0x34, 0x56, 0x78,
  194. 0x02, 0x81, 0x81, 0x00, 0xca, 0x69, 0xe5, 0xbb, 0x3a, 0x90, 0x82, 0xcb,
  195. 0x82, 0x50, 0x2f, 0x29, 0xe2, 0x76, 0x6a, 0x57, 0x55, 0x45, 0x4e, 0x35,
  196. 0x18, 0x61, 0xe0, 0x12, 0x70, 0xc0, 0xab, 0xc7, 0x80, 0xa2, 0xd4, 0x46,
  197. 0x34, 0x03, 0xa0, 0x19, 0x26, 0x23, 0x9e, 0xef, 0x1a, 0xcb, 0x75, 0xd6,
  198. 0xba, 0x81, 0xf4, 0x7e, 0x52, 0xe5, 0x2a, 0xe8, 0xf1, 0x49, 0x6c, 0x0f,
  199. 0x1a, 0xa0, 0xf9, 0xc6, 0xe7, 0xec, 0x60, 0xe4, 0xcb, 0x2a, 0xb5, 0x56,
  200. 0xe9, 0x9c, 0xcd, 0x19, 0x75, 0x92, 0xb1, 0x66, 0xce, 0xc3, 0xd9, 0x3d,
  201. 0x11, 0xcb, 0xc4, 0x09, 0xce, 0x1e, 0x30, 0xba, 0x2f, 0x60, 0x60, 0x55,
  202. 0x8d, 0x02, 0xdc, 0x5d, 0xaf, 0xf7, 0x52, 0x31, 0x17, 0x07, 0x53, 0x20,
  203. 0x33, 0xad, 0x8c, 0xd5, 0x2f, 0x5a, 0xd0, 0x57, 0xd7, 0xd1, 0x80, 0xd6,
  204. 0x3a, 0x9b, 0x04, 0x4f, 0x35, 0xbf, 0xe7, 0xd5, 0xbc, 0x8f, 0xd4, 0x81,
  205. 0x02, 0x81, 0x81, 0x00, 0xc0, 0x9f, 0xf8, 0xcd, 0xf7, 0x3f, 0x26, 0x8a,
  206. 0x3d, 0x4d, 0x2b, 0x0c, 0x01, 0xd0, 0xa2, 0xb4, 0x18, 0xfe, 0xf7, 0x5e,
  207. 0x2f, 0x06, 0x13, 0xcd, 0x63, 0xaa, 0x12, 0xa9, 0x24, 0x86, 0xe3, 0xf3,
  208. 0x7b, 0xda, 0x1a, 0x3c, 0xb1, 0x38, 0x80, 0x80, 0xef, 0x64, 0x64, 0xa1,
  209. 0x9b, 0xfe, 0x76, 0x63, 0x8e, 0x83, 0xd2, 0xd9, 0xb9, 0x86, 0xb0, 0xe6,
  210. 0xa6, 0x0c, 0x7e, 0xa8, 0x84, 0x90, 0x98, 0x0c, 0x1e, 0xf3, 0x14, 0x77,
  211. 0xe0, 0x5f, 0x81, 0x08, 0x11, 0x8f, 0xa6, 0x23, 0xc4, 0xba, 0xc0, 0x8a,
  212. 0xe4, 0xc6, 0xe3, 0x5c, 0xbe, 0xc5, 0xec, 0x2c, 0xb9, 0xd8, 0x8c, 0x4d,
  213. 0x1a, 0x9d, 0xe7, 0x7c, 0x85, 0x4c, 0x0d, 0x71, 0x4e, 0x72, 0x33, 0x1b,
  214. 0xfe, 0xa9, 0x17, 0x72, 0x76, 0x56, 0x9d, 0x74, 0x7e, 0x52, 0x67, 0x9a,
  215. 0x87, 0x9a, 0xdb, 0x30, 0xde, 0xe4, 0x49, 0x28, 0x3b, 0xd2, 0x67, 0xaf,
  216. 0x02, 0x81, 0x81, 0x00, 0x89, 0x74, 0x9a, 0x8e, 0xa7, 0xb9, 0xa5, 0x28,
  217. 0xc0, 0x68, 0xe5, 0x6e, 0x63, 0x1c, 0x99, 0x20, 0x8f, 0x86, 0x8e, 0x12,
  218. 0x9e, 0x69, 0x30, 0xfa, 0x34, 0xd9, 0x92, 0x8d, 0xdb, 0x7c, 0x37, 0xfd,
  219. 0x28, 0xab, 0x61, 0x98, 0x52, 0x7f, 0x14, 0x1a, 0x39, 0xae, 0xfb, 0x6a,
  220. 0x03, 0xa3, 0xe6, 0xbd, 0xb6, 0x5b, 0x6b, 0xe5, 0x5e, 0x9d, 0xc6, 0xa5,
  221. 0x07, 0x27, 0x54, 0x17, 0xd0, 0x3d, 0x84, 0x9b, 0x3a, 0xa0, 0xd9, 0x1e,
  222. 0x99, 0x6c, 0x63, 0x17, 0xab, 0xf1, 0x1f, 0x49, 0xba, 0x95, 0xe3, 0x3b,
  223. 0x86, 0x8f, 0x42, 0xa4, 0x89, 0xf5, 0x94, 0x8f, 0x8b, 0x46, 0xbe, 0x84,
  224. 0xba, 0x4a, 0xbc, 0x0d, 0x5f, 0x46, 0xeb, 0xe8, 0xec, 0x43, 0x8c, 0x1e,
  225. 0xad, 0x19, 0x69, 0x2f, 0x08, 0x86, 0x7a, 0x3f, 0x7d, 0x0f, 0x07, 0x97,
  226. 0xf3, 0x9a, 0x7b, 0xb5, 0xb2, 0xc1, 0x8c, 0x95, 0x68, 0x04, 0xa0, 0x81,
  227. 0x02, 0x81, 0x80, 0x4e, 0xbf, 0x7e, 0x1b, 0xcb, 0x13, 0x61, 0x75, 0x3b,
  228. 0xdb, 0x59, 0x5f, 0xb1, 0xd4, 0xb8, 0xeb, 0x9e, 0x73, 0xb5, 0xe7, 0xf6,
  229. 0x89, 0x3d, 0x1c, 0xda, 0xf0, 0x36, 0xff, 0x35, 0xbd, 0x1e, 0x0b, 0x74,
  230. 0xe3, 0x9e, 0xf0, 0xf2, 0xf7, 0xd7, 0x82, 0xb7, 0x7b, 0x6a, 0x1b, 0x0e,
  231. 0x30, 0x4a, 0x98, 0x0e, 0xb4, 0xf9, 0x81, 0x07, 0xe4, 0x75, 0x39, 0xe9,
  232. 0x53, 0xca, 0xbb, 0x5c, 0xaa, 0x93, 0x07, 0x0e, 0xa8, 0x2f, 0xba, 0x98,
  233. 0x49, 0x30, 0xa7, 0xcc, 0x1a, 0x3c, 0x68, 0x0c, 0xe1, 0xa4, 0xb1, 0x05,
  234. 0xe6, 0xe0, 0x25, 0x78, 0x58, 0x14, 0x37, 0xf5, 0x1f, 0xe3, 0x22, 0xef,
  235. 0xa8, 0x0e, 0x22, 0xa0, 0x94, 0x3a, 0xf6, 0xc9, 0x13, 0xe6, 0x06, 0xbf,
  236. 0x7f, 0x99, 0xc6, 0xcc, 0xd8, 0xc6, 0xbe, 0xd9, 0x2e, 0x24, 0xc7, 0x69,
  237. 0x8c, 0x95, 0xba, 0xf6, 0x04, 0xb3, 0x0a, 0xf4, 0xcb, 0xf0, 0xce,
  238. };
  239. static const unsigned char kMsg[] = { 1, 2, 3, 4 };
  240. static const unsigned char kSignature[] = {
  241. 0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d,
  242. 0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e,
  243. 0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04,
  244. 0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09,
  245. 0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67,
  246. 0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a,
  247. 0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda,
  248. 0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48,
  249. 0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04,
  250. 0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7,
  251. 0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42,
  252. };
  253. /*
  254. * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
  255. * PrivateKeyInfo.
  256. */
  257. static const unsigned char kExampleRSAKeyPKCS8[] = {
  258. 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  259. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
  260. 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
  261. 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
  262. 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
  263. 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
  264. 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
  265. 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
  266. 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
  267. 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
  268. 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
  269. 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
  270. 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
  271. 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
  272. 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
  273. 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
  274. 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
  275. 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
  276. 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
  277. 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
  278. 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
  279. 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
  280. 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
  281. 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
  282. 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
  283. 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
  284. 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
  285. 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
  286. 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
  287. 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
  288. 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
  289. 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
  290. 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
  291. 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
  292. 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
  293. 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
  294. 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
  295. 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
  296. 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
  297. 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
  298. 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
  299. 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
  300. 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
  301. 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
  302. 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
  303. 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
  304. 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
  305. 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
  306. 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
  307. 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
  308. 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
  309. 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
  310. 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  311. };
  312. #ifndef OPENSSL_NO_EC
  313. /*
  314. * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
  315. * structure.
  316. */
  317. static const unsigned char kExampleECKeyDER[] = {
  318. 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
  319. 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
  320. 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
  321. 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  322. 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
  323. 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
  324. 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
  325. 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
  326. 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
  327. 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
  328. 0xc1,
  329. };
  330. /*
  331. * kExampleBadECKeyDER is a sample EC private key encoded as an ECPrivateKey
  332. * structure. The private key is equal to the order and will fail to import
  333. */
  334. static const unsigned char kExampleBadECKeyDER[] = {
  335. 0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
  336. 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
  337. 0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF,
  338. 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  339. 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3,
  340. 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00,
  341. 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  342. 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
  343. 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
  344. };
  345. /* prime256v1 */
  346. static const unsigned char kExampleECPubKeyDER[] = {
  347. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  348. 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  349. 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
  350. 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
  351. 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
  352. 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
  353. 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
  354. 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
  355. };
  356. /*
  357. * kExampleBadECPubKeyDER is a sample EC public key with a wrong OID
  358. * 1.2.840.10045.2.2 instead of 1.2.840.10045.2.1 - EC Public Key
  359. */
  360. static const unsigned char kExampleBadECPubKeyDER[] = {
  361. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  362. 0x02, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  363. 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
  364. 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
  365. 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
  366. 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
  367. 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
  368. 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
  369. };
  370. static const unsigned char pExampleECParamDER[] = {
  371. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
  372. };
  373. static const unsigned char kExampleED25519KeyDER[] = {
  374. 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70,
  375. 0x04, 0x22, 0x04, 0x20, 0xba, 0x7b, 0xba, 0x20, 0x1b, 0x02, 0x75, 0x3a,
  376. 0xe8, 0x88, 0xfe, 0x00, 0xcd, 0x8b, 0xc6, 0xf4, 0x5c, 0x47, 0x09, 0x46,
  377. 0x66, 0xe4, 0x72, 0x85, 0x25, 0x26, 0x5e, 0x12, 0x33, 0x48, 0xf6, 0x50
  378. };
  379. static const unsigned char kExampleED25519PubKeyDER[] = {
  380. 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00,
  381. 0xf5, 0xc5, 0xeb, 0x52, 0x3e, 0x7d, 0x07, 0x86, 0xb2, 0x55, 0x07, 0x45,
  382. 0xef, 0x5b, 0x7c, 0x20, 0xe8, 0x66, 0x28, 0x30, 0x3c, 0x8a, 0x82, 0x40,
  383. 0x97, 0xa3, 0x08, 0xdc, 0x65, 0x80, 0x39, 0x29
  384. };
  385. #endif
  386. typedef struct APK_DATA_st {
  387. const unsigned char *kder;
  388. size_t size;
  389. const char *keytype;
  390. int evptype;
  391. int check;
  392. int pub_check;
  393. int param_check;
  394. int type; /* 0 for private, 1 for public, 2 for params */
  395. } APK_DATA;
  396. static APK_DATA keydata[] = {
  397. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), "RSA", EVP_PKEY_RSA},
  398. {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), "RSA", EVP_PKEY_RSA},
  399. #ifndef OPENSSL_NO_EC
  400. {kExampleECKeyDER, sizeof(kExampleECKeyDER), "EC", EVP_PKEY_EC}
  401. #endif
  402. };
  403. static APK_DATA keycheckdata[] = {
  404. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), "RSA", EVP_PKEY_RSA, 1, 1, 1,
  405. 0},
  406. {kExampleBadRSAKeyDER, sizeof(kExampleBadRSAKeyDER), "RSA", EVP_PKEY_RSA,
  407. 0, 1, 1, 0},
  408. #ifndef OPENSSL_NO_EC
  409. {kExampleECKeyDER, sizeof(kExampleECKeyDER), "EC", EVP_PKEY_EC, 1, 1, 1, 0},
  410. /* group is also associated in our pub key */
  411. {kExampleECPubKeyDER, sizeof(kExampleECPubKeyDER), "EC", EVP_PKEY_EC, 0, 1,
  412. 1, 1},
  413. {pExampleECParamDER, sizeof(pExampleECParamDER), "EC", EVP_PKEY_EC, 0, 0, 1,
  414. 2},
  415. {kExampleED25519KeyDER, sizeof(kExampleED25519KeyDER), "ED25519",
  416. EVP_PKEY_ED25519, 1, 1, 1, 0},
  417. {kExampleED25519PubKeyDER, sizeof(kExampleED25519PubKeyDER), "ED25519",
  418. EVP_PKEY_ED25519, 0, 1, 1, 1},
  419. #endif
  420. };
  421. static EVP_PKEY *load_example_key(const char *keytype,
  422. const unsigned char *data, size_t data_len)
  423. {
  424. const unsigned char **pdata = &data;
  425. EVP_PKEY *pkey = NULL;
  426. OSSL_DECODER_CTX *dctx =
  427. OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", NULL, keytype, 0,
  428. testctx, testpropq);
  429. /* |pkey| will be NULL on error */
  430. (void)OSSL_DECODER_from_data(dctx, pdata, &data_len);
  431. OSSL_DECODER_CTX_free(dctx);
  432. return pkey;
  433. }
  434. static EVP_PKEY *load_example_rsa_key(void)
  435. {
  436. return load_example_key("RSA", kExampleRSAKeyDER,
  437. sizeof(kExampleRSAKeyDER));
  438. }
  439. #ifndef OPENSSL_NO_DSA
  440. static EVP_PKEY *load_example_dsa_key(void)
  441. {
  442. return load_example_key("DSA", kExampleDSAKeyDER,
  443. sizeof(kExampleDSAKeyDER));
  444. }
  445. #endif
  446. static EVP_PKEY *load_example_hmac_key(void)
  447. {
  448. EVP_PKEY *pkey = NULL;
  449. unsigned char key[] = {
  450. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  451. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  452. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  453. };
  454. pkey = EVP_PKEY_new_raw_private_key_ex(testctx, "HMAC",
  455. NULL, key, sizeof(key));
  456. if (!TEST_ptr(pkey))
  457. return NULL;
  458. return pkey;
  459. }
  460. static int test_EVP_set_default_properties(void)
  461. {
  462. OSSL_LIB_CTX *ctx;
  463. EVP_MD *md = NULL;
  464. int res = 0;
  465. if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())
  466. || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
  467. goto err;
  468. EVP_MD_free(md);
  469. md = NULL;
  470. if (!TEST_true(EVP_set_default_properties(ctx, "provider=fizzbang"))
  471. || !TEST_ptr_null(md = EVP_MD_fetch(ctx, "sha256", NULL))
  472. || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", "-provider")))
  473. goto err;
  474. EVP_MD_free(md);
  475. md = NULL;
  476. if (!TEST_true(EVP_set_default_properties(ctx, NULL))
  477. || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
  478. goto err;
  479. res = 1;
  480. err:
  481. EVP_MD_free(md);
  482. OSSL_LIB_CTX_free(ctx);
  483. return res;
  484. }
  485. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
  486. static int test_fromdata(char *keytype, OSSL_PARAM *params)
  487. {
  488. EVP_PKEY_CTX *pctx = NULL;
  489. EVP_PKEY *pkey = NULL;
  490. int testresult = 0;
  491. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, keytype, testpropq)))
  492. goto err;
  493. if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  494. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
  495. params), 0))
  496. goto err;
  497. if (!TEST_ptr(pkey))
  498. goto err;
  499. testresult = 1;
  500. err:
  501. EVP_PKEY_free(pkey);
  502. EVP_PKEY_CTX_free(pctx);
  503. return testresult;
  504. }
  505. #endif /* !OPENSSL_NO_DH || !OPENSSL_NO_DSA || !OPENSSL_NO_EC */
  506. /*
  507. * Test combinations of private, public, missing and private + public key
  508. * params to ensure they are all accepted
  509. */
  510. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
  511. static int test_EVP_PKEY_ffc_priv_pub(char *keytype)
  512. {
  513. OSSL_PARAM_BLD *bld = NULL;
  514. OSSL_PARAM *params = NULL;
  515. BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
  516. int ret = 0;
  517. /*
  518. * Setup the parameters for our pkey object. For our purposes they don't
  519. * have to actually be *valid* parameters. We just need to set something.
  520. */
  521. if (!TEST_ptr(p = BN_new())
  522. || !TEST_ptr(q = BN_new())
  523. || !TEST_ptr(g = BN_new())
  524. || !TEST_ptr(pub = BN_new())
  525. || !TEST_ptr(priv = BN_new()))
  526. goto err;
  527. /* Test !priv and !pub */
  528. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  529. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  530. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  531. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g)))
  532. goto err;
  533. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  534. goto err;
  535. if (!test_fromdata(keytype, params))
  536. goto err;
  537. OSSL_PARAM_free(params);
  538. params = NULL;
  539. OSSL_PARAM_BLD_free(bld);
  540. /* Test priv and !pub */
  541. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  542. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  543. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  544. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  545. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  546. priv)))
  547. goto err;
  548. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  549. goto err;
  550. if (!test_fromdata(keytype, params))
  551. goto err;
  552. OSSL_PARAM_free(params);
  553. params = NULL;
  554. OSSL_PARAM_BLD_free(bld);
  555. /* Test !priv and pub */
  556. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  557. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  558. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  559. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  560. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  561. pub)))
  562. goto err;
  563. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  564. goto err;
  565. if (!test_fromdata(keytype, params))
  566. goto err;
  567. OSSL_PARAM_free(params);
  568. params = NULL;
  569. OSSL_PARAM_BLD_free(bld);
  570. /* Test priv and pub */
  571. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  572. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  573. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  574. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  575. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  576. pub))
  577. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  578. priv)))
  579. goto err;
  580. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  581. goto err;
  582. if (!test_fromdata(keytype, params))
  583. goto err;
  584. ret = 1;
  585. err:
  586. OSSL_PARAM_free(params);
  587. OSSL_PARAM_BLD_free(bld);
  588. BN_free(p);
  589. BN_free(q);
  590. BN_free(g);
  591. BN_free(pub);
  592. BN_free(priv);
  593. return ret;
  594. }
  595. #endif /* !OPENSSL_NO_DH || !OPENSSL_NO_DSA */
  596. /*
  597. * Test combinations of private, public, missing and private + public key
  598. * params to ensure they are all accepted for EC keys
  599. */
  600. #ifndef OPENSSL_NO_EC
  601. static unsigned char ec_priv[] = {
  602. 0xe9, 0x25, 0xf7, 0x66, 0x58, 0xa4, 0xdd, 0x99, 0x61, 0xe7, 0xe8, 0x23,
  603. 0x85, 0xc2, 0xe8, 0x33, 0x27, 0xc5, 0x5c, 0xeb, 0xdb, 0x43, 0x9f, 0xd5,
  604. 0xf2, 0x5a, 0x75, 0x55, 0xd0, 0x2e, 0x6d, 0x16
  605. };
  606. static unsigned char ec_pub[] = {
  607. 0x04, 0xad, 0x11, 0x90, 0x77, 0x4b, 0x46, 0xee, 0x72, 0x51, 0x15, 0x97,
  608. 0x4a, 0x6a, 0xa7, 0xaf, 0x59, 0xfa, 0x4b, 0xf2, 0x41, 0xc8, 0x3a, 0x81,
  609. 0x23, 0xb6, 0x90, 0x04, 0x6c, 0x67, 0x66, 0xd0, 0xdc, 0xf2, 0x15, 0x1d,
  610. 0x41, 0x61, 0xb7, 0x95, 0x85, 0x38, 0x5a, 0x84, 0x56, 0xe8, 0xb3, 0x0e,
  611. 0xf5, 0xc6, 0x5d, 0xa4, 0x54, 0x26, 0xb0, 0xf7, 0xa5, 0x4a, 0x33, 0xf1,
  612. 0x08, 0x09, 0xb8, 0xdb, 0x03
  613. };
  614. static int test_EC_priv_pub(void)
  615. {
  616. OSSL_PARAM_BLD *bld = NULL;
  617. OSSL_PARAM *params = NULL;
  618. BIGNUM *priv = NULL;
  619. int ret = 0;
  620. /*
  621. * Setup the parameters for our pkey object. For our purposes they don't
  622. * have to actually be *valid* parameters. We just need to set something.
  623. */
  624. if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
  625. goto err;
  626. /* Test !priv and !pub */
  627. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  628. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  629. OSSL_PKEY_PARAM_GROUP_NAME,
  630. "P-256", 0)))
  631. goto err;
  632. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  633. goto err;
  634. if (!test_fromdata("EC", params))
  635. goto err;
  636. OSSL_PARAM_free(params);
  637. params = NULL;
  638. OSSL_PARAM_BLD_free(bld);
  639. /* Test priv and !pub */
  640. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  641. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  642. OSSL_PKEY_PARAM_GROUP_NAME,
  643. "P-256", 0))
  644. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  645. priv)))
  646. goto err;
  647. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  648. goto err;
  649. if (!test_fromdata("EC", params))
  650. goto err;
  651. OSSL_PARAM_free(params);
  652. params = NULL;
  653. OSSL_PARAM_BLD_free(bld);
  654. /* Test !priv and pub */
  655. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  656. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  657. OSSL_PKEY_PARAM_GROUP_NAME,
  658. "P-256", 0))
  659. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  660. OSSL_PKEY_PARAM_PUB_KEY,
  661. ec_pub, sizeof(ec_pub))))
  662. goto err;
  663. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  664. goto err;
  665. if (!test_fromdata("EC", params))
  666. goto err;
  667. OSSL_PARAM_free(params);
  668. params = NULL;
  669. OSSL_PARAM_BLD_free(bld);
  670. /* Test priv and pub */
  671. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  672. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  673. OSSL_PKEY_PARAM_GROUP_NAME,
  674. "P-256", 0))
  675. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  676. OSSL_PKEY_PARAM_PUB_KEY,
  677. ec_pub, sizeof(ec_pub)))
  678. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  679. priv)))
  680. goto err;
  681. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  682. goto err;
  683. if (!test_fromdata("EC", params))
  684. goto err;
  685. ret = 1;
  686. err:
  687. OSSL_PARAM_free(params);
  688. OSSL_PARAM_BLD_free(bld);
  689. BN_free(priv);
  690. return ret;
  691. }
  692. /* Test that using a legacy EC key with only a private key in it works */
  693. # ifndef OPENSSL_NO_DEPRECATED_3_0
  694. static int test_EC_priv_only_legacy(void)
  695. {
  696. BIGNUM *priv = NULL;
  697. int ret = 0;
  698. EC_KEY *eckey = NULL;
  699. EVP_PKEY *pkey = NULL, *dup_pk = NULL;
  700. EVP_MD_CTX *ctx = NULL;
  701. /* Create the low level EC_KEY */
  702. if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
  703. goto err;
  704. eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  705. if (!TEST_ptr(eckey))
  706. goto err;
  707. if (!TEST_true(EC_KEY_set_private_key(eckey, priv)))
  708. goto err;
  709. pkey = EVP_PKEY_new();
  710. if (!TEST_ptr(pkey))
  711. goto err;
  712. if (!TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
  713. goto err;
  714. eckey = NULL;
  715. while (dup_pk == NULL) {
  716. ret = 0;
  717. ctx = EVP_MD_CTX_new();
  718. if (!TEST_ptr(ctx))
  719. goto err;
  720. /*
  721. * The EVP_DigestSignInit function should create the key on the
  722. * provider side which is sufficient for this test.
  723. */
  724. if (!TEST_true(EVP_DigestSignInit_ex(ctx, NULL, NULL, testctx,
  725. testpropq, pkey, NULL)))
  726. goto err;
  727. EVP_MD_CTX_free(ctx);
  728. ctx = NULL;
  729. if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)))
  730. goto err;
  731. /* EVP_PKEY_eq() returns -2 with missing public keys */
  732. ret = TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), -2);
  733. EVP_PKEY_free(pkey);
  734. pkey = dup_pk;
  735. if (!ret)
  736. goto err;
  737. }
  738. err:
  739. EVP_MD_CTX_free(ctx);
  740. EVP_PKEY_free(pkey);
  741. EC_KEY_free(eckey);
  742. BN_free(priv);
  743. return ret;
  744. }
  745. # endif /* OPENSSL_NO_DEPRECATED_3_0 */
  746. #endif /* OPENSSL_NO_EC */
  747. /*
  748. * n = 0 => test using legacy cipher
  749. * n = 1 => test using fetched cipher
  750. */
  751. static int test_EVP_Enveloped(int n)
  752. {
  753. int ret = 0;
  754. EVP_CIPHER_CTX *ctx = NULL;
  755. EVP_PKEY *keypair = NULL;
  756. unsigned char *kek = NULL;
  757. unsigned char iv[EVP_MAX_IV_LENGTH];
  758. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  759. int len, kek_len, ciphertext_len, plaintext_len;
  760. unsigned char ciphertext[32], plaintext[16];
  761. EVP_CIPHER *type = NULL;
  762. if (nullprov != NULL)
  763. return TEST_skip("Test does not support a non-default library context");
  764. if (n == 0)
  765. type = (EVP_CIPHER *)EVP_aes_256_cbc();
  766. else if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "AES-256-CBC",
  767. testpropq)))
  768. goto err;
  769. if (!TEST_ptr(keypair = load_example_rsa_key())
  770. || !TEST_ptr(kek = OPENSSL_zalloc(EVP_PKEY_get_size(keypair)))
  771. || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  772. || !TEST_true(EVP_SealInit(ctx, type, &kek, &kek_len, iv,
  773. &keypair, 1))
  774. || !TEST_true(EVP_SealUpdate(ctx, ciphertext, &ciphertext_len,
  775. msg, sizeof(msg)))
  776. || !TEST_true(EVP_SealFinal(ctx, ciphertext + ciphertext_len,
  777. &len)))
  778. goto err;
  779. ciphertext_len += len;
  780. if (!TEST_true(EVP_OpenInit(ctx, type, kek, kek_len, iv, keypair))
  781. || !TEST_true(EVP_OpenUpdate(ctx, plaintext, &plaintext_len,
  782. ciphertext, ciphertext_len))
  783. || !TEST_true(EVP_OpenFinal(ctx, plaintext + plaintext_len, &len)))
  784. goto err;
  785. plaintext_len += len;
  786. if (!TEST_mem_eq(msg, sizeof(msg), plaintext, plaintext_len))
  787. goto err;
  788. ret = 1;
  789. err:
  790. if (n != 0)
  791. EVP_CIPHER_free(type);
  792. OPENSSL_free(kek);
  793. EVP_PKEY_free(keypair);
  794. EVP_CIPHER_CTX_free(ctx);
  795. return ret;
  796. }
  797. /*
  798. * Test 0: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, RSA)
  799. * Test 1: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, DSA)
  800. * Test 2: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, HMAC)
  801. * Test 3: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch digest, RSA)
  802. * Test 4: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch digest, DSA)
  803. * Test 5: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch diegst, HMAC)
  804. * Test 6: Use an MD BIO to do the Update calls instead (RSA)
  805. * Test 7: Use an MD BIO to do the Update calls instead (DSA)
  806. * Test 8: Use an MD BIO to do the Update calls instead (HMAC)
  807. */
  808. static int test_EVP_DigestSignInit(int tst)
  809. {
  810. int ret = 0;
  811. EVP_PKEY *pkey = NULL;
  812. unsigned char *sig = NULL;
  813. size_t sig_len = 0;
  814. EVP_MD_CTX *md_ctx = NULL, *md_ctx_verify = NULL;
  815. EVP_MD_CTX *a_md_ctx = NULL, *a_md_ctx_verify = NULL;
  816. BIO *mdbio = NULL, *membio = NULL;
  817. size_t written;
  818. const EVP_MD *md;
  819. EVP_MD *mdexp = NULL;
  820. if (nullprov != NULL)
  821. return TEST_skip("Test does not support a non-default library context");
  822. if (tst >= 6) {
  823. membio = BIO_new(BIO_s_mem());
  824. mdbio = BIO_new(BIO_f_md());
  825. if (!TEST_ptr(membio) || !TEST_ptr(mdbio))
  826. goto out;
  827. BIO_push(mdbio, membio);
  828. if (!TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx), 0))
  829. goto out;
  830. } else {
  831. if (!TEST_ptr(a_md_ctx = md_ctx = EVP_MD_CTX_new())
  832. || !TEST_ptr(a_md_ctx_verify = md_ctx_verify = EVP_MD_CTX_new()))
  833. goto out;
  834. }
  835. if (tst == 0 || tst == 3 || tst == 6) {
  836. if (!TEST_ptr(pkey = load_example_rsa_key()))
  837. goto out;
  838. } else if (tst == 1 || tst == 4 || tst == 7) {
  839. #ifndef OPENSSL_NO_DSA
  840. if (!TEST_ptr(pkey = load_example_dsa_key()))
  841. goto out;
  842. #else
  843. ret = 1;
  844. goto out;
  845. #endif
  846. } else {
  847. if (!TEST_ptr(pkey = load_example_hmac_key()))
  848. goto out;
  849. }
  850. if (tst >= 3 && tst <= 5)
  851. md = mdexp = EVP_MD_fetch(NULL, "SHA256", NULL);
  852. else
  853. md = EVP_sha256();
  854. if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, md, NULL, pkey)))
  855. goto out;
  856. if (tst >= 6) {
  857. if (!BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written))
  858. goto out;
  859. } else {
  860. if (!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
  861. goto out;
  862. }
  863. /* Determine the size of the signature. */
  864. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len))
  865. || !TEST_ptr(sig = OPENSSL_malloc(sig_len))
  866. || !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
  867. goto out;
  868. if (tst >= 6) {
  869. if (!TEST_int_gt(BIO_reset(mdbio), 0)
  870. || !TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx_verify), 0))
  871. goto out;
  872. }
  873. /*
  874. * Ensure that the signature round-trips (Verification isn't supported for
  875. * HMAC via EVP_DigestVerify*)
  876. */
  877. if (tst != 2 && tst != 5 && tst != 8) {
  878. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, md,
  879. NULL, pkey)))
  880. goto out;
  881. if (tst >= 6) {
  882. if (!TEST_true(BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written)))
  883. goto out;
  884. } else {
  885. if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg,
  886. sizeof(kMsg))))
  887. goto out;
  888. }
  889. if (!TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))
  890. goto out;
  891. }
  892. ret = 1;
  893. out:
  894. BIO_free(membio);
  895. BIO_free(mdbio);
  896. EVP_MD_CTX_free(a_md_ctx);
  897. EVP_MD_CTX_free(a_md_ctx_verify);
  898. EVP_PKEY_free(pkey);
  899. OPENSSL_free(sig);
  900. EVP_MD_free(mdexp);
  901. return ret;
  902. }
  903. static int test_EVP_DigestVerifyInit(void)
  904. {
  905. int ret = 0;
  906. EVP_PKEY *pkey = NULL;
  907. EVP_MD_CTX *md_ctx = NULL;
  908. if (nullprov != NULL)
  909. return TEST_skip("Test does not support a non-default library context");
  910. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())
  911. || !TEST_ptr(pkey = load_example_rsa_key()))
  912. goto out;
  913. if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))
  914. || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg)))
  915. || !TEST_true(EVP_DigestVerifyFinal(md_ctx, kSignature,
  916. sizeof(kSignature))))
  917. goto out;
  918. ret = 1;
  919. out:
  920. EVP_MD_CTX_free(md_ctx);
  921. EVP_PKEY_free(pkey);
  922. return ret;
  923. }
  924. /*
  925. * Test corner cases of EVP_DigestInit/Update/Final API call behavior.
  926. */
  927. static int test_EVP_Digest(void)
  928. {
  929. int ret = 0;
  930. EVP_MD_CTX *md_ctx = NULL;
  931. unsigned char md[EVP_MAX_MD_SIZE];
  932. EVP_MD *sha256 = NULL;
  933. EVP_MD *shake256 = NULL;
  934. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
  935. goto out;
  936. if (!TEST_ptr(sha256 = EVP_MD_fetch(testctx, "sha256", testpropq))
  937. || !TEST_ptr(shake256 = EVP_MD_fetch(testctx, "shake256", testpropq)))
  938. goto out;
  939. if (!TEST_true(EVP_DigestInit_ex(md_ctx, sha256, NULL))
  940. || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
  941. || !TEST_true(EVP_DigestFinal(md_ctx, md, NULL))
  942. /* EVP_DigestFinal resets the EVP_MD_CTX. */
  943. || !TEST_ptr_eq(EVP_MD_CTX_get0_md(md_ctx), NULL))
  944. goto out;
  945. if (!TEST_true(EVP_DigestInit_ex(md_ctx, sha256, NULL))
  946. || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
  947. || !TEST_true(EVP_DigestFinal_ex(md_ctx, md, NULL))
  948. /* EVP_DigestFinal_ex does not reset the EVP_MD_CTX. */
  949. || !TEST_ptr(EVP_MD_CTX_get0_md(md_ctx))
  950. /*
  951. * EVP_DigestInit_ex with NULL type should work on
  952. * pre-initialized context.
  953. */
  954. || !TEST_true(EVP_DigestInit_ex(md_ctx, NULL, NULL)))
  955. goto out;
  956. if (!TEST_true(EVP_DigestInit_ex(md_ctx, shake256, NULL))
  957. || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
  958. || !TEST_true(EVP_DigestFinalXOF(md_ctx, md, sizeof(md)))
  959. /* EVP_DigestFinalXOF does not reset the EVP_MD_CTX. */
  960. || !TEST_ptr(EVP_MD_CTX_get0_md(md_ctx))
  961. || !TEST_true(EVP_DigestInit_ex(md_ctx, NULL, NULL)))
  962. goto out;
  963. ret = 1;
  964. out:
  965. EVP_MD_CTX_free(md_ctx);
  966. EVP_MD_free(sha256);
  967. EVP_MD_free(shake256);
  968. return ret;
  969. }
  970. static int test_d2i_AutoPrivateKey(int i)
  971. {
  972. int ret = 0;
  973. const unsigned char *p;
  974. EVP_PKEY *pkey = NULL;
  975. const APK_DATA *ak = &keydata[i];
  976. const unsigned char *input = ak->kder;
  977. size_t input_len = ak->size;
  978. int expected_id = ak->evptype;
  979. p = input;
  980. if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len))
  981. || !TEST_ptr_eq(p, input + input_len)
  982. || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
  983. goto done;
  984. ret = 1;
  985. done:
  986. EVP_PKEY_free(pkey);
  987. return ret;
  988. }
  989. #ifndef OPENSSL_NO_EC
  990. static const unsigned char ec_public_sect163k1_validxy[] = {
  991. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  992. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  993. 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  994. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x02, 0xd1, 0x7b,
  995. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  996. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
  997. };
  998. static const unsigned char ec_public_sect163k1_badx[] = {
  999. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  1000. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  1001. 0x0a, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  1002. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0xb0, 0x02, 0xd1, 0x7b,
  1003. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  1004. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
  1005. };
  1006. static const unsigned char ec_public_sect163k1_bady[] = {
  1007. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  1008. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  1009. 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  1010. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x0a, 0xd1, 0x7b,
  1011. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  1012. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0xe6
  1013. };
  1014. static struct ec_der_pub_keys_st {
  1015. const unsigned char *der;
  1016. size_t len;
  1017. int valid;
  1018. } ec_der_pub_keys[] = {
  1019. { ec_public_sect163k1_validxy, sizeof(ec_public_sect163k1_validxy), 1 },
  1020. { ec_public_sect163k1_badx, sizeof(ec_public_sect163k1_badx), 0 },
  1021. { ec_public_sect163k1_bady, sizeof(ec_public_sect163k1_bady), 0 },
  1022. };
  1023. /*
  1024. * Tests the range of the decoded EC char2 public point.
  1025. * See ec_GF2m_simple_oct2point().
  1026. */
  1027. static int test_invalide_ec_char2_pub_range_decode(int id)
  1028. {
  1029. int ret = 0;
  1030. EVP_PKEY *pkey;
  1031. pkey = load_example_key("EC", ec_der_pub_keys[id].der,
  1032. ec_der_pub_keys[id].len);
  1033. ret = (ec_der_pub_keys[id].valid && TEST_ptr(pkey))
  1034. || TEST_ptr_null(pkey);
  1035. EVP_PKEY_free(pkey);
  1036. return ret;
  1037. }
  1038. /* Tests loading a bad key in PKCS8 format */
  1039. static int test_EVP_PKCS82PKEY(void)
  1040. {
  1041. int ret = 0;
  1042. const unsigned char *derp = kExampleBadECKeyDER;
  1043. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  1044. EVP_PKEY *pkey = NULL;
  1045. if (!TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp,
  1046. sizeof(kExampleBadECKeyDER))))
  1047. goto done;
  1048. if (!TEST_ptr_eq(derp,
  1049. kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER)))
  1050. goto done;
  1051. if (!TEST_ptr_null(pkey = EVP_PKCS82PKEY(p8inf)))
  1052. goto done;
  1053. ret = 1;
  1054. done:
  1055. PKCS8_PRIV_KEY_INFO_free(p8inf);
  1056. EVP_PKEY_free(pkey);
  1057. return ret;
  1058. }
  1059. #endif
  1060. static int test_EVP_PKCS82PKEY_wrong_tag(void)
  1061. {
  1062. EVP_PKEY *pkey = NULL;
  1063. EVP_PKEY *pkey2 = NULL;
  1064. BIO *membio = NULL;
  1065. char *membuf = NULL;
  1066. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  1067. int ok = 0;
  1068. if (testctx != NULL)
  1069. /* test not supported with non-default context */
  1070. return 1;
  1071. if (!TEST_ptr(membio = BIO_new(BIO_s_mem()))
  1072. || !TEST_ptr(pkey = load_example_rsa_key())
  1073. || !TEST_int_gt(i2d_PKCS8PrivateKey_bio(membio, pkey, NULL,
  1074. NULL, 0, NULL, NULL),
  1075. 0)
  1076. || !TEST_int_gt(BIO_get_mem_data(membio, &membuf), 0)
  1077. || !TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(membio, NULL))
  1078. || !TEST_ptr(pkey2 = EVP_PKCS82PKEY(p8inf))
  1079. || !TEST_int_eq(ERR_get_error(), 0)) {
  1080. goto done;
  1081. }
  1082. ok = 1;
  1083. done:
  1084. EVP_PKEY_free(pkey);
  1085. EVP_PKEY_free(pkey2);
  1086. PKCS8_PRIV_KEY_INFO_free(p8inf);
  1087. BIO_free_all(membio);
  1088. return ok;
  1089. }
  1090. /* This uses kExampleRSAKeyDER and kExampleRSAKeyPKCS8 to verify encoding */
  1091. static int test_privatekey_to_pkcs8(void)
  1092. {
  1093. EVP_PKEY *pkey = NULL;
  1094. BIO *membio = NULL;
  1095. char *membuf = NULL;
  1096. long membuf_len = 0;
  1097. int ok = 0;
  1098. if (!TEST_ptr(membio = BIO_new(BIO_s_mem()))
  1099. || !TEST_ptr(pkey = load_example_rsa_key())
  1100. || !TEST_int_gt(i2d_PKCS8PrivateKey_bio(membio, pkey, NULL,
  1101. NULL, 0, NULL, NULL),
  1102. 0)
  1103. || !TEST_int_gt(membuf_len = BIO_get_mem_data(membio, &membuf), 0)
  1104. || !TEST_ptr(membuf)
  1105. || !TEST_mem_eq(membuf, (size_t)membuf_len,
  1106. kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8))
  1107. /*
  1108. * We try to write PEM as well, just to see that it doesn't err, but
  1109. * assume that the result is correct.
  1110. */
  1111. || !TEST_int_gt(PEM_write_bio_PKCS8PrivateKey(membio, pkey, NULL,
  1112. NULL, 0, NULL, NULL),
  1113. 0))
  1114. goto done;
  1115. ok = 1;
  1116. done:
  1117. EVP_PKEY_free(pkey);
  1118. BIO_free_all(membio);
  1119. return ok;
  1120. }
  1121. #ifndef OPENSSL_NO_EC
  1122. static const struct {
  1123. int encoding;
  1124. const char *encoding_name;
  1125. } ec_encodings[] = {
  1126. { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT },
  1127. { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP }
  1128. };
  1129. static int ec_export_get_encoding_cb(const OSSL_PARAM params[], void *arg)
  1130. {
  1131. const OSSL_PARAM *p;
  1132. const char *enc_name = NULL;
  1133. int *enc = arg;
  1134. size_t i;
  1135. *enc = -1;
  1136. if (!TEST_ptr(p = OSSL_PARAM_locate_const(params,
  1137. OSSL_PKEY_PARAM_EC_ENCODING))
  1138. || !TEST_true(OSSL_PARAM_get_utf8_string_ptr(p, &enc_name)))
  1139. return 0;
  1140. for (i = 0; i < OSSL_NELEM(ec_encodings); i++) {
  1141. if (strcasecmp(enc_name, ec_encodings[i].encoding_name) == 0) {
  1142. *enc = ec_encodings[i].encoding;
  1143. break;
  1144. }
  1145. }
  1146. return (*enc != -1);
  1147. }
  1148. static int test_EC_keygen_with_enc(int idx)
  1149. {
  1150. EVP_PKEY *params = NULL, *key = NULL;
  1151. EVP_PKEY_CTX *pctx = NULL, *kctx = NULL;
  1152. int enc;
  1153. int ret = 0;
  1154. enc = ec_encodings[idx].encoding;
  1155. /* Create key parameters */
  1156. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "EC", NULL))
  1157. || !TEST_true(EVP_PKEY_paramgen_init(pctx))
  1158. || !TEST_true(EVP_PKEY_CTX_set_group_name(pctx, "P-256"))
  1159. || !TEST_true(EVP_PKEY_CTX_set_ec_param_enc(pctx, enc))
  1160. || !TEST_true(EVP_PKEY_paramgen(pctx, &params))
  1161. || !TEST_ptr(params))
  1162. goto done;
  1163. /* Create key */
  1164. if (!TEST_ptr(kctx = EVP_PKEY_CTX_new_from_pkey(testctx, params, NULL))
  1165. || !TEST_true(EVP_PKEY_keygen_init(kctx))
  1166. || !TEST_true(EVP_PKEY_keygen(kctx, &key))
  1167. || !TEST_ptr(key))
  1168. goto done;
  1169. /* Check that the encoding got all the way into the key */
  1170. if (!TEST_true(evp_keymgmt_util_export(key, OSSL_KEYMGMT_SELECT_ALL,
  1171. ec_export_get_encoding_cb, &enc))
  1172. || !TEST_int_eq(enc, ec_encodings[idx].encoding))
  1173. goto done;
  1174. ret = 1;
  1175. done:
  1176. EVP_PKEY_free(key);
  1177. EVP_PKEY_free(params);
  1178. EVP_PKEY_CTX_free(kctx);
  1179. EVP_PKEY_CTX_free(pctx);
  1180. return ret;
  1181. }
  1182. #endif
  1183. #if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODULE)
  1184. static int test_EVP_SM2_verify(void)
  1185. {
  1186. const char *pubkey =
  1187. "-----BEGIN PUBLIC KEY-----\n"
  1188. "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEp1KLWq1ZE2jmoAnnBJE1LBGxVr18\n"
  1189. "YvvqECWCpXfAQ9qUJ+UmthnUPf0iM3SaXKHe6PlLIDyNlWMWb9RUh/yU3g==\n"
  1190. "-----END PUBLIC KEY-----\n";
  1191. const char *msg = "message digest";
  1192. const char *id = "ALICE123@YAHOO.COM";
  1193. const uint8_t signature[] = {
  1194. 0x30, 0x44, 0x02, 0x20, 0x5b, 0xdb, 0xab, 0x81, 0x4f, 0xbb,
  1195. 0x8b, 0x69, 0xb1, 0x05, 0x9c, 0x99, 0x3b, 0xb2, 0x45, 0x06,
  1196. 0x4a, 0x30, 0x15, 0x59, 0x84, 0xcd, 0xee, 0x30, 0x60, 0x36,
  1197. 0x57, 0x87, 0xef, 0x5c, 0xd0, 0xbe, 0x02, 0x20, 0x43, 0x8d,
  1198. 0x1f, 0xc7, 0x77, 0x72, 0x39, 0xbb, 0x72, 0xe1, 0xfd, 0x07,
  1199. 0x58, 0xd5, 0x82, 0xc8, 0x2d, 0xba, 0x3b, 0x2c, 0x46, 0x24,
  1200. 0xe3, 0x50, 0xff, 0x04, 0xc7, 0xa0, 0x71, 0x9f, 0xa4, 0x70
  1201. };
  1202. int rc = 0;
  1203. BIO *bio = NULL;
  1204. EVP_PKEY *pkey = NULL;
  1205. EVP_MD_CTX *mctx = NULL;
  1206. EVP_PKEY_CTX *pctx = NULL;
  1207. EVP_MD *sm3 = NULL;
  1208. bio = BIO_new_mem_buf(pubkey, strlen(pubkey));
  1209. if (!TEST_true(bio != NULL))
  1210. goto done;
  1211. pkey = PEM_read_bio_PUBKEY_ex(bio, NULL, NULL, NULL, testctx, testpropq);
  1212. if (!TEST_true(pkey != NULL))
  1213. goto done;
  1214. if (!TEST_true(EVP_PKEY_is_a(pkey, "SM2")))
  1215. goto done;
  1216. if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
  1217. goto done;
  1218. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
  1219. goto done;
  1220. EVP_MD_CTX_set_pkey_ctx(mctx, pctx);
  1221. if (!TEST_ptr(sm3 = EVP_MD_fetch(testctx, "sm3", testpropq)))
  1222. goto done;
  1223. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, sm3, NULL, pkey)))
  1224. goto done;
  1225. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, id, strlen(id)), 0))
  1226. goto done;
  1227. if (!TEST_true(EVP_DigestVerifyUpdate(mctx, msg, strlen(msg))))
  1228. goto done;
  1229. if (!TEST_true(EVP_DigestVerifyFinal(mctx, signature, sizeof(signature))))
  1230. goto done;
  1231. rc = 1;
  1232. done:
  1233. BIO_free(bio);
  1234. EVP_PKEY_free(pkey);
  1235. EVP_PKEY_CTX_free(pctx);
  1236. EVP_MD_CTX_free(mctx);
  1237. EVP_MD_free(sm3);
  1238. return rc;
  1239. }
  1240. static int test_EVP_SM2(void)
  1241. {
  1242. int ret = 0;
  1243. EVP_PKEY *pkey = NULL;
  1244. EVP_PKEY *pkeyparams = NULL;
  1245. EVP_PKEY_CTX *pctx = NULL;
  1246. EVP_PKEY_CTX *kctx = NULL;
  1247. EVP_PKEY_CTX *sctx = NULL;
  1248. size_t sig_len = 0;
  1249. unsigned char *sig = NULL;
  1250. EVP_MD_CTX *md_ctx = NULL;
  1251. EVP_MD_CTX *md_ctx_verify = NULL;
  1252. EVP_PKEY_CTX *cctx = NULL;
  1253. EVP_MD *check_md = NULL;
  1254. uint8_t ciphertext[128];
  1255. size_t ctext_len = sizeof(ciphertext);
  1256. uint8_t plaintext[8];
  1257. size_t ptext_len = sizeof(plaintext);
  1258. uint8_t sm2_id[] = {1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r'};
  1259. OSSL_PARAM sparams[2] = {OSSL_PARAM_END, OSSL_PARAM_END};
  1260. OSSL_PARAM gparams[2] = {OSSL_PARAM_END, OSSL_PARAM_END};
  1261. int i;
  1262. char mdname[OSSL_MAX_NAME_SIZE];
  1263. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx,
  1264. "SM2", testpropq)))
  1265. goto done;
  1266. if (!TEST_true(EVP_PKEY_paramgen_init(pctx) == 1))
  1267. goto done;
  1268. if (!TEST_true(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2)))
  1269. goto done;
  1270. if (!TEST_true(EVP_PKEY_paramgen(pctx, &pkeyparams)))
  1271. goto done;
  1272. if (!TEST_ptr(kctx = EVP_PKEY_CTX_new_from_pkey(testctx,
  1273. pkeyparams, testpropq)))
  1274. goto done;
  1275. if (!TEST_true(EVP_PKEY_keygen_init(kctx)))
  1276. goto done;
  1277. if (!TEST_true(EVP_PKEY_keygen(kctx, &pkey)))
  1278. goto done;
  1279. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
  1280. goto done;
  1281. if (!TEST_ptr(md_ctx_verify = EVP_MD_CTX_new()))
  1282. goto done;
  1283. if (!TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
  1284. goto done;
  1285. EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
  1286. EVP_MD_CTX_set_pkey_ctx(md_ctx_verify, sctx);
  1287. if (!TEST_ptr(check_md = EVP_MD_fetch(testctx, "sm3", testpropq)))
  1288. goto done;
  1289. if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, check_md, NULL, pkey)))
  1290. goto done;
  1291. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
  1292. goto done;
  1293. if (!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
  1294. goto done;
  1295. /* Determine the size of the signature. */
  1296. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len)))
  1297. goto done;
  1298. if (!TEST_ptr(sig = OPENSSL_malloc(sig_len)))
  1299. goto done;
  1300. if (!TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
  1301. goto done;
  1302. /* Ensure that the signature round-trips. */
  1303. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, check_md, NULL,
  1304. pkey)))
  1305. goto done;
  1306. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
  1307. goto done;
  1308. if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))))
  1309. goto done;
  1310. if (!TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))
  1311. goto done;
  1312. /* now check encryption/decryption */
  1313. gparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST,
  1314. mdname, sizeof(mdname));
  1315. for (i = 0; i < 2; i++) {
  1316. const char *mdnames[] = {
  1317. #ifndef OPENSSL_NO_SM3
  1318. "SM3",
  1319. #else
  1320. NULL,
  1321. #endif
  1322. "SHA2-256" };
  1323. EVP_PKEY_CTX_free(cctx);
  1324. if (mdnames[i] == NULL)
  1325. continue;
  1326. sparams[0] =
  1327. OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST,
  1328. (char *)mdnames[i], 0);
  1329. if (!TEST_ptr(cctx = EVP_PKEY_CTX_new_from_pkey(testctx,
  1330. pkey, testpropq)))
  1331. goto done;
  1332. if (!TEST_true(EVP_PKEY_encrypt_init(cctx)))
  1333. goto done;
  1334. if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams)))
  1335. goto done;
  1336. if (!TEST_true(EVP_PKEY_encrypt(cctx, ciphertext, &ctext_len, kMsg,
  1337. sizeof(kMsg))))
  1338. goto done;
  1339. if (!TEST_true(EVP_PKEY_decrypt_init(cctx)))
  1340. goto done;
  1341. if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams)))
  1342. goto done;
  1343. if (!TEST_true(EVP_PKEY_decrypt(cctx, plaintext, &ptext_len, ciphertext,
  1344. ctext_len)))
  1345. goto done;
  1346. if (!TEST_true(EVP_PKEY_CTX_get_params(cctx, gparams)))
  1347. goto done;
  1348. /*
  1349. * Test we're still using the digest we think we are.
  1350. * Because of aliases, the easiest is to fetch the digest and
  1351. * check the name with EVP_MD_is_a().
  1352. */
  1353. EVP_MD_free(check_md);
  1354. if (!TEST_ptr(check_md = EVP_MD_fetch(testctx, mdname, testpropq)))
  1355. goto done;
  1356. if (!TEST_true(EVP_MD_is_a(check_md, mdnames[i]))) {
  1357. TEST_info("Fetched md %s isn't %s", mdname, mdnames[i]);
  1358. goto done;
  1359. }
  1360. if (!TEST_true(ptext_len == sizeof(kMsg)))
  1361. goto done;
  1362. if (!TEST_true(memcmp(plaintext, kMsg, sizeof(kMsg)) == 0))
  1363. goto done;
  1364. }
  1365. ret = 1;
  1366. done:
  1367. EVP_PKEY_CTX_free(pctx);
  1368. EVP_PKEY_CTX_free(kctx);
  1369. EVP_PKEY_CTX_free(sctx);
  1370. EVP_PKEY_CTX_free(cctx);
  1371. EVP_PKEY_free(pkey);
  1372. EVP_PKEY_free(pkeyparams);
  1373. EVP_MD_CTX_free(md_ctx);
  1374. EVP_MD_CTX_free(md_ctx_verify);
  1375. EVP_MD_free(check_md);
  1376. OPENSSL_free(sig);
  1377. return ret;
  1378. }
  1379. #endif
  1380. static struct keys_st {
  1381. int type;
  1382. char *priv;
  1383. char *pub;
  1384. } keys[] = {
  1385. {
  1386. EVP_PKEY_HMAC, "0123456789", NULL
  1387. #ifndef OPENSSL_NO_POLY1305
  1388. }, {
  1389. EVP_PKEY_POLY1305, "01234567890123456789012345678901", NULL
  1390. #endif
  1391. #ifndef OPENSSL_NO_SIPHASH
  1392. }, {
  1393. EVP_PKEY_SIPHASH, "0123456789012345", NULL
  1394. #endif
  1395. },
  1396. #ifndef OPENSSL_NO_EC
  1397. {
  1398. EVP_PKEY_X25519, "01234567890123456789012345678901",
  1399. "abcdefghijklmnopqrstuvwxyzabcdef"
  1400. }, {
  1401. EVP_PKEY_ED25519, "01234567890123456789012345678901",
  1402. "abcdefghijklmnopqrstuvwxyzabcdef"
  1403. }, {
  1404. EVP_PKEY_X448,
  1405. "01234567890123456789012345678901234567890123456789012345",
  1406. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd"
  1407. }, {
  1408. EVP_PKEY_ED448,
  1409. "012345678901234567890123456789012345678901234567890123456",
  1410. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde"
  1411. }
  1412. #endif
  1413. };
  1414. static int test_set_get_raw_keys_int(int tst, int pub, int uselibctx)
  1415. {
  1416. int ret = 0;
  1417. unsigned char buf[80];
  1418. unsigned char *in;
  1419. size_t inlen, len = 0;
  1420. EVP_PKEY *pkey;
  1421. /* Check if this algorithm supports public keys */
  1422. if (pub && keys[tst].pub == NULL)
  1423. return 1;
  1424. memset(buf, 0, sizeof(buf));
  1425. if (pub) {
  1426. #ifndef OPENSSL_NO_EC
  1427. inlen = strlen(keys[tst].pub);
  1428. in = (unsigned char *)keys[tst].pub;
  1429. if (uselibctx) {
  1430. pkey = EVP_PKEY_new_raw_public_key_ex(
  1431. testctx,
  1432. OBJ_nid2sn(keys[tst].type),
  1433. NULL,
  1434. in,
  1435. inlen);
  1436. } else {
  1437. pkey = EVP_PKEY_new_raw_public_key(keys[tst].type,
  1438. NULL,
  1439. in,
  1440. inlen);
  1441. }
  1442. #else
  1443. return 1;
  1444. #endif
  1445. } else {
  1446. inlen = strlen(keys[tst].priv);
  1447. in = (unsigned char *)keys[tst].priv;
  1448. if (uselibctx) {
  1449. pkey = EVP_PKEY_new_raw_private_key_ex(
  1450. testctx, OBJ_nid2sn(keys[tst].type),
  1451. NULL,
  1452. in,
  1453. inlen);
  1454. } else {
  1455. pkey = EVP_PKEY_new_raw_private_key(keys[tst].type,
  1456. NULL,
  1457. in,
  1458. inlen);
  1459. }
  1460. }
  1461. if (!TEST_ptr(pkey)
  1462. || !TEST_int_eq(EVP_PKEY_eq(pkey, pkey), 1)
  1463. || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, NULL, &len)))
  1464. || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, NULL, &len)))
  1465. || !TEST_true(len == inlen)
  1466. || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, buf, &len)))
  1467. || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, buf, &len)))
  1468. || !TEST_mem_eq(in, inlen, buf, len))
  1469. goto done;
  1470. ret = 1;
  1471. done:
  1472. EVP_PKEY_free(pkey);
  1473. return ret;
  1474. }
  1475. static int test_set_get_raw_keys(int tst)
  1476. {
  1477. return (nullprov != NULL || test_set_get_raw_keys_int(tst, 0, 0))
  1478. && test_set_get_raw_keys_int(tst, 0, 1)
  1479. && (nullprov != NULL || test_set_get_raw_keys_int(tst, 1, 0))
  1480. && test_set_get_raw_keys_int(tst, 1, 1);
  1481. }
  1482. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1483. static int pkey_custom_check(EVP_PKEY *pkey)
  1484. {
  1485. return 0xbeef;
  1486. }
  1487. static int pkey_custom_pub_check(EVP_PKEY *pkey)
  1488. {
  1489. return 0xbeef;
  1490. }
  1491. static int pkey_custom_param_check(EVP_PKEY *pkey)
  1492. {
  1493. return 0xbeef;
  1494. }
  1495. static EVP_PKEY_METHOD *custom_pmeth;
  1496. #endif
  1497. static int test_EVP_PKEY_check(int i)
  1498. {
  1499. int ret = 0;
  1500. EVP_PKEY *pkey = NULL;
  1501. EVP_PKEY_CTX *ctx = NULL;
  1502. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1503. EVP_PKEY_CTX *ctx2 = NULL;
  1504. #endif
  1505. const APK_DATA *ak = &keycheckdata[i];
  1506. const unsigned char *input = ak->kder;
  1507. size_t input_len = ak->size;
  1508. int expected_id = ak->evptype;
  1509. int expected_check = ak->check;
  1510. int expected_pub_check = ak->pub_check;
  1511. int expected_param_check = ak->param_check;
  1512. int type = ak->type;
  1513. if (!TEST_ptr(pkey = load_example_key(ak->keytype, input, input_len)))
  1514. goto done;
  1515. if (type == 0
  1516. && !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
  1517. goto done;
  1518. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
  1519. goto done;
  1520. if (!TEST_int_eq(EVP_PKEY_check(ctx), expected_check))
  1521. goto done;
  1522. if (!TEST_int_eq(EVP_PKEY_public_check(ctx), expected_pub_check))
  1523. goto done;
  1524. if (!TEST_int_eq(EVP_PKEY_param_check(ctx), expected_param_check))
  1525. goto done;
  1526. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1527. ctx2 = EVP_PKEY_CTX_new_id(0xdefaced, NULL);
  1528. /* assign the pkey directly, as an internal test */
  1529. EVP_PKEY_up_ref(pkey);
  1530. ctx2->pkey = pkey;
  1531. if (!TEST_int_eq(EVP_PKEY_check(ctx2), 0xbeef))
  1532. goto done;
  1533. if (!TEST_int_eq(EVP_PKEY_public_check(ctx2), 0xbeef))
  1534. goto done;
  1535. if (!TEST_int_eq(EVP_PKEY_param_check(ctx2), 0xbeef))
  1536. goto done;
  1537. #endif
  1538. ret = 1;
  1539. done:
  1540. EVP_PKEY_CTX_free(ctx);
  1541. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1542. EVP_PKEY_CTX_free(ctx2);
  1543. #endif
  1544. EVP_PKEY_free(pkey);
  1545. return ret;
  1546. }
  1547. #ifndef OPENSSL_NO_CMAC
  1548. static int get_cmac_val(EVP_PKEY *pkey, unsigned char *mac)
  1549. {
  1550. EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
  1551. const char msg[] = "Hello World";
  1552. size_t maclen;
  1553. int ret = 1;
  1554. if (!TEST_ptr(mdctx)
  1555. || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, NULL, testctx,
  1556. testpropq, pkey, NULL))
  1557. || !TEST_true(EVP_DigestSignUpdate(mdctx, msg, sizeof(msg)))
  1558. || !TEST_true(EVP_DigestSignFinal(mdctx, mac, &maclen))
  1559. || !TEST_size_t_eq(maclen, AES_BLOCK_SIZE))
  1560. ret = 0;
  1561. EVP_MD_CTX_free(mdctx);
  1562. return ret;
  1563. }
  1564. static int test_CMAC_keygen(void)
  1565. {
  1566. static unsigned char key[] = {
  1567. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  1568. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  1569. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  1570. };
  1571. EVP_PKEY_CTX *kctx = NULL;
  1572. int ret = 0;
  1573. EVP_PKEY *pkey = NULL;
  1574. unsigned char mac[AES_BLOCK_SIZE];
  1575. # if !defined(OPENSSL_NO_DEPRECATED_3_0)
  1576. unsigned char mac2[AES_BLOCK_SIZE];
  1577. # endif
  1578. if (nullprov != NULL)
  1579. return TEST_skip("Test does not support a non-default library context");
  1580. /*
  1581. * This is a legacy method for CMACs, but should still work.
  1582. * This verifies that it works without an ENGINE.
  1583. */
  1584. kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_CMAC, NULL);
  1585. /* Test a CMAC key created using the "generated" method */
  1586. if (!TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)
  1587. || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
  1588. EVP_PKEY_CTRL_CIPHER,
  1589. 0, (void *)EVP_aes_256_ecb()), 0)
  1590. || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
  1591. EVP_PKEY_CTRL_SET_MAC_KEY,
  1592. sizeof(key), (void *)key), 0)
  1593. || !TEST_int_gt(EVP_PKEY_keygen(kctx, &pkey), 0)
  1594. || !TEST_ptr(pkey)
  1595. || !TEST_true(get_cmac_val(pkey, mac)))
  1596. goto done;
  1597. # if !defined(OPENSSL_NO_DEPRECATED_3_0)
  1598. EVP_PKEY_free(pkey);
  1599. /*
  1600. * Test a CMAC key using the direct method, and compare with the mac
  1601. * created above.
  1602. */
  1603. pkey = EVP_PKEY_new_CMAC_key(NULL, key, sizeof(key), EVP_aes_256_ecb());
  1604. if (!TEST_ptr(pkey)
  1605. || !TEST_true(get_cmac_val(pkey, mac2))
  1606. || !TEST_mem_eq(mac, sizeof(mac), mac2, sizeof(mac2)))
  1607. goto done;
  1608. # endif
  1609. ret = 1;
  1610. done:
  1611. EVP_PKEY_free(pkey);
  1612. EVP_PKEY_CTX_free(kctx);
  1613. return ret;
  1614. }
  1615. #endif
  1616. static int test_HKDF(void)
  1617. {
  1618. EVP_PKEY_CTX *pctx;
  1619. unsigned char out[20];
  1620. size_t outlen;
  1621. int i, ret = 0;
  1622. unsigned char salt[] = "0123456789";
  1623. unsigned char key[] = "012345678901234567890123456789";
  1624. unsigned char info[] = "infostring";
  1625. const unsigned char expected[] = {
  1626. 0xe5, 0x07, 0x70, 0x7f, 0xc6, 0x78, 0xd6, 0x54, 0x32, 0x5f, 0x7e, 0xc5,
  1627. 0x7b, 0x59, 0x3e, 0xd8, 0x03, 0x6b, 0xed, 0xca
  1628. };
  1629. size_t expectedlen = sizeof(expected);
  1630. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
  1631. goto done;
  1632. /* We do this twice to test reuse of the EVP_PKEY_CTX */
  1633. for (i = 0; i < 2; i++) {
  1634. outlen = sizeof(out);
  1635. memset(out, 0, outlen);
  1636. if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
  1637. || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
  1638. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
  1639. sizeof(salt) - 1), 0)
  1640. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
  1641. sizeof(key) - 1), 0)
  1642. || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
  1643. sizeof(info) - 1), 0)
  1644. || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
  1645. || !TEST_mem_eq(out, outlen, expected, expectedlen))
  1646. goto done;
  1647. }
  1648. ret = 1;
  1649. done:
  1650. EVP_PKEY_CTX_free(pctx);
  1651. return ret;
  1652. }
  1653. static int test_emptyikm_HKDF(void)
  1654. {
  1655. EVP_PKEY_CTX *pctx;
  1656. unsigned char out[20];
  1657. size_t outlen;
  1658. int ret = 0;
  1659. unsigned char salt[] = "9876543210";
  1660. unsigned char key[] = "";
  1661. unsigned char info[] = "stringinfo";
  1662. const unsigned char expected[] = {
  1663. 0x68, 0x81, 0xa5, 0x3e, 0x5b, 0x9c, 0x7b, 0x6f, 0x2e, 0xec, 0xc8, 0x47,
  1664. 0x7c, 0xfa, 0x47, 0x35, 0x66, 0x82, 0x15, 0x30
  1665. };
  1666. size_t expectedlen = sizeof(expected);
  1667. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
  1668. goto done;
  1669. outlen = sizeof(out);
  1670. memset(out, 0, outlen);
  1671. if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
  1672. || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
  1673. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
  1674. sizeof(salt) - 1), 0)
  1675. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
  1676. sizeof(key) - 1), 0)
  1677. || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
  1678. sizeof(info) - 1), 0)
  1679. || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
  1680. || !TEST_mem_eq(out, outlen, expected, expectedlen))
  1681. goto done;
  1682. ret = 1;
  1683. done:
  1684. EVP_PKEY_CTX_free(pctx);
  1685. return ret;
  1686. }
  1687. #ifndef OPENSSL_NO_EC
  1688. static int test_X509_PUBKEY_inplace(void)
  1689. {
  1690. int ret = 0;
  1691. X509_PUBKEY *xp = NULL;
  1692. const unsigned char *p = kExampleECPubKeyDER;
  1693. size_t input_len = sizeof(kExampleECPubKeyDER);
  1694. if (!TEST_ptr(xp = d2i_X509_PUBKEY(NULL, &p, input_len)))
  1695. goto done;
  1696. if (!TEST_ptr(X509_PUBKEY_get0(xp)))
  1697. goto done;
  1698. p = kExampleBadECPubKeyDER;
  1699. input_len = sizeof(kExampleBadECPubKeyDER);
  1700. if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len)))
  1701. goto done;
  1702. if (!TEST_true(X509_PUBKEY_get0(xp) == NULL))
  1703. goto done;
  1704. ret = 1;
  1705. done:
  1706. X509_PUBKEY_free(xp);
  1707. return ret;
  1708. }
  1709. static int test_X509_PUBKEY_dup(void)
  1710. {
  1711. int ret = 0;
  1712. X509_PUBKEY *xp = NULL, *xq = NULL;
  1713. const unsigned char *p = kExampleECPubKeyDER;
  1714. size_t input_len = sizeof(kExampleECPubKeyDER);
  1715. if (!TEST_ptr(xp = d2i_X509_PUBKEY(NULL, &p, input_len))
  1716. || !TEST_ptr(xq = X509_PUBKEY_dup(xp))
  1717. || !TEST_ptr_ne(xp, xq))
  1718. goto done;
  1719. if (!TEST_ptr(X509_PUBKEY_get0(xq))
  1720. || !TEST_ptr(X509_PUBKEY_get0(xp))
  1721. || !TEST_ptr_eq(X509_PUBKEY_get0(xq), X509_PUBKEY_get0(xp)))
  1722. goto done;
  1723. X509_PUBKEY_free(xq);
  1724. xq = NULL;
  1725. p = kExampleBadECPubKeyDER;
  1726. input_len = sizeof(kExampleBadECPubKeyDER);
  1727. if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len))
  1728. || !TEST_ptr(xq = X509_PUBKEY_dup(xp)))
  1729. goto done;
  1730. X509_PUBKEY_free(xp);
  1731. xp = NULL;
  1732. if (!TEST_true(X509_PUBKEY_get0(xq) == NULL))
  1733. goto done;
  1734. ret = 1;
  1735. done:
  1736. X509_PUBKEY_free(xp);
  1737. X509_PUBKEY_free(xq);
  1738. return ret;
  1739. }
  1740. #endif /* OPENSSL_NO_EC */
  1741. /* Test getting and setting parameters on an EVP_PKEY_CTX */
  1742. static int test_EVP_PKEY_CTX_get_set_params(EVP_PKEY *pkey)
  1743. {
  1744. EVP_MD_CTX *mdctx = NULL;
  1745. EVP_PKEY_CTX *ctx = NULL;
  1746. const OSSL_PARAM *params;
  1747. OSSL_PARAM ourparams[2], *param = ourparams, *param_md;
  1748. int ret = 0;
  1749. const EVP_MD *md;
  1750. char mdname[OSSL_MAX_NAME_SIZE];
  1751. char ssl3ms[48];
  1752. /* Initialise a sign operation */
  1753. ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq);
  1754. if (!TEST_ptr(ctx)
  1755. || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0))
  1756. goto err;
  1757. /*
  1758. * We should be able to query the parameters now.
  1759. */
  1760. params = EVP_PKEY_CTX_settable_params(ctx);
  1761. if (!TEST_ptr(params)
  1762. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  1763. OSSL_SIGNATURE_PARAM_DIGEST)))
  1764. goto err;
  1765. params = EVP_PKEY_CTX_gettable_params(ctx);
  1766. if (!TEST_ptr(params)
  1767. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  1768. OSSL_SIGNATURE_PARAM_ALGORITHM_ID))
  1769. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  1770. OSSL_SIGNATURE_PARAM_DIGEST)))
  1771. goto err;
  1772. /*
  1773. * Test getting and setting params via EVP_PKEY_CTX_set_params() and
  1774. * EVP_PKEY_CTX_get_params()
  1775. */
  1776. strcpy(mdname, "SHA512");
  1777. param_md = param;
  1778. *param++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  1779. mdname, 0);
  1780. *param++ = OSSL_PARAM_construct_end();
  1781. if (!TEST_true(EVP_PKEY_CTX_set_params(ctx, ourparams)))
  1782. goto err;
  1783. mdname[0] = '\0';
  1784. *param_md = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  1785. mdname, sizeof(mdname));
  1786. if (!TEST_true(EVP_PKEY_CTX_get_params(ctx, ourparams))
  1787. || !TEST_str_eq(mdname, "SHA512"))
  1788. goto err;
  1789. /*
  1790. * Test the TEST_PKEY_CTX_set_signature_md() and
  1791. * TEST_PKEY_CTX_get_signature_md() functions
  1792. */
  1793. if (!TEST_int_gt(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()), 0)
  1794. || !TEST_int_gt(EVP_PKEY_CTX_get_signature_md(ctx, &md), 0)
  1795. || !TEST_ptr_eq(md, EVP_sha256()))
  1796. goto err;
  1797. /*
  1798. * Test getting MD parameters via an associated EVP_PKEY_CTX
  1799. */
  1800. mdctx = EVP_MD_CTX_new();
  1801. if (!TEST_ptr(mdctx)
  1802. || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, "SHA1", testctx, testpropq,
  1803. pkey, NULL)))
  1804. goto err;
  1805. /*
  1806. * We now have an EVP_MD_CTX with an EVP_PKEY_CTX inside it. We should be
  1807. * able to obtain the digest's settable parameters from the provider.
  1808. */
  1809. params = EVP_MD_CTX_settable_params(mdctx);
  1810. if (!TEST_ptr(params)
  1811. || !TEST_int_eq(strcmp(params[0].key, OSSL_DIGEST_PARAM_SSL3_MS), 0)
  1812. /* The final key should be NULL */
  1813. || !TEST_ptr_null(params[1].key))
  1814. goto err;
  1815. param = ourparams;
  1816. memset(ssl3ms, 0, sizeof(ssl3ms));
  1817. *param++ = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
  1818. ssl3ms, sizeof(ssl3ms));
  1819. *param++ = OSSL_PARAM_construct_end();
  1820. if (!TEST_true(EVP_MD_CTX_set_params(mdctx, ourparams)))
  1821. goto err;
  1822. ret = 1;
  1823. err:
  1824. EVP_MD_CTX_free(mdctx);
  1825. EVP_PKEY_CTX_free(ctx);
  1826. return ret;
  1827. }
  1828. #ifndef OPENSSL_NO_DSA
  1829. static int test_DSA_get_set_params(void)
  1830. {
  1831. OSSL_PARAM_BLD *bld = NULL;
  1832. OSSL_PARAM *params = NULL;
  1833. BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
  1834. EVP_PKEY_CTX *pctx = NULL;
  1835. EVP_PKEY *pkey = NULL;
  1836. int ret = 0;
  1837. /*
  1838. * Setup the parameters for our DSA object. For our purposes they don't
  1839. * have to actually be *valid* parameters. We just need to set something.
  1840. */
  1841. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "DSA", NULL))
  1842. || !TEST_ptr(bld = OSSL_PARAM_BLD_new())
  1843. || !TEST_ptr(p = BN_new())
  1844. || !TEST_ptr(q = BN_new())
  1845. || !TEST_ptr(g = BN_new())
  1846. || !TEST_ptr(pub = BN_new())
  1847. || !TEST_ptr(priv = BN_new()))
  1848. goto err;
  1849. if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  1850. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  1851. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  1852. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  1853. pub))
  1854. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  1855. priv)))
  1856. goto err;
  1857. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  1858. goto err;
  1859. if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  1860. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
  1861. params), 0))
  1862. goto err;
  1863. if (!TEST_ptr(pkey))
  1864. goto err;
  1865. ret = test_EVP_PKEY_CTX_get_set_params(pkey);
  1866. err:
  1867. EVP_PKEY_free(pkey);
  1868. EVP_PKEY_CTX_free(pctx);
  1869. OSSL_PARAM_free(params);
  1870. OSSL_PARAM_BLD_free(bld);
  1871. BN_free(p);
  1872. BN_free(q);
  1873. BN_free(g);
  1874. BN_free(pub);
  1875. BN_free(priv);
  1876. return ret;
  1877. }
  1878. /*
  1879. * Test combinations of private, public, missing and private + public key
  1880. * params to ensure they are all accepted
  1881. */
  1882. static int test_DSA_priv_pub(void)
  1883. {
  1884. return test_EVP_PKEY_ffc_priv_pub("DSA");
  1885. }
  1886. #endif /* !OPENSSL_NO_DSA */
  1887. static int test_RSA_get_set_params(void)
  1888. {
  1889. OSSL_PARAM_BLD *bld = NULL;
  1890. OSSL_PARAM *params = NULL;
  1891. BIGNUM *n = NULL, *e = NULL, *d = NULL;
  1892. EVP_PKEY_CTX *pctx = NULL;
  1893. EVP_PKEY *pkey = NULL;
  1894. int ret = 0;
  1895. /*
  1896. * Setup the parameters for our RSA object. For our purposes they don't
  1897. * have to actually be *valid* parameters. We just need to set something.
  1898. */
  1899. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA", NULL))
  1900. || !TEST_ptr(bld = OSSL_PARAM_BLD_new())
  1901. || !TEST_ptr(n = BN_new())
  1902. || !TEST_ptr(e = BN_new())
  1903. || !TEST_ptr(d = BN_new()))
  1904. goto err;
  1905. if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
  1906. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
  1907. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)))
  1908. goto err;
  1909. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  1910. goto err;
  1911. if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  1912. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
  1913. params), 0))
  1914. goto err;
  1915. if (!TEST_ptr(pkey))
  1916. goto err;
  1917. ret = test_EVP_PKEY_CTX_get_set_params(pkey);
  1918. err:
  1919. EVP_PKEY_free(pkey);
  1920. EVP_PKEY_CTX_free(pctx);
  1921. OSSL_PARAM_free(params);
  1922. OSSL_PARAM_BLD_free(bld);
  1923. BN_free(n);
  1924. BN_free(e);
  1925. BN_free(d);
  1926. return ret;
  1927. }
  1928. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  1929. static int test_decrypt_null_chunks(void)
  1930. {
  1931. EVP_CIPHER_CTX* ctx = NULL;
  1932. EVP_CIPHER *cipher = NULL;
  1933. const unsigned char key[32] = {
  1934. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  1935. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  1936. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1
  1937. };
  1938. unsigned char iv[12] = {
  1939. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b
  1940. };
  1941. unsigned char msg[] = "It was the best of times, it was the worst of times";
  1942. unsigned char ciphertext[80];
  1943. unsigned char plaintext[80];
  1944. /* We initialise tmp to a non zero value on purpose */
  1945. int ctlen, ptlen, tmp = 99;
  1946. int ret = 0;
  1947. const int enc_offset = 10, dec_offset = 20;
  1948. if (!TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "ChaCha20-Poly1305", testpropq))
  1949. || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  1950. || !TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL,
  1951. key, iv))
  1952. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &ctlen, msg,
  1953. enc_offset))
  1954. /* Deliberate add a zero length update */
  1955. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp, NULL,
  1956. 0))
  1957. || !TEST_int_eq(tmp, 0)
  1958. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp,
  1959. msg + enc_offset,
  1960. sizeof(msg) - enc_offset))
  1961. || !TEST_int_eq(ctlen += tmp, sizeof(msg))
  1962. || !TEST_true(EVP_EncryptFinal(ctx, ciphertext + ctlen, &tmp))
  1963. || !TEST_int_eq(tmp, 0))
  1964. goto err;
  1965. /* Deliberately initialise tmp to a non zero value */
  1966. tmp = 99;
  1967. if (!TEST_true(EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv))
  1968. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext, &ptlen, ciphertext,
  1969. dec_offset))
  1970. /*
  1971. * Deliberately add a zero length update. We also deliberately do
  1972. * this at a different offset than for encryption.
  1973. */
  1974. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp, NULL,
  1975. 0))
  1976. || !TEST_int_eq(tmp, 0)
  1977. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp,
  1978. ciphertext + dec_offset,
  1979. ctlen - dec_offset))
  1980. || !TEST_int_eq(ptlen += tmp, sizeof(msg))
  1981. || !TEST_true(EVP_DecryptFinal(ctx, plaintext + ptlen, &tmp))
  1982. || !TEST_int_eq(tmp, 0)
  1983. || !TEST_mem_eq(msg, sizeof(msg), plaintext, ptlen))
  1984. goto err;
  1985. ret = 1;
  1986. err:
  1987. EVP_CIPHER_CTX_free(ctx);
  1988. EVP_CIPHER_free(cipher);
  1989. return ret;
  1990. }
  1991. #endif /* !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) */
  1992. #ifndef OPENSSL_NO_DH
  1993. /*
  1994. * Test combinations of private, public, missing and private + public key
  1995. * params to ensure they are all accepted
  1996. */
  1997. static int test_DH_priv_pub(void)
  1998. {
  1999. return test_EVP_PKEY_ffc_priv_pub("DH");
  2000. }
  2001. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2002. static int test_EVP_PKEY_set1_DH(void)
  2003. {
  2004. DH *x942dh = NULL, *noqdh = NULL;
  2005. EVP_PKEY *pkey1 = NULL, *pkey2 = NULL;
  2006. int ret = 0;
  2007. BIGNUM *p, *g = NULL;
  2008. if (!TEST_ptr(p = BN_new())
  2009. || !TEST_ptr(g = BN_new())
  2010. || !BN_set_word(p, 9999)
  2011. || !BN_set_word(g, 2)
  2012. || !TEST_ptr(noqdh = DH_new())
  2013. || !DH_set0_pqg(noqdh, p, NULL, g))
  2014. goto err;
  2015. p = g = NULL;
  2016. x942dh = DH_get_2048_256();
  2017. pkey1 = EVP_PKEY_new();
  2018. pkey2 = EVP_PKEY_new();
  2019. if (!TEST_ptr(x942dh)
  2020. || !TEST_ptr(noqdh)
  2021. || !TEST_ptr(pkey1)
  2022. || !TEST_ptr(pkey2))
  2023. goto err;
  2024. if(!TEST_true(EVP_PKEY_set1_DH(pkey1, x942dh))
  2025. || !TEST_int_eq(EVP_PKEY_get_id(pkey1), EVP_PKEY_DHX))
  2026. goto err;
  2027. if(!TEST_true(EVP_PKEY_set1_DH(pkey2, noqdh))
  2028. || !TEST_int_eq(EVP_PKEY_get_id(pkey2), EVP_PKEY_DH))
  2029. goto err;
  2030. ret = 1;
  2031. err:
  2032. BN_free(p);
  2033. BN_free(g);
  2034. EVP_PKEY_free(pkey1);
  2035. EVP_PKEY_free(pkey2);
  2036. DH_free(x942dh);
  2037. DH_free(noqdh);
  2038. return ret;
  2039. }
  2040. # endif /* !OPENSSL_NO_DEPRECATED_3_0 */
  2041. #endif /* !OPENSSL_NO_DH */
  2042. /*
  2043. * We test what happens with an empty template. For the sake of this test,
  2044. * the template must be ignored, and we know that's the case for RSA keys
  2045. * (this might arguably be a misfeature, but that's what we currently do,
  2046. * even in provider code, since that's how the legacy RSA implementation
  2047. * does things)
  2048. */
  2049. static int test_keygen_with_empty_template(int n)
  2050. {
  2051. EVP_PKEY_CTX *ctx = NULL;
  2052. EVP_PKEY *pkey = NULL;
  2053. EVP_PKEY *tkey = NULL;
  2054. int ret = 0;
  2055. if (nullprov != NULL)
  2056. return TEST_skip("Test does not support a non-default library context");
  2057. switch (n) {
  2058. case 0:
  2059. /* We do test with no template at all as well */
  2060. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)))
  2061. goto err;
  2062. break;
  2063. case 1:
  2064. /* Here we create an empty RSA key that serves as our template */
  2065. if (!TEST_ptr(tkey = EVP_PKEY_new())
  2066. || !TEST_true(EVP_PKEY_set_type(tkey, EVP_PKEY_RSA))
  2067. || !TEST_ptr(ctx = EVP_PKEY_CTX_new(tkey, NULL)))
  2068. goto err;
  2069. break;
  2070. }
  2071. if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  2072. || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0))
  2073. goto err;
  2074. ret = 1;
  2075. err:
  2076. EVP_PKEY_CTX_free(ctx);
  2077. EVP_PKEY_free(pkey);
  2078. EVP_PKEY_free(tkey);
  2079. return ret;
  2080. }
  2081. /*
  2082. * Test that we fail if we attempt to use an algorithm that is not available
  2083. * in the current library context (unless we are using an algorithm that
  2084. * should be made available via legacy codepaths).
  2085. *
  2086. * 0: RSA
  2087. * 1: SM2
  2088. */
  2089. static int test_pkey_ctx_fail_without_provider(int tst)
  2090. {
  2091. OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
  2092. OSSL_PROVIDER *tmpnullprov = NULL;
  2093. EVP_PKEY_CTX *pctx = NULL;
  2094. const char *keytype = NULL;
  2095. int expect_null = 0;
  2096. int ret = 0;
  2097. if (!TEST_ptr(tmpctx))
  2098. goto err;
  2099. tmpnullprov = OSSL_PROVIDER_load(tmpctx, "null");
  2100. if (!TEST_ptr(tmpnullprov))
  2101. goto err;
  2102. /*
  2103. * We check for certain algos in the null provider.
  2104. * If an algo is expected to have a provider keymgmt, contructing an
  2105. * EVP_PKEY_CTX is expected to fail (return NULL).
  2106. * Otherwise, if it's expected to have legacy support, contructing an
  2107. * EVP_PKEY_CTX is expected to succeed (return non-NULL).
  2108. */
  2109. switch (tst) {
  2110. case 0:
  2111. keytype = "RSA";
  2112. expect_null = 1;
  2113. break;
  2114. case 1:
  2115. keytype = "SM2";
  2116. expect_null = 1;
  2117. #ifdef OPENSSL_NO_EC
  2118. TEST_info("EC disable, skipping SM2 check...");
  2119. goto end;
  2120. #endif
  2121. #ifdef OPENSSL_NO_SM2
  2122. TEST_info("SM2 disable, skipping SM2 check...");
  2123. goto end;
  2124. #endif
  2125. break;
  2126. default:
  2127. TEST_error("No test for case %d", tst);
  2128. goto err;
  2129. }
  2130. pctx = EVP_PKEY_CTX_new_from_name(tmpctx, keytype, "");
  2131. if (expect_null ? !TEST_ptr_null(pctx) : !TEST_ptr(pctx))
  2132. goto err;
  2133. #if defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SM2)
  2134. end:
  2135. #endif
  2136. ret = 1;
  2137. err:
  2138. EVP_PKEY_CTX_free(pctx);
  2139. OSSL_PROVIDER_unload(tmpnullprov);
  2140. OSSL_LIB_CTX_free(tmpctx);
  2141. return ret;
  2142. }
  2143. static int test_rand_agglomeration(void)
  2144. {
  2145. EVP_RAND *rand;
  2146. EVP_RAND_CTX *ctx;
  2147. OSSL_PARAM params[3], *p = params;
  2148. int res;
  2149. unsigned int step = 7;
  2150. static unsigned char seed[] = "It does not matter how slowly you go "
  2151. "as long as you do not stop.";
  2152. unsigned char out[sizeof(seed)];
  2153. if (!TEST_int_ne(sizeof(seed) % step, 0)
  2154. || !TEST_ptr(rand = EVP_RAND_fetch(testctx, "TEST-RAND", testpropq)))
  2155. return 0;
  2156. ctx = EVP_RAND_CTX_new(rand, NULL);
  2157. EVP_RAND_free(rand);
  2158. if (!TEST_ptr(ctx))
  2159. return 0;
  2160. memset(out, 0, sizeof(out));
  2161. *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  2162. seed, sizeof(seed));
  2163. *p++ = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_MAX_REQUEST, &step);
  2164. *p = OSSL_PARAM_construct_end();
  2165. res = TEST_true(EVP_RAND_CTX_set_params(ctx, params))
  2166. && TEST_true(EVP_RAND_generate(ctx, out, sizeof(out), 0, 1, NULL, 0))
  2167. && TEST_mem_eq(seed, sizeof(seed), out, sizeof(out));
  2168. EVP_RAND_CTX_free(ctx);
  2169. return res;
  2170. }
  2171. /*
  2172. * Test that we correctly return the original or "running" IV after
  2173. * an encryption operation.
  2174. * Run multiple times for some different relevant algorithms/modes.
  2175. */
  2176. static int test_evp_iv_aes(int idx)
  2177. {
  2178. int ret = 0;
  2179. EVP_CIPHER_CTX *ctx = NULL;
  2180. unsigned char key[16] = {0x4c, 0x43, 0xdb, 0xdd, 0x42, 0x73, 0x47, 0xd1,
  2181. 0xe5, 0x62, 0x7d, 0xcd, 0x4d, 0x76, 0x4d, 0x57};
  2182. unsigned char init_iv[EVP_MAX_IV_LENGTH] =
  2183. {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
  2184. 0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34};
  2185. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8,
  2186. 9, 10, 11, 12, 13, 14, 15, 16 };
  2187. unsigned char ciphertext[32], oiv[16], iv[16];
  2188. unsigned char *ref_iv;
  2189. unsigned char cbc_state[16] = {0x10, 0x2f, 0x05, 0xcc, 0xc2, 0x55, 0x72, 0xb9,
  2190. 0x88, 0xe6, 0x4a, 0x17, 0x10, 0x74, 0x22, 0x5e};
  2191. unsigned char ofb_state[16] = {0x76, 0xe6, 0x66, 0x61, 0xd0, 0x8a, 0xe4, 0x64,
  2192. 0xdd, 0x66, 0xbf, 0x00, 0xf0, 0xe3, 0x6f, 0xfd};
  2193. unsigned char cfb_state[16] = {0x77, 0xe4, 0x65, 0x65, 0xd5, 0x8c, 0xe3, 0x6c,
  2194. 0xd4, 0x6c, 0xb4, 0x0c, 0xfd, 0xed, 0x60, 0xed};
  2195. unsigned char gcm_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
  2196. 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81};
  2197. unsigned char ccm_state[7] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98};
  2198. #ifndef OPENSSL_NO_OCB
  2199. unsigned char ocb_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
  2200. 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81};
  2201. #endif
  2202. int len = sizeof(ciphertext);
  2203. size_t ivlen, ref_len;
  2204. const EVP_CIPHER *type = NULL;
  2205. int iv_reset = 0;
  2206. if (nullprov != NULL && idx < 6)
  2207. return TEST_skip("Test does not support a non-default library context");
  2208. switch(idx) {
  2209. case 0:
  2210. type = EVP_aes_128_cbc();
  2211. /* FALLTHROUGH */
  2212. case 6:
  2213. type = (type != NULL) ? type :
  2214. EVP_CIPHER_fetch(testctx, "aes-128-cbc", testpropq);
  2215. ref_iv = cbc_state;
  2216. ref_len = sizeof(cbc_state);
  2217. iv_reset = 1;
  2218. break;
  2219. case 1:
  2220. type = EVP_aes_128_ofb();
  2221. /* FALLTHROUGH */
  2222. case 7:
  2223. type = (type != NULL) ? type :
  2224. EVP_CIPHER_fetch(testctx, "aes-128-ofb", testpropq);
  2225. ref_iv = ofb_state;
  2226. ref_len = sizeof(ofb_state);
  2227. iv_reset = 1;
  2228. break;
  2229. case 2:
  2230. type = EVP_aes_128_cfb();
  2231. /* FALLTHROUGH */
  2232. case 8:
  2233. type = (type != NULL) ? type :
  2234. EVP_CIPHER_fetch(testctx, "aes-128-cfb", testpropq);
  2235. ref_iv = cfb_state;
  2236. ref_len = sizeof(cfb_state);
  2237. iv_reset = 1;
  2238. break;
  2239. case 3:
  2240. type = EVP_aes_128_gcm();
  2241. /* FALLTHROUGH */
  2242. case 9:
  2243. type = (type != NULL) ? type :
  2244. EVP_CIPHER_fetch(testctx, "aes-128-gcm", testpropq);
  2245. ref_iv = gcm_state;
  2246. ref_len = sizeof(gcm_state);
  2247. break;
  2248. case 4:
  2249. type = EVP_aes_128_ccm();
  2250. /* FALLTHROUGH */
  2251. case 10:
  2252. type = (type != NULL) ? type :
  2253. EVP_CIPHER_fetch(testctx, "aes-128-ccm", testpropq);
  2254. ref_iv = ccm_state;
  2255. ref_len = sizeof(ccm_state);
  2256. break;
  2257. #ifdef OPENSSL_NO_OCB
  2258. case 5:
  2259. case 11:
  2260. return 1;
  2261. #else
  2262. case 5:
  2263. type = EVP_aes_128_ocb();
  2264. /* FALLTHROUGH */
  2265. case 11:
  2266. type = (type != NULL) ? type :
  2267. EVP_CIPHER_fetch(testctx, "aes-128-ocb", testpropq);
  2268. ref_iv = ocb_state;
  2269. ref_len = sizeof(ocb_state);
  2270. break;
  2271. #endif
  2272. default:
  2273. return 0;
  2274. }
  2275. if (!TEST_ptr(type)
  2276. || !TEST_ptr((ctx = EVP_CIPHER_CTX_new()))
  2277. || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv))
  2278. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg,
  2279. (int)sizeof(msg)))
  2280. || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv)))
  2281. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
  2282. || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len)))
  2283. goto err;
  2284. ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
  2285. if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen)
  2286. || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen))
  2287. goto err;
  2288. /* CBC, OFB, and CFB modes: the updated iv must be reset after reinit */
  2289. if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL))
  2290. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))))
  2291. goto err;
  2292. if (iv_reset) {
  2293. if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen))
  2294. goto err;
  2295. } else {
  2296. if (!TEST_mem_eq(ref_iv, ivlen, iv, ivlen))
  2297. goto err;
  2298. }
  2299. ret = 1;
  2300. err:
  2301. EVP_CIPHER_CTX_free(ctx);
  2302. if (idx >= 6)
  2303. EVP_CIPHER_free((EVP_CIPHER *)type);
  2304. return ret;
  2305. }
  2306. #ifndef OPENSSL_NO_DES
  2307. static int test_evp_iv_des(int idx)
  2308. {
  2309. int ret = 0;
  2310. EVP_CIPHER_CTX *ctx = NULL;
  2311. static const unsigned char key[24] = {
  2312. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  2313. 0xf1, 0xe0, 0xd3, 0xc2, 0xb5, 0xa4, 0x97, 0x86,
  2314. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  2315. };
  2316. static const unsigned char init_iv[8] = {
  2317. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  2318. };
  2319. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8,
  2320. 9, 10, 11, 12, 13, 14, 15, 16 };
  2321. unsigned char ciphertext[32], oiv[8], iv[8];
  2322. unsigned const char *ref_iv;
  2323. static const unsigned char cbc_state_des[8] = {
  2324. 0x4f, 0xa3, 0x85, 0xcd, 0x8b, 0xf3, 0x06, 0x2a
  2325. };
  2326. static const unsigned char cbc_state_3des[8] = {
  2327. 0x35, 0x27, 0x7d, 0x65, 0x6c, 0xfb, 0x50, 0xd9
  2328. };
  2329. static const unsigned char ofb_state_des[8] = {
  2330. 0xa7, 0x0d, 0x1d, 0x45, 0xf9, 0x96, 0x3f, 0x2c
  2331. };
  2332. static const unsigned char ofb_state_3des[8] = {
  2333. 0xab, 0x16, 0x24, 0xbb, 0x5b, 0xac, 0xed, 0x5e
  2334. };
  2335. static const unsigned char cfb_state_des[8] = {
  2336. 0x91, 0xeb, 0x6d, 0x29, 0x4b, 0x08, 0xbd, 0x73
  2337. };
  2338. static const unsigned char cfb_state_3des[8] = {
  2339. 0x34, 0xdd, 0xfb, 0x47, 0x33, 0x1c, 0x61, 0xf7
  2340. };
  2341. int len = sizeof(ciphertext);
  2342. size_t ivlen, ref_len;
  2343. EVP_CIPHER *type = NULL;
  2344. if (lgcyprov == NULL && idx < 3)
  2345. return TEST_skip("Test requires legacy provider to be loaded");
  2346. switch(idx) {
  2347. case 0:
  2348. type = EVP_CIPHER_fetch(testctx, "des-cbc", testpropq);
  2349. ref_iv = cbc_state_des;
  2350. ref_len = sizeof(cbc_state_des);
  2351. break;
  2352. case 1:
  2353. type = EVP_CIPHER_fetch(testctx, "des-ofb", testpropq);
  2354. ref_iv = ofb_state_des;
  2355. ref_len = sizeof(ofb_state_des);
  2356. break;
  2357. case 2:
  2358. type = EVP_CIPHER_fetch(testctx, "des-cfb", testpropq);
  2359. ref_iv = cfb_state_des;
  2360. ref_len = sizeof(cfb_state_des);
  2361. break;
  2362. case 3:
  2363. type = EVP_CIPHER_fetch(testctx, "des-ede3-cbc", testpropq);
  2364. ref_iv = cbc_state_3des;
  2365. ref_len = sizeof(cbc_state_3des);
  2366. break;
  2367. case 4:
  2368. type = EVP_CIPHER_fetch(testctx, "des-ede3-ofb", testpropq);
  2369. ref_iv = ofb_state_3des;
  2370. ref_len = sizeof(ofb_state_3des);
  2371. break;
  2372. case 5:
  2373. type = EVP_CIPHER_fetch(testctx, "des-ede3-cfb", testpropq);
  2374. ref_iv = cfb_state_3des;
  2375. ref_len = sizeof(cfb_state_3des);
  2376. break;
  2377. default:
  2378. return 0;
  2379. }
  2380. if (!TEST_ptr(type)
  2381. || !TEST_ptr((ctx = EVP_CIPHER_CTX_new()))
  2382. || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv))
  2383. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg,
  2384. (int)sizeof(msg)))
  2385. || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv)))
  2386. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
  2387. || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len)))
  2388. goto err;
  2389. ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
  2390. if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen)
  2391. || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen))
  2392. goto err;
  2393. if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL))
  2394. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))))
  2395. goto err;
  2396. if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen))
  2397. goto err;
  2398. ret = 1;
  2399. err:
  2400. EVP_CIPHER_CTX_free(ctx);
  2401. EVP_CIPHER_free(type);
  2402. return ret;
  2403. }
  2404. #endif
  2405. #ifndef OPENSSL_NO_EC
  2406. static int ecpub_nids[] = {
  2407. NID_brainpoolP256r1, NID_X9_62_prime256v1,
  2408. NID_secp384r1, NID_secp521r1,
  2409. # ifndef OPENSSL_NO_EC2M
  2410. NID_sect233k1, NID_sect233r1, NID_sect283r1,
  2411. NID_sect409k1, NID_sect409r1, NID_sect571k1, NID_sect571r1,
  2412. # endif
  2413. NID_brainpoolP384r1, NID_brainpoolP512r1
  2414. };
  2415. static int test_ecpub(int idx)
  2416. {
  2417. int ret = 0, len, savelen;
  2418. int nid;
  2419. unsigned char buf[1024];
  2420. unsigned char *p;
  2421. EVP_PKEY *pkey = NULL;
  2422. EVP_PKEY_CTX *ctx = NULL;
  2423. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2424. const unsigned char *q;
  2425. EVP_PKEY *pkey2 = NULL;
  2426. EC_KEY *ec = NULL;
  2427. # endif
  2428. if (nullprov != NULL)
  2429. return TEST_skip("Test does not support a non-default library context");
  2430. nid = ecpub_nids[idx];
  2431. ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
  2432. if (!TEST_ptr(ctx)
  2433. || !TEST_true(EVP_PKEY_keygen_init(ctx))
  2434. || !TEST_true(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid))
  2435. || !TEST_true(EVP_PKEY_keygen(ctx, &pkey)))
  2436. goto done;
  2437. len = i2d_PublicKey(pkey, NULL);
  2438. savelen = len;
  2439. if (!TEST_int_ge(len, 1)
  2440. || !TEST_int_lt(len, 1024))
  2441. goto done;
  2442. p = buf;
  2443. len = i2d_PublicKey(pkey, &p);
  2444. if (!TEST_int_ge(len, 1)
  2445. || !TEST_int_eq(len, savelen))
  2446. goto done;
  2447. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2448. /* Now try to decode the just-created DER. */
  2449. q = buf;
  2450. if (!TEST_ptr((pkey2 = EVP_PKEY_new()))
  2451. || !TEST_ptr((ec = EC_KEY_new_by_curve_name(nid)))
  2452. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey2, ec)))
  2453. goto done;
  2454. /* EC_KEY ownership transferred */
  2455. ec = NULL;
  2456. if (!TEST_ptr(d2i_PublicKey(EVP_PKEY_EC, &pkey2, &q, savelen)))
  2457. goto done;
  2458. /* The keys should match. */
  2459. if (!TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1))
  2460. goto done;
  2461. # endif
  2462. ret = 1;
  2463. done:
  2464. EVP_PKEY_CTX_free(ctx);
  2465. EVP_PKEY_free(pkey);
  2466. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2467. EVP_PKEY_free(pkey2);
  2468. EC_KEY_free(ec);
  2469. # endif
  2470. return ret;
  2471. }
  2472. #endif
  2473. static int test_EVP_rsa_pss_with_keygen_bits(void)
  2474. {
  2475. int ret = 0;
  2476. EVP_PKEY_CTX *ctx = NULL;
  2477. EVP_PKEY *pkey = NULL;
  2478. EVP_MD *md;
  2479. md = EVP_MD_fetch(testctx, "sha256", testpropq);
  2480. ret = TEST_ptr(md)
  2481. && TEST_ptr((ctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA", testpropq)))
  2482. && TEST_true(EVP_PKEY_keygen_init(ctx))
  2483. && TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 512), 0)
  2484. && TEST_true(EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx, md))
  2485. && TEST_true(EVP_PKEY_keygen(ctx, &pkey));
  2486. EVP_MD_free(md);
  2487. EVP_PKEY_free(pkey);
  2488. EVP_PKEY_CTX_free(ctx);
  2489. return ret;
  2490. }
  2491. static int success = 1;
  2492. static void md_names(const char *name, void *vctx)
  2493. {
  2494. OSSL_LIB_CTX *ctx = (OSSL_LIB_CTX *)vctx;
  2495. /* Force a namemap update */
  2496. EVP_CIPHER *aes128 = EVP_CIPHER_fetch(ctx, "AES-128-CBC", NULL);
  2497. if (!TEST_ptr(aes128))
  2498. success = 0;
  2499. EVP_CIPHER_free(aes128);
  2500. }
  2501. /*
  2502. * Test that changing the namemap in a user callback works in a names_do_all
  2503. * function.
  2504. */
  2505. static int test_names_do_all(void)
  2506. {
  2507. /* We use a custom libctx so that we know the state of the namemap */
  2508. OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
  2509. EVP_MD *sha256 = NULL;
  2510. int testresult = 0;
  2511. if (!TEST_ptr(ctx))
  2512. goto err;
  2513. sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL);
  2514. if (!TEST_ptr(sha256))
  2515. goto err;
  2516. /*
  2517. * We loop through all the names for a given digest. This should still work
  2518. * even if the namemap changes part way through.
  2519. */
  2520. if (!TEST_true(EVP_MD_names_do_all(sha256, md_names, ctx)))
  2521. goto err;
  2522. if (!TEST_true(success))
  2523. goto err;
  2524. testresult = 1;
  2525. err:
  2526. EVP_MD_free(sha256);
  2527. OSSL_LIB_CTX_free(ctx);
  2528. return testresult;
  2529. }
  2530. typedef enum OPTION_choice {
  2531. OPT_ERR = -1,
  2532. OPT_EOF = 0,
  2533. OPT_CONTEXT,
  2534. OPT_TEST_ENUM
  2535. } OPTION_CHOICE;
  2536. const OPTIONS *test_get_options(void)
  2537. {
  2538. static const OPTIONS options[] = {
  2539. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  2540. { "context", OPT_CONTEXT, '-', "Explicitly use a non-default library context" },
  2541. { NULL }
  2542. };
  2543. return options;
  2544. }
  2545. int setup_tests(void)
  2546. {
  2547. OPTION_CHOICE o;
  2548. while ((o = opt_next()) != OPT_EOF) {
  2549. switch (o) {
  2550. case OPT_CONTEXT:
  2551. /* Set up an alternate library context */
  2552. testctx = OSSL_LIB_CTX_new();
  2553. if (!TEST_ptr(testctx))
  2554. return 0;
  2555. /* Swap the libctx to test non-default context only */
  2556. nullprov = OSSL_PROVIDER_load(NULL, "null");
  2557. deflprov = OSSL_PROVIDER_load(testctx, "default");
  2558. lgcyprov = OSSL_PROVIDER_load(testctx, "legacy");
  2559. break;
  2560. case OPT_TEST_CASES:
  2561. break;
  2562. default:
  2563. return 0;
  2564. }
  2565. }
  2566. ADD_TEST(test_EVP_set_default_properties);
  2567. ADD_ALL_TESTS(test_EVP_DigestSignInit, 9);
  2568. ADD_TEST(test_EVP_DigestVerifyInit);
  2569. ADD_TEST(test_EVP_Digest);
  2570. ADD_ALL_TESTS(test_EVP_Enveloped, 2);
  2571. ADD_ALL_TESTS(test_d2i_AutoPrivateKey, OSSL_NELEM(keydata));
  2572. ADD_TEST(test_privatekey_to_pkcs8);
  2573. ADD_TEST(test_EVP_PKCS82PKEY_wrong_tag);
  2574. #ifndef OPENSSL_NO_EC
  2575. ADD_TEST(test_EVP_PKCS82PKEY);
  2576. #endif
  2577. #ifndef OPENSSL_NO_EC
  2578. ADD_ALL_TESTS(test_EC_keygen_with_enc, OSSL_NELEM(ec_encodings));
  2579. #endif
  2580. #if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODULE)
  2581. ADD_TEST(test_EVP_SM2);
  2582. ADD_TEST(test_EVP_SM2_verify);
  2583. #endif
  2584. ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys));
  2585. #ifndef OPENSSL_NO_DEPRECATED_3_0
  2586. custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0);
  2587. if (!TEST_ptr(custom_pmeth))
  2588. return 0;
  2589. EVP_PKEY_meth_set_check(custom_pmeth, pkey_custom_check);
  2590. EVP_PKEY_meth_set_public_check(custom_pmeth, pkey_custom_pub_check);
  2591. EVP_PKEY_meth_set_param_check(custom_pmeth, pkey_custom_param_check);
  2592. if (!TEST_int_eq(EVP_PKEY_meth_add0(custom_pmeth), 1))
  2593. return 0;
  2594. #endif
  2595. ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata));
  2596. #ifndef OPENSSL_NO_CMAC
  2597. ADD_TEST(test_CMAC_keygen);
  2598. #endif
  2599. ADD_TEST(test_HKDF);
  2600. ADD_TEST(test_emptyikm_HKDF);
  2601. #ifndef OPENSSL_NO_EC
  2602. ADD_TEST(test_X509_PUBKEY_inplace);
  2603. ADD_TEST(test_X509_PUBKEY_dup);
  2604. ADD_ALL_TESTS(test_invalide_ec_char2_pub_range_decode,
  2605. OSSL_NELEM(ec_der_pub_keys));
  2606. #endif
  2607. #ifndef OPENSSL_NO_DSA
  2608. ADD_TEST(test_DSA_get_set_params);
  2609. ADD_TEST(test_DSA_priv_pub);
  2610. #endif
  2611. ADD_TEST(test_RSA_get_set_params);
  2612. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  2613. ADD_TEST(test_decrypt_null_chunks);
  2614. #endif
  2615. #ifndef OPENSSL_NO_DH
  2616. ADD_TEST(test_DH_priv_pub);
  2617. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2618. ADD_TEST(test_EVP_PKEY_set1_DH);
  2619. # endif
  2620. #endif
  2621. #ifndef OPENSSL_NO_EC
  2622. ADD_TEST(test_EC_priv_pub);
  2623. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2624. ADD_TEST(test_EC_priv_only_legacy);
  2625. # endif
  2626. #endif
  2627. ADD_ALL_TESTS(test_keygen_with_empty_template, 2);
  2628. ADD_ALL_TESTS(test_pkey_ctx_fail_without_provider, 2);
  2629. ADD_TEST(test_rand_agglomeration);
  2630. ADD_ALL_TESTS(test_evp_iv_aes, 12);
  2631. #ifndef OPENSSL_NO_DES
  2632. ADD_ALL_TESTS(test_evp_iv_des, 6);
  2633. #endif
  2634. ADD_TEST(test_EVP_rsa_pss_with_keygen_bits);
  2635. #ifndef OPENSSL_NO_EC
  2636. ADD_ALL_TESTS(test_ecpub, OSSL_NELEM(ecpub_nids));
  2637. #endif
  2638. ADD_TEST(test_names_do_all);
  2639. return 1;
  2640. }
  2641. void cleanup_tests(void)
  2642. {
  2643. OSSL_PROVIDER_unload(nullprov);
  2644. OSSL_PROVIDER_unload(deflprov);
  2645. OSSL_PROVIDER_unload(lgcyprov);
  2646. OSSL_LIB_CTX_free(testctx);
  2647. }