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