cmp.c 137 KB

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