cmp.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103
  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. static X509_REQ *load_csr_autofmt(const char *infile, const char *desc)
  621. {
  622. X509_REQ *csr;
  623. BIO *bio_bak = bio_err;
  624. bio_err = NULL; /* do not show errors on more than one try */
  625. csr = load_csr(infile, FORMAT_PEM, desc);
  626. bio_err = bio_bak;
  627. if (csr == NULL) {
  628. ERR_clear_error();
  629. csr = load_csr(infile, FORMAT_ASN1, desc);
  630. }
  631. if (csr == NULL) {
  632. ERR_print_errors(bio_err);
  633. BIO_printf(bio_err, "error: unable to load %s from file '%s'\n", desc,
  634. infile);
  635. } else {
  636. EVP_PKEY *pkey = X509_REQ_get0_pubkey(csr);
  637. int ret = do_X509_REQ_verify(csr, pkey, NULL /* vfyopts */);
  638. if (pkey == NULL || ret < 0)
  639. CMP_warn("error while verifying CSR self-signature");
  640. else if (ret == 0)
  641. CMP_warn("CSR self-signature does not match the contents");
  642. }
  643. return csr;
  644. }
  645. /* set expected hostname/IP addr and clears the email addr in the given ts */
  646. static int truststore_set_host_etc(X509_STORE *ts, const char *host)
  647. {
  648. X509_VERIFY_PARAM *ts_vpm = X509_STORE_get0_param(ts);
  649. /* first clear any hostnames, IP, and email addresses */
  650. if (!X509_VERIFY_PARAM_set1_host(ts_vpm, NULL, 0)
  651. || !X509_VERIFY_PARAM_set1_ip(ts_vpm, NULL, 0)
  652. || !X509_VERIFY_PARAM_set1_email(ts_vpm, NULL, 0))
  653. return 0;
  654. X509_VERIFY_PARAM_set_hostflags(ts_vpm,
  655. X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT |
  656. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  657. return (host != NULL && X509_VERIFY_PARAM_set1_ip_asc(ts_vpm, host))
  658. || X509_VERIFY_PARAM_set1_host(ts_vpm, host, 0);
  659. }
  660. /* write OSSL_CMP_MSG DER-encoded to the specified file name item */
  661. static int write_PKIMESSAGE(const OSSL_CMP_MSG *msg, char **filenames)
  662. {
  663. char *file;
  664. if (msg == NULL || filenames == NULL) {
  665. CMP_err("NULL arg to write_PKIMESSAGE");
  666. return 0;
  667. }
  668. if (*filenames == NULL) {
  669. CMP_err("not enough file names provided for writing PKIMessage");
  670. return 0;
  671. }
  672. file = *filenames;
  673. *filenames = next_item(file);
  674. if (OSSL_CMP_MSG_write(file, msg) < 0) {
  675. CMP_err1("cannot write PKIMessage to file '%s'", file);
  676. return 0;
  677. }
  678. return 1;
  679. }
  680. /* read DER-encoded OSSL_CMP_MSG from the specified file name item */
  681. static OSSL_CMP_MSG *read_PKIMESSAGE(char **filenames)
  682. {
  683. char *file;
  684. OSSL_CMP_MSG *ret;
  685. if (filenames == NULL) {
  686. CMP_err("NULL arg to read_PKIMESSAGE");
  687. return NULL;
  688. }
  689. if (*filenames == NULL) {
  690. CMP_err("not enough file names provided for reading PKIMessage");
  691. return NULL;
  692. }
  693. file = *filenames;
  694. *filenames = next_item(file);
  695. ret = OSSL_CMP_MSG_read(file, app_get0_libctx(), app_get0_propq());
  696. if (ret == NULL)
  697. CMP_err1("cannot read PKIMessage from file '%s'", file);
  698. return ret;
  699. }
  700. /*-
  701. * Sends the PKIMessage req and on success place the response in *res
  702. * basically like OSSL_CMP_MSG_http_perform(), but in addition allows
  703. * to dump the sequence of requests and responses to files and/or
  704. * to take the sequence of requests and responses from files.
  705. */
  706. static OSSL_CMP_MSG *read_write_req_resp(OSSL_CMP_CTX *ctx,
  707. const OSSL_CMP_MSG *req)
  708. {
  709. OSSL_CMP_MSG *req_new = NULL;
  710. OSSL_CMP_MSG *res = NULL;
  711. OSSL_CMP_PKIHEADER *hdr;
  712. const char *prev_opt_rspin = opt_rspin;
  713. if (opt_reqout != NULL && !write_PKIMESSAGE(req, &opt_reqout))
  714. goto err;
  715. if (opt_reqin != NULL && opt_rspin == NULL) {
  716. if ((req_new = read_PKIMESSAGE(&opt_reqin)) == NULL)
  717. goto err;
  718. /*-
  719. * The transaction ID in req_new read from opt_reqin may not be fresh.
  720. * In this case the server may complain "Transaction id already in use."
  721. * The following workaround unfortunately requires re-protection.
  722. */
  723. if (opt_reqin_new_tid
  724. && !OSSL_CMP_MSG_update_transactionID(ctx, req_new))
  725. goto err;
  726. }
  727. if (opt_rspin != NULL) {
  728. res = read_PKIMESSAGE(&opt_rspin);
  729. } else {
  730. const OSSL_CMP_MSG *actual_req = opt_reqin != NULL ? req_new : req;
  731. res = opt_use_mock_srv
  732. ? OSSL_CMP_CTX_server_perform(ctx, actual_req)
  733. : OSSL_CMP_MSG_http_perform(ctx, actual_req);
  734. }
  735. if (res == NULL)
  736. goto err;
  737. if (opt_reqin != NULL || prev_opt_rspin != NULL) {
  738. /* need to satisfy nonce and transactionID checks */
  739. ASN1_OCTET_STRING *nonce;
  740. ASN1_OCTET_STRING *tid;
  741. hdr = OSSL_CMP_MSG_get0_header(res);
  742. nonce = OSSL_CMP_HDR_get0_recipNonce(hdr);
  743. tid = OSSL_CMP_HDR_get0_transactionID(hdr);
  744. if (!OSSL_CMP_CTX_set1_senderNonce(ctx, nonce)
  745. || !OSSL_CMP_CTX_set1_transactionID(ctx, tid)) {
  746. OSSL_CMP_MSG_free(res);
  747. res = NULL;
  748. goto err;
  749. }
  750. }
  751. if (opt_rspout != NULL && !write_PKIMESSAGE(res, &opt_rspout)) {
  752. OSSL_CMP_MSG_free(res);
  753. res = NULL;
  754. }
  755. err:
  756. OSSL_CMP_MSG_free(req_new);
  757. return res;
  758. }
  759. static int set_name(const char *str,
  760. int (*set_fn) (OSSL_CMP_CTX *ctx, const X509_NAME *name),
  761. OSSL_CMP_CTX *ctx, const char *desc)
  762. {
  763. if (str != NULL) {
  764. X509_NAME *n = parse_name(str, MBSTRING_ASC, 1, desc);
  765. if (n == NULL)
  766. return 0;
  767. if (!(*set_fn) (ctx, n)) {
  768. X509_NAME_free(n);
  769. CMP_err("out of memory");
  770. return 0;
  771. }
  772. X509_NAME_free(n);
  773. }
  774. return 1;
  775. }
  776. static int set_gennames(OSSL_CMP_CTX *ctx, char *names, const char *desc)
  777. {
  778. char *next;
  779. for (; names != NULL; names = next) {
  780. GENERAL_NAME *n;
  781. next = next_item(names);
  782. if (strcmp(names, "critical") == 0) {
  783. (void)OSSL_CMP_CTX_set_option(ctx,
  784. OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL,
  785. 1);
  786. continue;
  787. }
  788. /* try IP address first, then email/URI/domain name */
  789. (void)ERR_set_mark();
  790. n = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_IPADD, names, 0);
  791. if (n == NULL)
  792. n = a2i_GENERAL_NAME(NULL, NULL, NULL,
  793. strchr(names, '@') != NULL ? GEN_EMAIL :
  794. strchr(names, ':') != NULL ? GEN_URI : GEN_DNS,
  795. names, 0);
  796. (void)ERR_pop_to_mark();
  797. if (n == NULL) {
  798. CMP_err2("bad syntax of %s '%s'", desc, names);
  799. return 0;
  800. }
  801. if (!OSSL_CMP_CTX_push1_subjectAltName(ctx, n)) {
  802. GENERAL_NAME_free(n);
  803. CMP_err("out of memory");
  804. return 0;
  805. }
  806. GENERAL_NAME_free(n);
  807. }
  808. return 1;
  809. }
  810. static X509_STORE *load_trusted(char *input, int for_new_cert, const char *desc)
  811. {
  812. X509_STORE *ts = load_certstore(input, opt_otherpass, desc, vpm);
  813. if (ts == NULL)
  814. return NULL;
  815. X509_STORE_set_verify_cb(ts, X509_STORE_CTX_print_verify_cb);
  816. /* copy vpm to store */
  817. if (X509_STORE_set1_param(ts, vpm /* may be NULL */)
  818. && (for_new_cert || truststore_set_host_etc(ts, NULL)))
  819. return ts;
  820. BIO_printf(bio_err, "error setting verification parameters for %s\n", desc);
  821. OSSL_CMP_CTX_print_errors(cmp_ctx);
  822. X509_STORE_free(ts);
  823. return NULL;
  824. }
  825. typedef int (*add_X509_stack_fn_t)(void *ctx, const STACK_OF(X509) *certs);
  826. static int setup_certs(char *files, const char *desc, void *ctx,
  827. add_X509_stack_fn_t set1_fn)
  828. {
  829. STACK_OF(X509) *certs;
  830. int ok;
  831. if (files == NULL)
  832. return 1;
  833. if ((certs = load_certs_multifile(files, opt_otherpass, desc, vpm)) == NULL)
  834. return 0;
  835. ok = (*set1_fn)(ctx, certs);
  836. OSSL_STACK_OF_X509_free(certs);
  837. return ok;
  838. }
  839. /*
  840. * parse and transform some options, checking their syntax.
  841. * Returns 1 on success, 0 on error
  842. */
  843. static int transform_opts(void)
  844. {
  845. if (opt_cmd_s != NULL) {
  846. if (!strcmp(opt_cmd_s, "ir")) {
  847. opt_cmd = CMP_IR;
  848. } else if (!strcmp(opt_cmd_s, "kur")) {
  849. opt_cmd = CMP_KUR;
  850. } else if (!strcmp(opt_cmd_s, "cr")) {
  851. opt_cmd = CMP_CR;
  852. } else if (!strcmp(opt_cmd_s, "p10cr")) {
  853. opt_cmd = CMP_P10CR;
  854. } else if (!strcmp(opt_cmd_s, "rr")) {
  855. opt_cmd = CMP_RR;
  856. } else if (!strcmp(opt_cmd_s, "genm")) {
  857. opt_cmd = CMP_GENM;
  858. } else {
  859. CMP_err1("unknown cmp command '%s'", opt_cmd_s);
  860. return 0;
  861. }
  862. } else {
  863. CMP_err("no cmp command to execute");
  864. return 0;
  865. }
  866. #ifndef OPENSSL_NO_ENGINE
  867. # define FORMAT_OPTIONS (OPT_FMT_PEMDER | OPT_FMT_PKCS12 | OPT_FMT_ENGINE)
  868. #else
  869. # define FORMAT_OPTIONS (OPT_FMT_PEMDER | OPT_FMT_PKCS12)
  870. #endif
  871. if (opt_keyform_s != NULL
  872. && !opt_format(opt_keyform_s, FORMAT_OPTIONS, &opt_keyform)) {
  873. CMP_err("unknown option given for key loading format");
  874. return 0;
  875. }
  876. #undef FORMAT_OPTIONS
  877. if (opt_certform_s != NULL
  878. && !opt_format(opt_certform_s, OPT_FMT_PEMDER, &opt_certform)) {
  879. CMP_err("unknown option given for certificate storing format");
  880. return 0;
  881. }
  882. return 1;
  883. }
  884. static OSSL_CMP_SRV_CTX *setup_srv_ctx(ENGINE *engine)
  885. {
  886. OSSL_CMP_CTX *ctx; /* extra CMP (client) ctx partly used by server */
  887. OSSL_CMP_SRV_CTX *srv_ctx = ossl_cmp_mock_srv_new(app_get0_libctx(),
  888. app_get0_propq());
  889. if (srv_ctx == NULL)
  890. return NULL;
  891. ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx);
  892. if (opt_srv_ref == NULL) {
  893. if (opt_srv_cert == NULL) {
  894. /* opt_srv_cert should determine the sender */
  895. CMP_err("must give -srv_ref for mock server if no -srv_cert given");
  896. goto err;
  897. }
  898. } else {
  899. if (!OSSL_CMP_CTX_set1_referenceValue(ctx, (unsigned char *)opt_srv_ref,
  900. strlen(opt_srv_ref)))
  901. goto err;
  902. }
  903. if (opt_srv_secret != NULL) {
  904. int res;
  905. char *pass_str = get_passwd(opt_srv_secret, "PBMAC secret of mock server");
  906. if (pass_str != NULL) {
  907. cleanse(opt_srv_secret);
  908. res = OSSL_CMP_CTX_set1_secretValue(ctx, (unsigned char *)pass_str,
  909. strlen(pass_str));
  910. clear_free(pass_str);
  911. if (res == 0)
  912. goto err;
  913. }
  914. } else if (opt_srv_cert == NULL) {
  915. CMP_err("mock server credentials must be given if -use_mock_srv or -port is used");
  916. goto err;
  917. } else {
  918. CMP_warn("mock server will not be able to handle PBM-protected requests since -srv_secret is not given");
  919. }
  920. if (opt_srv_secret == NULL
  921. && ((opt_srv_cert == NULL) != (opt_srv_key == NULL))) {
  922. CMP_err("must give both -srv_cert and -srv_key options or neither");
  923. goto err;
  924. }
  925. if (opt_srv_cert != NULL) {
  926. X509 *srv_cert = load_cert_pwd(opt_srv_cert, opt_srv_keypass,
  927. "certificate of the mock server");
  928. if (srv_cert == NULL || !OSSL_CMP_CTX_set1_cert(ctx, srv_cert)) {
  929. X509_free(srv_cert);
  930. goto err;
  931. }
  932. X509_free(srv_cert);
  933. }
  934. if (opt_srv_key != NULL) {
  935. EVP_PKEY *pkey = load_key_pwd(opt_srv_key, opt_keyform,
  936. opt_srv_keypass,
  937. engine, "private key for mock server cert");
  938. if (pkey == NULL || !OSSL_CMP_CTX_set1_pkey(ctx, pkey)) {
  939. EVP_PKEY_free(pkey);
  940. goto err;
  941. }
  942. EVP_PKEY_free(pkey);
  943. }
  944. cleanse(opt_srv_keypass);
  945. if (opt_srv_trusted != NULL) {
  946. X509_STORE *ts =
  947. load_trusted(opt_srv_trusted, 0, "certs trusted by mock server");
  948. if (ts == NULL || !OSSL_CMP_CTX_set0_trusted(ctx, ts)) {
  949. X509_STORE_free(ts);
  950. goto err;
  951. }
  952. } else {
  953. CMP_warn("mock server will not be able to handle signature-protected requests since -srv_trusted is not given");
  954. }
  955. if (!setup_certs(opt_srv_untrusted,
  956. "untrusted certificates for mock server", ctx,
  957. (add_X509_stack_fn_t)OSSL_CMP_CTX_set1_untrusted))
  958. goto err;
  959. if (opt_ref_cert != NULL) {
  960. X509 *cert = load_cert_pwd(opt_ref_cert, opt_keypass,
  961. "reference cert to be expected by the mock server");
  962. if (cert == NULL)
  963. goto err;
  964. if (!ossl_cmp_mock_srv_set1_refCert(srv_ctx, cert)) {
  965. X509_free(cert);
  966. goto err;
  967. }
  968. X509_free(cert);
  969. }
  970. if (opt_rsp_cert == NULL) {
  971. CMP_warn("no -rsp_cert given for mock server");
  972. } else {
  973. X509 *cert = load_cert_pwd(opt_rsp_cert, opt_keypass,
  974. "cert to be returned by the mock server");
  975. if (cert == NULL)
  976. goto err;
  977. if (!ossl_cmp_mock_srv_set1_certOut(srv_ctx, cert)) {
  978. X509_free(cert);
  979. goto err;
  980. }
  981. X509_free(cert);
  982. }
  983. if (!setup_certs(opt_rsp_extracerts,
  984. "CMP extra certificates for mock server", srv_ctx,
  985. (add_X509_stack_fn_t)ossl_cmp_mock_srv_set1_chainOut))
  986. goto err;
  987. if (!setup_certs(opt_rsp_capubs, "caPubs for mock server", srv_ctx,
  988. (add_X509_stack_fn_t)ossl_cmp_mock_srv_set1_caPubsOut))
  989. goto err;
  990. (void)ossl_cmp_mock_srv_set_pollCount(srv_ctx, opt_poll_count);
  991. (void)ossl_cmp_mock_srv_set_checkAfterTime(srv_ctx, opt_check_after);
  992. if (opt_grant_implicitconf)
  993. (void)OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(srv_ctx, 1);
  994. if (opt_failure != INT_MIN) { /* option has been set explicitly */
  995. if (opt_failure < 0 || OSSL_CMP_PKIFAILUREINFO_MAX < opt_failure) {
  996. CMP_err1("-failure out of range, should be >= 0 and <= %d",
  997. OSSL_CMP_PKIFAILUREINFO_MAX);
  998. goto err;
  999. }
  1000. if (opt_failurebits != 0)
  1001. CMP_warn("-failurebits overrides -failure");
  1002. else
  1003. opt_failurebits = 1 << opt_failure;
  1004. }
  1005. if ((unsigned)opt_failurebits > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) {
  1006. CMP_err("-failurebits out of range");
  1007. goto err;
  1008. }
  1009. if (!ossl_cmp_mock_srv_set_statusInfo(srv_ctx, opt_pkistatus,
  1010. opt_failurebits, opt_statusstring))
  1011. goto err;
  1012. if (opt_send_error)
  1013. (void)ossl_cmp_mock_srv_set_send_error(srv_ctx, 1);
  1014. if (opt_send_unprotected)
  1015. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1);
  1016. if (opt_send_unprot_err)
  1017. (void)OSSL_CMP_SRV_CTX_set_send_unprotected_errors(srv_ctx, 1);
  1018. if (opt_accept_unprotected)
  1019. (void)OSSL_CMP_SRV_CTX_set_accept_unprotected(srv_ctx, 1);
  1020. if (opt_accept_unprot_err)
  1021. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1);
  1022. if (opt_accept_raverified)
  1023. (void)OSSL_CMP_SRV_CTX_set_accept_raverified(srv_ctx, 1);
  1024. return srv_ctx;
  1025. err:
  1026. ossl_cmp_mock_srv_free(srv_ctx);
  1027. return NULL;
  1028. }
  1029. /*
  1030. * set up verification aspects of OSSL_CMP_CTX w.r.t. opts from config file/CLI.
  1031. * Returns pointer on success, NULL on error
  1032. */
  1033. static int setup_verification_ctx(OSSL_CMP_CTX *ctx)
  1034. {
  1035. if (!setup_certs(opt_untrusted, "untrusted certificates", ctx,
  1036. (add_X509_stack_fn_t)OSSL_CMP_CTX_set1_untrusted))
  1037. return 0;
  1038. if (opt_srvcert != NULL || opt_trusted != NULL) {
  1039. X509 *srvcert;
  1040. X509_STORE *ts;
  1041. int ok;
  1042. if (opt_srvcert != NULL) {
  1043. if (opt_trusted != NULL) {
  1044. CMP_warn("-trusted option is ignored since -srvcert option is present");
  1045. opt_trusted = NULL;
  1046. }
  1047. if (opt_recipient != NULL) {
  1048. CMP_warn("-recipient option is ignored since -srvcert option is present");
  1049. opt_recipient = NULL;
  1050. }
  1051. srvcert = load_cert_pwd(opt_srvcert, opt_otherpass,
  1052. "directly trusted CMP server certificate");
  1053. ok = srvcert != NULL && OSSL_CMP_CTX_set1_srvCert(ctx, srvcert);
  1054. X509_free(srvcert);
  1055. if (!ok)
  1056. return 0;
  1057. }
  1058. if (opt_trusted != NULL) {
  1059. /*
  1060. * the 0 arg below clears any expected host/ip/email address;
  1061. * opt_expect_sender is used instead
  1062. */
  1063. ts = load_trusted(opt_trusted, 0, "certs trusted by client");
  1064. if (ts == NULL || !OSSL_CMP_CTX_set0_trusted(ctx, ts)) {
  1065. X509_STORE_free(ts);
  1066. return 0;
  1067. }
  1068. }
  1069. }
  1070. if (opt_ignore_keyusage)
  1071. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_IGNORE_KEYUSAGE, 1);
  1072. if (opt_unprotected_errors)
  1073. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1);
  1074. if (opt_out_trusted != NULL) { /* for use in OSSL_CMP_certConf_cb() */
  1075. X509_VERIFY_PARAM *out_vpm = NULL;
  1076. X509_STORE *out_trusted =
  1077. load_trusted(opt_out_trusted, 1,
  1078. "trusted certs for verifying newly enrolled cert");
  1079. if (out_trusted == NULL)
  1080. return 0;
  1081. /* ignore any -attime here, new certs are current anyway */
  1082. out_vpm = X509_STORE_get0_param(out_trusted);
  1083. X509_VERIFY_PARAM_clear_flags(out_vpm, X509_V_FLAG_USE_CHECK_TIME);
  1084. (void)OSSL_CMP_CTX_set_certConf_cb_arg(ctx, out_trusted);
  1085. }
  1086. if (opt_disable_confirm)
  1087. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_DISABLE_CONFIRM, 1);
  1088. if (opt_implicit_confirm)
  1089. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM, 1);
  1090. return 1;
  1091. }
  1092. #ifndef OPENSSL_NO_SOCK
  1093. /*
  1094. * set up ssl_ctx for the OSSL_CMP_CTX based on options from config file/CLI.
  1095. * Returns pointer on success, NULL on error
  1096. */
  1097. static SSL_CTX *setup_ssl_ctx(OSSL_CMP_CTX *ctx, const char *host,
  1098. ENGINE *engine)
  1099. {
  1100. STACK_OF(X509) *untrusted = OSSL_CMP_CTX_get0_untrusted(ctx);
  1101. EVP_PKEY *pkey = NULL;
  1102. X509_STORE *trust_store = NULL;
  1103. SSL_CTX *ssl_ctx;
  1104. int i;
  1105. ssl_ctx = SSL_CTX_new(TLS_client_method());
  1106. if (ssl_ctx == NULL)
  1107. return NULL;
  1108. if (opt_tls_trusted != NULL) {
  1109. trust_store = load_trusted(opt_tls_trusted, 0, "trusted TLS certs");
  1110. if (trust_store == NULL)
  1111. goto err;
  1112. SSL_CTX_set_cert_store(ssl_ctx, trust_store);
  1113. SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
  1114. } else {
  1115. CMP_warn("-tls_used given without -tls_trusted; will not authenticate the TLS server");
  1116. }
  1117. if (opt_tls_cert != NULL && opt_tls_key != NULL) {
  1118. X509 *cert;
  1119. STACK_OF(X509) *certs = NULL;
  1120. int ok;
  1121. if (!load_cert_certs(opt_tls_cert, &cert, &certs, 0, opt_tls_keypass,
  1122. "TLS client certificate (optionally with chain)",
  1123. vpm))
  1124. /* need opt_tls_keypass if opt_tls_cert is encrypted PKCS#12 file */
  1125. goto err;
  1126. ok = SSL_CTX_use_certificate(ssl_ctx, cert) > 0;
  1127. X509_free(cert);
  1128. /*
  1129. * Any further certs and any untrusted certs are used for constructing
  1130. * the chain to be provided with the TLS client cert to the TLS server.
  1131. */
  1132. if (!ok || !SSL_CTX_set0_chain(ssl_ctx, certs)) {
  1133. CMP_err1("unable to use client TLS certificate file '%s'",
  1134. opt_tls_cert);
  1135. OSSL_STACK_OF_X509_free(certs);
  1136. goto err;
  1137. }
  1138. for (i = 0; i < sk_X509_num(untrusted); i++) {
  1139. cert = sk_X509_value(untrusted, i);
  1140. if (!SSL_CTX_add1_chain_cert(ssl_ctx, cert)) {
  1141. CMP_err("could not add untrusted cert to TLS client cert chain");
  1142. goto err;
  1143. }
  1144. }
  1145. {
  1146. X509_VERIFY_PARAM *tls_vpm = NULL;
  1147. unsigned long bak_flags = 0; /* compiler warns without init */
  1148. if (trust_store != NULL) {
  1149. tls_vpm = X509_STORE_get0_param(trust_store);
  1150. bak_flags = X509_VERIFY_PARAM_get_flags(tls_vpm);
  1151. /* disable any cert status/revocation checking etc. */
  1152. X509_VERIFY_PARAM_clear_flags(tls_vpm,
  1153. ~(X509_V_FLAG_USE_CHECK_TIME
  1154. | X509_V_FLAG_NO_CHECK_TIME));
  1155. }
  1156. CMP_debug("trying to build cert chain for own TLS cert");
  1157. if (SSL_CTX_build_cert_chain(ssl_ctx,
  1158. SSL_BUILD_CHAIN_FLAG_UNTRUSTED |
  1159. SSL_BUILD_CHAIN_FLAG_NO_ROOT)) {
  1160. CMP_debug("success building cert chain for own TLS cert");
  1161. } else {
  1162. OSSL_CMP_CTX_print_errors(ctx);
  1163. CMP_warn("could not build cert chain for own TLS cert");
  1164. }
  1165. if (trust_store != NULL)
  1166. X509_VERIFY_PARAM_set_flags(tls_vpm, bak_flags);
  1167. }
  1168. /* If present we append to the list also the certs from opt_tls_extra */
  1169. if (opt_tls_extra != NULL) {
  1170. STACK_OF(X509) *tls_extra = load_certs_multifile(opt_tls_extra,
  1171. opt_otherpass,
  1172. "extra certificates for TLS",
  1173. vpm);
  1174. int res = 1;
  1175. if (tls_extra == NULL)
  1176. goto err;
  1177. for (i = 0; i < sk_X509_num(tls_extra); i++) {
  1178. cert = sk_X509_value(tls_extra, i);
  1179. if (res != 0)
  1180. res = SSL_CTX_add_extra_chain_cert(ssl_ctx, cert);
  1181. if (res == 0)
  1182. X509_free(cert);
  1183. }
  1184. sk_X509_free(tls_extra);
  1185. if (res == 0) {
  1186. BIO_printf(bio_err, "error: unable to add TLS extra certs\n");
  1187. goto err;
  1188. }
  1189. }
  1190. pkey = load_key_pwd(opt_tls_key, opt_keyform, opt_tls_keypass,
  1191. engine, "TLS client private key");
  1192. cleanse(opt_tls_keypass);
  1193. if (pkey == NULL)
  1194. goto err;
  1195. /*
  1196. * verify the key matches the cert,
  1197. * not using SSL_CTX_check_private_key(ssl_ctx)
  1198. * because it gives poor and sometimes misleading diagnostics
  1199. */
  1200. if (!X509_check_private_key(SSL_CTX_get0_certificate(ssl_ctx),
  1201. pkey)) {
  1202. CMP_err2("TLS private key '%s' does not match the TLS certificate '%s'\n",
  1203. opt_tls_key, opt_tls_cert);
  1204. EVP_PKEY_free(pkey);
  1205. pkey = NULL; /* otherwise, for some reason double free! */
  1206. goto err;
  1207. }
  1208. if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) <= 0) {
  1209. CMP_err1("unable to use TLS client private key '%s'", opt_tls_key);
  1210. EVP_PKEY_free(pkey);
  1211. pkey = NULL; /* otherwise, for some reason double free! */
  1212. goto err;
  1213. }
  1214. EVP_PKEY_free(pkey); /* we do not need the handle any more */
  1215. } else {
  1216. CMP_warn("-tls_used given without -tls_key; cannot authenticate to the TLS server");
  1217. }
  1218. if (trust_store != NULL) {
  1219. /*
  1220. * Enable and parameterize server hostname/IP address check.
  1221. * If we did this before checking our own TLS cert
  1222. * the expected hostname would mislead the check.
  1223. */
  1224. if (!truststore_set_host_etc(trust_store,
  1225. opt_tls_host != NULL ? opt_tls_host : host))
  1226. goto err;
  1227. }
  1228. return ssl_ctx;
  1229. err:
  1230. SSL_CTX_free(ssl_ctx);
  1231. return NULL;
  1232. }
  1233. #endif /* OPENSSL_NO_SOCK */
  1234. /*
  1235. * set up protection aspects of OSSL_CMP_CTX based on options from config
  1236. * file/CLI while parsing options and checking their consistency.
  1237. * Returns 1 on success, 0 on error
  1238. */
  1239. static int setup_protection_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
  1240. {
  1241. if (!opt_unprotected_requests && opt_secret == NULL && opt_key == NULL) {
  1242. CMP_err("must give -key or -secret unless -unprotected_requests is used");
  1243. return 0;
  1244. }
  1245. if (opt_ref == NULL && opt_cert == NULL && opt_subject == NULL) {
  1246. /* cert or subject should determine the sender */
  1247. CMP_err("must give -ref if no -cert and no -subject given");
  1248. return 0;
  1249. }
  1250. if (!opt_secret && ((opt_cert == NULL) != (opt_key == NULL))) {
  1251. CMP_err("must give both -cert and -key options or neither");
  1252. return 0;
  1253. }
  1254. if (opt_secret != NULL) {
  1255. char *pass_string = get_passwd(opt_secret, "PBMAC");
  1256. int res;
  1257. if (pass_string != NULL) {
  1258. cleanse(opt_secret);
  1259. res = OSSL_CMP_CTX_set1_secretValue(ctx,
  1260. (unsigned char *)pass_string,
  1261. strlen(pass_string));
  1262. clear_free(pass_string);
  1263. if (res == 0)
  1264. return 0;
  1265. }
  1266. if (opt_cert != NULL || opt_key != NULL)
  1267. CMP_warn("-cert and -key not used for protection since -secret is given");
  1268. }
  1269. if (opt_ref != NULL
  1270. && !OSSL_CMP_CTX_set1_referenceValue(ctx, (unsigned char *)opt_ref,
  1271. strlen(opt_ref)))
  1272. return 0;
  1273. if (opt_key != NULL) {
  1274. EVP_PKEY *pkey = load_key_pwd(opt_key, opt_keyform, opt_keypass, engine,
  1275. "private key for CMP client certificate");
  1276. if (pkey == NULL || !OSSL_CMP_CTX_set1_pkey(ctx, pkey)) {
  1277. EVP_PKEY_free(pkey);
  1278. return 0;
  1279. }
  1280. EVP_PKEY_free(pkey);
  1281. }
  1282. if (opt_secret == NULL && opt_srvcert == NULL && opt_trusted == NULL)
  1283. CMP_warn("will not authenticate server due to missing -secret, -trusted, or -srvcert");
  1284. if (opt_cert != NULL) {
  1285. X509 *cert;
  1286. STACK_OF(X509) *certs = NULL;
  1287. X509_STORE *own_trusted = NULL;
  1288. int ok;
  1289. if (!load_cert_certs(opt_cert, &cert, &certs, 0, opt_keypass,
  1290. "CMP client certificate (optionally with chain)",
  1291. vpm))
  1292. /* opt_keypass is needed if opt_cert is an encrypted PKCS#12 file */
  1293. return 0;
  1294. ok = OSSL_CMP_CTX_set1_cert(ctx, cert);
  1295. X509_free(cert);
  1296. if (!ok) {
  1297. CMP_err("out of memory");
  1298. } else {
  1299. if (opt_own_trusted != NULL) {
  1300. own_trusted = load_trusted(opt_own_trusted, 0,
  1301. "trusted certs for verifying own CMP signer cert");
  1302. ok = own_trusted != NULL;
  1303. }
  1304. ok = ok && OSSL_CMP_CTX_build_cert_chain(ctx, own_trusted, certs);
  1305. }
  1306. X509_STORE_free(own_trusted);
  1307. OSSL_STACK_OF_X509_free(certs);
  1308. if (!ok)
  1309. return 0;
  1310. } else if (opt_own_trusted != NULL) {
  1311. CMP_warn("-own_trusted option is ignored without -cert");
  1312. }
  1313. if (!setup_certs(opt_extracerts, "extra certificates for CMP", ctx,
  1314. (add_X509_stack_fn_t)OSSL_CMP_CTX_set1_extraCertsOut))
  1315. return 0;
  1316. cleanse(opt_otherpass);
  1317. if (opt_unprotected_requests)
  1318. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1);
  1319. if (opt_digest != NULL) {
  1320. int digest = OBJ_ln2nid(opt_digest);
  1321. if (digest == NID_undef) {
  1322. CMP_err1("digest algorithm name not recognized: '%s'", opt_digest);
  1323. return 0;
  1324. }
  1325. if (!OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_DIGEST_ALGNID, digest)
  1326. || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_OWF_ALGNID, digest)) {
  1327. CMP_err1("digest algorithm name not supported: '%s'", opt_digest);
  1328. return 0;
  1329. }
  1330. }
  1331. if (opt_mac != NULL) {
  1332. int mac = OBJ_ln2nid(opt_mac);
  1333. if (mac == NID_undef) {
  1334. CMP_err1("MAC algorithm name not recognized: '%s'", opt_mac);
  1335. return 0;
  1336. }
  1337. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_MAC_ALGNID, mac);
  1338. }
  1339. return 1;
  1340. }
  1341. /*
  1342. * set up IR/CR/KUR/CertConf/RR specific parts of the OSSL_CMP_CTX
  1343. * based on options from config file/CLI.
  1344. * Returns pointer on success, NULL on error
  1345. */
  1346. static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
  1347. {
  1348. X509_REQ *csr = NULL;
  1349. X509_EXTENSIONS *exts = NULL;
  1350. X509V3_CTX ext_ctx;
  1351. if (opt_subject == NULL
  1352. && opt_csr == NULL && opt_oldcert == NULL && opt_cert == NULL
  1353. && opt_cmd != CMP_RR && opt_cmd != CMP_GENM)
  1354. CMP_warn("no -subject given; no -csr or -oldcert or -cert available for fallback");
  1355. if (opt_cmd == CMP_IR || opt_cmd == CMP_CR || opt_cmd == CMP_KUR) {
  1356. if (opt_newkey == NULL && opt_key == NULL && opt_csr == NULL) {
  1357. CMP_err("missing -newkey (or -key) to be certified and no -csr given");
  1358. return 0;
  1359. }
  1360. if (opt_certout == NULL) {
  1361. CMP_err("-certout not given, nowhere to save newly enrolled certificate");
  1362. return 0;
  1363. }
  1364. if (!set_name(opt_subject, OSSL_CMP_CTX_set1_subjectName, ctx, "subject")
  1365. || !set_name(opt_issuer, OSSL_CMP_CTX_set1_issuer, ctx, "issuer"))
  1366. return 0;
  1367. } else {
  1368. const char *msg = "option is ignored for commands other than 'ir', 'cr', and 'kur'";
  1369. if (opt_subject != NULL) {
  1370. if (opt_ref == NULL && opt_cert == NULL) {
  1371. /* will use subject as sender unless oldcert subject is used */
  1372. if (!set_name(opt_subject, OSSL_CMP_CTX_set1_subjectName, ctx, "subject"))
  1373. return 0;
  1374. } else {
  1375. CMP_warn1("-subject %s since -ref or -cert is given", msg);
  1376. }
  1377. }
  1378. if (opt_issuer != NULL)
  1379. CMP_warn1("-issuer %s", msg);
  1380. if (opt_reqexts != NULL)
  1381. CMP_warn1("-reqexts %s", msg);
  1382. if (opt_san_nodefault)
  1383. CMP_warn1("-san_nodefault %s", msg);
  1384. if (opt_sans != NULL)
  1385. CMP_warn1("-sans %s", msg);
  1386. if (opt_policies != NULL)
  1387. CMP_warn1("-policies %s", msg);
  1388. if (opt_policy_oids != NULL)
  1389. CMP_warn1("-policy_oids %s", msg);
  1390. }
  1391. if (opt_cmd == CMP_KUR) {
  1392. char *ref_cert = opt_oldcert != NULL ? opt_oldcert : opt_cert;
  1393. if (ref_cert == NULL && opt_csr == NULL) {
  1394. CMP_err("missing -oldcert for certificate to be updated and no -csr given");
  1395. return 0;
  1396. }
  1397. if (opt_subject != NULL)
  1398. CMP_warn2("given -subject '%s' overrides the subject of '%s' for KUR",
  1399. opt_subject, ref_cert != NULL ? ref_cert : opt_csr);
  1400. }
  1401. if (opt_cmd == CMP_RR) {
  1402. if (opt_oldcert == NULL && opt_csr == NULL) {
  1403. CMP_err("missing -oldcert for certificate to be revoked and no -csr given");
  1404. return 0;
  1405. }
  1406. if (opt_oldcert != NULL && opt_csr != NULL)
  1407. CMP_warn("ignoring -csr since certificate to be revoked is given");
  1408. }
  1409. if (opt_cmd == CMP_P10CR && opt_csr == NULL) {
  1410. CMP_err("missing PKCS#10 CSR for p10cr");
  1411. return 0;
  1412. }
  1413. if (opt_recipient == NULL && opt_srvcert == NULL && opt_issuer == NULL
  1414. && opt_oldcert == NULL && opt_cert == NULL)
  1415. CMP_warn("missing -recipient, -srvcert, -issuer, -oldcert or -cert; recipient will be set to \"NULL-DN\"");
  1416. if (opt_cmd == CMP_P10CR || opt_cmd == CMP_RR || opt_cmd == CMP_GENM) {
  1417. const char *msg = "option is ignored for 'p10cr', 'rr', and 'genm' commands";
  1418. if (opt_newkeypass != NULL)
  1419. CMP_warn1("-newkeypass %s", msg);
  1420. if (opt_newkey != NULL)
  1421. CMP_warn1("-newkey %s", msg);
  1422. if (opt_days != 0)
  1423. CMP_warn1("-days %s", msg);
  1424. if (opt_popo != OSSL_CRMF_POPO_NONE - 1)
  1425. CMP_warn1("-popo %s", msg);
  1426. } else if (opt_newkey != NULL) {
  1427. const char *file = opt_newkey;
  1428. const int format = opt_keyform;
  1429. const char *pass = opt_newkeypass;
  1430. const char *desc = "new private key for cert to be enrolled";
  1431. EVP_PKEY *pkey;
  1432. int priv = 1;
  1433. BIO *bio_bak = bio_err;
  1434. bio_err = NULL; /* suppress diagnostics on first try loading key */
  1435. pkey = load_key_pwd(file, format, pass, engine, desc);
  1436. bio_err = bio_bak;
  1437. if (pkey == NULL) {
  1438. ERR_clear_error();
  1439. desc = opt_csr == NULL
  1440. ? "fallback public key for cert to be enrolled"
  1441. : "public key for checking cert resulting from p10cr";
  1442. pkey = load_pubkey(file, format, 0, pass, engine, desc);
  1443. priv = 0;
  1444. }
  1445. cleanse(opt_newkeypass);
  1446. if (pkey == NULL || !OSSL_CMP_CTX_set0_newPkey(ctx, priv, pkey)) {
  1447. EVP_PKEY_free(pkey);
  1448. return 0;
  1449. }
  1450. }
  1451. if (opt_days > 0
  1452. && !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_VALIDITY_DAYS,
  1453. opt_days)) {
  1454. CMP_err("could not set requested cert validity period");
  1455. return 0;
  1456. }
  1457. if (opt_policies != NULL && opt_policy_oids != NULL) {
  1458. CMP_err("cannot have policies both via -policies and via -policy_oids");
  1459. return 0;
  1460. }
  1461. if (opt_csr != NULL) {
  1462. if (opt_cmd == CMP_GENM) {
  1463. CMP_warn("-csr option is ignored for command 'genm'");
  1464. } else {
  1465. if ((csr = load_csr_autofmt(opt_csr, "PKCS#10 CSR")) == NULL)
  1466. return 0;
  1467. if (!OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
  1468. goto oom;
  1469. }
  1470. }
  1471. if (opt_reqexts != NULL || opt_policies != NULL) {
  1472. if ((exts = sk_X509_EXTENSION_new_null()) == NULL)
  1473. goto oom;
  1474. X509V3_set_ctx(&ext_ctx, NULL, NULL, csr, NULL, X509V3_CTX_REPLACE);
  1475. X509V3_set_nconf(&ext_ctx, conf);
  1476. if (opt_reqexts != NULL
  1477. && !X509V3_EXT_add_nconf_sk(conf, &ext_ctx, opt_reqexts, &exts)) {
  1478. CMP_err1("cannot load certificate request extension section '%s'",
  1479. opt_reqexts);
  1480. goto exts_err;
  1481. }
  1482. if (opt_policies != NULL
  1483. && !X509V3_EXT_add_nconf_sk(conf, &ext_ctx, opt_policies, &exts)) {
  1484. CMP_err1("cannot load policy cert request extension section '%s'",
  1485. opt_policies);
  1486. goto exts_err;
  1487. }
  1488. OSSL_CMP_CTX_set0_reqExtensions(ctx, exts);
  1489. }
  1490. X509_REQ_free(csr);
  1491. /* After here, must not goto oom/exts_err */
  1492. if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) && opt_sans != NULL) {
  1493. CMP_err("cannot have Subject Alternative Names both via -reqexts and via -sans");
  1494. return 0;
  1495. }
  1496. if (!set_gennames(ctx, opt_sans, "Subject Alternative Name"))
  1497. return 0;
  1498. if (opt_san_nodefault) {
  1499. if (opt_sans != NULL)
  1500. CMP_warn("-opt_san_nodefault has no effect when -sans is used");
  1501. (void)OSSL_CMP_CTX_set_option(ctx,
  1502. OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT, 1);
  1503. }
  1504. if (opt_policy_oids_critical) {
  1505. if (opt_policy_oids == NULL)
  1506. CMP_warn("-opt_policy_oids_critical has no effect unless -policy_oids is given");
  1507. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_POLICIES_CRITICAL, 1);
  1508. }
  1509. while (opt_policy_oids != NULL) {
  1510. ASN1_OBJECT *policy;
  1511. POLICYINFO *pinfo;
  1512. char *next = next_item(opt_policy_oids);
  1513. if ((policy = OBJ_txt2obj(opt_policy_oids, 1)) == 0) {
  1514. CMP_err1("unknown policy OID '%s'", opt_policy_oids);
  1515. return 0;
  1516. }
  1517. if ((pinfo = POLICYINFO_new()) == NULL) {
  1518. ASN1_OBJECT_free(policy);
  1519. return 0;
  1520. }
  1521. pinfo->policyid = policy;
  1522. if (!OSSL_CMP_CTX_push0_policy(ctx, pinfo)) {
  1523. CMP_err1("cannot add policy with OID '%s'", opt_policy_oids);
  1524. POLICYINFO_free(pinfo);
  1525. return 0;
  1526. }
  1527. opt_policy_oids = next;
  1528. }
  1529. if (opt_popo >= OSSL_CRMF_POPO_NONE)
  1530. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_POPO_METHOD, opt_popo);
  1531. if (opt_oldcert != NULL) {
  1532. if (opt_cmd == CMP_GENM) {
  1533. CMP_warn("-oldcert option is ignored for command 'genm'");
  1534. } else {
  1535. X509 *oldcert = load_cert_pwd(opt_oldcert, opt_keypass,
  1536. opt_cmd == CMP_KUR ?
  1537. "certificate to be updated" :
  1538. opt_cmd == CMP_RR ?
  1539. "certificate to be revoked" :
  1540. "reference certificate (oldcert)");
  1541. /* opt_keypass needed if opt_oldcert is an encrypted PKCS#12 file */
  1542. if (oldcert == NULL)
  1543. return 0;
  1544. if (!OSSL_CMP_CTX_set1_oldCert(ctx, oldcert)) {
  1545. X509_free(oldcert);
  1546. CMP_err("out of memory");
  1547. return 0;
  1548. }
  1549. X509_free(oldcert);
  1550. }
  1551. }
  1552. cleanse(opt_keypass);
  1553. if (opt_revreason > CRL_REASON_NONE)
  1554. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_REVOCATION_REASON,
  1555. opt_revreason);
  1556. return 1;
  1557. oom:
  1558. CMP_err("out of memory");
  1559. exts_err:
  1560. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  1561. X509_REQ_free(csr);
  1562. return 0;
  1563. }
  1564. static int handle_opt_geninfo(OSSL_CMP_CTX *ctx)
  1565. {
  1566. long value;
  1567. ASN1_OBJECT *type;
  1568. ASN1_INTEGER *aint;
  1569. ASN1_TYPE *val;
  1570. OSSL_CMP_ITAV *itav;
  1571. char *endstr;
  1572. char *valptr = strchr(opt_geninfo, ':');
  1573. if (valptr == NULL) {
  1574. CMP_err("missing ':' in -geninfo option");
  1575. return 0;
  1576. }
  1577. valptr[0] = '\0';
  1578. valptr++;
  1579. if (!CHECK_AND_SKIP_CASE_PREFIX(valptr, "int:")) {
  1580. CMP_err("missing 'int:' in -geninfo option");
  1581. return 0;
  1582. }
  1583. value = strtol(valptr, &endstr, 10);
  1584. if (endstr == valptr || *endstr != '\0') {
  1585. CMP_err("cannot parse int in -geninfo option");
  1586. return 0;
  1587. }
  1588. type = OBJ_txt2obj(opt_geninfo, 1);
  1589. if (type == NULL) {
  1590. CMP_err("cannot parse OID in -geninfo option");
  1591. return 0;
  1592. }
  1593. if ((aint = ASN1_INTEGER_new()) == NULL)
  1594. goto oom;
  1595. val = ASN1_TYPE_new();
  1596. if (!ASN1_INTEGER_set(aint, value) || val == NULL) {
  1597. ASN1_INTEGER_free(aint);
  1598. goto oom;
  1599. }
  1600. ASN1_TYPE_set(val, V_ASN1_INTEGER, aint);
  1601. itav = OSSL_CMP_ITAV_create(type, val);
  1602. if (itav == NULL) {
  1603. ASN1_TYPE_free(val);
  1604. goto oom;
  1605. }
  1606. if (!OSSL_CMP_CTX_push0_geninfo_ITAV(ctx, itav)) {
  1607. OSSL_CMP_ITAV_free(itav);
  1608. return 0;
  1609. }
  1610. return 1;
  1611. oom:
  1612. ASN1_OBJECT_free(type);
  1613. CMP_err("out of memory");
  1614. return 0;
  1615. }
  1616. /*
  1617. * set up the client-side OSSL_CMP_CTX based on options from config file/CLI
  1618. * while parsing options and checking their consistency.
  1619. * Prints reason for error to bio_err.
  1620. * Returns 1 on success, 0 on error
  1621. */
  1622. static int setup_client_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
  1623. {
  1624. int ret = 0;
  1625. char *host = NULL, *port = NULL, *path = NULL, *used_path = opt_path;
  1626. #ifndef OPENSSL_NO_SOCK
  1627. int portnum, use_ssl;
  1628. static char server_port[32] = { '\0' };
  1629. const char *proxy_host = NULL;
  1630. #endif
  1631. char server_buf[200] = "mock server";
  1632. char proxy_buf[200] = "";
  1633. if (!opt_use_mock_srv && opt_rspin == NULL) { /* note: -port is not given */
  1634. #ifndef OPENSSL_NO_SOCK
  1635. if (opt_server == NULL) {
  1636. CMP_err("missing -server or -use_mock_srv or -rspin option");
  1637. goto err;
  1638. }
  1639. #else
  1640. CMP_err("missing -use_mock_srv or -rspin option; -server option is not supported due to no-sock build");
  1641. goto err;
  1642. #endif
  1643. }
  1644. #ifndef OPENSSL_NO_SOCK
  1645. if (opt_server == NULL) {
  1646. if (opt_proxy != NULL)
  1647. CMP_warn("ignoring -proxy option since -server is not given");
  1648. if (opt_no_proxy != NULL)
  1649. CMP_warn("ignoring -no_proxy option since -server is not given");
  1650. if (opt_tls_used) {
  1651. CMP_warn("ignoring -tls_used option since -server is not given");
  1652. opt_tls_used = 0;
  1653. }
  1654. goto set_path;
  1655. }
  1656. if (!OSSL_HTTP_parse_url(opt_server, &use_ssl, NULL /* user */, &host, &port,
  1657. &portnum, &path, NULL /* q */, NULL /* frag */)) {
  1658. CMP_err1("cannot parse -server URL: %s", opt_server);
  1659. goto err;
  1660. }
  1661. if (use_ssl && !opt_tls_used) {
  1662. CMP_err("missing -tls_used option since -server URL indicates HTTPS");
  1663. goto err;
  1664. }
  1665. BIO_snprintf(server_port, sizeof(server_port), "%s", port);
  1666. if (opt_path == NULL)
  1667. used_path = path;
  1668. if (!OSSL_CMP_CTX_set1_server(ctx, host)
  1669. || !OSSL_CMP_CTX_set_serverPort(ctx, portnum))
  1670. goto oom;
  1671. if (opt_proxy != NULL && !OSSL_CMP_CTX_set1_proxy(ctx, opt_proxy))
  1672. goto oom;
  1673. if (opt_no_proxy != NULL && !OSSL_CMP_CTX_set1_no_proxy(ctx, opt_no_proxy))
  1674. goto oom;
  1675. (void)BIO_snprintf(server_buf, sizeof(server_buf), "http%s://%s:%s/%s",
  1676. opt_tls_used ? "s" : "", host, port,
  1677. *used_path == '/' ? used_path + 1 : used_path);
  1678. proxy_host = OSSL_HTTP_adapt_proxy(opt_proxy, opt_no_proxy, host, use_ssl);
  1679. if (proxy_host != NULL)
  1680. (void)BIO_snprintf(proxy_buf, sizeof(proxy_buf), " via %s", proxy_host);
  1681. set_path:
  1682. #endif
  1683. if (!OSSL_CMP_CTX_set1_serverPath(ctx, used_path))
  1684. goto oom;
  1685. if (!transform_opts())
  1686. goto err;
  1687. if (opt_infotype_s != NULL) {
  1688. char id_buf[100] = "id-it-";
  1689. strncat(id_buf, opt_infotype_s, sizeof(id_buf) - strlen(id_buf) - 1);
  1690. if ((opt_infotype = OBJ_sn2nid(id_buf)) == NID_undef) {
  1691. CMP_err("unknown OID name in -infotype option");
  1692. goto err;
  1693. }
  1694. }
  1695. if (!setup_verification_ctx(ctx))
  1696. goto err;
  1697. if (opt_keep_alive != 1)
  1698. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_KEEP_ALIVE,
  1699. opt_keep_alive);
  1700. if (opt_total_timeout > 0 && opt_msg_timeout > 0
  1701. && opt_total_timeout < opt_msg_timeout) {
  1702. CMP_err2("-total_timeout argument = %d must not be < %d (-msg_timeout)",
  1703. opt_total_timeout, opt_msg_timeout);
  1704. goto err;
  1705. }
  1706. if (opt_msg_timeout >= 0) /* must do this before setup_ssl_ctx() */
  1707. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_MSG_TIMEOUT,
  1708. opt_msg_timeout);
  1709. if (opt_total_timeout >= 0)
  1710. (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_TOTAL_TIMEOUT,
  1711. opt_total_timeout);
  1712. if (opt_reqin != NULL && opt_rspin != NULL)
  1713. CMP_warn("-reqin is ignored since -rspin is present");
  1714. if (opt_reqin_new_tid && opt_reqin == NULL)
  1715. CMP_warn("-reqin_new_tid is ignored since -reqin is not present");
  1716. if (opt_reqin != NULL || opt_reqout != NULL
  1717. || opt_rspin != NULL || opt_rspout != NULL || opt_use_mock_srv)
  1718. (void)OSSL_CMP_CTX_set_transfer_cb(ctx, read_write_req_resp);
  1719. #ifndef OPENSSL_NO_SOCK
  1720. if (opt_tls_used) {
  1721. APP_HTTP_TLS_INFO *info;
  1722. if (opt_tls_cert != NULL
  1723. || opt_tls_key != NULL || opt_tls_keypass != NULL) {
  1724. if (opt_tls_key == NULL) {
  1725. CMP_err("missing -tls_key option");
  1726. goto err;
  1727. } else if (opt_tls_cert == NULL) {
  1728. CMP_err("missing -tls_cert option");
  1729. goto err;
  1730. }
  1731. }
  1732. if ((info = OPENSSL_zalloc(sizeof(*info))) == NULL)
  1733. goto err;
  1734. (void)OSSL_CMP_CTX_set_http_cb_arg(ctx, info);
  1735. info->server = opt_server;
  1736. info->port = server_port;
  1737. /* workaround for callback design flaw, see #17088: */
  1738. info->use_proxy = proxy_host != NULL;
  1739. info->timeout = OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_MSG_TIMEOUT);
  1740. info->ssl_ctx = setup_ssl_ctx(ctx, host, engine);
  1741. if (info->ssl_ctx == NULL)
  1742. goto err;
  1743. (void)OSSL_CMP_CTX_set_http_cb(ctx, app_http_tls_cb);
  1744. }
  1745. #endif
  1746. if (!setup_protection_ctx(ctx, engine))
  1747. goto err;
  1748. if (!setup_request_ctx(ctx, engine))
  1749. goto err;
  1750. if (!set_name(opt_recipient, OSSL_CMP_CTX_set1_recipient, ctx, "recipient")
  1751. || !set_name(opt_expect_sender, OSSL_CMP_CTX_set1_expected_sender,
  1752. ctx, "expected sender"))
  1753. goto err;
  1754. if (opt_geninfo != NULL && !handle_opt_geninfo(ctx))
  1755. goto err;
  1756. /* not printing earlier, to minimize confusion in case setup fails before */
  1757. if (opt_rspin != NULL)
  1758. CMP_info("will not contact any server since -rspin is given");
  1759. else
  1760. CMP_info2("will contact %s%s", server_buf, proxy_buf);
  1761. ret = 1;
  1762. err:
  1763. OPENSSL_free(host);
  1764. OPENSSL_free(port);
  1765. OPENSSL_free(path);
  1766. return ret;
  1767. oom:
  1768. CMP_err("out of memory");
  1769. goto err;
  1770. }
  1771. /*
  1772. * write out the given certificate to the output specified by bio.
  1773. * Depending on options use either PEM or DER format.
  1774. * Returns 1 on success, 0 on error
  1775. */
  1776. static int write_cert(BIO *bio, X509 *cert)
  1777. {
  1778. if ((opt_certform == FORMAT_PEM && PEM_write_bio_X509(bio, cert))
  1779. || (opt_certform == FORMAT_ASN1 && i2d_X509_bio(bio, cert)))
  1780. return 1;
  1781. if (opt_certform != FORMAT_PEM && opt_certform != FORMAT_ASN1)
  1782. BIO_printf(bio_err,
  1783. "error: unsupported type '%s' for writing certificates\n",
  1784. opt_certform_s);
  1785. return 0;
  1786. }
  1787. /*
  1788. * If file != NULL writes out a stack of certs to the given file.
  1789. * If certs is NULL, the file is emptied.
  1790. * Frees the certs if present.
  1791. * Depending on options use either PEM or DER format,
  1792. * where DER does not make much sense for writing more than one cert!
  1793. * Returns number of written certificates on success, -1 on error.
  1794. */
  1795. static int save_free_certs(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs,
  1796. const char *file, const char *desc)
  1797. {
  1798. BIO *bio = NULL;
  1799. int i;
  1800. int n = sk_X509_num(certs /* may be NULL */);
  1801. if (n < 0)
  1802. n = 0;
  1803. if (file == NULL)
  1804. goto end;
  1805. if (certs != NULL)
  1806. CMP_info3("received %d %s certificate(s), saving to file '%s'",
  1807. n, desc, file);
  1808. if (n > 1 && opt_certform != FORMAT_PEM)
  1809. CMP_warn("saving more than one certificate in non-PEM format");
  1810. if ((bio = BIO_new(BIO_s_file())) == NULL
  1811. || !BIO_write_filename(bio, (char *)file)) {
  1812. CMP_err3("could not open file '%s' for %s %s certificate(s)",
  1813. file, certs == NULL ? "deleting" : "writing", desc);
  1814. n = -1;
  1815. goto end;
  1816. }
  1817. for (i = 0; i < n; i++) {
  1818. if (!write_cert(bio, sk_X509_value(certs, i))) {
  1819. CMP_err2("cannot write %s certificate to file '%s'", desc, file);
  1820. n = -1;
  1821. goto end;
  1822. }
  1823. }
  1824. end:
  1825. BIO_free(bio);
  1826. OSSL_STACK_OF_X509_free(certs);
  1827. return n;
  1828. }
  1829. static int delete_certfile(const char *file, const char *desc)
  1830. {
  1831. if (file == NULL)
  1832. return 1;
  1833. if (unlink(file) != 0 && errno != ENOENT) {
  1834. CMP_err2("Failed to delete %s, which should be done to indicate there is no %s cert",
  1835. file, desc);
  1836. return 0;
  1837. }
  1838. return 1;
  1839. }
  1840. static int save_cert(OSSL_CMP_CTX *ctx, X509 *cert,
  1841. const char *file, const char *desc)
  1842. {
  1843. if (file == NULL || cert == NULL) {
  1844. return 1;
  1845. } else {
  1846. STACK_OF(X509) *certs = sk_X509_new_null();
  1847. if (!X509_add_cert(certs, cert, X509_ADD_FLAG_UP_REF)) {
  1848. sk_X509_free(certs);
  1849. return 0;
  1850. }
  1851. return save_free_certs(ctx, certs, file, desc) >= 0;
  1852. }
  1853. }
  1854. static int print_itavs(const STACK_OF(OSSL_CMP_ITAV) *itavs)
  1855. {
  1856. int i, ret = 1;
  1857. int n = sk_OSSL_CMP_ITAV_num(itavs);
  1858. if (n <= 0) { /* also in case itavs == NULL */
  1859. CMP_info("genp does not contain any ITAV");
  1860. return ret;
  1861. }
  1862. for (i = 1; i <= n; i++) {
  1863. OSSL_CMP_ITAV *itav = sk_OSSL_CMP_ITAV_value(itavs, i - 1);
  1864. ASN1_OBJECT *type = OSSL_CMP_ITAV_get0_type(itav);
  1865. char name[80];
  1866. if (itav == NULL) {
  1867. CMP_err1("could not get ITAV #%d from genp", i);
  1868. ret = 0;
  1869. continue;
  1870. }
  1871. if (i2t_ASN1_OBJECT(name, sizeof(name), type) <= 0) {
  1872. CMP_err1("error parsing type of ITAV #%d from genp", i);
  1873. ret = 0;
  1874. }
  1875. else {
  1876. CMP_info2("ITAV #%d from genp type=%s", i, name);
  1877. }
  1878. }
  1879. return ret;
  1880. }
  1881. static char opt_item[SECTION_NAME_MAX + 1];
  1882. /* get previous name from a comma or space-separated list of names */
  1883. static const char *prev_item(const char *opt, const char *end)
  1884. {
  1885. const char *beg;
  1886. size_t len;
  1887. if (end == opt)
  1888. return NULL;
  1889. beg = end;
  1890. while (beg > opt) {
  1891. --beg;
  1892. if (beg[0] == ',' || isspace(beg[0])) {
  1893. ++beg;
  1894. break;
  1895. }
  1896. }
  1897. len = end - beg;
  1898. if (len > SECTION_NAME_MAX) {
  1899. CMP_warn3("using only first %d characters of section name starting with \"%.*s\"",
  1900. SECTION_NAME_MAX, SECTION_NAME_MAX, beg);
  1901. len = SECTION_NAME_MAX;
  1902. }
  1903. memcpy(opt_item, beg, len);
  1904. opt_item[len] = '\0';
  1905. while (beg > opt) {
  1906. --beg;
  1907. if (beg[0] != ',' && !isspace(beg[0])) {
  1908. ++beg;
  1909. break;
  1910. }
  1911. }
  1912. return beg;
  1913. }
  1914. /* get str value for name from a comma-separated hierarchy of config sections */
  1915. static char *conf_get_string(const CONF *src_conf, const char *groups,
  1916. const char *name)
  1917. {
  1918. char *res = NULL;
  1919. const char *end = groups + strlen(groups);
  1920. while ((end = prev_item(groups, end)) != NULL) {
  1921. if ((res = NCONF_get_string(src_conf, opt_item, name)) != NULL)
  1922. return res;
  1923. }
  1924. return res;
  1925. }
  1926. /* get long val for name from a comma-separated hierarchy of config sections */
  1927. static int conf_get_number_e(const CONF *conf_, const char *groups,
  1928. const char *name, long *result)
  1929. {
  1930. char *str = conf_get_string(conf_, groups, name);
  1931. char *tailptr;
  1932. long res;
  1933. if (str == NULL || *str == '\0')
  1934. return 0;
  1935. res = strtol(str, &tailptr, 10);
  1936. if (res == LONG_MIN || res == LONG_MAX || *tailptr != '\0')
  1937. return 0;
  1938. *result = res;
  1939. return 1;
  1940. }
  1941. /*
  1942. * use the command line option table to read values from the CMP section
  1943. * of openssl.cnf. Defaults are taken from the config file, they can be
  1944. * overwritten on the command line.
  1945. */
  1946. static int read_config(void)
  1947. {
  1948. unsigned int i;
  1949. long num = 0;
  1950. char *txt = NULL;
  1951. const OPTIONS *opt;
  1952. int start_opt = OPT_VERBOSITY - OPT_HELP;
  1953. int start_idx = OPT_VERBOSITY - 2;
  1954. /*
  1955. * starting with offset OPT_VERBOSITY because OPT_CONFIG and OPT_SECTION
  1956. * would not make sense within the config file.
  1957. */
  1958. int n_options = OSSL_NELEM(cmp_options) - 1;
  1959. for (opt = &cmp_options[start_opt], i = start_idx;
  1960. opt->name != NULL; i++, opt++)
  1961. if (!strcmp(opt->name, OPT_SECTION_STR)
  1962. || !strcmp(opt->name, OPT_MORE_STR))
  1963. n_options--;
  1964. OPENSSL_assert(OSSL_NELEM(cmp_vars) == n_options
  1965. + OPT_PROV__FIRST + 1 - OPT_PROV__LAST
  1966. + OPT_R__FIRST + 1 - OPT_R__LAST
  1967. + OPT_V__FIRST + 1 - OPT_V__LAST);
  1968. for (opt = &cmp_options[start_opt], i = start_idx;
  1969. opt->name != NULL; i++, opt++) {
  1970. int provider_option = (OPT_PROV__FIRST <= opt->retval
  1971. && opt->retval < OPT_PROV__LAST);
  1972. int rand_state_option = (OPT_R__FIRST <= opt->retval
  1973. && opt->retval < OPT_R__LAST);
  1974. int verification_option = (OPT_V__FIRST <= opt->retval
  1975. && opt->retval < OPT_V__LAST);
  1976. if (strcmp(opt->name, OPT_SECTION_STR) == 0
  1977. || strcmp(opt->name, OPT_MORE_STR) == 0) {
  1978. i--;
  1979. continue;
  1980. }
  1981. if (provider_option || rand_state_option || verification_option)
  1982. i--;
  1983. switch (opt->valtype) {
  1984. case '-':
  1985. case 'p':
  1986. case 'n':
  1987. case 'N':
  1988. case 'l':
  1989. if (!conf_get_number_e(conf, opt_section, opt->name, &num)) {
  1990. ERR_clear_error();
  1991. continue; /* option not provided */
  1992. }
  1993. if (opt->valtype == 'p' && num <= 0) {
  1994. opt_printf_stderr("Non-positive number \"%ld\" for config option -%s\n",
  1995. num, opt->name);
  1996. return -1;
  1997. }
  1998. if (opt->valtype == 'N' && num < 0) {
  1999. opt_printf_stderr("Negative number \"%ld\" for config option -%s\n",
  2000. num, opt->name);
  2001. return -1;
  2002. }
  2003. break;
  2004. case 's':
  2005. case '>':
  2006. case 'M':
  2007. txt = conf_get_string(conf, opt_section, opt->name);
  2008. if (txt == NULL) {
  2009. ERR_clear_error();
  2010. continue; /* option not provided */
  2011. }
  2012. break;
  2013. default:
  2014. CMP_err2("internal: unsupported type '%c' for option '%s'",
  2015. opt->valtype, opt->name);
  2016. return 0;
  2017. break;
  2018. }
  2019. if (provider_option || verification_option) {
  2020. int conf_argc = 1;
  2021. char *conf_argv[3];
  2022. char arg1[82];
  2023. BIO_snprintf(arg1, 81, "-%s", (char *)opt->name);
  2024. conf_argv[0] = prog;
  2025. conf_argv[1] = arg1;
  2026. if (opt->valtype == '-') {
  2027. if (num != 0)
  2028. conf_argc = 2;
  2029. } else {
  2030. conf_argc = 3;
  2031. conf_argv[2] = conf_get_string(conf, opt_section, opt->name);
  2032. /* not NULL */
  2033. }
  2034. if (conf_argc > 1) {
  2035. (void)opt_init(conf_argc, conf_argv, cmp_options);
  2036. if (provider_option
  2037. ? !opt_provider(opt_next())
  2038. : !opt_verify(opt_next(), vpm)) {
  2039. CMP_err2("for option '%s' in config file section '%s'",
  2040. opt->name, opt_section);
  2041. return 0;
  2042. }
  2043. }
  2044. } else {
  2045. switch (opt->valtype) {
  2046. case '-':
  2047. case 'p':
  2048. case 'n':
  2049. case 'N':
  2050. if (num < INT_MIN || INT_MAX < num) {
  2051. BIO_printf(bio_err,
  2052. "integer value out of range for option '%s'\n",
  2053. opt->name);
  2054. return 0;
  2055. }
  2056. *cmp_vars[i].num = (int)num;
  2057. break;
  2058. case 'l':
  2059. *cmp_vars[i].num_long = num;
  2060. break;
  2061. default:
  2062. if (txt != NULL && txt[0] == '\0')
  2063. txt = NULL; /* reset option on empty string input */
  2064. *cmp_vars[i].txt = txt;
  2065. break;
  2066. }
  2067. }
  2068. }
  2069. return 1;
  2070. }
  2071. static char *opt_str(void)
  2072. {
  2073. char *arg = opt_arg();
  2074. if (arg[0] == '\0') {
  2075. CMP_warn1("%s option argument is empty string, resetting option",
  2076. opt_flag());
  2077. arg = NULL;
  2078. } else if (arg[0] == '-') {
  2079. CMP_warn1("%s option argument starts with hyphen", opt_flag());
  2080. }
  2081. return arg;
  2082. }
  2083. /* returns 1 on success, 0 on error, -1 on -help (i.e., stop with success) */
  2084. static int get_opts(int argc, char **argv)
  2085. {
  2086. OPTION_CHOICE o;
  2087. prog = opt_init(argc, argv, cmp_options);
  2088. while ((o = opt_next()) != OPT_EOF) {
  2089. switch (o) {
  2090. case OPT_EOF:
  2091. case OPT_ERR:
  2092. opthelp:
  2093. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  2094. return 0;
  2095. case OPT_HELP:
  2096. opt_help(cmp_options);
  2097. return -1;
  2098. case OPT_CONFIG: /* has already been handled */
  2099. case OPT_SECTION: /* has already been handled */
  2100. break;
  2101. case OPT_VERBOSITY:
  2102. if (!set_verbosity(opt_int_arg()))
  2103. goto opthelp;
  2104. break;
  2105. #ifndef OPENSSL_NO_SOCK
  2106. case OPT_SERVER:
  2107. opt_server = opt_str();
  2108. break;
  2109. case OPT_PROXY:
  2110. opt_proxy = opt_str();
  2111. break;
  2112. case OPT_NO_PROXY:
  2113. opt_no_proxy = opt_str();
  2114. break;
  2115. #endif
  2116. case OPT_RECIPIENT:
  2117. opt_recipient = opt_str();
  2118. break;
  2119. case OPT_PATH:
  2120. opt_path = opt_str();
  2121. break;
  2122. case OPT_KEEP_ALIVE:
  2123. opt_keep_alive = opt_int_arg();
  2124. if (opt_keep_alive > 2) {
  2125. CMP_err("-keep_alive argument must be 0, 1, or 2");
  2126. goto opthelp;
  2127. }
  2128. break;
  2129. case OPT_MSG_TIMEOUT:
  2130. opt_msg_timeout = opt_int_arg();
  2131. break;
  2132. case OPT_TOTAL_TIMEOUT:
  2133. opt_total_timeout = opt_int_arg();
  2134. break;
  2135. #ifndef OPENSSL_NO_SOCK
  2136. case OPT_TLS_USED:
  2137. opt_tls_used = 1;
  2138. break;
  2139. case OPT_TLS_CERT:
  2140. opt_tls_cert = opt_str();
  2141. break;
  2142. case OPT_TLS_KEY:
  2143. opt_tls_key = opt_str();
  2144. break;
  2145. case OPT_TLS_KEYPASS:
  2146. opt_tls_keypass = opt_str();
  2147. break;
  2148. case OPT_TLS_EXTRA:
  2149. opt_tls_extra = opt_str();
  2150. break;
  2151. case OPT_TLS_TRUSTED:
  2152. opt_tls_trusted = opt_str();
  2153. break;
  2154. case OPT_TLS_HOST:
  2155. opt_tls_host = opt_str();
  2156. break;
  2157. #endif
  2158. case OPT_REF:
  2159. opt_ref = opt_str();
  2160. break;
  2161. case OPT_SECRET:
  2162. opt_secret = opt_str();
  2163. break;
  2164. case OPT_CERT:
  2165. opt_cert = opt_str();
  2166. break;
  2167. case OPT_OWN_TRUSTED:
  2168. opt_own_trusted = opt_str();
  2169. break;
  2170. case OPT_KEY:
  2171. opt_key = opt_str();
  2172. break;
  2173. case OPT_KEYPASS:
  2174. opt_keypass = opt_str();
  2175. break;
  2176. case OPT_DIGEST:
  2177. opt_digest = opt_str();
  2178. break;
  2179. case OPT_MAC:
  2180. opt_mac = opt_str();
  2181. break;
  2182. case OPT_EXTRACERTS:
  2183. opt_extracerts = opt_str();
  2184. break;
  2185. case OPT_UNPROTECTED_REQUESTS:
  2186. opt_unprotected_requests = 1;
  2187. break;
  2188. case OPT_TRUSTED:
  2189. opt_trusted = opt_str();
  2190. break;
  2191. case OPT_UNTRUSTED:
  2192. opt_untrusted = opt_str();
  2193. break;
  2194. case OPT_SRVCERT:
  2195. opt_srvcert = opt_str();
  2196. break;
  2197. case OPT_EXPECT_SENDER:
  2198. opt_expect_sender = opt_str();
  2199. break;
  2200. case OPT_IGNORE_KEYUSAGE:
  2201. opt_ignore_keyusage = 1;
  2202. break;
  2203. case OPT_UNPROTECTED_ERRORS:
  2204. opt_unprotected_errors = 1;
  2205. break;
  2206. case OPT_SRVCERTOUT:
  2207. opt_srvcertout = opt_str();
  2208. break;
  2209. case OPT_EXTRACERTSOUT:
  2210. opt_extracertsout = opt_str();
  2211. break;
  2212. case OPT_CACERTSOUT:
  2213. opt_cacertsout = opt_str();
  2214. break;
  2215. case OPT_V_CASES:
  2216. if (!opt_verify(o, vpm))
  2217. goto opthelp;
  2218. break;
  2219. case OPT_CMD:
  2220. opt_cmd_s = opt_str();
  2221. break;
  2222. case OPT_INFOTYPE:
  2223. opt_infotype_s = opt_str();
  2224. break;
  2225. case OPT_GENINFO:
  2226. opt_geninfo = opt_str();
  2227. break;
  2228. case OPT_NEWKEY:
  2229. opt_newkey = opt_str();
  2230. break;
  2231. case OPT_NEWKEYPASS:
  2232. opt_newkeypass = opt_str();
  2233. break;
  2234. case OPT_SUBJECT:
  2235. opt_subject = opt_str();
  2236. break;
  2237. case OPT_ISSUER:
  2238. opt_issuer = opt_str();
  2239. break;
  2240. case OPT_DAYS:
  2241. opt_days = opt_int_arg();
  2242. break;
  2243. case OPT_REQEXTS:
  2244. opt_reqexts = opt_str();
  2245. break;
  2246. case OPT_SANS:
  2247. opt_sans = opt_str();
  2248. break;
  2249. case OPT_SAN_NODEFAULT:
  2250. opt_san_nodefault = 1;
  2251. break;
  2252. case OPT_POLICIES:
  2253. opt_policies = opt_str();
  2254. break;
  2255. case OPT_POLICY_OIDS:
  2256. opt_policy_oids = opt_str();
  2257. break;
  2258. case OPT_POLICY_OIDS_CRITICAL:
  2259. opt_policy_oids_critical = 1;
  2260. break;
  2261. case OPT_POPO:
  2262. opt_popo = opt_int_arg();
  2263. if (opt_popo < OSSL_CRMF_POPO_NONE
  2264. || opt_popo > OSSL_CRMF_POPO_KEYENC) {
  2265. CMP_err("invalid popo spec. Valid values are -1 .. 2");
  2266. goto opthelp;
  2267. }
  2268. break;
  2269. case OPT_CSR:
  2270. opt_csr = opt_arg();
  2271. break;
  2272. case OPT_OUT_TRUSTED:
  2273. opt_out_trusted = opt_str();
  2274. break;
  2275. case OPT_IMPLICIT_CONFIRM:
  2276. opt_implicit_confirm = 1;
  2277. break;
  2278. case OPT_DISABLE_CONFIRM:
  2279. opt_disable_confirm = 1;
  2280. break;
  2281. case OPT_CERTOUT:
  2282. opt_certout = opt_str();
  2283. break;
  2284. case OPT_CHAINOUT:
  2285. opt_chainout = opt_str();
  2286. break;
  2287. case OPT_OLDCERT:
  2288. opt_oldcert = opt_str();
  2289. break;
  2290. case OPT_REVREASON:
  2291. opt_revreason = opt_int_arg();
  2292. if (opt_revreason < CRL_REASON_NONE
  2293. || opt_revreason > CRL_REASON_AA_COMPROMISE
  2294. || opt_revreason == 7) {
  2295. CMP_err("invalid revreason. Valid values are -1 .. 6, 8 .. 10");
  2296. goto opthelp;
  2297. }
  2298. break;
  2299. case OPT_CERTFORM:
  2300. opt_certform_s = opt_str();
  2301. break;
  2302. case OPT_KEYFORM:
  2303. opt_keyform_s = opt_str();
  2304. break;
  2305. case OPT_OTHERPASS:
  2306. opt_otherpass = opt_str();
  2307. break;
  2308. #ifndef OPENSSL_NO_ENGINE
  2309. case OPT_ENGINE:
  2310. opt_engine = opt_str();
  2311. break;
  2312. #endif
  2313. case OPT_PROV_CASES:
  2314. if (!opt_provider(o))
  2315. goto opthelp;
  2316. break;
  2317. case OPT_R_CASES:
  2318. if (!opt_rand(o))
  2319. goto opthelp;
  2320. break;
  2321. case OPT_BATCH:
  2322. opt_batch = 1;
  2323. break;
  2324. case OPT_REPEAT:
  2325. opt_repeat = opt_int_arg();
  2326. break;
  2327. case OPT_REQIN:
  2328. opt_reqin = opt_str();
  2329. break;
  2330. case OPT_REQIN_NEW_TID:
  2331. opt_reqin_new_tid = 1;
  2332. break;
  2333. case OPT_REQOUT:
  2334. opt_reqout = opt_str();
  2335. break;
  2336. case OPT_RSPIN:
  2337. opt_rspin = opt_str();
  2338. break;
  2339. case OPT_RSPOUT:
  2340. opt_rspout = opt_str();
  2341. break;
  2342. case OPT_USE_MOCK_SRV:
  2343. opt_use_mock_srv = 1;
  2344. break;
  2345. #ifndef OPENSSL_NO_SOCK
  2346. case OPT_PORT:
  2347. opt_port = opt_str();
  2348. break;
  2349. case OPT_MAX_MSGS:
  2350. opt_max_msgs = opt_int_arg();
  2351. break;
  2352. #endif
  2353. case OPT_SRV_REF:
  2354. opt_srv_ref = opt_str();
  2355. break;
  2356. case OPT_SRV_SECRET:
  2357. opt_srv_secret = opt_str();
  2358. break;
  2359. case OPT_SRV_CERT:
  2360. opt_srv_cert = opt_str();
  2361. break;
  2362. case OPT_SRV_KEY:
  2363. opt_srv_key = opt_str();
  2364. break;
  2365. case OPT_SRV_KEYPASS:
  2366. opt_srv_keypass = opt_str();
  2367. break;
  2368. case OPT_SRV_TRUSTED:
  2369. opt_srv_trusted = opt_str();
  2370. break;
  2371. case OPT_SRV_UNTRUSTED:
  2372. opt_srv_untrusted = opt_str();
  2373. break;
  2374. case OPT_REF_CERT:
  2375. opt_ref_cert = opt_str();
  2376. break;
  2377. case OPT_RSP_CERT:
  2378. opt_rsp_cert = opt_str();
  2379. break;
  2380. case OPT_RSP_EXTRACERTS:
  2381. opt_rsp_extracerts = opt_str();
  2382. break;
  2383. case OPT_RSP_CAPUBS:
  2384. opt_rsp_capubs = opt_str();
  2385. break;
  2386. case OPT_POLL_COUNT:
  2387. opt_poll_count = opt_int_arg();
  2388. break;
  2389. case OPT_CHECK_AFTER:
  2390. opt_check_after = opt_int_arg();
  2391. break;
  2392. case OPT_GRANT_IMPLICITCONF:
  2393. opt_grant_implicitconf = 1;
  2394. break;
  2395. case OPT_PKISTATUS:
  2396. opt_pkistatus = opt_int_arg();
  2397. break;
  2398. case OPT_FAILURE:
  2399. opt_failure = opt_int_arg();
  2400. break;
  2401. case OPT_FAILUREBITS:
  2402. opt_failurebits = opt_int_arg();
  2403. break;
  2404. case OPT_STATUSSTRING:
  2405. opt_statusstring = opt_str();
  2406. break;
  2407. case OPT_SEND_ERROR:
  2408. opt_send_error = 1;
  2409. break;
  2410. case OPT_SEND_UNPROTECTED:
  2411. opt_send_unprotected = 1;
  2412. break;
  2413. case OPT_SEND_UNPROT_ERR:
  2414. opt_send_unprot_err = 1;
  2415. break;
  2416. case OPT_ACCEPT_UNPROTECTED:
  2417. opt_accept_unprotected = 1;
  2418. break;
  2419. case OPT_ACCEPT_UNPROT_ERR:
  2420. opt_accept_unprot_err = 1;
  2421. break;
  2422. case OPT_ACCEPT_RAVERIFIED:
  2423. opt_accept_raverified = 1;
  2424. break;
  2425. }
  2426. }
  2427. /* No extra args. */
  2428. if (!opt_check_rest_arg(NULL))
  2429. goto opthelp;
  2430. return 1;
  2431. }
  2432. #ifndef OPENSSL_NO_SOCK
  2433. static int cmp_server(OSSL_CMP_CTX *srv_cmp_ctx)
  2434. {
  2435. BIO *acbio;
  2436. BIO *cbio = NULL;
  2437. int keep_alive = 0;
  2438. int msgs = 0;
  2439. int retry = 1;
  2440. int ret = 1;
  2441. if ((acbio = http_server_init(prog, opt_port, opt_verbosity)) == NULL)
  2442. return 0;
  2443. while (opt_max_msgs <= 0 || msgs < opt_max_msgs) {
  2444. char *path = NULL;
  2445. OSSL_CMP_MSG *req = NULL;
  2446. OSSL_CMP_MSG *resp = NULL;
  2447. ret = http_server_get_asn1_req(ASN1_ITEM_rptr(OSSL_CMP_MSG),
  2448. (ASN1_VALUE **)&req, &path,
  2449. &cbio, acbio, &keep_alive,
  2450. prog, 0, 0);
  2451. if (ret == 0) { /* no request yet */
  2452. if (retry) {
  2453. ossl_sleep(1000);
  2454. retry = 0;
  2455. continue;
  2456. }
  2457. ret = 0;
  2458. goto next;
  2459. }
  2460. if (ret++ == -1) /* fatal error */
  2461. break;
  2462. ret = 0;
  2463. msgs++;
  2464. if (req != NULL) {
  2465. if (strcmp(path, "") != 0 && strcmp(path, "pkix/") != 0) {
  2466. (void)http_server_send_status(cbio, 404, "Not Found");
  2467. CMP_err1("expecting empty path or 'pkix/' but got '%s'",
  2468. path);
  2469. OPENSSL_free(path);
  2470. OSSL_CMP_MSG_free(req);
  2471. goto next;
  2472. }
  2473. OPENSSL_free(path);
  2474. resp = OSSL_CMP_CTX_server_perform(cmp_ctx, req);
  2475. OSSL_CMP_MSG_free(req);
  2476. if (resp == NULL) {
  2477. (void)http_server_send_status(cbio,
  2478. 500, "Internal Server Error");
  2479. break; /* treated as fatal error */
  2480. }
  2481. ret = http_server_send_asn1_resp(cbio, keep_alive,
  2482. "application/pkixcmp",
  2483. ASN1_ITEM_rptr(OSSL_CMP_MSG),
  2484. (const ASN1_VALUE *)resp);
  2485. OSSL_CMP_MSG_free(resp);
  2486. if (!ret)
  2487. break; /* treated as fatal error */
  2488. }
  2489. next:
  2490. if (!ret) { /* on transmission error, cancel CMP transaction */
  2491. (void)OSSL_CMP_CTX_set1_transactionID(srv_cmp_ctx, NULL);
  2492. (void)OSSL_CMP_CTX_set1_senderNonce(srv_cmp_ctx, NULL);
  2493. }
  2494. if (!ret || !keep_alive
  2495. || OSSL_CMP_CTX_get_status(srv_cmp_ctx) == -1
  2496. /* transaction closed by OSSL_CMP_CTX_server_perform() */) {
  2497. BIO_free_all(cbio);
  2498. cbio = NULL;
  2499. }
  2500. }
  2501. BIO_free_all(cbio);
  2502. BIO_free_all(acbio);
  2503. return ret;
  2504. }
  2505. #endif
  2506. int cmp_main(int argc, char **argv)
  2507. {
  2508. char *configfile = NULL;
  2509. int i;
  2510. X509 *newcert = NULL;
  2511. ENGINE *engine = NULL;
  2512. OSSL_CMP_CTX *srv_cmp_ctx = NULL;
  2513. int ret = 0; /* default: failure */
  2514. prog = opt_appname(argv[0]);
  2515. if (argc <= 1) {
  2516. opt_help(cmp_options);
  2517. goto err;
  2518. }
  2519. /*
  2520. * handle options -config, -section, and -verbosity upfront
  2521. * to take effect for other options
  2522. */
  2523. for (i = 1; i < argc - 1; i++) {
  2524. if (*argv[i] == '-') {
  2525. if (!strcmp(argv[i] + 1, cmp_options[OPT_CONFIG - OPT_HELP].name))
  2526. opt_config = argv[++i];
  2527. else if (!strcmp(argv[i] + 1,
  2528. cmp_options[OPT_SECTION - OPT_HELP].name))
  2529. opt_section = argv[++i];
  2530. else if (strcmp(argv[i] + 1,
  2531. cmp_options[OPT_VERBOSITY - OPT_HELP].name) == 0
  2532. && !set_verbosity(atoi(argv[++i])))
  2533. goto err;
  2534. }
  2535. }
  2536. if (opt_section[0] == '\0') /* empty string */
  2537. opt_section = DEFAULT_SECTION;
  2538. vpm = X509_VERIFY_PARAM_new();
  2539. if (vpm == NULL) {
  2540. CMP_err("out of memory");
  2541. goto err;
  2542. }
  2543. /* read default values for options from config file */
  2544. configfile = opt_config != NULL ? opt_config : default_config_file;
  2545. if (configfile != NULL && configfile[0] != '\0' /* non-empty string */
  2546. && (configfile != default_config_file
  2547. || access(configfile, F_OK) != -1)) {
  2548. CMP_info2("using section(s) '%s' of OpenSSL configuration file '%s'",
  2549. opt_section, configfile);
  2550. conf = app_load_config(configfile);
  2551. if (conf == NULL) {
  2552. goto err;
  2553. } else {
  2554. if (strcmp(opt_section, CMP_SECTION) == 0) { /* default */
  2555. if (!NCONF_get_section(conf, opt_section))
  2556. CMP_info2("no [%s] section found in config file '%s';"
  2557. " will thus use just [default] and unnamed section if present",
  2558. opt_section, configfile);
  2559. } else {
  2560. const char *end = opt_section + strlen(opt_section);
  2561. while ((end = prev_item(opt_section, end)) != NULL) {
  2562. if (!NCONF_get_section(conf, opt_item)) {
  2563. CMP_err2("no [%s] section found in config file '%s'",
  2564. opt_item, configfile);
  2565. goto err;
  2566. }
  2567. }
  2568. }
  2569. ret = read_config();
  2570. if (!set_verbosity(opt_verbosity)) /* just for checking range */
  2571. ret = -1;
  2572. if (ret <= 0) {
  2573. if (ret == -1)
  2574. BIO_printf(bio_err, "Use -help for summary.\n");
  2575. goto err;
  2576. }
  2577. }
  2578. }
  2579. (void)BIO_flush(bio_err); /* prevent interference with opt_help() */
  2580. ret = get_opts(argc, argv);
  2581. if (ret <= 0)
  2582. goto err;
  2583. ret = 0;
  2584. if (!delete_certfile(opt_srvcertout, "validated server")
  2585. || !delete_certfile(opt_certout, "enrolled")
  2586. || save_free_certs(NULL, NULL, opt_extracertsout, "extra") < 0
  2587. || save_free_certs(NULL, NULL, opt_cacertsout, "CA") < 0
  2588. || save_free_certs(NULL, NULL, opt_chainout, "chain") < 0)
  2589. goto err;
  2590. if (!app_RAND_load())
  2591. goto err;
  2592. if (opt_batch)
  2593. set_base_ui_method(UI_null());
  2594. if (opt_engine != NULL) {
  2595. engine = setup_engine_methods(opt_engine,
  2596. 0 /* not: ENGINE_METHOD_ALL */, 0);
  2597. if (engine == NULL) {
  2598. CMP_err1("cannot load engine %s", opt_engine);
  2599. goto err;
  2600. }
  2601. }
  2602. cmp_ctx = OSSL_CMP_CTX_new(app_get0_libctx(), app_get0_propq());
  2603. if (cmp_ctx == NULL)
  2604. goto err;
  2605. OSSL_CMP_CTX_set_log_verbosity(cmp_ctx, opt_verbosity);
  2606. if (!OSSL_CMP_CTX_set_log_cb(cmp_ctx, print_to_bio_out)) {
  2607. CMP_err1("cannot set up error reporting and logging for %s", prog);
  2608. goto err;
  2609. }
  2610. #ifndef OPENSSL_NO_SOCK
  2611. if (opt_tls_cert == NULL && opt_tls_key == NULL && opt_tls_keypass == NULL
  2612. && opt_tls_extra == NULL && opt_tls_trusted == NULL
  2613. && opt_tls_host == NULL) {
  2614. if (opt_tls_used)
  2615. CMP_warn("-tls_used given without any other TLS options");
  2616. } else if (!opt_tls_used) {
  2617. CMP_warn("ignoring TLS options(s) since -tls_used is not given");
  2618. }
  2619. if (opt_port != NULL) {
  2620. if (opt_tls_used) {
  2621. CMP_err("-tls_used option not supported with -port option");
  2622. goto err;
  2623. }
  2624. if (opt_use_mock_srv || opt_server != NULL || opt_rspin != NULL) {
  2625. CMP_err("cannot use -port with -use_mock_srv, -server, or -rspin options");
  2626. goto err;
  2627. }
  2628. }
  2629. if (opt_server != NULL && opt_use_mock_srv) {
  2630. CMP_err("cannot use both -server and -use_mock_srv options");
  2631. goto err;
  2632. }
  2633. #endif
  2634. if (opt_rspin != NULL && opt_use_mock_srv) {
  2635. CMP_err("cannot use both -rspin and -use_mock_srv options");
  2636. goto err;
  2637. }
  2638. if (opt_use_mock_srv
  2639. #ifndef OPENSSL_NO_SOCK
  2640. || opt_port != NULL
  2641. #endif
  2642. ) {
  2643. OSSL_CMP_SRV_CTX *srv_ctx;
  2644. if ((srv_ctx = setup_srv_ctx(engine)) == NULL)
  2645. goto err;
  2646. srv_cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx);
  2647. OSSL_CMP_CTX_set_transfer_cb_arg(cmp_ctx, srv_ctx);
  2648. if (!OSSL_CMP_CTX_set_log_cb(srv_cmp_ctx, print_to_bio_err)) {
  2649. CMP_err1("cannot set up error reporting and logging for %s", prog);
  2650. goto err;
  2651. }
  2652. OSSL_CMP_CTX_set_log_verbosity(srv_cmp_ctx, opt_verbosity);
  2653. }
  2654. #ifndef OPENSSL_NO_SOCK
  2655. if (opt_tls_used && (opt_use_mock_srv || opt_rspin != NULL)) {
  2656. CMP_warn("ignoring -tls_used option since -use_mock_srv or -rspin is given");
  2657. opt_tls_used = 0;
  2658. }
  2659. if (opt_port != NULL) { /* act as very basic CMP HTTP server */
  2660. ret = cmp_server(srv_cmp_ctx);
  2661. goto err;
  2662. }
  2663. /* act as CMP client, possibly using internal mock server */
  2664. if (opt_server != NULL) {
  2665. if (opt_rspin != NULL) {
  2666. CMP_warn("ignoring -server option since -rspin is given");
  2667. opt_server = NULL;
  2668. }
  2669. }
  2670. #endif
  2671. if (!setup_client_ctx(cmp_ctx, engine)) {
  2672. CMP_err("cannot set up CMP context");
  2673. goto err;
  2674. }
  2675. for (i = 0; i < opt_repeat; i++) {
  2676. /* everything is ready, now connect and perform the command! */
  2677. switch (opt_cmd) {
  2678. case CMP_IR:
  2679. newcert = OSSL_CMP_exec_IR_ses(cmp_ctx);
  2680. if (newcert != NULL)
  2681. ret = 1;
  2682. break;
  2683. case CMP_KUR:
  2684. newcert = OSSL_CMP_exec_KUR_ses(cmp_ctx);
  2685. if (newcert != NULL)
  2686. ret = 1;
  2687. break;
  2688. case CMP_CR:
  2689. newcert = OSSL_CMP_exec_CR_ses(cmp_ctx);
  2690. if (newcert != NULL)
  2691. ret = 1;
  2692. break;
  2693. case CMP_P10CR:
  2694. newcert = OSSL_CMP_exec_P10CR_ses(cmp_ctx);
  2695. if (newcert != NULL)
  2696. ret = 1;
  2697. break;
  2698. case CMP_RR:
  2699. ret = OSSL_CMP_exec_RR_ses(cmp_ctx);
  2700. break;
  2701. case CMP_GENM:
  2702. {
  2703. STACK_OF(OSSL_CMP_ITAV) *itavs;
  2704. if (opt_infotype != NID_undef) {
  2705. OSSL_CMP_ITAV *itav =
  2706. OSSL_CMP_ITAV_create(OBJ_nid2obj(opt_infotype), NULL);
  2707. if (itav == NULL)
  2708. goto err;
  2709. OSSL_CMP_CTX_push0_genm_ITAV(cmp_ctx, itav);
  2710. }
  2711. if ((itavs = OSSL_CMP_exec_GENM_ses(cmp_ctx)) != NULL) {
  2712. ret = print_itavs(itavs);
  2713. sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
  2714. } else {
  2715. CMP_err("could not obtain ITAVs from genp");
  2716. }
  2717. break;
  2718. }
  2719. default:
  2720. break;
  2721. }
  2722. if (OSSL_CMP_CTX_get_status(cmp_ctx) < 0)
  2723. goto err; /* we got no response, maybe even did not send request */
  2724. {
  2725. /* print PKIStatusInfo */
  2726. int status = OSSL_CMP_CTX_get_status(cmp_ctx);
  2727. char *buf = app_malloc(OSSL_CMP_PKISI_BUFLEN, "PKIStatusInfo buf");
  2728. const char *string =
  2729. OSSL_CMP_CTX_snprint_PKIStatus(cmp_ctx, buf,
  2730. OSSL_CMP_PKISI_BUFLEN);
  2731. const char *from = "", *server = "";
  2732. #ifndef OPENSSL_NO_SOCK
  2733. if (opt_server != NULL) {
  2734. from = " from ";
  2735. server = opt_server;
  2736. }
  2737. #endif
  2738. CMP_print(bio_err,
  2739. status == OSSL_CMP_PKISTATUS_accepted
  2740. ? OSSL_CMP_LOG_INFO :
  2741. status == OSSL_CMP_PKISTATUS_rejection
  2742. || status == OSSL_CMP_PKISTATUS_waiting
  2743. ? OSSL_CMP_LOG_ERR : OSSL_CMP_LOG_WARNING,
  2744. status == OSSL_CMP_PKISTATUS_accepted ? "info" :
  2745. status == OSSL_CMP_PKISTATUS_rejection ? "server error" :
  2746. status == OSSL_CMP_PKISTATUS_waiting ? "internal error"
  2747. : "warning",
  2748. "received%s%s %s", from, server,
  2749. string != NULL ? string : "<unknown PKIStatus>");
  2750. OPENSSL_free(buf);
  2751. }
  2752. if (save_free_certs(cmp_ctx, OSSL_CMP_CTX_get1_extraCertsIn(cmp_ctx),
  2753. opt_extracertsout, "extra") < 0)
  2754. ret = 0;
  2755. if (!ret)
  2756. goto err;
  2757. ret = 0;
  2758. if (!save_cert(cmp_ctx, OSSL_CMP_CTX_get0_validatedSrvCert(cmp_ctx),
  2759. opt_srvcertout, "validated server"))
  2760. goto err;
  2761. if (save_free_certs(cmp_ctx, OSSL_CMP_CTX_get1_caPubs(cmp_ctx),
  2762. opt_cacertsout, "CA") < 0)
  2763. goto err;
  2764. if (!save_cert(cmp_ctx, newcert, opt_certout, "enrolled"))
  2765. goto err;
  2766. if (save_free_certs(cmp_ctx, OSSL_CMP_CTX_get1_newChain(cmp_ctx),
  2767. opt_chainout, "chain") < 0)
  2768. goto err;
  2769. if (!OSSL_CMP_CTX_reinit(cmp_ctx))
  2770. goto err;
  2771. }
  2772. ret = 1;
  2773. err:
  2774. /* in case we ended up here on error without proper cleaning */
  2775. cleanse(opt_keypass);
  2776. cleanse(opt_newkeypass);
  2777. cleanse(opt_otherpass);
  2778. #ifndef OPENSSL_NO_SOCK
  2779. cleanse(opt_tls_keypass);
  2780. #endif
  2781. cleanse(opt_secret);
  2782. cleanse(opt_srv_keypass);
  2783. cleanse(opt_srv_secret);
  2784. if (ret != 1)
  2785. OSSL_CMP_CTX_print_errors(cmp_ctx);
  2786. if (cmp_ctx != NULL) {
  2787. #ifndef OPENSSL_NO_SOCK
  2788. APP_HTTP_TLS_INFO *info = OSSL_CMP_CTX_get_http_cb_arg(cmp_ctx);
  2789. #endif
  2790. ossl_cmp_mock_srv_free(OSSL_CMP_CTX_get_transfer_cb_arg(cmp_ctx));
  2791. X509_STORE_free(OSSL_CMP_CTX_get_certConf_cb_arg(cmp_ctx));
  2792. /* cannot free info already here, as it may be used indirectly by: */
  2793. OSSL_CMP_CTX_free(cmp_ctx);
  2794. #ifndef OPENSSL_NO_SOCK
  2795. APP_HTTP_TLS_INFO_free(info);
  2796. #endif
  2797. }
  2798. X509_VERIFY_PARAM_free(vpm);
  2799. release_engine(engine);
  2800. NCONF_free(conf); /* must not do as long as opt_... variables are used */
  2801. OSSL_CMP_log_close();
  2802. return ret == 0 ? EXIT_FAILURE : EXIT_SUCCESS; /* ret == -1 for -help */
  2803. }