cmp.c 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490
  1. /*
  2. * Copyright 2007-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Nokia 2007-2019
  4. * Copyright Siemens AG 2015-2019
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. /* This app is disabled when OPENSSL_NO_CMP is defined. */
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include "apps.h"
  15. #include "http_server.h"
  16. #include "s_apps.h"
  17. #include "progs.h"
  18. #include "cmp_mock_srv.h"
  19. /* tweaks needed due to missing unistd.h on Windows */
  20. #if defined(_WIN32) && !defined(__BORLANDC__)
  21. # define access _access
  22. #endif
  23. #ifndef F_OK
  24. # define F_OK 0
  25. #endif
  26. #include <openssl/ui.h>
  27. #include <openssl/pkcs12.h>
  28. #include <openssl/ssl.h>
  29. /* explicit #includes not strictly needed since implied by the above: */
  30. #include <stdlib.h>
  31. #include <openssl/cmp.h>
  32. #include <openssl/cmp_util.h>
  33. #include <openssl/crmf.h>
  34. #include <openssl/crypto.h>
  35. #include <openssl/err.h>
  36. #include <openssl/store.h>
  37. #include <openssl/objects.h>
  38. #include <openssl/x509.h>
  39. static char *prog;
  40. static char *opt_config = NULL;
  41. #define CMP_SECTION "cmp"
  42. #define SECTION_NAME_MAX 40 /* max length of section name */
  43. #define DEFAULT_SECTION "default"
  44. static char *opt_section = CMP_SECTION;
  45. static int opt_verbosity = OSSL_CMP_LOG_INFO;
  46. static int read_config(void);
  47. static CONF *conf = NULL; /* OpenSSL config file context structure */
  48. static OSSL_CMP_CTX *cmp_ctx = NULL; /* the client-side CMP context */
  49. /* the type of cmp command we want to send */
  50. typedef enum {
  51. CMP_IR,
  52. CMP_KUR,
  53. CMP_CR,
  54. CMP_P10CR,
  55. CMP_RR,
  56. CMP_GENM
  57. } cmp_cmd_t;
  58. /* message transfer */
  59. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  60. static char *opt_server = NULL;
  61. static char *opt_proxy = NULL;
  62. static char *opt_no_proxy = NULL;
  63. #endif
  64. static char *opt_recipient = NULL;
  65. static char *opt_path = NULL;
  66. static int opt_keep_alive = 1;
  67. static int opt_msg_timeout = -1;
  68. static int opt_total_timeout = -1;
  69. /* server authentication */
  70. static char *opt_trusted = NULL;
  71. static char *opt_untrusted = NULL;
  72. static char *opt_srvcert = NULL;
  73. static char *opt_expect_sender = NULL;
  74. static int opt_ignore_keyusage = 0;
  75. static int opt_unprotected_errors = 0;
  76. static int opt_no_cache_extracerts = 0;
  77. static char *opt_srvcertout = NULL;
  78. static char *opt_extracertsout = NULL;
  79. static char *opt_cacertsout = NULL;
  80. static char *opt_oldwithold = NULL;
  81. static char *opt_newwithnew = NULL;
  82. static char *opt_newwithold = NULL;
  83. static char *opt_oldwithnew = NULL;
  84. /* client authentication */
  85. static char *opt_ref = NULL;
  86. static char *opt_secret = NULL;
  87. static char *opt_cert = NULL;
  88. static char *opt_own_trusted = NULL;
  89. static char *opt_key = NULL;
  90. static char *opt_keypass = NULL;
  91. static char *opt_digest = NULL;
  92. static char *opt_mac = NULL;
  93. static char *opt_extracerts = NULL;
  94. static int opt_unprotected_requests = 0;
  95. /* generic message */
  96. static char *opt_cmd_s = NULL;
  97. static int opt_cmd = -1;
  98. static char *opt_geninfo = NULL;
  99. static char *opt_infotype_s = NULL;
  100. static int opt_infotype = NID_undef;
  101. static char *opt_profile = NULL;
  102. /* certificate enrollment */
  103. static char *opt_newkey = NULL;
  104. static char *opt_newkeypass = NULL;
  105. static char *opt_subject = NULL;
  106. static int opt_days = 0;
  107. static char *opt_reqexts = NULL;
  108. static char *opt_sans = NULL;
  109. static int opt_san_nodefault = 0;
  110. static char *opt_policies = NULL;
  111. static char *opt_policy_oids = NULL;
  112. static int opt_policy_oids_critical = 0;
  113. static int opt_popo = OSSL_CRMF_POPO_NONE - 1;
  114. static char *opt_csr = NULL;
  115. static char *opt_out_trusted = NULL;
  116. static int opt_implicit_confirm = 0;
  117. static int opt_disable_confirm = 0;
  118. static char *opt_certout = NULL;
  119. static char *opt_chainout = NULL;
  120. /* certificate enrollment and revocation */
  121. static char *opt_oldcert = NULL;
  122. static char *opt_issuer = NULL;
  123. static char *opt_serial = NULL;
  124. static int opt_revreason = CRL_REASON_NONE;
  125. /* credentials format */
  126. static char *opt_certform_s = "PEM";
  127. static int opt_certform = FORMAT_PEM;
  128. static char *opt_keyform_s = NULL;
  129. static int opt_keyform = FORMAT_UNDEF;
  130. static char *opt_otherpass = NULL;
  131. static char *opt_engine = NULL;
  132. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  133. /* TLS connection */
  134. static int opt_tls_used = 0;
  135. static char *opt_tls_cert = NULL;
  136. static char *opt_tls_key = NULL;
  137. static char *opt_tls_keypass = NULL;
  138. static char *opt_tls_extra = NULL;
  139. static char *opt_tls_trusted = NULL;
  140. static char *opt_tls_host = NULL;
  141. #endif
  142. /* client-side debugging */
  143. static int opt_batch = 0;
  144. static int opt_repeat = 1;
  145. static char *opt_reqin = NULL;
  146. static int opt_reqin_new_tid = 0;
  147. static char *opt_reqout = NULL;
  148. static char *opt_reqout_only = NULL;
  149. static int reqout_only_done = 0;
  150. static char *opt_rspin = NULL;
  151. static int rspin_in_use = 0;
  152. static char *opt_rspout = NULL;
  153. static int opt_use_mock_srv = 0;
  154. /* mock server */
  155. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  156. static char *opt_port = NULL;
  157. static int opt_max_msgs = 0;
  158. #endif
  159. static char *opt_srv_ref = NULL;
  160. static char *opt_srv_secret = NULL;
  161. static char *opt_srv_cert = NULL;
  162. static char *opt_srv_key = NULL;
  163. static char *opt_srv_keypass = NULL;
  164. static char *opt_srv_trusted = NULL;
  165. static char *opt_srv_untrusted = NULL;
  166. static char *opt_ref_cert = NULL;
  167. static char *opt_rsp_cert = NULL;
  168. static char *opt_rsp_extracerts = NULL;
  169. static char *opt_rsp_capubs = NULL;
  170. static char *opt_rsp_newwithnew = NULL;
  171. static char *opt_rsp_newwithold = NULL;
  172. static char *opt_rsp_oldwithnew = NULL;
  173. static int opt_poll_count = 0;
  174. static int opt_check_after = 1;
  175. static int opt_grant_implicitconf = 0;
  176. static int opt_pkistatus = OSSL_CMP_PKISTATUS_accepted;
  177. static int opt_failure = INT_MIN;
  178. static int opt_failurebits = 0;
  179. static char *opt_statusstring = NULL;
  180. static int opt_send_error = 0;
  181. static int opt_send_unprotected = 0;
  182. static int opt_send_unprot_err = 0;
  183. static int opt_accept_unprotected = 0;
  184. static int opt_accept_unprot_err = 0;
  185. static int opt_accept_raverified = 0;
  186. static X509_VERIFY_PARAM *vpm = NULL;
  187. typedef enum OPTION_choice {
  188. OPT_COMMON,
  189. OPT_CONFIG, OPT_SECTION, OPT_VERBOSITY,
  190. OPT_CMD, OPT_INFOTYPE, OPT_PROFILE, OPT_GENINFO,
  191. OPT_NEWKEY, OPT_NEWKEYPASS, OPT_SUBJECT,
  192. OPT_DAYS, OPT_REQEXTS,
  193. OPT_SANS, OPT_SAN_NODEFAULT,
  194. OPT_POLICIES, OPT_POLICY_OIDS, OPT_POLICY_OIDS_CRITICAL,
  195. OPT_POPO, OPT_CSR,
  196. OPT_OUT_TRUSTED, OPT_IMPLICIT_CONFIRM, OPT_DISABLE_CONFIRM,
  197. OPT_CERTOUT, OPT_CHAINOUT,
  198. OPT_OLDCERT, OPT_ISSUER, OPT_SERIAL, OPT_REVREASON,
  199. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  200. OPT_SERVER, OPT_PROXY, OPT_NO_PROXY,
  201. #endif
  202. OPT_RECIPIENT, OPT_PATH,
  203. OPT_KEEP_ALIVE, OPT_MSG_TIMEOUT, OPT_TOTAL_TIMEOUT,
  204. OPT_TRUSTED, OPT_UNTRUSTED, OPT_SRVCERT,
  205. OPT_EXPECT_SENDER,
  206. OPT_IGNORE_KEYUSAGE, OPT_UNPROTECTED_ERRORS, OPT_NO_CACHE_EXTRACERTS,
  207. OPT_SRVCERTOUT, OPT_EXTRACERTSOUT, OPT_CACERTSOUT,
  208. OPT_OLDWITHOLD, OPT_NEWWITHNEW, OPT_NEWWITHOLD, OPT_OLDWITHNEW,
  209. OPT_REF, OPT_SECRET, OPT_CERT, OPT_OWN_TRUSTED, OPT_KEY, OPT_KEYPASS,
  210. OPT_DIGEST, OPT_MAC, OPT_EXTRACERTS,
  211. OPT_UNPROTECTED_REQUESTS,
  212. OPT_CERTFORM, OPT_KEYFORM,
  213. OPT_OTHERPASS,
  214. #ifndef OPENSSL_NO_ENGINE
  215. OPT_ENGINE,
  216. #endif
  217. OPT_PROV_ENUM,
  218. OPT_R_ENUM,
  219. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  220. OPT_TLS_USED, OPT_TLS_CERT, OPT_TLS_KEY,
  221. OPT_TLS_KEYPASS,
  222. OPT_TLS_EXTRA, OPT_TLS_TRUSTED, OPT_TLS_HOST,
  223. #endif
  224. OPT_BATCH, OPT_REPEAT,
  225. OPT_REQIN, OPT_REQIN_NEW_TID, OPT_REQOUT, OPT_REQOUT_ONLY,
  226. OPT_RSPIN, OPT_RSPOUT,
  227. OPT_USE_MOCK_SRV,
  228. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  229. OPT_PORT, OPT_MAX_MSGS,
  230. #endif
  231. OPT_SRV_REF, OPT_SRV_SECRET,
  232. OPT_SRV_CERT, OPT_SRV_KEY, OPT_SRV_KEYPASS,
  233. OPT_SRV_TRUSTED, OPT_SRV_UNTRUSTED,
  234. OPT_REF_CERT, OPT_RSP_CERT, OPT_RSP_EXTRACERTS, OPT_RSP_CAPUBS,
  235. OPT_RSP_NEWWITHNEW, OPT_RSP_NEWWITHOLD, OPT_RSP_OLDWITHNEW,
  236. OPT_POLL_COUNT, OPT_CHECK_AFTER,
  237. OPT_GRANT_IMPLICITCONF,
  238. OPT_PKISTATUS, OPT_FAILURE,
  239. OPT_FAILUREBITS, OPT_STATUSSTRING,
  240. OPT_SEND_ERROR, OPT_SEND_UNPROTECTED,
  241. OPT_SEND_UNPROT_ERR, OPT_ACCEPT_UNPROTECTED,
  242. OPT_ACCEPT_UNPROT_ERR, OPT_ACCEPT_RAVERIFIED,
  243. OPT_V_ENUM
  244. } OPTION_CHOICE;
  245. const OPTIONS cmp_options[] = {
  246. /* entries must be in the same order as enumerated above!! */
  247. {"help", OPT_HELP, '-', "Display this summary"},
  248. {"config", OPT_CONFIG, 's',
  249. "Configuration file to use. \"\" = none. Default from env variable OPENSSL_CONF"},
  250. {"section", OPT_SECTION, 's',
  251. "Section(s) in config file to get options from. \"\" = 'default'. Default 'cmp'"},
  252. {"verbosity", OPT_VERBOSITY, 'N',
  253. "Log level; 3=ERR, 4=WARN, 6=INFO, 7=DEBUG, 8=TRACE. Default 6 = INFO"},
  254. OPT_SECTION("Generic message"),
  255. {"cmd", OPT_CMD, 's', "CMP request to send: ir/cr/kur/p10cr/rr/genm"},
  256. {"infotype", OPT_INFOTYPE, 's',
  257. "InfoType name for requesting specific info in genm, with specific support"},
  258. {OPT_MORE_STR, 0, 0,
  259. "for 'caCerts' and 'rootCaCert'"},
  260. {"profile", OPT_PROFILE, 's',
  261. "Certificate profile name to place in generalInfo field of request PKIHeader"},
  262. {"geninfo", OPT_GENINFO, 's',
  263. "Comma-separated list of OID and value to place in generalInfo PKIHeader"},
  264. {OPT_MORE_STR, 0, 0,
  265. "of form <OID>:int:<n> or <OID>:str:<s>, e.g. \'1.2.3.4:int:56789, id-kp:str:name'"},
  266. OPT_SECTION("Certificate enrollment"),
  267. {"newkey", OPT_NEWKEY, 's',
  268. "Private or public key for the requested cert. Default: CSR key or client key"},
  269. {"newkeypass", OPT_NEWKEYPASS, 's', "New private key pass phrase source"},
  270. {"subject", OPT_SUBJECT, 's',
  271. "Distinguished Name (DN) of subject to use in the requested cert template"},
  272. {OPT_MORE_STR, 0, 0,
  273. "For kur, default is subject of -csr arg or reference cert (see -oldcert)"},
  274. {OPT_MORE_STR, 0, 0,
  275. "this default is used for ir and cr only if no Subject Alt Names are set"},
  276. {"days", OPT_DAYS, 'N',
  277. "Requested validity time of the new certificate in number of days"},
  278. {"reqexts", OPT_REQEXTS, 's',
  279. "Name of config file section defining certificate request extensions."},
  280. {OPT_MORE_STR, 0, 0,
  281. "Augments or replaces any extensions contained CSR given with -csr"},
  282. {"sans", OPT_SANS, 's',
  283. "Subject Alt Names (IPADDR/DNS/URI) to add as (critical) cert req extension"},
  284. {"san_nodefault", OPT_SAN_NODEFAULT, '-',
  285. "Do not take default SANs from reference certificate (see -oldcert)"},
  286. {"policies", OPT_POLICIES, 's',
  287. "Name of config file section defining policies certificate request extension"},
  288. {"policy_oids", OPT_POLICY_OIDS, 's',
  289. "Policy OID(s) to add as policies certificate request extension"},
  290. {"policy_oids_critical", OPT_POLICY_OIDS_CRITICAL, '-',
  291. "Flag the policy OID(s) given with -policy_oids as critical"},
  292. {"popo", OPT_POPO, 'n',
  293. "Proof-of-Possession (POPO) method to use for ir/cr/kur where"},
  294. {OPT_MORE_STR, 0, 0,
  295. "-1 = NONE, 0 = RAVERIFIED, 1 = SIGNATURE (default), 2 = KEYENC"},
  296. {"csr", OPT_CSR, 's',
  297. "PKCS#10 CSR file in PEM or DER format to convert or to use in p10cr"},
  298. {"out_trusted", OPT_OUT_TRUSTED, 's',
  299. "Certificates to trust when verifying newly enrolled certificates"},
  300. {"implicit_confirm", OPT_IMPLICIT_CONFIRM, '-',
  301. "Request implicit confirmation of newly enrolled certificates"},
  302. {"disable_confirm", OPT_DISABLE_CONFIRM, '-',
  303. "Do not confirm newly enrolled certificate w/o requesting implicit"},
  304. {OPT_MORE_STR, 0, 0,
  305. "confirmation. WARNING: This leads to behavior violating RFC 4210"},
  306. {"certout", OPT_CERTOUT, 's',
  307. "File to save newly enrolled certificate"},
  308. {"chainout", OPT_CHAINOUT, 's',
  309. "File to save the chain of newly enrolled certificate"},
  310. OPT_SECTION("Certificate enrollment and revocation"),
  311. {"oldcert", OPT_OLDCERT, 's',
  312. "Certificate to be updated (defaulting to -cert) or to be revoked in rr;"},
  313. {OPT_MORE_STR, 0, 0,
  314. "also used as reference (defaulting to -cert) for subject DN and SANs."},
  315. {OPT_MORE_STR, 0, 0,
  316. "Issuer is used as recipient unless -recipient, -srvcert, or -issuer given"},
  317. {"issuer", OPT_ISSUER, 's',
  318. "DN of the issuer to place in the certificate template of ir/cr/kur/rr;"},
  319. {OPT_MORE_STR, 0, 0,
  320. "also used as recipient if neither -recipient nor -srvcert are given"},
  321. {"serial", OPT_SERIAL, 's',
  322. "Serial number of certificate to be revoked in revocation request (rr)"},
  323. {"revreason", OPT_REVREASON, 'n',
  324. "Reason code to include in revocation request (rr); possible values:"},
  325. {OPT_MORE_STR, 0, 0,
  326. "0..6, 8..10 (see RFC5280, 5.3.1) or -1. Default -1 = none included"},
  327. OPT_SECTION("Message transfer"),
  328. #if defined(OPENSSL_NO_SOCK) || defined(OPENSSL_NO_HTTP)
  329. {OPT_MORE_STR, 0, 0,
  330. "NOTE: -server, -proxy, and -no_proxy not supported due to no-sock/no-http build"},
  331. #else
  332. {"server", OPT_SERVER, 's',
  333. "[http[s]://]address[:port][/path] of CMP server. Default port 80 or 443."},
  334. {OPT_MORE_STR, 0, 0,
  335. "address may be a DNS name or an IP address; path can be overridden by -path"},
  336. {"proxy", OPT_PROXY, 's',
  337. "[http[s]://]address[:port][/path] of HTTP(S) proxy to use; path is ignored"},
  338. {"no_proxy", OPT_NO_PROXY, 's',
  339. "List of addresses of servers not to use HTTP(S) proxy for"},
  340. {OPT_MORE_STR, 0, 0,
  341. "Default from environment variable 'no_proxy', else 'NO_PROXY', else none"},
  342. #endif
  343. {"recipient", OPT_RECIPIENT, 's',
  344. "DN of CA. Default: subject of -srvcert, -issuer, issuer of -oldcert or -cert"},
  345. {"path", OPT_PATH, 's',
  346. "HTTP path (aka CMP alias) at the CMP server. Default from -server, else \"/\""},
  347. {"keep_alive", OPT_KEEP_ALIVE, 'N',
  348. "Persistent HTTP connections. 0: no, 1 (the default): request, 2: require"},
  349. {"msg_timeout", OPT_MSG_TIMEOUT, 'N',
  350. "Number of seconds allowed per CMP message round trip, or 0 for infinite"},
  351. {"total_timeout", OPT_TOTAL_TIMEOUT, 'N',
  352. "Overall time an enrollment incl. polling may take. Default 0 = infinite"},
  353. OPT_SECTION("Server authentication"),
  354. {"trusted", OPT_TRUSTED, 's',
  355. "Certificates to use as trust anchors when verifying signed CMP responses"},
  356. {OPT_MORE_STR, 0, 0, "unless -srvcert is given"},
  357. {"untrusted", OPT_UNTRUSTED, 's',
  358. "Intermediate CA certs for chain construction for CMP/TLS/enrolled certs"},
  359. {"srvcert", OPT_SRVCERT, 's',
  360. "Server cert to pin and trust directly when verifying signed CMP responses"},
  361. {"expect_sender", OPT_EXPECT_SENDER, 's',
  362. "DN of expected sender of responses. Defaults to subject of -srvcert, if any"},
  363. {"ignore_keyusage", OPT_IGNORE_KEYUSAGE, '-',
  364. "Ignore CMP signer cert key usage, else 'digitalSignature' must be allowed"},
  365. {"unprotected_errors", OPT_UNPROTECTED_ERRORS, '-',
  366. "Accept missing or invalid protection of regular error messages and negative"},
  367. {OPT_MORE_STR, 0, 0,
  368. "certificate responses (ip/cp/kup), revocation responses (rp), and PKIConf"},
  369. {OPT_MORE_STR, 0, 0,
  370. "WARNING: This setting leads to behavior allowing violation of RFC 4210"},
  371. {"no_cache_extracerts", OPT_NO_CACHE_EXTRACERTS, '-',
  372. "Do not keep certificates received in the extraCerts CMP message field"},
  373. { "srvcertout", OPT_SRVCERTOUT, 's',
  374. "File to save the server cert used and validated for CMP response protection"},
  375. {"extracertsout", OPT_EXTRACERTSOUT, 's',
  376. "File to save extra certificates received in the extraCerts field"},
  377. {"cacertsout", OPT_CACERTSOUT, 's',
  378. "File to save CA certs received in caPubs field or genp with id-it-caCerts"},
  379. { "oldwithold", OPT_OLDWITHOLD, 's',
  380. "Root CA certificate to request update for in genm of type rootCaCert"},
  381. { "newwithnew", OPT_NEWWITHNEW, 's',
  382. "File to save NewWithNew cert received in genp of type rootCaKeyUpdate"},
  383. { "newwithold", OPT_NEWWITHOLD, 's',
  384. "File to save NewWithOld cert received in genp of type rootCaKeyUpdate"},
  385. { "oldwithnew", OPT_OLDWITHNEW, 's',
  386. "File to save OldWithNew cert received in genp of type rootCaKeyUpdate"},
  387. OPT_SECTION("Client authentication"),
  388. {"ref", OPT_REF, 's',
  389. "Reference value to use as senderKID in case no -cert is given"},
  390. {"secret", OPT_SECRET, 's',
  391. "Prefer PBM (over signatures) for protecting msgs with given password source"},
  392. {"cert", OPT_CERT, 's',
  393. "Client's CMP signer certificate; its public key must match the -key argument"},
  394. {OPT_MORE_STR, 0, 0,
  395. "This also used as default reference for subject DN and SANs."},
  396. {OPT_MORE_STR, 0, 0,
  397. "Any further certs included are appended to the untrusted certs"},
  398. {"own_trusted", OPT_OWN_TRUSTED, 's',
  399. "Optional certs to verify chain building for own CMP signer cert"},
  400. {"key", OPT_KEY, 's', "CMP signer private key, not used when -secret given"},
  401. {"keypass", OPT_KEYPASS, 's',
  402. "Client private key (and cert and old cert) pass phrase source"},
  403. {"digest", OPT_DIGEST, 's',
  404. "Digest to use in message protection and POPO signatures. Default \"sha256\""},
  405. {"mac", OPT_MAC, 's',
  406. "MAC algorithm to use in PBM-based message protection. Default \"hmac-sha1\""},
  407. {"extracerts", OPT_EXTRACERTS, 's',
  408. "Certificates to append in extraCerts field of outgoing messages."},
  409. {OPT_MORE_STR, 0, 0,
  410. "This can be used as the default CMP signer cert chain to include"},
  411. {"unprotected_requests", OPT_UNPROTECTED_REQUESTS, '-',
  412. "Send request messages without CMP-level protection"},
  413. OPT_SECTION("Credentials format"),
  414. {"certform", OPT_CERTFORM, 's',
  415. "Format (PEM or DER) to use when saving a certificate to a file. Default PEM"},
  416. {"keyform", OPT_KEYFORM, 's',
  417. "Format of the key input (ENGINE, other values ignored)"},
  418. {"otherpass", OPT_OTHERPASS, 's',
  419. "Pass phrase source potentially needed for loading certificates of others"},
  420. #ifndef OPENSSL_NO_ENGINE
  421. {"engine", OPT_ENGINE, 's',
  422. "Use crypto engine with given identifier, possibly a hardware device."},
  423. {OPT_MORE_STR, 0, 0,
  424. "Engines may also be defined in OpenSSL config file engine section."},
  425. #endif
  426. OPT_PROV_OPTIONS,
  427. OPT_R_OPTIONS,
  428. OPT_SECTION("TLS connection"),
  429. #if defined(OPENSSL_NO_SOCK) || defined(OPENSSL_NO_HTTP)
  430. {OPT_MORE_STR, 0, 0,
  431. "NOTE: -tls_used and all other TLS options not supported due to no-sock/no-http build"},
  432. #else
  433. {"tls_used", OPT_TLS_USED, '-',
  434. "Enable using TLS (also when other TLS options are not set)"},
  435. {"tls_cert", OPT_TLS_CERT, 's',
  436. "Client's TLS certificate. May include chain to be provided to TLS server"},
  437. {"tls_key", OPT_TLS_KEY, 's',
  438. "Private key for the client's TLS certificate"},
  439. {"tls_keypass", OPT_TLS_KEYPASS, 's',
  440. "Pass phrase source for the client's private TLS key (and TLS cert)"},
  441. {"tls_extra", OPT_TLS_EXTRA, 's',
  442. "Extra certificates to provide to TLS server during TLS handshake"},
  443. {"tls_trusted", OPT_TLS_TRUSTED, 's',
  444. "Trusted certificates to use for verifying the TLS server certificate;"},
  445. {OPT_MORE_STR, 0, 0, "this implies hostname validation"},
  446. {"tls_host", OPT_TLS_HOST, 's',
  447. "Address to be checked (rather than -server) during TLS hostname validation"},
  448. #endif
  449. OPT_SECTION("Client-side debugging"),
  450. {"batch", OPT_BATCH, '-',
  451. "Do not interactively prompt for input when a password is required etc."},
  452. {"repeat", OPT_REPEAT, 'p',
  453. "Invoke the transaction the given positive number of times. Default 1"},
  454. {"reqin", OPT_REQIN, 's',
  455. "Take sequence of CMP requests to send to server from file(s)"},
  456. {"reqin_new_tid", OPT_REQIN_NEW_TID, '-',
  457. "Use fresh transactionID for CMP requests read from -reqin"},
  458. {"reqout", OPT_REQOUT, 's',
  459. "Save sequence of CMP requests created by the client to file(s)"},
  460. {"reqout_only", OPT_REQOUT_ONLY, 's',
  461. "Save first CMP request created by the client to file and exit"},
  462. {"rspin", OPT_RSPIN, 's',
  463. "Process sequence of CMP responses provided in file(s), skipping server"},
  464. {"rspout", OPT_RSPOUT, 's',
  465. "Save sequence of actually used CMP responses to file(s)"},
  466. {"use_mock_srv", OPT_USE_MOCK_SRV, '-',
  467. "Use internal mock server at API level, bypassing socket-based HTTP"},
  468. OPT_SECTION("Mock server"),
  469. #if defined(OPENSSL_NO_SOCK) || defined(OPENSSL_NO_HTTP)
  470. {OPT_MORE_STR, 0, 0,
  471. "NOTE: -port and -max_msgs not supported due to no-sock/no-http build"},
  472. #else
  473. {"port", OPT_PORT, 's',
  474. "Act as HTTP-based mock server listening on given port"},
  475. {"max_msgs", OPT_MAX_MSGS, 'N',
  476. "max number of messages handled by HTTP mock server. Default: 0 = unlimited"},
  477. #endif
  478. {"srv_ref", OPT_SRV_REF, 's',
  479. "Reference value to use as senderKID of server in case no -srv_cert is given"},
  480. {"srv_secret", OPT_SRV_SECRET, 's',
  481. "Password source for server authentication with a pre-shared key (secret)"},
  482. {"srv_cert", OPT_SRV_CERT, 's', "Certificate of the server"},
  483. {"srv_key", OPT_SRV_KEY, 's',
  484. "Private key used by the server for signing messages"},
  485. {"srv_keypass", OPT_SRV_KEYPASS, 's',
  486. "Server private key (and cert) pass phrase source"},
  487. {"srv_trusted", OPT_SRV_TRUSTED, 's',
  488. "Trusted certificates for client authentication"},
  489. {"srv_untrusted", OPT_SRV_UNTRUSTED, 's',
  490. "Intermediate certs that may be useful for verifying CMP protection"},
  491. {"ref_cert", OPT_RSP_CERT, 's',
  492. "Certificate to be expected for rr and any oldCertID in kur messages"},
  493. {"rsp_cert", OPT_RSP_CERT, 's',
  494. "Certificate to be returned as mock enrollment result"},
  495. {"rsp_extracerts", OPT_RSP_EXTRACERTS, 's',
  496. "Extra certificates to be included in mock certification responses"},
  497. {"rsp_capubs", OPT_RSP_CAPUBS, 's',
  498. "CA certificates to be included in mock ip response"},
  499. {"rsp_newwithnew", OPT_RSP_NEWWITHNEW, 's',
  500. "New root CA certificate to include in genp of type rootCaKeyUpdate"},
  501. {"rsp_newwithold", OPT_RSP_NEWWITHOLD, 's',
  502. "NewWithOld transition cert to include in genp of type rootCaKeyUpdate"},
  503. {"rsp_oldwithnew", OPT_RSP_OLDWITHNEW, 's',
  504. "OldWithNew transition cert to include in genp of type rootCaKeyUpdate"},
  505. {"poll_count", OPT_POLL_COUNT, 'N',
  506. "Number of times the client must poll before receiving a certificate"},
  507. {"check_after", OPT_CHECK_AFTER, 'N',
  508. "The check_after value (time to wait) to include in poll response"},
  509. {"grant_implicitconf", OPT_GRANT_IMPLICITCONF, '-',
  510. "Grant implicit confirmation of newly enrolled certificate"},
  511. {"pkistatus", OPT_PKISTATUS, 'N',
  512. "PKIStatus to be included in server response. Possible values: 0..6"},
  513. {"failure", OPT_FAILURE, 'N',
  514. "A single failure info bit number to include in server response, 0..26"},
  515. {"failurebits", OPT_FAILUREBITS, 'N',
  516. "Number representing failure bits to include in server response, 0..2^27 - 1"},
  517. {"statusstring", OPT_STATUSSTRING, 's',
  518. "Status string to be included in server response"},
  519. {"send_error", OPT_SEND_ERROR, '-',
  520. "Force server to reply with error message"},
  521. {"send_unprotected", OPT_SEND_UNPROTECTED, '-',
  522. "Send response messages without CMP-level protection"},
  523. {"send_unprot_err", OPT_SEND_UNPROT_ERR, '-',
  524. "In case of negative responses, server shall send unprotected error messages,"},
  525. {OPT_MORE_STR, 0, 0,
  526. "certificate responses (ip/cp/kup), and revocation responses (rp)."},
  527. {OPT_MORE_STR, 0, 0,
  528. "WARNING: This setting leads to behavior violating RFC 4210"},
  529. {"accept_unprotected", OPT_ACCEPT_UNPROTECTED, '-',
  530. "Accept missing or invalid protection of requests"},
  531. {"accept_unprot_err", OPT_ACCEPT_UNPROT_ERR, '-',
  532. "Accept unprotected error messages from client"},
  533. {"accept_raverified", OPT_ACCEPT_RAVERIFIED, '-',
  534. "Accept RAVERIFIED as proof-of-possession (POPO)"},
  535. OPT_V_OPTIONS,
  536. {NULL}
  537. };
  538. typedef union {
  539. char **txt;
  540. int *num;
  541. long *num_long;
  542. } varref;
  543. static varref cmp_vars[] = { /* must be in same order as enumerated above! */
  544. {&opt_config}, {&opt_section}, {(char **)&opt_verbosity},
  545. {&opt_cmd_s}, {&opt_infotype_s}, {&opt_profile}, {&opt_geninfo},
  546. {&opt_newkey}, {&opt_newkeypass}, {&opt_subject},
  547. {(char **)&opt_days}, {&opt_reqexts},
  548. {&opt_sans}, {(char **)&opt_san_nodefault},
  549. {&opt_policies}, {&opt_policy_oids}, {(char **)&opt_policy_oids_critical},
  550. {(char **)&opt_popo}, {&opt_csr},
  551. {&opt_out_trusted},
  552. {(char **)&opt_implicit_confirm}, {(char **)&opt_disable_confirm},
  553. {&opt_certout}, {&opt_chainout},
  554. {&opt_oldcert}, {&opt_issuer}, {&opt_serial}, {(char **)&opt_revreason},
  555. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  556. {&opt_server}, {&opt_proxy}, {&opt_no_proxy},
  557. #endif
  558. {&opt_recipient}, {&opt_path}, {(char **)&opt_keep_alive},
  559. {(char **)&opt_msg_timeout}, {(char **)&opt_total_timeout},
  560. {&opt_trusted}, {&opt_untrusted}, {&opt_srvcert},
  561. {&opt_expect_sender},
  562. {(char **)&opt_ignore_keyusage}, {(char **)&opt_unprotected_errors},
  563. {(char **)&opt_no_cache_extracerts},
  564. {&opt_srvcertout}, {&opt_extracertsout}, {&opt_cacertsout},
  565. {&opt_oldwithold}, {&opt_newwithnew}, {&opt_newwithold}, {&opt_oldwithnew},
  566. {&opt_ref}, {&opt_secret},
  567. {&opt_cert}, {&opt_own_trusted}, {&opt_key}, {&opt_keypass},
  568. {&opt_digest}, {&opt_mac}, {&opt_extracerts},
  569. {(char **)&opt_unprotected_requests},
  570. {&opt_certform_s}, {&opt_keyform_s},
  571. {&opt_otherpass},
  572. #ifndef OPENSSL_NO_ENGINE
  573. {&opt_engine},
  574. #endif
  575. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  576. {(char **)&opt_tls_used}, {&opt_tls_cert}, {&opt_tls_key},
  577. {&opt_tls_keypass},
  578. {&opt_tls_extra}, {&opt_tls_trusted}, {&opt_tls_host},
  579. #endif
  580. {(char **)&opt_batch}, {(char **)&opt_repeat},
  581. {&opt_reqin}, {(char **)&opt_reqin_new_tid},
  582. {&opt_reqout}, {&opt_reqout_only}, {&opt_rspin}, {&opt_rspout},
  583. {(char **)&opt_use_mock_srv},
  584. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  585. {&opt_port}, {(char **)&opt_max_msgs},
  586. #endif
  587. {&opt_srv_ref}, {&opt_srv_secret},
  588. {&opt_srv_cert}, {&opt_srv_key}, {&opt_srv_keypass},
  589. {&opt_srv_trusted}, {&opt_srv_untrusted},
  590. {&opt_ref_cert}, {&opt_rsp_cert}, {&opt_rsp_extracerts}, {&opt_rsp_capubs},
  591. {&opt_rsp_newwithnew}, {&opt_rsp_newwithold}, {&opt_rsp_oldwithnew},
  592. {(char **)&opt_poll_count}, {(char **)&opt_check_after},
  593. {(char **)&opt_grant_implicitconf},
  594. {(char **)&opt_pkistatus}, {(char **)&opt_failure},
  595. {(char **)&opt_failurebits}, {&opt_statusstring},
  596. {(char **)&opt_send_error}, {(char **)&opt_send_unprotected},
  597. {(char **)&opt_send_unprot_err}, {(char **)&opt_accept_unprotected},
  598. {(char **)&opt_accept_unprot_err}, {(char **)&opt_accept_raverified},
  599. {NULL}
  600. };
  601. #define FUNC (strcmp(OPENSSL_FUNC, "(unknown function)") == 0 \
  602. ? "CMP" : OPENSSL_FUNC)
  603. #define CMP_print(bio, level, prefix, msg, a1, a2, a3) \
  604. ((void)(level > opt_verbosity ? 0 : \
  605. (BIO_printf(bio, "%s:%s:%d:CMP %s: " msg "\n", \
  606. FUNC, OPENSSL_FILE, OPENSSL_LINE, prefix, a1, a2, a3))))
  607. #define CMP_DEBUG(m, a1, a2, a3) \
  608. CMP_print(bio_out, OSSL_CMP_LOG_DEBUG, "debug", m, a1, a2, a3)
  609. #define CMP_debug(msg) CMP_DEBUG(msg"%s%s%s", "", "", "")
  610. #define CMP_debug1(msg, a1) CMP_DEBUG(msg"%s%s", a1, "", "")
  611. #define CMP_debug2(msg, a1, a2) CMP_DEBUG(msg"%s", a1, a2, "")
  612. #define CMP_debug3(msg, a1, a2, a3) CMP_DEBUG(msg, a1, a2, a3)
  613. #define CMP_INFO(msg, a1, a2, a3) \
  614. CMP_print(bio_out, OSSL_CMP_LOG_INFO, "info", msg, a1, a2, a3)
  615. #define CMP_info(msg) CMP_INFO(msg"%s%s%s", "", "", "")
  616. #define CMP_info1(msg, a1) CMP_INFO(msg"%s%s", a1, "", "")
  617. #define CMP_info2(msg, a1, a2) CMP_INFO(msg"%s", a1, a2, "")
  618. #define CMP_info3(msg, a1, a2, a3) CMP_INFO(msg, a1, a2, a3)
  619. #define CMP_WARN(m, a1, a2, a3) \
  620. CMP_print(bio_out, OSSL_CMP_LOG_WARNING, "warning", m, a1, a2, a3)
  621. #define CMP_warn(msg) CMP_WARN(msg"%s%s%s", "", "", "")
  622. #define CMP_warn1(msg, a1) CMP_WARN(msg"%s%s", a1, "", "")
  623. #define CMP_warn2(msg, a1, a2) CMP_WARN(msg"%s", a1, a2, "")
  624. #define CMP_warn3(msg, a1, a2, a3) CMP_WARN(msg, a1, a2, a3)
  625. #define CMP_ERR(msg, a1, a2, a3) \
  626. CMP_print(bio_err, OSSL_CMP_LOG_ERR, "error", msg, a1, a2, a3)
  627. #define CMP_err(msg) CMP_ERR(msg"%s%s%s", "", "", "")
  628. #define CMP_err1(msg, a1) CMP_ERR(msg"%s%s", a1, "", "")
  629. #define CMP_err2(msg, a1, a2) CMP_ERR(msg"%s", a1, a2, "")
  630. #define CMP_err3(msg, a1, a2, a3) CMP_ERR(msg, a1, a2, a3)
  631. static int print_to_bio_out(const char *func, const char *file, int line,
  632. OSSL_CMP_severity level, const char *msg)
  633. {
  634. return OSSL_CMP_print_to_bio(bio_out, func, file, line, level, msg);
  635. }
  636. static int print_to_bio_err(const char *func, const char *file, int line,
  637. OSSL_CMP_severity level, const char *msg)
  638. {
  639. return OSSL_CMP_print_to_bio(bio_err, func, file, line, level, msg);
  640. }
  641. static int set_verbosity(int level)
  642. {
  643. if (level < OSSL_CMP_LOG_EMERG || level > OSSL_CMP_LOG_MAX) {
  644. CMP_err1("Logging verbosity level %d out of range (0 .. 8)", level);
  645. return 0;
  646. }
  647. opt_verbosity = level;
  648. return 1;
  649. }
  650. static EVP_PKEY *load_key_pwd(const char *uri, int format,
  651. const char *pass, ENGINE *eng, const char *desc)
  652. {
  653. char *pass_string = get_passwd(pass, desc);
  654. EVP_PKEY *pkey = load_key(uri, format, 0, pass_string, eng, desc);
  655. clear_free(pass_string);
  656. return pkey;
  657. }
  658. static X509 *load_cert_pwd(const char *uri, const char *pass, const char *desc)
  659. {
  660. X509 *cert;
  661. char *pass_string = get_passwd(pass, desc);
  662. cert = load_cert_pass(uri, FORMAT_UNDEF, 0, pass_string, desc);
  663. clear_free(pass_string);
  664. return cert;
  665. }
  666. /* set expected hostname/IP addr and clears the email addr in the given ts */
  667. static int truststore_set_host_etc(X509_STORE *ts, const char *host)
  668. {
  669. X509_VERIFY_PARAM *ts_vpm = X509_STORE_get0_param(ts);
  670. /* first clear any hostnames, IP, and email addresses */
  671. if (!X509_VERIFY_PARAM_set1_host(ts_vpm, NULL, 0)
  672. || !X509_VERIFY_PARAM_set1_ip(ts_vpm, NULL, 0)
  673. || !X509_VERIFY_PARAM_set1_email(ts_vpm, NULL, 0))
  674. return 0;
  675. X509_VERIFY_PARAM_set_hostflags(ts_vpm,
  676. X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT |
  677. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  678. return (host != NULL && X509_VERIFY_PARAM_set1_ip_asc(ts_vpm, host))
  679. || X509_VERIFY_PARAM_set1_host(ts_vpm, host, 0);
  680. }
  681. /* write OSSL_CMP_MSG DER-encoded to the specified file name item */
  682. static int write_PKIMESSAGE(const OSSL_CMP_MSG *msg, char **filenames)
  683. {
  684. char *file;
  685. if (msg == NULL || filenames == NULL) {
  686. CMP_err("NULL arg to write_PKIMESSAGE");
  687. return 0;
  688. }
  689. if (*filenames == NULL) {
  690. CMP_err("not enough file names provided for writing PKIMessage");
  691. return 0;
  692. }
  693. file = *filenames;
  694. *filenames = next_item(file);
  695. if (OSSL_CMP_MSG_write(file, msg) < 0) {
  696. CMP_err1("cannot write PKIMessage to file '%s'", file);
  697. return 0;
  698. }
  699. return 1;
  700. }
  701. /* read DER-encoded OSSL_CMP_MSG from the specified file name item */
  702. static OSSL_CMP_MSG *read_PKIMESSAGE(const char *desc, char **filenames)
  703. {
  704. char *file;
  705. OSSL_CMP_MSG *ret;
  706. if (filenames == NULL || desc == NULL) {
  707. CMP_err("NULL arg to read_PKIMESSAGE");
  708. return NULL;
  709. }
  710. if (*filenames == NULL) {
  711. CMP_err("not enough file names provided for reading PKIMessage");
  712. return NULL;
  713. }
  714. file = *filenames;
  715. *filenames = next_item(file);
  716. ret = OSSL_CMP_MSG_read(file, app_get0_libctx(), app_get0_propq());
  717. if (ret == NULL)
  718. CMP_err1("cannot read PKIMessage from file '%s'", file);
  719. else
  720. CMP_info2("%s %s", desc, file);
  721. return ret;
  722. }
  723. /*-
  724. * Sends the PKIMessage req and on success place the response in *res
  725. * basically like OSSL_CMP_MSG_http_perform(), but in addition allows
  726. * to dump the sequence of requests and responses to files and/or
  727. * to take the sequence of requests and responses from files.
  728. */
  729. static OSSL_CMP_MSG *read_write_req_resp(OSSL_CMP_CTX *ctx,
  730. const OSSL_CMP_MSG *req)
  731. {
  732. OSSL_CMP_MSG *req_new = NULL;
  733. OSSL_CMP_MSG *res = NULL;
  734. OSSL_CMP_PKIHEADER *hdr;
  735. const char *prev_opt_rspin = opt_rspin;
  736. if (opt_reqout_only != NULL) {
  737. if (OSSL_CMP_MSG_write(opt_reqout_only, req) < 0)
  738. CMP_err1("cannot write request PKIMessage to file '%s'",
  739. opt_reqout_only);
  740. else
  741. reqout_only_done = 1;
  742. return NULL; /* stop at this point, not contacting any server */
  743. }
  744. if (opt_reqout != NULL && !write_PKIMESSAGE(req, &opt_reqout))
  745. goto err;
  746. if (opt_reqin != NULL && opt_rspin == NULL) {
  747. if ((req_new = read_PKIMESSAGE("actually sending", &opt_reqin)) == NULL)
  748. goto err;
  749. /*-
  750. * The transaction ID in req_new read from opt_reqin may not be fresh.
  751. * In this case the server may complain "Transaction id already in use."
  752. * The following workaround unfortunately requires re-protection.
  753. */
  754. if (opt_reqin_new_tid
  755. && !OSSL_CMP_MSG_update_transactionID(ctx, req_new))
  756. goto err;
  757. /*
  758. * Except for first request, need to satisfy recipNonce check by server.
  759. * Unfortunately requires re-protection if protection is required.
  760. */
  761. if (!OSSL_CMP_MSG_update_recipNonce(ctx, req_new))
  762. goto err;
  763. }
  764. if (opt_rspin != NULL) {
  765. res = read_PKIMESSAGE("actually using", &opt_rspin);
  766. } else {
  767. const OSSL_CMP_MSG *actual_req = req_new != NULL ? req_new : req;
  768. if (opt_use_mock_srv) {
  769. if (rspin_in_use)
  770. CMP_warn("too few -rspin filename arguments; resorting to using mock server");
  771. res = OSSL_CMP_CTX_server_perform(ctx, actual_req);
  772. } else {
  773. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  774. if (opt_server == NULL) {
  775. CMP_err("missing -server or -use_mock_srv option, or too few -rspin filename arguments");
  776. goto err;
  777. }
  778. if (rspin_in_use)
  779. CMP_warn("too few -rspin filename arguments; resorting to contacting server");
  780. res = OSSL_CMP_MSG_http_perform(ctx, actual_req);
  781. #else
  782. CMP_err("-server not supported on no-sock/no-http build; missing -use_mock_srv option or too few -rspin filename arguments");
  783. #endif
  784. }
  785. rspin_in_use = 0;
  786. }
  787. if (res == NULL)
  788. goto err;
  789. if (req_new != NULL || prev_opt_rspin != NULL) {
  790. /* need to satisfy nonce and transactionID checks by client */
  791. ASN1_OCTET_STRING *nonce;
  792. ASN1_OCTET_STRING *tid;
  793. hdr = OSSL_CMP_MSG_get0_header(res);
  794. nonce = OSSL_CMP_HDR_get0_recipNonce(hdr);
  795. tid = OSSL_CMP_HDR_get0_transactionID(hdr);
  796. if (!OSSL_CMP_CTX_set1_senderNonce(ctx, nonce)
  797. || !OSSL_CMP_CTX_set1_transactionID(ctx, tid)) {
  798. OSSL_CMP_MSG_free(res);
  799. res = NULL;
  800. goto err;
  801. }
  802. }
  803. if (opt_rspout != NULL && !write_PKIMESSAGE(res, &opt_rspout)) {
  804. OSSL_CMP_MSG_free(res);
  805. res = NULL;
  806. }
  807. err:
  808. OSSL_CMP_MSG_free(req_new);
  809. return res;
  810. }
  811. static int set_name(const char *str,
  812. int (*set_fn) (OSSL_CMP_CTX *ctx, const X509_NAME *name),
  813. OSSL_CMP_CTX *ctx, const char *desc)
  814. {
  815. if (str != NULL) {
  816. X509_NAME *n = parse_name(str, MBSTRING_ASC, 1, desc);
  817. if (n == NULL)
  818. return 0;
  819. if (!(*set_fn) (ctx, n)) {
  820. X509_NAME_free(n);
  821. CMP_err("out of memory");
  822. return 0;
  823. }
  824. X509_NAME_free(n);
  825. }
  826. return 1;
  827. }
  828. static int set_gennames(OSSL_CMP_CTX *ctx, char *names, const char *desc)
  829. {
  830. char *next;
  831. for (; names != NULL; names = next) {
  832. GENERAL_NAME *n;
  833. next = next_item(names);
  834. if (strcmp(names, "critical") == 0) {
  835. (void)OSSL_CMP_CTX_set_option(ctx,
  836. OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL,
  837. 1);
  838. continue;
  839. }
  840. /* try IP address first, then email/URI/domain name */
  841. (void)ERR_set_mark();
  842. n = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_IPADD, names, 0);
  843. if (n == NULL)
  844. n = a2i_GENERAL_NAME(NULL, NULL, NULL,
  845. strchr(names, '@') != NULL ? GEN_EMAIL :
  846. strchr(names, ':') != NULL ? GEN_URI : GEN_DNS,
  847. names, 0);
  848. (void)ERR_pop_to_mark();
  849. if (n == NULL) {
  850. CMP_err2("bad syntax of %s '%s'", desc, names);
  851. return 0;
  852. }
  853. if (!OSSL_CMP_CTX_push1_subjectAltName(ctx, n)) {
  854. GENERAL_NAME_free(n);
  855. CMP_err("out of memory");
  856. return 0;
  857. }
  858. GENERAL_NAME_free(n);
  859. }
  860. return 1;
  861. }
  862. static X509_STORE *load_trusted(char *input, int for_new_cert, const char *desc)
  863. {
  864. X509_STORE *ts = load_certstore(input, opt_otherpass, desc, vpm);
  865. if (ts == NULL)
  866. return NULL;
  867. X509_STORE_set_verify_cb(ts, X509_STORE_CTX_print_verify_cb);
  868. /* copy vpm to store */
  869. if (X509_STORE_set1_param(ts, vpm /* may be NULL */)
  870. && (for_new_cert || truststore_set_host_etc(ts, NULL)))
  871. return ts;
  872. BIO_printf(bio_err, "error setting verification parameters for %s\n", desc);
  873. OSSL_CMP_CTX_print_errors(cmp_ctx);
  874. X509_STORE_free(ts);
  875. return NULL;
  876. }
  877. typedef int (*add_X509_fn_t)(void *ctx, const X509 *cert);
  878. static int setup_cert(void *ctx, const char *file, const char *pass,
  879. const char *desc, add_X509_fn_t set1_fn)
  880. {
  881. X509 *cert;
  882. int ok;
  883. if (file == NULL)
  884. return 1;
  885. if ((cert = load_cert_pwd(file, pass, desc)) == NULL)
  886. return 0;
  887. ok = (*set1_fn)(ctx, cert);
  888. X509_free(cert);
  889. return ok;
  890. }
  891. typedef int (*add_X509_stack_fn_t)(void *ctx, const STACK_OF(X509) *certs);
  892. static int setup_certs(char *files, const char *desc, void *ctx,
  893. add_X509_stack_fn_t set1_fn)
  894. {
  895. STACK_OF(X509) *certs;
  896. int ok;
  897. if (files == NULL)
  898. return 1;
  899. if ((certs = load_certs_multifile(files, opt_otherpass, desc, vpm)) == NULL)
  900. return 0;
  901. ok = (*set1_fn)(ctx, certs);
  902. OSSL_STACK_OF_X509_free(certs);
  903. return ok;
  904. }
  905. /*
  906. * parse and transform some options, checking their syntax.
  907. * Returns 1 on success, 0 on error
  908. */
  909. static int transform_opts(void)
  910. {
  911. if (opt_cmd_s != NULL) {
  912. if (!strcmp(opt_cmd_s, "ir")) {
  913. opt_cmd = CMP_IR;
  914. } else if (!strcmp(opt_cmd_s, "kur")) {
  915. opt_cmd = CMP_KUR;
  916. } else if (!strcmp(opt_cmd_s, "cr")) {
  917. opt_cmd = CMP_CR;
  918. } else if (!strcmp(opt_cmd_s, "p10cr")) {
  919. opt_cmd = CMP_P10CR;
  920. } else if (!strcmp(opt_cmd_s, "rr")) {
  921. opt_cmd = CMP_RR;
  922. } else if (!strcmp(opt_cmd_s, "genm")) {
  923. opt_cmd = CMP_GENM;
  924. } else {
  925. CMP_err1("unknown cmp command '%s'", opt_cmd_s);
  926. return 0;
  927. }
  928. } else {
  929. CMP_err("no cmp command to execute");
  930. return 0;
  931. }
  932. #ifndef OPENSSL_NO_ENGINE
  933. # define FORMAT_OPTIONS (OPT_FMT_PEMDER | OPT_FMT_PKCS12 | OPT_FMT_ENGINE)
  934. #else
  935. # define FORMAT_OPTIONS (OPT_FMT_PEMDER | OPT_FMT_PKCS12)
  936. #endif
  937. if (opt_keyform_s != NULL
  938. && !opt_format(opt_keyform_s, FORMAT_OPTIONS, &opt_keyform)) {
  939. CMP_err("unknown option given for key loading format");
  940. return 0;
  941. }
  942. #undef FORMAT_OPTIONS
  943. if (opt_certform_s != NULL
  944. && !opt_format(opt_certform_s, OPT_FMT_PEMDER, &opt_certform)) {
  945. CMP_err("unknown option given for certificate storing format");
  946. return 0;
  947. }
  948. return 1;
  949. }
  950. static OSSL_CMP_SRV_CTX *setup_srv_ctx(ENGINE *engine)
  951. {
  952. OSSL_CMP_CTX *ctx; /* extra CMP (client) ctx partly used by server */
  953. OSSL_CMP_SRV_CTX *srv_ctx = ossl_cmp_mock_srv_new(app_get0_libctx(),
  954. app_get0_propq());
  955. if (srv_ctx == NULL)
  956. return NULL;
  957. ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx);
  958. if (opt_srv_ref == NULL) {
  959. if (opt_srv_cert == NULL) {
  960. /* opt_srv_cert should determine the sender */
  961. CMP_err("must give -srv_ref for mock server if no -srv_cert given");
  962. goto err;
  963. }
  964. } else {
  965. if (!OSSL_CMP_CTX_set1_referenceValue(ctx, (unsigned char *)opt_srv_ref,
  966. strlen(opt_srv_ref)))
  967. goto err;
  968. }
  969. if (opt_srv_secret != NULL) {
  970. int res;
  971. char *pass_str = get_passwd(opt_srv_secret, "PBMAC secret of mock server");
  972. if (pass_str != NULL) {
  973. cleanse(opt_srv_secret);
  974. res = OSSL_CMP_CTX_set1_secretValue(ctx, (unsigned char *)pass_str,
  975. strlen(pass_str));
  976. clear_free(pass_str);
  977. if (res == 0)
  978. goto err;
  979. }
  980. } else if (opt_srv_cert == NULL) {
  981. CMP_err("server credentials (-srv_secret or -srv_cert) must be given if -use_mock_srv or -port is used");
  982. goto err;
  983. } else {
  984. CMP_warn("server will not be able to handle PBM-protected requests since -srv_secret is not given");
  985. }
  986. if (opt_srv_secret == NULL
  987. && ((opt_srv_cert == NULL) != (opt_srv_key == NULL))) {
  988. CMP_err("must give both -srv_cert and -srv_key options or neither");
  989. goto err;
  990. }
  991. if (!setup_cert(ctx, opt_srv_cert, opt_srv_keypass,
  992. "signer certificate of the mock server",
  993. (add_X509_fn_t)OSSL_CMP_CTX_set1_cert))
  994. goto err;
  995. if (opt_srv_key != NULL) {
  996. EVP_PKEY *pkey = load_key_pwd(opt_srv_key, opt_keyform,
  997. opt_srv_keypass,
  998. engine, "private key for mock server cert");
  999. if (pkey == NULL || !OSSL_CMP_CTX_set1_pkey(ctx, pkey)) {
  1000. EVP_PKEY_free(pkey);
  1001. goto err;
  1002. }
  1003. EVP_PKEY_free(pkey);
  1004. }
  1005. cleanse(opt_srv_keypass);
  1006. if (opt_srv_trusted != NULL) {
  1007. X509_STORE *ts =
  1008. load_trusted(opt_srv_trusted, 0, "certs trusted by mock server");
  1009. if (ts == NULL || !OSSL_CMP_CTX_set0_trusted(ctx, ts)) {
  1010. X509_STORE_free(ts);
  1011. goto err;
  1012. }
  1013. } else {
  1014. CMP_warn("mock server will not be able to handle signature-protected requests since -srv_trusted is not given");
  1015. }
  1016. if (!setup_certs(opt_srv_untrusted,
  1017. "untrusted certificates for mock server", ctx,
  1018. (add_X509_stack_fn_t)OSSL_CMP_CTX_set1_untrusted))
  1019. goto err;
  1020. if (!setup_cert(srv_ctx, opt_ref_cert, opt_otherpass,
  1021. "reference cert to be expected by the mock server",
  1022. (add_X509_fn_t)ossl_cmp_mock_srv_set1_refCert))
  1023. goto err;
  1024. if (opt_rsp_cert == NULL) {
  1025. CMP_warn("no -rsp_cert given for mock server");
  1026. } else {
  1027. if (!setup_cert(srv_ctx, opt_rsp_cert, opt_keypass,
  1028. "cert the mock server returns on certificate requests",
  1029. (add_X509_fn_t)ossl_cmp_mock_srv_set1_certOut))
  1030. goto err;
  1031. }
  1032. if (!setup_certs(opt_rsp_extracerts,
  1033. "CMP extra certificates for mock server", srv_ctx,
  1034. (add_X509_stack_fn_t)ossl_cmp_mock_srv_set1_chainOut))
  1035. goto err;
  1036. if (!setup_certs(opt_rsp_capubs, "caPubs for mock server", srv_ctx,
  1037. (add_X509_stack_fn_t)ossl_cmp_mock_srv_set1_caPubsOut))
  1038. goto err;
  1039. if (!setup_cert(srv_ctx, opt_rsp_newwithnew, opt_otherpass,
  1040. "NewWithNew cert the mock server returns in rootCaKeyUpdate",
  1041. (add_X509_fn_t)ossl_cmp_mock_srv_set1_newWithNew)
  1042. || !setup_cert(srv_ctx, opt_rsp_newwithold, opt_otherpass,
  1043. "NewWithOld cert the mock server returns in rootCaKeyUpdate",
  1044. (add_X509_fn_t)ossl_cmp_mock_srv_set1_newWithOld)
  1045. || !setup_cert(srv_ctx, opt_rsp_oldwithnew, opt_otherpass,
  1046. "OldWithNew cert the mock server returns in rootCaKeyUpdate",
  1047. (add_X509_fn_t)ossl_cmp_mock_srv_set1_oldWithNew))
  1048. goto err;
  1049. (void)ossl_cmp_mock_srv_set_pollCount(srv_ctx, opt_poll_count);
  1050. (void)ossl_cmp_mock_srv_set_checkAfterTime(srv_ctx, opt_check_after);
  1051. if (opt_grant_implicitconf)
  1052. (void)OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(srv_ctx, 1);
  1053. if (opt_failure != INT_MIN) { /* option has been set explicitly */
  1054. if (opt_failure < 0 || OSSL_CMP_PKIFAILUREINFO_MAX < opt_failure) {
  1055. CMP_err1("-failure out of range, should be >= 0 and <= %d",
  1056. OSSL_CMP_PKIFAILUREINFO_MAX);
  1057. goto err;
  1058. }
  1059. if (opt_failurebits != 0)
  1060. CMP_warn("-failurebits overrides -failure");
  1061. else
  1062. opt_failurebits = 1 << opt_failure;
  1063. }
  1064. if ((unsigned)opt_failurebits > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) {
  1065. CMP_err("-failurebits out of range");
  1066. goto err;
  1067. }
  1068. if (!ossl_cmp_mock_srv_set_statusInfo(srv_ctx, opt_pkistatus,
  1069. opt_failurebits, opt_statusstring))
  1070. goto err;
  1071. if (opt_send_error)
  1072. (void)ossl_cmp_mock_srv_set_sendError(srv_ctx, 1);
  1073. if (opt_send_unprotected)
  1074. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1);
  1075. if (opt_send_unprot_err)
  1076. (void)OSSL_CMP_SRV_CTX_set_send_unprotected_errors(srv_ctx, 1);
  1077. if (opt_accept_unprotected)
  1078. (void)OSSL_CMP_SRV_CTX_set_accept_unprotected(srv_ctx, 1);
  1079. if (opt_accept_unprot_err)
  1080. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1);
  1081. if (opt_accept_raverified)
  1082. (void)OSSL_CMP_SRV_CTX_set_accept_raverified(srv_ctx, 1);
  1083. return srv_ctx;
  1084. err:
  1085. ossl_cmp_mock_srv_free(srv_ctx);
  1086. return NULL;
  1087. }
  1088. /*
  1089. * set up verification aspects of OSSL_CMP_CTX w.r.t. opts from config file/CLI.
  1090. * Returns pointer on success, NULL on error
  1091. */
  1092. static int setup_verification_ctx(OSSL_CMP_CTX *ctx)
  1093. {
  1094. if (!setup_certs(opt_untrusted, "untrusted certificates", ctx,
  1095. (add_X509_stack_fn_t)OSSL_CMP_CTX_set1_untrusted))
  1096. return 0;
  1097. if (opt_srvcert != NULL || opt_trusted != NULL) {
  1098. if (opt_srvcert != NULL) {
  1099. if (opt_trusted != NULL) {
  1100. CMP_warn("-trusted option is ignored since -srvcert option is present");
  1101. opt_trusted = NULL;
  1102. }
  1103. if (opt_recipient != NULL) {
  1104. CMP_warn("-recipient option is ignored since -srvcert option is present");
  1105. opt_recipient = NULL;
  1106. }
  1107. if (!setup_cert(ctx, opt_srvcert, opt_otherpass,
  1108. "directly trusted CMP server certificate",
  1109. (add_X509_fn_t)OSSL_CMP_CTX_set1_srvCert))
  1110. return 0;
  1111. }
  1112. if (opt_trusted != NULL) {
  1113. X509_STORE *ts;
  1114. /*
  1115. * the 0 arg below clears any expected host/ip/email address;
  1116. * opt_expect_sender is used instead
  1117. */
  1118. ts = load_trusted(opt_trusted, 0, "certs trusted by client");
  1119. if (ts == NULL || !OSSL_CMP_CTX_set0_trusted(ctx, ts)) {
  1120. X509_STORE_free(ts);
  1121. return 0;
  1122. }
  1123. }
  1124. }
  1125. if (opt_unprotected_errors)
  1126. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1);
  1127. if (opt_out_trusted != NULL) { /* for use in OSSL_CMP_certConf_cb() */
  1128. X509_VERIFY_PARAM *out_vpm = NULL;
  1129. X509_STORE *out_trusted =
  1130. load_trusted(opt_out_trusted, 1,
  1131. "trusted certs for verifying newly enrolled cert");
  1132. if (out_trusted == NULL)
  1133. return 0;
  1134. /* ignore any -attime here, new certs are current anyway */
  1135. out_vpm = X509_STORE_get0_param(out_trusted);
  1136. X509_VERIFY_PARAM_clear_flags(out_vpm, X509_V_FLAG_USE_CHECK_TIME);
  1137. (void)OSSL_CMP_CTX_set_certConf_cb_arg(ctx, out_trusted);
  1138. }
  1139. if (opt_disable_confirm)
  1140. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_DISABLE_CONFIRM, 1);
  1141. if (opt_implicit_confirm)
  1142. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM, 1);
  1143. return 1;
  1144. }
  1145. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  1146. /*
  1147. * set up ssl_ctx for the OSSL_CMP_CTX based on options from config file/CLI.
  1148. * Returns pointer on success, NULL on error
  1149. */
  1150. static SSL_CTX *setup_ssl_ctx(OSSL_CMP_CTX *ctx, const char *host,
  1151. ENGINE *engine)
  1152. {
  1153. STACK_OF(X509) *untrusted = OSSL_CMP_CTX_get0_untrusted(ctx);
  1154. EVP_PKEY *pkey = NULL;
  1155. X509_STORE *trust_store = NULL;
  1156. SSL_CTX *ssl_ctx;
  1157. int i;
  1158. ssl_ctx = SSL_CTX_new(TLS_client_method());
  1159. if (ssl_ctx == NULL)
  1160. return NULL;
  1161. if (opt_tls_trusted != NULL) {
  1162. trust_store = load_trusted(opt_tls_trusted, 0, "trusted TLS certs");
  1163. if (trust_store == NULL)
  1164. goto err;
  1165. SSL_CTX_set_cert_store(ssl_ctx, trust_store);
  1166. SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
  1167. } else {
  1168. CMP_warn("-tls_used given without -tls_trusted; will not authenticate the TLS server");
  1169. }
  1170. if (opt_tls_cert != NULL && opt_tls_key != NULL) {
  1171. X509 *cert;
  1172. STACK_OF(X509) *certs = NULL;
  1173. int ok;
  1174. if (!load_cert_certs(opt_tls_cert, &cert, &certs, 0, opt_tls_keypass,
  1175. "TLS client certificate (optionally with chain)",
  1176. vpm))
  1177. /* need opt_tls_keypass if opt_tls_cert is encrypted PKCS#12 file */
  1178. goto err;
  1179. ok = SSL_CTX_use_certificate(ssl_ctx, cert) > 0;
  1180. X509_free(cert);
  1181. /*
  1182. * Any further certs and any untrusted certs are used for constructing
  1183. * the chain to be provided with the TLS client cert to the TLS server.
  1184. */
  1185. if (!ok || !SSL_CTX_set0_chain(ssl_ctx, certs)) {
  1186. CMP_err1("unable to use client TLS certificate file '%s'",
  1187. opt_tls_cert);
  1188. OSSL_STACK_OF_X509_free(certs);
  1189. goto err;
  1190. }
  1191. for (i = 0; i < sk_X509_num(untrusted); i++) {
  1192. cert = sk_X509_value(untrusted, i);
  1193. if (!SSL_CTX_add1_chain_cert(ssl_ctx, cert)) {
  1194. CMP_err("could not add untrusted cert to TLS client cert chain");
  1195. goto err;
  1196. }
  1197. }
  1198. {
  1199. X509_VERIFY_PARAM *tls_vpm = NULL;
  1200. unsigned long bak_flags = 0; /* compiler warns without init */
  1201. if (trust_store != NULL) {
  1202. tls_vpm = X509_STORE_get0_param(trust_store);
  1203. bak_flags = X509_VERIFY_PARAM_get_flags(tls_vpm);
  1204. /* disable any cert status/revocation checking etc. */
  1205. X509_VERIFY_PARAM_clear_flags(tls_vpm,
  1206. ~(X509_V_FLAG_USE_CHECK_TIME
  1207. | X509_V_FLAG_NO_CHECK_TIME
  1208. | X509_V_FLAG_PARTIAL_CHAIN
  1209. | X509_V_FLAG_POLICY_CHECK));
  1210. }
  1211. CMP_debug("trying to build cert chain for own TLS cert");
  1212. if (SSL_CTX_build_cert_chain(ssl_ctx,
  1213. SSL_BUILD_CHAIN_FLAG_UNTRUSTED |
  1214. SSL_BUILD_CHAIN_FLAG_NO_ROOT)) {
  1215. CMP_debug("success building cert chain for own TLS cert");
  1216. } else {
  1217. OSSL_CMP_CTX_print_errors(ctx);
  1218. CMP_warn("could not build cert chain for own TLS cert");
  1219. }
  1220. if (trust_store != NULL)
  1221. X509_VERIFY_PARAM_set_flags(tls_vpm, bak_flags);
  1222. }
  1223. /* If present we append to the list also the certs from opt_tls_extra */
  1224. if (opt_tls_extra != NULL) {
  1225. STACK_OF(X509) *tls_extra = load_certs_multifile(opt_tls_extra,
  1226. opt_otherpass,
  1227. "extra certificates for TLS",
  1228. vpm);
  1229. int res = 1;
  1230. if (tls_extra == NULL)
  1231. goto err;
  1232. for (i = 0; i < sk_X509_num(tls_extra); i++) {
  1233. cert = sk_X509_value(tls_extra, i);
  1234. if (res != 0)
  1235. res = SSL_CTX_add_extra_chain_cert(ssl_ctx, cert);
  1236. if (res == 0)
  1237. X509_free(cert);
  1238. }
  1239. sk_X509_free(tls_extra);
  1240. if (res == 0) {
  1241. BIO_printf(bio_err, "error: unable to add TLS extra certs\n");
  1242. goto err;
  1243. }
  1244. }
  1245. pkey = load_key_pwd(opt_tls_key, opt_keyform, opt_tls_keypass,
  1246. engine, "TLS client private key");
  1247. cleanse(opt_tls_keypass);
  1248. if (pkey == NULL)
  1249. goto err;
  1250. /*
  1251. * verify the key matches the cert,
  1252. * not using SSL_CTX_check_private_key(ssl_ctx)
  1253. * because it gives poor and sometimes misleading diagnostics
  1254. */
  1255. if (!X509_check_private_key(SSL_CTX_get0_certificate(ssl_ctx),
  1256. pkey)) {
  1257. CMP_err2("TLS private key '%s' does not match the TLS certificate '%s'\n",
  1258. opt_tls_key, opt_tls_cert);
  1259. EVP_PKEY_free(pkey);
  1260. pkey = NULL; /* otherwise, for some reason double free! */
  1261. goto err;
  1262. }
  1263. if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) <= 0) {
  1264. CMP_err1("unable to use TLS client private key '%s'", opt_tls_key);
  1265. EVP_PKEY_free(pkey);
  1266. pkey = NULL; /* otherwise, for some reason double free! */
  1267. goto err;
  1268. }
  1269. EVP_PKEY_free(pkey); /* we do not need the handle any more */
  1270. } else {
  1271. CMP_warn("-tls_used given without -tls_key; cannot authenticate to the TLS server");
  1272. }
  1273. if (trust_store != NULL) {
  1274. /*
  1275. * Enable and parameterize server hostname/IP address check.
  1276. * If we did this before checking our own TLS cert
  1277. * the expected hostname would mislead the check.
  1278. */
  1279. if (!truststore_set_host_etc(trust_store,
  1280. opt_tls_host != NULL ? opt_tls_host : host))
  1281. goto err;
  1282. }
  1283. return ssl_ctx;
  1284. err:
  1285. SSL_CTX_free(ssl_ctx);
  1286. return NULL;
  1287. }
  1288. #endif /* OPENSSL_NO_SOCK */
  1289. /*
  1290. * set up protection aspects of OSSL_CMP_CTX based on options from config
  1291. * file/CLI while parsing options and checking their consistency.
  1292. * Returns 1 on success, 0 on error
  1293. */
  1294. static int setup_protection_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
  1295. {
  1296. if (!opt_unprotected_requests && opt_secret == NULL && opt_key == NULL) {
  1297. CMP_err("must give -key or -secret unless -unprotected_requests is used");
  1298. return 0;
  1299. }
  1300. if (opt_ref == NULL && opt_cert == NULL && opt_subject == NULL) {
  1301. /* cert or subject should determine the sender */
  1302. CMP_err("must give -ref if no -cert and no -subject given");
  1303. return 0;
  1304. }
  1305. if (opt_secret == NULL && ((opt_cert == NULL) != (opt_key == NULL))) {
  1306. CMP_err("must give both -cert and -key options or neither");
  1307. return 0;
  1308. }
  1309. if (opt_secret != NULL) {
  1310. char *pass_string = get_passwd(opt_secret, "PBMAC");
  1311. int res;
  1312. if (pass_string != NULL) {
  1313. cleanse(opt_secret);
  1314. res = OSSL_CMP_CTX_set1_secretValue(ctx,
  1315. (unsigned char *)pass_string,
  1316. strlen(pass_string));
  1317. clear_free(pass_string);
  1318. if (res == 0)
  1319. return 0;
  1320. }
  1321. if (opt_cert != NULL || opt_key != NULL)
  1322. CMP_warn("-cert and -key not used for protection since -secret is given");
  1323. }
  1324. if (opt_ref != NULL
  1325. && !OSSL_CMP_CTX_set1_referenceValue(ctx, (unsigned char *)opt_ref,
  1326. strlen(opt_ref)))
  1327. return 0;
  1328. if (opt_key != NULL) {
  1329. EVP_PKEY *pkey = load_key_pwd(opt_key, opt_keyform, opt_keypass, engine,
  1330. "private key for CMP client certificate");
  1331. if (pkey == NULL || !OSSL_CMP_CTX_set1_pkey(ctx, pkey)) {
  1332. EVP_PKEY_free(pkey);
  1333. return 0;
  1334. }
  1335. EVP_PKEY_free(pkey);
  1336. }
  1337. if (opt_secret == NULL && opt_srvcert == NULL && opt_trusted == NULL)
  1338. CMP_warn("will not authenticate server due to missing -secret, -trusted, or -srvcert");
  1339. if (opt_cert != NULL) {
  1340. X509 *cert;
  1341. STACK_OF(X509) *certs = NULL;
  1342. X509_STORE *own_trusted = NULL;
  1343. int ok;
  1344. if (!load_cert_certs(opt_cert, &cert, &certs, 0, opt_keypass,
  1345. "CMP client certificate (optionally with chain)",
  1346. vpm))
  1347. /* opt_keypass is needed if opt_cert is an encrypted PKCS#12 file */
  1348. return 0;
  1349. ok = OSSL_CMP_CTX_set1_cert(ctx, cert);
  1350. X509_free(cert);
  1351. if (!ok) {
  1352. CMP_err("out of memory");
  1353. } else {
  1354. if (opt_own_trusted != NULL) {
  1355. own_trusted = load_trusted(opt_own_trusted, 0,
  1356. "trusted certs for verifying own CMP signer cert");
  1357. ok = own_trusted != NULL;
  1358. }
  1359. ok = ok && OSSL_CMP_CTX_build_cert_chain(ctx, own_trusted, certs);
  1360. }
  1361. X509_STORE_free(own_trusted);
  1362. OSSL_STACK_OF_X509_free(certs);
  1363. if (!ok)
  1364. return 0;
  1365. } else if (opt_own_trusted != NULL) {
  1366. CMP_warn("-own_trusted option is ignored without -cert");
  1367. }
  1368. if (!setup_certs(opt_extracerts, "extra certificates for CMP", ctx,
  1369. (add_X509_stack_fn_t)OSSL_CMP_CTX_set1_extraCertsOut))
  1370. return 0;
  1371. cleanse(opt_otherpass);
  1372. if (opt_unprotected_requests)
  1373. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1);
  1374. if (opt_digest != NULL) {
  1375. int digest = OBJ_ln2nid(opt_digest);
  1376. if (digest == NID_undef) {
  1377. CMP_err1("digest algorithm name not recognized: '%s'", opt_digest);
  1378. return 0;
  1379. }
  1380. if (!OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_DIGEST_ALGNID, digest)
  1381. || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_OWF_ALGNID, digest)) {
  1382. CMP_err1("digest algorithm name not supported: '%s'", opt_digest);
  1383. return 0;
  1384. }
  1385. }
  1386. if (opt_mac != NULL) {
  1387. int mac = OBJ_ln2nid(opt_mac);
  1388. if (mac == NID_undef) {
  1389. CMP_err1("MAC algorithm name not recognized: '%s'", opt_mac);
  1390. return 0;
  1391. }
  1392. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_MAC_ALGNID, mac);
  1393. }
  1394. return 1;
  1395. }
  1396. static int set_fallback_pubkey(OSSL_CMP_CTX *ctx)
  1397. {
  1398. char *file = opt_reqin, *end = file, bak;
  1399. OSSL_CMP_MSG *req;
  1400. const X509_PUBKEY *pubkey;
  1401. EVP_PKEY *pkey;
  1402. EVP_PKEY *pkey1;
  1403. int res = 0;
  1404. /* temporarily separate first file name in opt_reqin */
  1405. while (*end != ',' && !isspace(_UC(*end)) && *end != '\0')
  1406. end++;
  1407. bak = *end;
  1408. *end = '\0';
  1409. req = OSSL_CMP_MSG_read(file, app_get0_libctx(), app_get0_propq());
  1410. *end = bak;
  1411. if (req == NULL) {
  1412. CMP_err1("failed to load ir/cr/kur file '%s' attempting to get fallback public key",
  1413. file);
  1414. return 0;
  1415. }
  1416. if ((pubkey = OSSL_CMP_MSG_get0_certreq_publickey(req)) == NULL
  1417. || (pkey = X509_PUBKEY_get0(pubkey)) == NULL) {
  1418. CMP_err1("failed to get fallback public key from ir/cr/kur file '%s'",
  1419. file);
  1420. goto err;
  1421. }
  1422. pkey1 = EVP_PKEY_dup(pkey);
  1423. if (pkey == NULL || !OSSL_CMP_CTX_set0_newPkey(ctx, 0 /* priv */, pkey1)) {
  1424. EVP_PKEY_free(pkey1);
  1425. CMP_err1("failed to get fallback public key obtained from ir/cr/kur file '%s'",
  1426. file);
  1427. goto err;
  1428. }
  1429. res = 1;
  1430. err:
  1431. OSSL_CMP_MSG_free(req);
  1432. return res;
  1433. }
  1434. /*
  1435. * Set up IR/CR/P10CR/KUR/CertConf/RR/GENM specific parts of the OSSL_CMP_CTX
  1436. * based on options from CLI and/or config file.
  1437. * Returns 1 on success, 0 on error
  1438. */
  1439. static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
  1440. {
  1441. X509_REQ *csr = NULL;
  1442. X509_EXTENSIONS *exts = NULL;
  1443. X509V3_CTX ext_ctx;
  1444. if (opt_subject == NULL
  1445. && opt_csr == NULL && opt_oldcert == NULL && opt_cert == NULL
  1446. && opt_cmd != CMP_RR && opt_cmd != CMP_GENM)
  1447. CMP_warn("no -subject given; no -csr or -oldcert or -cert available for fallback");
  1448. if (!set_name(opt_issuer, OSSL_CMP_CTX_set1_issuer, ctx, "issuer"))
  1449. return 0;
  1450. if (opt_cmd == CMP_IR || opt_cmd == CMP_CR || opt_cmd == CMP_KUR) {
  1451. if (opt_reqin == NULL && opt_newkey == NULL
  1452. && opt_key == NULL && opt_csr == NULL && opt_oldcert == NULL) {
  1453. CMP_err("missing -newkey (or -key) to be certified and no -csr, -oldcert, -cert, or -reqin option given, which could provide fallback public key");
  1454. return 0;
  1455. }
  1456. if (opt_newkey == NULL
  1457. && opt_popo != OSSL_CRMF_POPO_NONE
  1458. && opt_popo != OSSL_CRMF_POPO_RAVERIFIED) {
  1459. if (opt_csr != NULL) {
  1460. CMP_err1("no -newkey option given with private key for POPO, -csr option provides just public key%s",
  1461. opt_key == NULL ? "" :
  1462. ", and -key option superseded by -csr");
  1463. if (opt_reqin != NULL)
  1464. CMP_info("since -reqin is used, may use -popo -1 or -popo 0 to disable the needless generation of a POPO");
  1465. return 0;
  1466. }
  1467. if (opt_key == NULL) {
  1468. CMP_err("missing -newkey (or -key) option for key to be certified and for POPO");
  1469. return 0;
  1470. }
  1471. }
  1472. if (opt_certout == NULL && opt_reqout_only == NULL) {
  1473. CMP_err("-certout not given, nowhere to save newly enrolled certificate");
  1474. return 0;
  1475. }
  1476. if (!set_name(opt_subject, OSSL_CMP_CTX_set1_subjectName, ctx, "subject"))
  1477. return 0;
  1478. } else {
  1479. const char *msg = "option is ignored for commands other than 'ir', 'cr', and 'kur'";
  1480. if (opt_subject != NULL) {
  1481. if (opt_ref == NULL && opt_cert == NULL) {
  1482. /* will use subject as sender unless oldcert subject is used */
  1483. if (!set_name(opt_subject, OSSL_CMP_CTX_set1_subjectName, ctx, "subject"))
  1484. return 0;
  1485. } else {
  1486. CMP_warn1("-subject %s since sender is taken from -ref or -cert",
  1487. msg);
  1488. }
  1489. }
  1490. if (opt_issuer != NULL && opt_cmd != CMP_RR)
  1491. CMP_warn1("-issuer %s and 'rr'", msg);
  1492. if (opt_reqexts != NULL)
  1493. CMP_warn1("-reqexts %s", msg);
  1494. if (opt_san_nodefault)
  1495. CMP_warn1("-san_nodefault %s", msg);
  1496. if (opt_sans != NULL)
  1497. CMP_warn1("-sans %s", msg);
  1498. if (opt_policies != NULL)
  1499. CMP_warn1("-policies %s", msg);
  1500. if (opt_policy_oids != NULL)
  1501. CMP_warn1("-policy_oids %s", msg);
  1502. if (opt_cmd != CMP_P10CR) {
  1503. if (opt_implicit_confirm)
  1504. CMP_warn1("-implicit_confirm %s, and 'p10cr'", msg);
  1505. if (opt_disable_confirm)
  1506. CMP_warn1("-disable_confirm %s, and 'p10cr'", msg);
  1507. if (opt_certout != NULL)
  1508. CMP_warn1("-certout %s, and 'p10cr'", msg);
  1509. if (opt_chainout != NULL)
  1510. CMP_warn1("-chainout %s, and 'p10cr'", msg);
  1511. }
  1512. }
  1513. if (opt_cmd == CMP_KUR) {
  1514. char *ref_cert = opt_oldcert != NULL ? opt_oldcert : opt_cert;
  1515. if (ref_cert == NULL && opt_csr == NULL) {
  1516. CMP_err("missing -oldcert for certificate to be updated and no -csr given");
  1517. return 0;
  1518. }
  1519. if (opt_subject != NULL)
  1520. CMP_warn2("given -subject '%s' overrides the subject of '%s' for KUR",
  1521. opt_subject, ref_cert != NULL ? ref_cert : opt_csr);
  1522. }
  1523. if (opt_cmd == CMP_RR) {
  1524. if (opt_issuer == NULL && opt_serial == NULL) {
  1525. if (opt_oldcert == NULL && opt_csr == NULL) {
  1526. CMP_err("missing -oldcert or -issuer and -serial for certificate to be revoked and no -csr given");
  1527. return 0;
  1528. }
  1529. if (opt_oldcert != NULL && opt_csr != NULL)
  1530. CMP_warn("ignoring -csr since certificate to be revoked is given");
  1531. } else {
  1532. #define OSSL_CMP_RR_MSG "since -issuer and -serial is given for command 'rr'"
  1533. if (opt_issuer == NULL || opt_serial == NULL) {
  1534. CMP_err("Must give both -issuer and -serial options or neither");
  1535. return 0;
  1536. }
  1537. if (opt_oldcert != NULL)
  1538. CMP_warn("Ignoring -oldcert " OSSL_CMP_RR_MSG);
  1539. if (opt_csr != NULL)
  1540. CMP_warn("Ignoring -csr " OSSL_CMP_RR_MSG);
  1541. }
  1542. if (opt_serial != NULL) {
  1543. ASN1_INTEGER *sno;
  1544. if ((sno = s2i_ASN1_INTEGER(NULL, opt_serial)) == NULL) {
  1545. CMP_err1("cannot read serial number: '%s'", opt_serial);
  1546. return 0;
  1547. }
  1548. if (!OSSL_CMP_CTX_set1_serialNumber(ctx, sno)) {
  1549. ASN1_INTEGER_free(sno);
  1550. CMP_err("out of memory");
  1551. return 0;
  1552. }
  1553. ASN1_INTEGER_free(sno);
  1554. }
  1555. if (opt_revreason > CRL_REASON_NONE)
  1556. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_REVOCATION_REASON,
  1557. opt_revreason);
  1558. } else {
  1559. if (opt_serial != NULL)
  1560. CMP_warn("Ignoring -serial for command other than 'rr'");
  1561. }
  1562. if (opt_cmd == CMP_P10CR && opt_csr == NULL) {
  1563. CMP_err("missing PKCS#10 CSR for p10cr");
  1564. return 0;
  1565. }
  1566. if (opt_recipient == NULL && opt_srvcert == NULL && opt_issuer == NULL
  1567. && opt_oldcert == NULL && opt_cert == NULL)
  1568. CMP_warn("missing -recipient, -srvcert, -issuer, -oldcert or -cert; recipient for any requests not covered by -reqin will be set to \"NULL-DN\"");
  1569. if (opt_cmd == CMP_P10CR || opt_cmd == CMP_RR || opt_cmd == CMP_GENM) {
  1570. const char *msg = "option is ignored for 'p10cr', 'rr', and 'genm' commands";
  1571. if (opt_newkeypass != NULL)
  1572. CMP_warn1("-newkeypass %s", msg);
  1573. if (opt_newkey != NULL)
  1574. CMP_warn1("-newkey %s", msg);
  1575. if (opt_days != 0)
  1576. CMP_warn1("-days %s", msg);
  1577. if (opt_popo != OSSL_CRMF_POPO_NONE - 1)
  1578. CMP_warn1("-popo %s", msg);
  1579. if (opt_out_trusted != NULL)
  1580. CMP_warn1("-out_trusted %s", msg);
  1581. } else if (opt_newkey != NULL) {
  1582. const char *file = opt_newkey;
  1583. const int format = opt_keyform;
  1584. const char *pass = opt_newkeypass;
  1585. const char *desc = "new private key for cert to be enrolled";
  1586. EVP_PKEY *pkey;
  1587. int priv = 1;
  1588. BIO *bio_bak = bio_err;
  1589. bio_err = NULL; /* suppress diagnostics on first try loading key */
  1590. pkey = load_key_pwd(file, format, pass, engine, desc);
  1591. bio_err = bio_bak;
  1592. if (pkey == NULL) {
  1593. ERR_clear_error();
  1594. desc = opt_csr == NULL
  1595. ? "fallback public key for cert to be enrolled"
  1596. : "public key for checking cert resulting from p10cr";
  1597. pkey = load_pubkey(file, format, 0, pass, engine, desc);
  1598. priv = 0;
  1599. }
  1600. cleanse(opt_newkeypass);
  1601. if (pkey == NULL || !OSSL_CMP_CTX_set0_newPkey(ctx, priv, pkey)) {
  1602. EVP_PKEY_free(pkey);
  1603. return 0;
  1604. }
  1605. } else if (opt_reqin != NULL
  1606. && opt_key == NULL && opt_csr == NULL && opt_oldcert == NULL) {
  1607. if (!set_fallback_pubkey(ctx))
  1608. return 0;
  1609. }
  1610. if (opt_days > 0
  1611. && !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_VALIDITY_DAYS,
  1612. opt_days)) {
  1613. CMP_err("could not set requested cert validity period");
  1614. return 0;
  1615. }
  1616. if (opt_policies != NULL && opt_policy_oids != NULL) {
  1617. CMP_err("cannot have policies both via -policies and via -policy_oids");
  1618. return 0;
  1619. }
  1620. if (opt_csr != NULL) {
  1621. if (opt_cmd == CMP_GENM) {
  1622. CMP_warn("-csr option is ignored for 'genm' command");
  1623. } else {
  1624. csr = load_csr_autofmt(opt_csr, FORMAT_UNDEF, NULL, "PKCS#10 CSR");
  1625. if (csr == NULL)
  1626. return 0;
  1627. if (!OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
  1628. goto oom;
  1629. }
  1630. }
  1631. if (opt_reqexts != NULL || opt_policies != NULL) {
  1632. if ((exts = sk_X509_EXTENSION_new_null()) == NULL)
  1633. goto oom;
  1634. X509V3_set_ctx(&ext_ctx, NULL, NULL, csr, NULL, X509V3_CTX_REPLACE);
  1635. X509V3_set_nconf(&ext_ctx, conf);
  1636. if (opt_reqexts != NULL
  1637. && !X509V3_EXT_add_nconf_sk(conf, &ext_ctx, opt_reqexts, &exts)) {
  1638. CMP_err1("cannot load certificate request extension section '%s'",
  1639. opt_reqexts);
  1640. goto exts_err;
  1641. }
  1642. if (opt_policies != NULL
  1643. && !X509V3_EXT_add_nconf_sk(conf, &ext_ctx, opt_policies, &exts)) {
  1644. CMP_err1("cannot load policy cert request extension section '%s'",
  1645. opt_policies);
  1646. goto exts_err;
  1647. }
  1648. OSSL_CMP_CTX_set0_reqExtensions(ctx, exts);
  1649. }
  1650. X509_REQ_free(csr);
  1651. /* After here, must not goto oom/exts_err */
  1652. if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) && opt_sans != NULL) {
  1653. CMP_err("cannot have Subject Alternative Names both via -reqexts and via -sans");
  1654. return 0;
  1655. }
  1656. if (!set_gennames(ctx, opt_sans, "Subject Alternative Name"))
  1657. return 0;
  1658. if (opt_san_nodefault) {
  1659. if (opt_sans != NULL)
  1660. CMP_warn("-opt_san_nodefault has no effect when -sans is used");
  1661. (void)OSSL_CMP_CTX_set_option(ctx,
  1662. OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT, 1);
  1663. }
  1664. if (opt_policy_oids_critical) {
  1665. if (opt_policy_oids == NULL)
  1666. CMP_warn("-opt_policy_oids_critical has no effect unless -policy_oids is given");
  1667. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_POLICIES_CRITICAL, 1);
  1668. }
  1669. while (opt_policy_oids != NULL) {
  1670. ASN1_OBJECT *policy;
  1671. POLICYINFO *pinfo;
  1672. char *next = next_item(opt_policy_oids);
  1673. if ((policy = OBJ_txt2obj(opt_policy_oids, 1)) == 0) {
  1674. CMP_err1("Invalid -policy_oids arg '%s'", opt_policy_oids);
  1675. return 0;
  1676. }
  1677. if (OBJ_obj2nid(policy) == NID_undef)
  1678. CMP_warn1("Unknown -policy_oids arg: %.40s", opt_policy_oids);
  1679. if ((pinfo = POLICYINFO_new()) == NULL) {
  1680. ASN1_OBJECT_free(policy);
  1681. return 0;
  1682. }
  1683. pinfo->policyid = policy;
  1684. if (!OSSL_CMP_CTX_push0_policy(ctx, pinfo)) {
  1685. CMP_err1("cannot add policy with OID '%s'", opt_policy_oids);
  1686. POLICYINFO_free(pinfo);
  1687. return 0;
  1688. }
  1689. opt_policy_oids = next;
  1690. }
  1691. if (opt_popo >= OSSL_CRMF_POPO_NONE)
  1692. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_POPO_METHOD, opt_popo);
  1693. if (opt_oldcert != NULL) {
  1694. if (opt_cmd == CMP_GENM) {
  1695. CMP_warn("-oldcert option is ignored for 'genm' command");
  1696. } else {
  1697. if (!setup_cert(ctx, opt_oldcert, opt_keypass,
  1698. /* needed if opt_oldcert is encrypted PKCS12 file */
  1699. opt_cmd == CMP_KUR ? "certificate to be updated" :
  1700. opt_cmd == CMP_RR ? "certificate to be revoked" :
  1701. "reference certificate (oldcert)",
  1702. (add_X509_fn_t)OSSL_CMP_CTX_set1_oldCert))
  1703. return 0;
  1704. }
  1705. }
  1706. cleanse(opt_keypass);
  1707. return 1;
  1708. oom:
  1709. CMP_err("out of memory");
  1710. exts_err:
  1711. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  1712. X509_REQ_free(csr);
  1713. return 0;
  1714. }
  1715. static int add_certProfile(OSSL_CMP_CTX *ctx, const char *name)
  1716. {
  1717. OSSL_CMP_ITAV *itav = NULL;
  1718. STACK_OF(ASN1_UTF8STRING) *sk;
  1719. ASN1_UTF8STRING *utf8string;
  1720. if (ctx == NULL || name == NULL)
  1721. return 0;
  1722. if ((sk = sk_ASN1_UTF8STRING_new_reserve(NULL, 1)) == NULL)
  1723. return 0;
  1724. if ((utf8string = ASN1_UTF8STRING_new()) == NULL)
  1725. goto err;
  1726. if (!ASN1_STRING_set(utf8string, name, (int)strlen(name))) {
  1727. ASN1_STRING_free(utf8string);
  1728. goto err;
  1729. }
  1730. /* Due to sk_ASN1_UTF8STRING_new_reserve(NULL, 1), this surely succeeds: */
  1731. (void)sk_ASN1_UTF8STRING_push(sk, utf8string);
  1732. if ((itav = OSSL_CMP_ITAV_new0_certProfile(sk)) == NULL)
  1733. goto err;
  1734. if (OSSL_CMP_CTX_push0_geninfo_ITAV(ctx, itav))
  1735. return 1;
  1736. OSSL_CMP_ITAV_free(itav);
  1737. return 0;
  1738. err:
  1739. sk_ASN1_UTF8STRING_pop_free(sk, ASN1_UTF8STRING_free);
  1740. return 0;
  1741. }
  1742. static int handle_opt_geninfo(OSSL_CMP_CTX *ctx)
  1743. {
  1744. ASN1_OBJECT *obj = NULL;
  1745. ASN1_TYPE *type = NULL;
  1746. long value;
  1747. ASN1_INTEGER *aint = NULL;
  1748. ASN1_UTF8STRING *text = NULL;
  1749. OSSL_CMP_ITAV *itav;
  1750. char *ptr = opt_geninfo, *oid, *end;
  1751. do {
  1752. while (isspace(_UC(*ptr)))
  1753. ptr++;
  1754. oid = ptr;
  1755. if ((ptr = strchr(oid, ':')) == NULL) {
  1756. CMP_err1("Missing ':' in -geninfo arg %.40s", oid);
  1757. return 0;
  1758. }
  1759. *ptr++ = '\0';
  1760. if ((obj = OBJ_txt2obj(oid, 0)) == NULL) {
  1761. CMP_err1("Invalid OID in -geninfo arg %.40s", oid);
  1762. return 0;
  1763. }
  1764. if (OBJ_obj2nid(obj) == NID_undef)
  1765. CMP_warn1("Unknown OID in -geninfo arg: %.40s", oid);
  1766. if ((type = ASN1_TYPE_new()) == NULL)
  1767. goto oom;
  1768. if (CHECK_AND_SKIP_CASE_PREFIX(ptr, "int:")) {
  1769. value = strtol(ptr, &end, 10);
  1770. if (end == ptr) {
  1771. CMP_err1("Cannot parse int in -geninfo arg %.40s", ptr);
  1772. goto err;
  1773. }
  1774. ptr = end;
  1775. if (*ptr != '\0') {
  1776. if (*ptr != ',') {
  1777. CMP_err1("Missing ',' or end of -geninfo arg after int at %.40s",
  1778. ptr);
  1779. goto err;
  1780. }
  1781. ptr++;
  1782. }
  1783. if ((aint = ASN1_INTEGER_new()) == NULL
  1784. || !ASN1_INTEGER_set(aint, value))
  1785. goto oom;
  1786. ASN1_TYPE_set(type, V_ASN1_INTEGER, aint);
  1787. aint = NULL;
  1788. } else if (CHECK_AND_SKIP_CASE_PREFIX(ptr, "str:")) {
  1789. end = strchr(ptr, ',');
  1790. if (end == NULL)
  1791. end = ptr + strlen(ptr);
  1792. else
  1793. *end++ = '\0';
  1794. if ((text = ASN1_UTF8STRING_new()) == NULL
  1795. || !ASN1_STRING_set(text, ptr, -1))
  1796. goto oom;
  1797. ptr = end;
  1798. ASN1_TYPE_set(type, V_ASN1_UTF8STRING, text);
  1799. text = NULL;
  1800. } else {
  1801. CMP_err1("Missing 'int:' or 'str:' in -geninfo arg %.40s", ptr);
  1802. goto err;
  1803. }
  1804. if ((itav = OSSL_CMP_ITAV_create(obj, type)) == NULL) {
  1805. CMP_err("Unable to create 'OSSL_CMP_ITAV' structure");
  1806. goto err;
  1807. }
  1808. obj = NULL;
  1809. type = NULL;
  1810. if (!OSSL_CMP_CTX_push0_geninfo_ITAV(ctx, itav)) {
  1811. CMP_err("Failed to add ITAV for geninfo of the PKI message header");
  1812. OSSL_CMP_ITAV_free(itav);
  1813. return 0;
  1814. }
  1815. } while (*ptr != '\0');
  1816. return 1;
  1817. oom:
  1818. CMP_err("out of memory");
  1819. err:
  1820. ASN1_OBJECT_free(obj);
  1821. ASN1_TYPE_free(type);
  1822. ASN1_INTEGER_free(aint);
  1823. ASN1_UTF8STRING_free(text);
  1824. return 0;
  1825. }
  1826. /*
  1827. * set up the client-side OSSL_CMP_CTX based on options from config file/CLI
  1828. * while parsing options and checking their consistency.
  1829. * Prints reason for error to bio_err.
  1830. * Returns 1 on success, 0 on error
  1831. */
  1832. static int setup_client_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
  1833. {
  1834. int ret = 0;
  1835. char *host = NULL, *port = NULL, *path = NULL, *used_path = opt_path;
  1836. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  1837. int portnum, use_ssl;
  1838. static char server_port[32] = { '\0' };
  1839. const char *proxy_host = NULL;
  1840. #endif
  1841. char server_buf[200] = "mock server";
  1842. char proxy_buf[200] = "";
  1843. if (!opt_use_mock_srv)
  1844. strcpy(server_buf, "no server");
  1845. if (!opt_use_mock_srv && opt_rspin == NULL) { /* note: -port is not given */
  1846. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  1847. if (opt_server == NULL && opt_reqout_only == NULL) {
  1848. CMP_err("missing -server or -use_mock_srv or -rspin option");
  1849. goto err;
  1850. }
  1851. #else
  1852. CMP_err("missing -use_mock_srv or -rspin option; -server option is not supported due to no-sock build");
  1853. goto err;
  1854. #endif
  1855. }
  1856. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  1857. if (opt_server == NULL) {
  1858. if (opt_proxy != NULL)
  1859. CMP_warn("ignoring -proxy option since -server is not given");
  1860. if (opt_no_proxy != NULL)
  1861. CMP_warn("ignoring -no_proxy option since -server is not given");
  1862. goto set_path;
  1863. }
  1864. if (!OSSL_HTTP_parse_url(opt_server, &use_ssl, NULL /* user */,
  1865. &host, &port, &portnum,
  1866. &path, NULL /* q */, NULL /* frag */)) {
  1867. CMP_err1("cannot parse -server URL: %s", opt_server);
  1868. goto err;
  1869. }
  1870. if (use_ssl && !opt_tls_used) {
  1871. CMP_warn("assuming -tls_used since -server URL indicates HTTPS");
  1872. opt_tls_used = 1;
  1873. }
  1874. if (!OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_USE_TLS, opt_tls_used))
  1875. goto err;
  1876. BIO_snprintf(server_port, sizeof(server_port), "%s", port);
  1877. if (opt_path == NULL)
  1878. used_path = path;
  1879. if (!OSSL_CMP_CTX_set1_server(ctx, host)
  1880. || !OSSL_CMP_CTX_set_serverPort(ctx, portnum))
  1881. goto oom;
  1882. if (opt_proxy != NULL && !OSSL_CMP_CTX_set1_proxy(ctx, opt_proxy))
  1883. goto oom;
  1884. if (opt_no_proxy != NULL && !OSSL_CMP_CTX_set1_no_proxy(ctx, opt_no_proxy))
  1885. goto oom;
  1886. (void)BIO_snprintf(server_buf, sizeof(server_buf), "http%s://%s:%s/%s",
  1887. opt_tls_used ? "s" : "", host, port,
  1888. *used_path == '/' ? used_path + 1 : used_path);
  1889. proxy_host = OSSL_HTTP_adapt_proxy(opt_proxy, opt_no_proxy, host, use_ssl);
  1890. if (proxy_host != NULL)
  1891. (void)BIO_snprintf(proxy_buf, sizeof(proxy_buf), " via %s", proxy_host);
  1892. set_path:
  1893. #endif
  1894. if (!OSSL_CMP_CTX_set1_serverPath(ctx, used_path))
  1895. goto oom;
  1896. if (!transform_opts())
  1897. goto err;
  1898. if (opt_infotype_s == NULL) {
  1899. if (opt_cmd == CMP_GENM)
  1900. CMP_warn("no -infotype option given for genm");
  1901. } else if (opt_cmd != CMP_GENM) {
  1902. CMP_warn("-infotype option is ignored for commands other than 'genm'");
  1903. } else {
  1904. char id_buf[100] = "id-it-";
  1905. strncat(id_buf, opt_infotype_s, sizeof(id_buf) - strlen(id_buf) - 1);
  1906. if ((opt_infotype = OBJ_sn2nid(id_buf)) == NID_undef) {
  1907. CMP_err("unknown OID name in -infotype option");
  1908. goto err;
  1909. }
  1910. }
  1911. if (opt_cmd != CMP_GENM || opt_infotype != NID_id_it_rootCaCert) {
  1912. const char *msg = "option is ignored unless -cmd 'genm' and -infotype rootCaCert is given";
  1913. if (opt_oldwithold != NULL)
  1914. CMP_warn1("-oldwithold %s", msg);
  1915. if (opt_newwithnew != NULL)
  1916. CMP_warn1("-newwithnew %s", msg);
  1917. if (opt_newwithold != NULL)
  1918. CMP_warn1("-newwithold %s", msg);
  1919. if (opt_oldwithnew != NULL)
  1920. CMP_warn1("-oldwithnew %s", msg);
  1921. }
  1922. if (!setup_verification_ctx(ctx))
  1923. goto err;
  1924. if (opt_keep_alive != 1)
  1925. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_KEEP_ALIVE,
  1926. opt_keep_alive);
  1927. if (opt_total_timeout > 0 && opt_msg_timeout > 0
  1928. && opt_total_timeout < opt_msg_timeout) {
  1929. CMP_err2("-total_timeout argument = %d must not be < %d (-msg_timeout)",
  1930. opt_total_timeout, opt_msg_timeout);
  1931. goto err;
  1932. }
  1933. if (opt_msg_timeout >= 0) /* must do this before setup_ssl_ctx() */
  1934. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_MSG_TIMEOUT,
  1935. opt_msg_timeout);
  1936. if (opt_total_timeout >= 0)
  1937. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_TOTAL_TIMEOUT,
  1938. opt_total_timeout);
  1939. if (opt_rspin != NULL) {
  1940. rspin_in_use = 1;
  1941. if (opt_reqin != NULL)
  1942. CMP_warn("-reqin is ignored since -rspin is present");
  1943. }
  1944. if (opt_reqin_new_tid && opt_reqin == NULL)
  1945. CMP_warn("-reqin_new_tid is ignored since -reqin is not present");
  1946. if (opt_reqin != NULL || opt_reqout != NULL
  1947. || opt_rspin != NULL || opt_rspout != NULL || opt_use_mock_srv)
  1948. (void)OSSL_CMP_CTX_set_transfer_cb(ctx, read_write_req_resp);
  1949. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  1950. if (opt_tls_used) {
  1951. APP_HTTP_TLS_INFO *info;
  1952. if (opt_tls_cert != NULL
  1953. || opt_tls_key != NULL || opt_tls_keypass != NULL) {
  1954. if (opt_tls_key == NULL) {
  1955. CMP_err("missing -tls_key option");
  1956. goto err;
  1957. } else if (opt_tls_cert == NULL) {
  1958. CMP_err("missing -tls_cert option");
  1959. goto err;
  1960. }
  1961. }
  1962. if ((info = OPENSSL_zalloc(sizeof(*info))) == NULL)
  1963. goto err;
  1964. APP_HTTP_TLS_INFO_free(OSSL_CMP_CTX_get_http_cb_arg(ctx));
  1965. (void)OSSL_CMP_CTX_set_http_cb_arg(ctx, info);
  1966. info->ssl_ctx = setup_ssl_ctx(ctx, host, engine);
  1967. info->server = host;
  1968. host = NULL; /* prevent deallocation */
  1969. if ((info->port = OPENSSL_strdup(server_port)) == NULL)
  1970. goto err;
  1971. /* workaround for callback design flaw, see #17088: */
  1972. info->use_proxy = proxy_host != NULL;
  1973. info->timeout = OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_MSG_TIMEOUT);
  1974. if (info->ssl_ctx == NULL)
  1975. goto err;
  1976. (void)OSSL_CMP_CTX_set_http_cb(ctx, app_http_tls_cb);
  1977. }
  1978. #endif
  1979. if (!setup_protection_ctx(ctx, engine))
  1980. goto err;
  1981. if (!setup_request_ctx(ctx, engine))
  1982. goto err;
  1983. if (!set_name(opt_recipient, OSSL_CMP_CTX_set1_recipient, ctx, "recipient")
  1984. || !set_name(opt_expect_sender, OSSL_CMP_CTX_set1_expected_sender,
  1985. ctx, "expected sender"))
  1986. goto err;
  1987. if (opt_geninfo != NULL && !handle_opt_geninfo(ctx))
  1988. goto err;
  1989. if (opt_profile != NULL && !add_certProfile(ctx, opt_profile))
  1990. goto err;
  1991. /* not printing earlier, to minimize confusion in case setup fails before */
  1992. if (opt_reqout_only == NULL)
  1993. CMP_info3("will contact %s%s%s ", server_buf, proxy_buf,
  1994. opt_rspin == NULL ? "" :
  1995. " only if -rspin argument gives too few filenames");
  1996. ret = 1;
  1997. err:
  1998. OPENSSL_free(host);
  1999. OPENSSL_free(port);
  2000. OPENSSL_free(path);
  2001. return ret;
  2002. oom:
  2003. CMP_err("out of memory");
  2004. goto err;
  2005. }
  2006. /*
  2007. * write out the given certificate to the output specified by bio.
  2008. * Depending on options use either PEM or DER format.
  2009. * Returns 1 on success, 0 on error
  2010. */
  2011. static int write_cert(BIO *bio, X509 *cert)
  2012. {
  2013. if ((opt_certform == FORMAT_PEM && PEM_write_bio_X509(bio, cert))
  2014. || (opt_certform == FORMAT_ASN1 && i2d_X509_bio(bio, cert)))
  2015. return 1;
  2016. if (opt_certform != FORMAT_PEM && opt_certform != FORMAT_ASN1)
  2017. BIO_printf(bio_err,
  2018. "error: unsupported type '%s' for writing certificates\n",
  2019. opt_certform_s);
  2020. return 0;
  2021. }
  2022. /*
  2023. * If file != NULL writes out a stack of certs to the given file.
  2024. * If certs is NULL, the file is emptied.
  2025. * Frees the certs if present.
  2026. * Depending on options use either PEM or DER format,
  2027. * where DER does not make much sense for writing more than one cert!
  2028. * Returns number of written certificates on success, -1 on error.
  2029. */
  2030. static int save_free_certs(STACK_OF(X509) *certs,
  2031. const char *file, const char *desc)
  2032. {
  2033. BIO *bio = NULL;
  2034. int i;
  2035. int n = sk_X509_num(certs /* may be NULL */);
  2036. if (n < 0)
  2037. n = 0;
  2038. if (file == NULL)
  2039. goto end;
  2040. if (certs != NULL)
  2041. CMP_info3("received %d %s certificate(s), saving to file '%s'",
  2042. n, desc, file);
  2043. if (n > 1 && opt_certform != FORMAT_PEM)
  2044. CMP_warn("saving more than one certificate in non-PEM format");
  2045. if ((bio = BIO_new(BIO_s_file())) == NULL
  2046. || !BIO_write_filename(bio, (char *)file)) {
  2047. CMP_err3("could not open file '%s' for %s %s certificate(s)",
  2048. file, certs == NULL ? "deleting" : "writing", desc);
  2049. n = -1;
  2050. goto end;
  2051. }
  2052. for (i = 0; i < n; i++) {
  2053. if (!write_cert(bio, sk_X509_value(certs, i))) {
  2054. CMP_err2("cannot write %s certificate to file '%s'", desc, file);
  2055. n = -1;
  2056. goto end;
  2057. }
  2058. }
  2059. end:
  2060. BIO_free(bio);
  2061. OSSL_STACK_OF_X509_free(certs);
  2062. return n;
  2063. }
  2064. static int delete_file(const char *file, const char *desc)
  2065. {
  2066. if (file == NULL)
  2067. return 1;
  2068. if (unlink(file) != 0 && errno != ENOENT) {
  2069. CMP_err2("Failed to delete %s, which should be done to indicate there is no %s",
  2070. file, desc);
  2071. return 0;
  2072. }
  2073. return 1;
  2074. }
  2075. static int save_cert_or_delete(X509 *cert, const char *file, const char *desc)
  2076. {
  2077. if (file == NULL)
  2078. return 1;
  2079. if (cert == NULL) {
  2080. char desc_cert[80];
  2081. BIO_snprintf(desc_cert, sizeof(desc_cert), "%s certificate", desc);
  2082. return delete_file(file, desc_cert);
  2083. } else {
  2084. STACK_OF(X509) *certs = sk_X509_new_null();
  2085. if (!X509_add_cert(certs, cert, X509_ADD_FLAG_UP_REF)) {
  2086. sk_X509_free(certs);
  2087. return 0;
  2088. }
  2089. return save_free_certs(certs, file, desc) >= 0;
  2090. }
  2091. }
  2092. static int print_itavs(const STACK_OF(OSSL_CMP_ITAV) *itavs)
  2093. {
  2094. int i, ret = 1;
  2095. int n = sk_OSSL_CMP_ITAV_num(itavs);
  2096. if (n <= 0) { /* also in case itavs == NULL */
  2097. CMP_info("genp does not contain any ITAV");
  2098. return ret;
  2099. }
  2100. for (i = 1; i <= n; i++) {
  2101. OSSL_CMP_ITAV *itav = sk_OSSL_CMP_ITAV_value(itavs, i - 1);
  2102. ASN1_OBJECT *type = OSSL_CMP_ITAV_get0_type(itav);
  2103. char name[80];
  2104. if (itav == NULL) {
  2105. CMP_err1("could not get ITAV #%d from genp", i);
  2106. ret = 0;
  2107. continue;
  2108. }
  2109. if (i2t_ASN1_OBJECT(name, sizeof(name), type) <= 0) {
  2110. CMP_err1("error parsing type of ITAV #%d from genp", i);
  2111. ret = 0;
  2112. } else {
  2113. CMP_info2("ITAV #%d from genp infoType=%s", i, name);
  2114. }
  2115. }
  2116. return ret;
  2117. }
  2118. static char opt_item[SECTION_NAME_MAX + 1];
  2119. /* get previous name from a comma or space-separated list of names */
  2120. static const char *prev_item(const char *opt, const char *end)
  2121. {
  2122. const char *beg;
  2123. size_t len;
  2124. if (end == opt)
  2125. return NULL;
  2126. beg = end;
  2127. while (beg > opt) {
  2128. --beg;
  2129. if (beg[0] == ',' || isspace(_UC(beg[0]))) {
  2130. ++beg;
  2131. break;
  2132. }
  2133. }
  2134. len = end - beg;
  2135. if (len > SECTION_NAME_MAX) {
  2136. CMP_warn3("using only first %d characters of section name starting with \"%.*s\"",
  2137. SECTION_NAME_MAX, SECTION_NAME_MAX, beg);
  2138. len = SECTION_NAME_MAX;
  2139. }
  2140. memcpy(opt_item, beg, len);
  2141. opt_item[len] = '\0';
  2142. while (beg > opt) {
  2143. --beg;
  2144. if (beg[0] != ',' && !isspace(_UC(beg[0]))) {
  2145. ++beg;
  2146. break;
  2147. }
  2148. }
  2149. return beg;
  2150. }
  2151. /* get str value for name from a comma-separated hierarchy of config sections */
  2152. static char *conf_get_string(const CONF *src_conf, const char *groups,
  2153. const char *name)
  2154. {
  2155. char *res = NULL;
  2156. const char *end = groups + strlen(groups);
  2157. while ((end = prev_item(groups, end)) != NULL) {
  2158. if ((res = app_conf_try_string(src_conf, opt_item, name)) != NULL)
  2159. return res;
  2160. }
  2161. return res;
  2162. }
  2163. /* get long val for name from a comma-separated hierarchy of config sections */
  2164. static int conf_get_number_e(const CONF *conf_, const char *groups,
  2165. const char *name, long *result)
  2166. {
  2167. char *str = conf_get_string(conf_, groups, name);
  2168. char *tailptr;
  2169. long res;
  2170. if (str == NULL || *str == '\0')
  2171. return 0;
  2172. res = strtol(str, &tailptr, 10);
  2173. if (res == LONG_MIN || res == LONG_MAX || *tailptr != '\0')
  2174. return 0;
  2175. *result = res;
  2176. return 1;
  2177. }
  2178. /*
  2179. * use the command line option table to read values from the CMP section
  2180. * of openssl.cnf. Defaults are taken from the config file, they can be
  2181. * overwritten on the command line.
  2182. */
  2183. static int read_config(void)
  2184. {
  2185. unsigned int i;
  2186. long num = 0;
  2187. char *txt = NULL;
  2188. const OPTIONS *opt;
  2189. int start_opt = OPT_VERBOSITY - OPT_HELP;
  2190. int start_idx = OPT_VERBOSITY - 2;
  2191. /*
  2192. * starting with offset OPT_VERBOSITY because OPT_CONFIG and OPT_SECTION
  2193. * would not make sense within the config file.
  2194. */
  2195. int n_options = OSSL_NELEM(cmp_options) - 1;
  2196. for (opt = &cmp_options[start_opt], i = start_idx;
  2197. opt->name != NULL; i++, opt++)
  2198. if (!strcmp(opt->name, OPT_SECTION_STR)
  2199. || !strcmp(opt->name, OPT_MORE_STR))
  2200. n_options--;
  2201. OPENSSL_assert(OSSL_NELEM(cmp_vars) == n_options
  2202. + OPT_PROV__FIRST + 1 - OPT_PROV__LAST
  2203. + OPT_R__FIRST + 1 - OPT_R__LAST
  2204. + OPT_V__FIRST + 1 - OPT_V__LAST);
  2205. for (opt = &cmp_options[start_opt], i = start_idx;
  2206. opt->name != NULL; i++, opt++) {
  2207. int provider_option = (OPT_PROV__FIRST <= opt->retval
  2208. && opt->retval < OPT_PROV__LAST);
  2209. int rand_state_option = (OPT_R__FIRST <= opt->retval
  2210. && opt->retval < OPT_R__LAST);
  2211. int verification_option = (OPT_V__FIRST <= opt->retval
  2212. && opt->retval < OPT_V__LAST);
  2213. if (strcmp(opt->name, OPT_SECTION_STR) == 0
  2214. || strcmp(opt->name, OPT_MORE_STR) == 0) {
  2215. i--;
  2216. continue;
  2217. }
  2218. if (provider_option || rand_state_option || verification_option)
  2219. i--;
  2220. switch (opt->valtype) {
  2221. case '-':
  2222. case 'p':
  2223. case 'n':
  2224. case 'N':
  2225. case 'l':
  2226. if (!conf_get_number_e(conf, opt_section, opt->name, &num)) {
  2227. ERR_clear_error();
  2228. continue; /* option not provided */
  2229. }
  2230. if (opt->valtype == 'p' && num <= 0) {
  2231. opt_printf_stderr("Non-positive number \"%ld\" for config option -%s\n",
  2232. num, opt->name);
  2233. return -1;
  2234. }
  2235. if (opt->valtype == 'N' && num < 0) {
  2236. opt_printf_stderr("Negative number \"%ld\" for config option -%s\n",
  2237. num, opt->name);
  2238. return -1;
  2239. }
  2240. break;
  2241. case 's':
  2242. case '>':
  2243. case 'M':
  2244. txt = conf_get_string(conf, opt_section, opt->name);
  2245. if (txt == NULL) {
  2246. ERR_clear_error();
  2247. continue; /* option not provided */
  2248. }
  2249. break;
  2250. default:
  2251. CMP_err2("internal: unsupported type '%c' for option '%s'",
  2252. opt->valtype, opt->name);
  2253. return 0;
  2254. break;
  2255. }
  2256. if (provider_option || verification_option) {
  2257. int conf_argc = 1;
  2258. char *conf_argv[3];
  2259. char arg1[82];
  2260. BIO_snprintf(arg1, 81, "-%s", (char *)opt->name);
  2261. conf_argv[0] = prog;
  2262. conf_argv[1] = arg1;
  2263. if (opt->valtype == '-') {
  2264. if (num != 0)
  2265. conf_argc = 2;
  2266. } else {
  2267. conf_argc = 3;
  2268. conf_argv[2] = conf_get_string(conf, opt_section, opt->name);
  2269. /* not NULL */
  2270. }
  2271. if (conf_argc > 1) {
  2272. (void)opt_init(conf_argc, conf_argv, cmp_options);
  2273. if (provider_option
  2274. ? !opt_provider(opt_next())
  2275. : !opt_verify(opt_next(), vpm)) {
  2276. CMP_err2("for option '%s' in config file section '%s'",
  2277. opt->name, opt_section);
  2278. return 0;
  2279. }
  2280. }
  2281. } else {
  2282. switch (opt->valtype) {
  2283. case '-':
  2284. case 'p':
  2285. case 'n':
  2286. case 'N':
  2287. if (num < INT_MIN || INT_MAX < num) {
  2288. BIO_printf(bio_err,
  2289. "integer value out of range for option '%s'\n",
  2290. opt->name);
  2291. return 0;
  2292. }
  2293. *cmp_vars[i].num = (int)num;
  2294. break;
  2295. case 'l':
  2296. *cmp_vars[i].num_long = num;
  2297. break;
  2298. default:
  2299. if (txt != NULL && txt[0] == '\0')
  2300. txt = NULL; /* reset option on empty string input */
  2301. *cmp_vars[i].txt = txt;
  2302. break;
  2303. }
  2304. }
  2305. }
  2306. return 1;
  2307. }
  2308. static char *opt_str(void)
  2309. {
  2310. char *arg = opt_arg();
  2311. if (arg[0] == '\0') {
  2312. CMP_warn1("%s option argument is empty string, resetting option",
  2313. opt_flag());
  2314. arg = NULL;
  2315. } else if (arg[0] == '-') {
  2316. CMP_warn1("%s option argument starts with hyphen", opt_flag());
  2317. }
  2318. return arg;
  2319. }
  2320. /* returns 1 on success, 0 on error, -1 on -help (i.e., stop with success) */
  2321. static int get_opts(int argc, char **argv)
  2322. {
  2323. OPTION_CHOICE o;
  2324. prog = opt_init(argc, argv, cmp_options);
  2325. while ((o = opt_next()) != OPT_EOF) {
  2326. switch (o) {
  2327. case OPT_EOF:
  2328. case OPT_ERR:
  2329. opthelp:
  2330. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  2331. return 0;
  2332. case OPT_HELP:
  2333. opt_help(cmp_options);
  2334. return -1;
  2335. case OPT_CONFIG: /* has already been handled */
  2336. case OPT_SECTION: /* has already been handled */
  2337. break;
  2338. case OPT_VERBOSITY:
  2339. if (!set_verbosity(opt_int_arg()))
  2340. goto opthelp;
  2341. break;
  2342. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  2343. case OPT_SERVER:
  2344. opt_server = opt_str();
  2345. break;
  2346. case OPT_PROXY:
  2347. opt_proxy = opt_str();
  2348. break;
  2349. case OPT_NO_PROXY:
  2350. opt_no_proxy = opt_str();
  2351. break;
  2352. #endif
  2353. case OPT_RECIPIENT:
  2354. opt_recipient = opt_str();
  2355. break;
  2356. case OPT_PATH:
  2357. opt_path = opt_str();
  2358. break;
  2359. case OPT_KEEP_ALIVE:
  2360. opt_keep_alive = opt_int_arg();
  2361. if (opt_keep_alive > 2) {
  2362. CMP_err("-keep_alive argument must be 0, 1, or 2");
  2363. goto opthelp;
  2364. }
  2365. break;
  2366. case OPT_MSG_TIMEOUT:
  2367. opt_msg_timeout = opt_int_arg();
  2368. break;
  2369. case OPT_TOTAL_TIMEOUT:
  2370. opt_total_timeout = opt_int_arg();
  2371. break;
  2372. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  2373. case OPT_TLS_USED:
  2374. opt_tls_used = 1;
  2375. break;
  2376. case OPT_TLS_CERT:
  2377. opt_tls_cert = opt_str();
  2378. break;
  2379. case OPT_TLS_KEY:
  2380. opt_tls_key = opt_str();
  2381. break;
  2382. case OPT_TLS_KEYPASS:
  2383. opt_tls_keypass = opt_str();
  2384. break;
  2385. case OPT_TLS_EXTRA:
  2386. opt_tls_extra = opt_str();
  2387. break;
  2388. case OPT_TLS_TRUSTED:
  2389. opt_tls_trusted = opt_str();
  2390. break;
  2391. case OPT_TLS_HOST:
  2392. opt_tls_host = opt_str();
  2393. break;
  2394. #endif
  2395. case OPT_REF:
  2396. opt_ref = opt_str();
  2397. break;
  2398. case OPT_SECRET:
  2399. opt_secret = opt_str();
  2400. break;
  2401. case OPT_CERT:
  2402. opt_cert = opt_str();
  2403. break;
  2404. case OPT_OWN_TRUSTED:
  2405. opt_own_trusted = opt_str();
  2406. break;
  2407. case OPT_KEY:
  2408. opt_key = opt_str();
  2409. break;
  2410. case OPT_KEYPASS:
  2411. opt_keypass = opt_str();
  2412. break;
  2413. case OPT_DIGEST:
  2414. opt_digest = opt_str();
  2415. break;
  2416. case OPT_MAC:
  2417. opt_mac = opt_str();
  2418. break;
  2419. case OPT_EXTRACERTS:
  2420. opt_extracerts = opt_str();
  2421. break;
  2422. case OPT_UNPROTECTED_REQUESTS:
  2423. opt_unprotected_requests = 1;
  2424. break;
  2425. case OPT_TRUSTED:
  2426. opt_trusted = opt_str();
  2427. break;
  2428. case OPT_UNTRUSTED:
  2429. opt_untrusted = opt_str();
  2430. break;
  2431. case OPT_SRVCERT:
  2432. opt_srvcert = opt_str();
  2433. break;
  2434. case OPT_EXPECT_SENDER:
  2435. opt_expect_sender = opt_str();
  2436. break;
  2437. case OPT_IGNORE_KEYUSAGE:
  2438. opt_ignore_keyusage = 1;
  2439. break;
  2440. case OPT_UNPROTECTED_ERRORS:
  2441. opt_unprotected_errors = 1;
  2442. break;
  2443. case OPT_NO_CACHE_EXTRACERTS:
  2444. opt_no_cache_extracerts = 1;
  2445. break;
  2446. case OPT_SRVCERTOUT:
  2447. opt_srvcertout = opt_str();
  2448. break;
  2449. case OPT_EXTRACERTSOUT:
  2450. opt_extracertsout = opt_str();
  2451. break;
  2452. case OPT_CACERTSOUT:
  2453. opt_cacertsout = opt_str();
  2454. break;
  2455. case OPT_OLDWITHOLD:
  2456. opt_oldwithold = opt_str();
  2457. break;
  2458. case OPT_NEWWITHNEW:
  2459. opt_newwithnew = opt_str();
  2460. break;
  2461. case OPT_NEWWITHOLD:
  2462. opt_newwithold = opt_str();
  2463. break;
  2464. case OPT_OLDWITHNEW:
  2465. opt_oldwithnew = opt_str();
  2466. break;
  2467. case OPT_V_CASES:
  2468. if (!opt_verify(o, vpm))
  2469. goto opthelp;
  2470. break;
  2471. case OPT_CMD:
  2472. opt_cmd_s = opt_str();
  2473. break;
  2474. case OPT_INFOTYPE:
  2475. opt_infotype_s = opt_str();
  2476. break;
  2477. case OPT_PROFILE:
  2478. opt_profile = opt_str();
  2479. break;
  2480. case OPT_GENINFO:
  2481. opt_geninfo = opt_str();
  2482. break;
  2483. case OPT_NEWKEY:
  2484. opt_newkey = opt_str();
  2485. break;
  2486. case OPT_NEWKEYPASS:
  2487. opt_newkeypass = opt_str();
  2488. break;
  2489. case OPT_SUBJECT:
  2490. opt_subject = opt_str();
  2491. break;
  2492. case OPT_DAYS:
  2493. opt_days = opt_int_arg();
  2494. break;
  2495. case OPT_REQEXTS:
  2496. opt_reqexts = opt_str();
  2497. break;
  2498. case OPT_SANS:
  2499. opt_sans = opt_str();
  2500. break;
  2501. case OPT_SAN_NODEFAULT:
  2502. opt_san_nodefault = 1;
  2503. break;
  2504. case OPT_POLICIES:
  2505. opt_policies = opt_str();
  2506. break;
  2507. case OPT_POLICY_OIDS:
  2508. opt_policy_oids = opt_str();
  2509. break;
  2510. case OPT_POLICY_OIDS_CRITICAL:
  2511. opt_policy_oids_critical = 1;
  2512. break;
  2513. case OPT_POPO:
  2514. opt_popo = opt_int_arg();
  2515. if (opt_popo < OSSL_CRMF_POPO_NONE
  2516. || opt_popo > OSSL_CRMF_POPO_KEYENC) {
  2517. CMP_err("invalid popo spec. Valid values are -1 .. 2");
  2518. goto opthelp;
  2519. }
  2520. break;
  2521. case OPT_CSR:
  2522. opt_csr = opt_str();
  2523. break;
  2524. case OPT_OUT_TRUSTED:
  2525. opt_out_trusted = opt_str();
  2526. break;
  2527. case OPT_IMPLICIT_CONFIRM:
  2528. opt_implicit_confirm = 1;
  2529. break;
  2530. case OPT_DISABLE_CONFIRM:
  2531. opt_disable_confirm = 1;
  2532. break;
  2533. case OPT_CERTOUT:
  2534. opt_certout = opt_str();
  2535. break;
  2536. case OPT_CHAINOUT:
  2537. opt_chainout = opt_str();
  2538. break;
  2539. case OPT_OLDCERT:
  2540. opt_oldcert = opt_str();
  2541. break;
  2542. case OPT_REVREASON:
  2543. opt_revreason = opt_int_arg();
  2544. if (opt_revreason < CRL_REASON_NONE
  2545. || opt_revreason > CRL_REASON_AA_COMPROMISE
  2546. || opt_revreason == 7) {
  2547. CMP_err("invalid revreason. Valid values are -1 .. 6, 8 .. 10");
  2548. goto opthelp;
  2549. }
  2550. break;
  2551. case OPT_ISSUER:
  2552. opt_issuer = opt_str();
  2553. break;
  2554. case OPT_SERIAL:
  2555. opt_serial = opt_str();
  2556. break;
  2557. case OPT_CERTFORM:
  2558. opt_certform_s = opt_str();
  2559. break;
  2560. case OPT_KEYFORM:
  2561. opt_keyform_s = opt_str();
  2562. break;
  2563. case OPT_OTHERPASS:
  2564. opt_otherpass = opt_str();
  2565. break;
  2566. #ifndef OPENSSL_NO_ENGINE
  2567. case OPT_ENGINE:
  2568. opt_engine = opt_str();
  2569. break;
  2570. #endif
  2571. case OPT_PROV_CASES:
  2572. if (!opt_provider(o))
  2573. goto opthelp;
  2574. break;
  2575. case OPT_R_CASES:
  2576. if (!opt_rand(o))
  2577. goto opthelp;
  2578. break;
  2579. case OPT_BATCH:
  2580. opt_batch = 1;
  2581. break;
  2582. case OPT_REPEAT:
  2583. opt_repeat = opt_int_arg();
  2584. break;
  2585. case OPT_REQIN:
  2586. opt_reqin = opt_str();
  2587. break;
  2588. case OPT_REQIN_NEW_TID:
  2589. opt_reqin_new_tid = 1;
  2590. break;
  2591. case OPT_REQOUT:
  2592. opt_reqout = opt_str();
  2593. break;
  2594. case OPT_REQOUT_ONLY:
  2595. opt_reqout_only = opt_str();
  2596. break;
  2597. case OPT_RSPIN:
  2598. opt_rspin = opt_str();
  2599. break;
  2600. case OPT_RSPOUT:
  2601. opt_rspout = opt_str();
  2602. break;
  2603. case OPT_USE_MOCK_SRV:
  2604. opt_use_mock_srv = 1;
  2605. break;
  2606. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  2607. case OPT_PORT:
  2608. opt_port = opt_str();
  2609. break;
  2610. case OPT_MAX_MSGS:
  2611. opt_max_msgs = opt_int_arg();
  2612. break;
  2613. #endif
  2614. case OPT_SRV_REF:
  2615. opt_srv_ref = opt_str();
  2616. break;
  2617. case OPT_SRV_SECRET:
  2618. opt_srv_secret = opt_str();
  2619. break;
  2620. case OPT_SRV_CERT:
  2621. opt_srv_cert = opt_str();
  2622. break;
  2623. case OPT_SRV_KEY:
  2624. opt_srv_key = opt_str();
  2625. break;
  2626. case OPT_SRV_KEYPASS:
  2627. opt_srv_keypass = opt_str();
  2628. break;
  2629. case OPT_SRV_TRUSTED:
  2630. opt_srv_trusted = opt_str();
  2631. break;
  2632. case OPT_SRV_UNTRUSTED:
  2633. opt_srv_untrusted = opt_str();
  2634. break;
  2635. case OPT_REF_CERT:
  2636. opt_ref_cert = opt_str();
  2637. break;
  2638. case OPT_RSP_CERT:
  2639. opt_rsp_cert = opt_str();
  2640. break;
  2641. case OPT_RSP_EXTRACERTS:
  2642. opt_rsp_extracerts = opt_str();
  2643. break;
  2644. case OPT_RSP_CAPUBS:
  2645. opt_rsp_capubs = opt_str();
  2646. break;
  2647. case OPT_RSP_NEWWITHNEW:
  2648. opt_rsp_newwithnew = opt_str();
  2649. break;
  2650. case OPT_RSP_NEWWITHOLD:
  2651. opt_rsp_newwithold = opt_str();
  2652. break;
  2653. case OPT_RSP_OLDWITHNEW:
  2654. opt_rsp_oldwithnew = opt_str();
  2655. break;
  2656. case OPT_POLL_COUNT:
  2657. opt_poll_count = opt_int_arg();
  2658. break;
  2659. case OPT_CHECK_AFTER:
  2660. opt_check_after = opt_int_arg();
  2661. break;
  2662. case OPT_GRANT_IMPLICITCONF:
  2663. opt_grant_implicitconf = 1;
  2664. break;
  2665. case OPT_PKISTATUS:
  2666. opt_pkistatus = opt_int_arg();
  2667. break;
  2668. case OPT_FAILURE:
  2669. opt_failure = opt_int_arg();
  2670. break;
  2671. case OPT_FAILUREBITS:
  2672. opt_failurebits = opt_int_arg();
  2673. break;
  2674. case OPT_STATUSSTRING:
  2675. opt_statusstring = opt_str();
  2676. break;
  2677. case OPT_SEND_ERROR:
  2678. opt_send_error = 1;
  2679. break;
  2680. case OPT_SEND_UNPROTECTED:
  2681. opt_send_unprotected = 1;
  2682. break;
  2683. case OPT_SEND_UNPROT_ERR:
  2684. opt_send_unprot_err = 1;
  2685. break;
  2686. case OPT_ACCEPT_UNPROTECTED:
  2687. opt_accept_unprotected = 1;
  2688. break;
  2689. case OPT_ACCEPT_UNPROT_ERR:
  2690. opt_accept_unprot_err = 1;
  2691. break;
  2692. case OPT_ACCEPT_RAVERIFIED:
  2693. opt_accept_raverified = 1;
  2694. break;
  2695. }
  2696. }
  2697. /* No extra args. */
  2698. if (!opt_check_rest_arg(NULL))
  2699. goto opthelp;
  2700. return 1;
  2701. }
  2702. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  2703. static int cmp_server(OSSL_CMP_CTX *srv_cmp_ctx)
  2704. {
  2705. BIO *acbio;
  2706. BIO *cbio = NULL;
  2707. int keep_alive = 0;
  2708. int msgs = 0;
  2709. int retry = 1;
  2710. int ret = 1;
  2711. if ((acbio = http_server_init(prog, opt_port, opt_verbosity)) == NULL)
  2712. return 0;
  2713. while (opt_max_msgs <= 0 || msgs < opt_max_msgs) {
  2714. char *path = NULL;
  2715. OSSL_CMP_MSG *req = NULL;
  2716. OSSL_CMP_MSG *resp = NULL;
  2717. ret = http_server_get_asn1_req(ASN1_ITEM_rptr(OSSL_CMP_MSG),
  2718. (ASN1_VALUE **)&req, &path,
  2719. &cbio, acbio, &keep_alive,
  2720. prog, 0, 0);
  2721. if (ret == 0) { /* no request yet */
  2722. if (retry) {
  2723. OSSL_sleep(1000);
  2724. retry = 0;
  2725. continue;
  2726. }
  2727. ret = 0;
  2728. goto next;
  2729. }
  2730. if (ret++ == -1) /* fatal error */
  2731. break;
  2732. ret = 0;
  2733. msgs++;
  2734. if (req != NULL) {
  2735. if (strcmp(path, "") != 0 && strcmp(path, "pkix/") != 0) {
  2736. (void)http_server_send_status(prog, cbio, 404, "Not Found");
  2737. CMP_err1("expecting empty path or 'pkix/' but got '%s'",
  2738. path);
  2739. OPENSSL_free(path);
  2740. OSSL_CMP_MSG_free(req);
  2741. goto next;
  2742. }
  2743. OPENSSL_free(path);
  2744. resp = OSSL_CMP_CTX_server_perform(cmp_ctx, req);
  2745. OSSL_CMP_MSG_free(req);
  2746. if (resp == NULL) {
  2747. (void)http_server_send_status(prog, cbio,
  2748. 500, "Internal Server Error");
  2749. break; /* treated as fatal error */
  2750. }
  2751. ret = http_server_send_asn1_resp(prog, cbio, keep_alive,
  2752. "application/pkixcmp",
  2753. ASN1_ITEM_rptr(OSSL_CMP_MSG),
  2754. (const ASN1_VALUE *)resp);
  2755. OSSL_CMP_MSG_free(resp);
  2756. if (!ret)
  2757. break; /* treated as fatal error */
  2758. }
  2759. next:
  2760. if (!ret) { /* on transmission error, cancel CMP transaction */
  2761. (void)OSSL_CMP_CTX_set1_transactionID(srv_cmp_ctx, NULL);
  2762. (void)OSSL_CMP_CTX_set1_senderNonce(srv_cmp_ctx, NULL);
  2763. }
  2764. if (!ret || !keep_alive
  2765. || OSSL_CMP_CTX_get_status(srv_cmp_ctx) != OSSL_CMP_PKISTATUS_trans
  2766. /* transaction closed by OSSL_CMP_CTX_server_perform() */) {
  2767. BIO_free_all(cbio);
  2768. cbio = NULL;
  2769. }
  2770. }
  2771. BIO_free_all(cbio);
  2772. BIO_free_all(acbio);
  2773. return ret;
  2774. }
  2775. #endif
  2776. static void print_status(void)
  2777. {
  2778. /* print PKIStatusInfo */
  2779. int status = OSSL_CMP_CTX_get_status(cmp_ctx);
  2780. char *buf = app_malloc(OSSL_CMP_PKISI_BUFLEN, "PKIStatusInfo buf");
  2781. const char *string =
  2782. OSSL_CMP_CTX_snprint_PKIStatus(cmp_ctx, buf, OSSL_CMP_PKISI_BUFLEN);
  2783. const char *from = "", *server = "";
  2784. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  2785. if (opt_server != NULL) {
  2786. from = " from ";
  2787. server = opt_server;
  2788. }
  2789. #endif
  2790. CMP_print(bio_err,
  2791. status == OSSL_CMP_PKISTATUS_accepted
  2792. ? OSSL_CMP_LOG_INFO :
  2793. status == OSSL_CMP_PKISTATUS_rejection
  2794. || status == OSSL_CMP_PKISTATUS_waiting
  2795. ? OSSL_CMP_LOG_ERR : OSSL_CMP_LOG_WARNING,
  2796. status == OSSL_CMP_PKISTATUS_accepted ? "info" :
  2797. status == OSSL_CMP_PKISTATUS_rejection ? "server error" :
  2798. status == OSSL_CMP_PKISTATUS_waiting ? "internal error"
  2799. : "warning", "received%s%s %s", from, server,
  2800. string != NULL ? string : "<unknown PKIStatus>");
  2801. OPENSSL_free(buf);
  2802. }
  2803. static int do_genm(OSSL_CMP_CTX *ctx)
  2804. {
  2805. if (opt_infotype == NID_id_it_caCerts) {
  2806. STACK_OF(X509) *cacerts = NULL;
  2807. if (opt_cacertsout == NULL) {
  2808. CMP_err("Missing -cacertsout option for -infotype caCerts");
  2809. return 0;
  2810. }
  2811. if (!OSSL_CMP_get1_caCerts(ctx, &cacerts))
  2812. return 0;
  2813. /* could check authorization of sender/origin at this point */
  2814. if (cacerts == NULL) {
  2815. CMP_warn("no CA certificates provided by server");
  2816. } else if (save_free_certs(cacerts, opt_cacertsout, "CA") < 0) {
  2817. CMP_err1("Failed to store CA certificates from genp in %s",
  2818. opt_cacertsout);
  2819. return 0;
  2820. }
  2821. return 1;
  2822. } else if (opt_infotype == NID_id_it_rootCaCert) {
  2823. X509 *oldwithold = NULL;
  2824. X509 *newwithnew = NULL;
  2825. X509 *newwithold = NULL;
  2826. X509 *oldwithnew = NULL;
  2827. int res = 0;
  2828. if (opt_newwithnew == NULL) {
  2829. CMP_err("Missing -newwithnew option for -infotype rootCaCert");
  2830. return 0;
  2831. }
  2832. if (opt_oldwithold == NULL) {
  2833. CMP_warn("No -oldwithold given, will use all certs given with -trusted as trust anchors for verifying the newWithNew cert");
  2834. } else {
  2835. oldwithold = load_cert_pwd(opt_oldwithold, opt_otherpass,
  2836. "OldWithOld cert for genm with -infotype rootCaCert");
  2837. if (oldwithold == NULL)
  2838. goto end_upd;
  2839. }
  2840. if (!OSSL_CMP_get1_rootCaKeyUpdate(ctx, oldwithold, &newwithnew,
  2841. &newwithold, &oldwithnew))
  2842. goto end_upd;
  2843. /* At this point might check authorization of response sender/origin */
  2844. if (newwithnew == NULL)
  2845. CMP_info("no root CA certificate update available");
  2846. else if (oldwithold == NULL && oldwithnew != NULL)
  2847. CMP_warn("oldWithNew certificate received in genp for verifying oldWithOld, but oldWithOld was not provided");
  2848. if (save_cert_or_delete(newwithnew, opt_newwithnew,
  2849. "NewWithNew cert from genp")
  2850. && save_cert_or_delete(newwithold, opt_newwithold,
  2851. "NewWithOld cert from genp")
  2852. && save_cert_or_delete(oldwithnew, opt_oldwithnew,
  2853. "OldWithNew cert from genp"))
  2854. res = 1;
  2855. X509_free(newwithnew);
  2856. X509_free(newwithold);
  2857. X509_free(oldwithnew);
  2858. end_upd:
  2859. X509_free(oldwithold);
  2860. return res;
  2861. } else {
  2862. OSSL_CMP_ITAV *req;
  2863. STACK_OF(OSSL_CMP_ITAV) *itavs;
  2864. if (opt_infotype != NID_undef) {
  2865. CMP_warn1("No specific support for -infotype %s available",
  2866. opt_infotype_s);
  2867. req = OSSL_CMP_ITAV_create(OBJ_nid2obj(opt_infotype), NULL);
  2868. if (req == NULL || !OSSL_CMP_CTX_push0_genm_ITAV(ctx, req)) {
  2869. CMP_err1("Failed to create genm for -infotype %s",
  2870. opt_infotype_s);
  2871. return 0;
  2872. }
  2873. }
  2874. if ((itavs = OSSL_CMP_exec_GENM_ses(ctx)) != NULL) {
  2875. int res = print_itavs(itavs);
  2876. sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
  2877. return res;
  2878. }
  2879. if (OSSL_CMP_CTX_get_status(ctx) != OSSL_CMP_PKISTATUS_request)
  2880. CMP_err("Did not receive response on genm or genp is not valid");
  2881. return 0;
  2882. }
  2883. }
  2884. static int handle_opts_upfront(int argc, char **argv)
  2885. {
  2886. int i;
  2887. prog = opt_appname(argv[0]);
  2888. if (argc <= 1) {
  2889. opt_help(cmp_options);
  2890. return 0;
  2891. }
  2892. /* handle -config, -section, and -verbosity to take effect for other opts */
  2893. for (i = 1; i < argc - 1; i++) {
  2894. if (*argv[i] == '-') {
  2895. if (!strcmp(argv[i] + 1, cmp_options[OPT_CONFIG - OPT_HELP].name))
  2896. opt_config = argv[++i];
  2897. else if (!strcmp(argv[i] + 1,
  2898. cmp_options[OPT_SECTION - OPT_HELP].name))
  2899. opt_section = argv[++i];
  2900. else if (strcmp(argv[i] + 1,
  2901. cmp_options[OPT_VERBOSITY - OPT_HELP].name) == 0
  2902. && !set_verbosity(atoi(argv[++i])))
  2903. return 0;
  2904. }
  2905. }
  2906. if (opt_section[0] == '\0') /* empty string */
  2907. opt_section = DEFAULT_SECTION;
  2908. return 1;
  2909. }
  2910. int cmp_main(int argc, char **argv)
  2911. {
  2912. char *configfile = NULL;
  2913. int i;
  2914. X509 *newcert = NULL;
  2915. ENGINE *engine = NULL;
  2916. OSSL_CMP_CTX *srv_cmp_ctx = NULL;
  2917. int ret = 0; /* default: failure */
  2918. if (!handle_opts_upfront(argc, argv))
  2919. goto err;
  2920. vpm = X509_VERIFY_PARAM_new();
  2921. if (vpm == NULL) {
  2922. CMP_err("out of memory");
  2923. goto err;
  2924. }
  2925. /* read default values for options from config file */
  2926. configfile = opt_config != NULL ? opt_config : default_config_file;
  2927. if (configfile != NULL && configfile[0] != '\0' /* non-empty string */
  2928. && (configfile != default_config_file
  2929. || access(configfile, F_OK) != -1)) {
  2930. CMP_info2("using section(s) '%s' of OpenSSL configuration file '%s'",
  2931. opt_section, configfile);
  2932. conf = app_load_config(configfile);
  2933. if (conf == NULL) {
  2934. goto err;
  2935. } else {
  2936. if (strcmp(opt_section, CMP_SECTION) == 0) { /* default */
  2937. if (!NCONF_get_section(conf, opt_section))
  2938. CMP_info2("no [%s] section found in config file '%s';"
  2939. " will thus use just [default] and unnamed section if present",
  2940. opt_section, configfile);
  2941. } else {
  2942. const char *end = opt_section + strlen(opt_section);
  2943. while ((end = prev_item(opt_section, end)) != NULL) {
  2944. if (!NCONF_get_section(conf, opt_item)) {
  2945. CMP_err2("no [%s] section found in config file '%s'",
  2946. opt_item, configfile);
  2947. goto err;
  2948. }
  2949. }
  2950. }
  2951. ret = read_config();
  2952. if (!set_verbosity(opt_verbosity)) /* just for checking range */
  2953. ret = -1;
  2954. if (ret <= 0) {
  2955. if (ret == -1)
  2956. BIO_printf(bio_err, "Use -help for summary.\n");
  2957. goto err;
  2958. }
  2959. }
  2960. }
  2961. (void)BIO_flush(bio_err); /* prevent interference with opt_help() */
  2962. cmp_ctx = OSSL_CMP_CTX_new(app_get0_libctx(), app_get0_propq());
  2963. if (cmp_ctx == NULL)
  2964. goto err;
  2965. ret = get_opts(argc, argv);
  2966. if (ret <= 0)
  2967. goto err;
  2968. ret = 0;
  2969. if (!app_RAND_load())
  2970. goto err;
  2971. if (opt_batch)
  2972. set_base_ui_method(UI_null());
  2973. if (opt_engine != NULL) {
  2974. engine = setup_engine_methods(opt_engine,
  2975. 0 /* not: ENGINE_METHOD_ALL */, 0);
  2976. if (engine == NULL) {
  2977. CMP_err1("cannot load engine %s", opt_engine);
  2978. goto err;
  2979. }
  2980. }
  2981. OSSL_CMP_CTX_set_log_verbosity(cmp_ctx, opt_verbosity);
  2982. if (!OSSL_CMP_CTX_set_log_cb(cmp_ctx, print_to_bio_out)) {
  2983. CMP_err1("cannot set up error reporting and logging for %s", prog);
  2984. goto err;
  2985. }
  2986. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  2987. if (opt_tls_cert == NULL && opt_tls_key == NULL && opt_tls_keypass == NULL
  2988. && opt_tls_extra == NULL && opt_tls_trusted == NULL
  2989. && opt_tls_host == NULL) {
  2990. if (opt_tls_used)
  2991. CMP_warn("-tls_used given without any other TLS options");
  2992. } else if (!opt_tls_used) {
  2993. CMP_warn("ignoring TLS options(s) since -tls_used is not given");
  2994. }
  2995. if (opt_port != NULL) {
  2996. if (opt_tls_used) {
  2997. CMP_err("-tls_used option not supported with -port option");
  2998. goto err;
  2999. }
  3000. if (opt_server != NULL || opt_use_mock_srv) {
  3001. CMP_err("The -port option excludes -server and -use_mock_srv");
  3002. goto err;
  3003. }
  3004. if (opt_reqin != NULL || opt_reqout != NULL) {
  3005. CMP_err("The -port option does not support -reqin and -reqout");
  3006. goto err;
  3007. }
  3008. if (opt_rspin != NULL || opt_rspout != NULL) {
  3009. CMP_err("The -port option does not support -rspin and -rspout");
  3010. goto err;
  3011. }
  3012. }
  3013. if (opt_server != NULL && opt_use_mock_srv) {
  3014. CMP_err("cannot use both -server and -use_mock_srv options");
  3015. goto err;
  3016. }
  3017. #endif
  3018. if (opt_ignore_keyusage)
  3019. (void)OSSL_CMP_CTX_set_option(cmp_ctx, OSSL_CMP_OPT_IGNORE_KEYUSAGE, 1);
  3020. if (opt_no_cache_extracerts)
  3021. (void)OSSL_CMP_CTX_set_option(cmp_ctx, OSSL_CMP_OPT_NO_CACHE_EXTRACERTS,
  3022. 1);
  3023. if (opt_reqout_only == NULL && (opt_use_mock_srv
  3024. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  3025. || opt_port != NULL
  3026. #endif
  3027. )) {
  3028. OSSL_CMP_SRV_CTX *srv_ctx;
  3029. if ((srv_ctx = setup_srv_ctx(engine)) == NULL)
  3030. goto err;
  3031. srv_cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx);
  3032. OSSL_CMP_CTX_set_transfer_cb_arg(cmp_ctx, srv_ctx);
  3033. if (!OSSL_CMP_CTX_set_log_cb(srv_cmp_ctx, print_to_bio_err)) {
  3034. CMP_err1("cannot set up error reporting and logging for %s", prog);
  3035. goto err;
  3036. }
  3037. OSSL_CMP_CTX_set_log_verbosity(srv_cmp_ctx, opt_verbosity);
  3038. }
  3039. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  3040. if (opt_tls_used && (opt_use_mock_srv || opt_server == NULL)) {
  3041. CMP_warn("ignoring -tls_used option since -use_mock_srv is given or -server is not given");
  3042. opt_tls_used = 0;
  3043. }
  3044. if (opt_port != NULL) { /* act as very basic CMP HTTP server */
  3045. ret = cmp_server(srv_cmp_ctx);
  3046. goto err;
  3047. }
  3048. /* act as CMP client, possibly using internal mock server */
  3049. if (opt_reqout_only != NULL) {
  3050. const char *msg = "option is ignored since -reqout_only option is given";
  3051. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  3052. if (opt_server != NULL)
  3053. CMP_warn1("-server %s", msg);
  3054. #endif
  3055. if (opt_use_mock_srv)
  3056. CMP_warn1("-use_mock_srv %s", msg);
  3057. if (opt_reqout != NULL)
  3058. CMP_warn1("-reqout %s", msg);
  3059. if (opt_rspin != NULL)
  3060. CMP_warn1("-rspin %s", msg);
  3061. if (opt_rspout != NULL)
  3062. CMP_warn1("-rspout %s", msg);
  3063. opt_reqout = opt_reqout_only;
  3064. }
  3065. if (opt_rspin != NULL) {
  3066. if (opt_server != NULL)
  3067. CMP_warn("-server option is not used if enough filenames given for -rspin");
  3068. if (opt_use_mock_srv)
  3069. CMP_warn("-use_mock_srv option is not used if enough filenames given for -rspin");
  3070. }
  3071. #endif
  3072. if (!setup_client_ctx(cmp_ctx, engine)) {
  3073. CMP_err("cannot set up CMP context");
  3074. goto err;
  3075. }
  3076. for (i = 0; i < opt_repeat; i++) {
  3077. /* everything is ready, now connect and perform the command! */
  3078. switch (opt_cmd) {
  3079. case CMP_IR:
  3080. newcert = OSSL_CMP_exec_IR_ses(cmp_ctx);
  3081. if (newcert != NULL)
  3082. ret = 1;
  3083. break;
  3084. case CMP_KUR:
  3085. newcert = OSSL_CMP_exec_KUR_ses(cmp_ctx);
  3086. if (newcert != NULL)
  3087. ret = 1;
  3088. break;
  3089. case CMP_CR:
  3090. newcert = OSSL_CMP_exec_CR_ses(cmp_ctx);
  3091. if (newcert != NULL)
  3092. ret = 1;
  3093. break;
  3094. case CMP_P10CR:
  3095. newcert = OSSL_CMP_exec_P10CR_ses(cmp_ctx);
  3096. if (newcert != NULL)
  3097. ret = 1;
  3098. break;
  3099. case CMP_RR:
  3100. ret = OSSL_CMP_exec_RR_ses(cmp_ctx);
  3101. break;
  3102. case CMP_GENM:
  3103. ret = do_genm(cmp_ctx);
  3104. default:
  3105. break;
  3106. }
  3107. if (OSSL_CMP_CTX_get_status(cmp_ctx) < OSSL_CMP_PKISTATUS_accepted) {
  3108. /* we got no response, maybe even did not send request */
  3109. ret = 0;
  3110. if (reqout_only_done) {
  3111. ERR_clear_error();
  3112. ret = 1;
  3113. }
  3114. goto err;
  3115. }
  3116. print_status();
  3117. if (!save_cert_or_delete(OSSL_CMP_CTX_get0_validatedSrvCert(cmp_ctx),
  3118. opt_srvcertout, "validated server"))
  3119. ret = 0;
  3120. if (!ret)
  3121. goto err;
  3122. ret = 0;
  3123. if (save_free_certs(OSSL_CMP_CTX_get1_extraCertsIn(cmp_ctx),
  3124. opt_extracertsout, "extra") < 0)
  3125. goto err;
  3126. if (newcert != NULL && (opt_cmd == CMP_IR || opt_cmd == CMP_CR
  3127. || opt_cmd == CMP_KUR || opt_cmd == CMP_P10CR))
  3128. if (!save_cert_or_delete(newcert, opt_certout, "newly enrolled")
  3129. || save_free_certs(OSSL_CMP_CTX_get1_newChain(cmp_ctx),
  3130. opt_chainout, "chain") < 0
  3131. || save_free_certs(OSSL_CMP_CTX_get1_caPubs(cmp_ctx),
  3132. opt_cacertsout, "CA") < 0)
  3133. goto err;
  3134. if (!OSSL_CMP_CTX_reinit(cmp_ctx))
  3135. goto err;
  3136. }
  3137. ret = 1;
  3138. err:
  3139. /* in case we ended up here on error without proper cleaning */
  3140. cleanse(opt_keypass);
  3141. cleanse(opt_newkeypass);
  3142. cleanse(opt_otherpass);
  3143. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  3144. cleanse(opt_tls_keypass);
  3145. #endif
  3146. cleanse(opt_secret);
  3147. cleanse(opt_srv_keypass);
  3148. cleanse(opt_srv_secret);
  3149. if (ret != 1)
  3150. OSSL_CMP_CTX_print_errors(cmp_ctx);
  3151. if (cmp_ctx != NULL) {
  3152. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  3153. APP_HTTP_TLS_INFO *info = OSSL_CMP_CTX_get_http_cb_arg(cmp_ctx);
  3154. (void)OSSL_CMP_CTX_set_http_cb_arg(cmp_ctx, NULL);
  3155. #endif
  3156. ossl_cmp_mock_srv_free(OSSL_CMP_CTX_get_transfer_cb_arg(cmp_ctx));
  3157. X509_STORE_free(OSSL_CMP_CTX_get_certConf_cb_arg(cmp_ctx));
  3158. /* cannot free info already here, as it may be used indirectly by: */
  3159. OSSL_CMP_CTX_free(cmp_ctx);
  3160. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
  3161. if (info != NULL) {
  3162. OPENSSL_free((char *)info->server);
  3163. OPENSSL_free((char *)info->port);
  3164. APP_HTTP_TLS_INFO_free(info);
  3165. }
  3166. #endif
  3167. }
  3168. X509_VERIFY_PARAM_free(vpm);
  3169. release_engine(engine);
  3170. NCONF_free(conf); /* must not do as long as opt_... variables are used */
  3171. OSSL_CMP_log_close();
  3172. return ret == 0 ? EXIT_FAILURE : EXIT_SUCCESS; /* ret == -1 for -help */
  3173. }