cmp.c 127 KB

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