cmp.c 100 KB

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