cmp.c 100 KB

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