cmp.c 100 KB

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