cmp.c 100 KB

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