cmp.c 116 KB

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