cmp.c 104 KB

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