cmp.c 101 KB

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