cmp.c 106 KB

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