ca.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639
  1. /*
  2. * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/e_os.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include <sys/types.h>
  15. #include <openssl/conf.h>
  16. #include <openssl/bio.h>
  17. #include <openssl/err.h>
  18. #include <openssl/bn.h>
  19. #include <openssl/txt_db.h>
  20. #include <openssl/evp.h>
  21. #include <openssl/x509.h>
  22. #include <openssl/x509v3.h>
  23. #include <openssl/objects.h>
  24. #include <openssl/ocsp.h>
  25. #include <openssl/pem.h>
  26. #ifndef W_OK
  27. # ifdef OPENSSL_SYS_VMS
  28. # include <unistd.h>
  29. # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_TANDEM)
  30. # include <sys/file.h>
  31. # endif
  32. #endif
  33. #include "apps.h"
  34. #include "progs.h"
  35. #ifndef W_OK
  36. # define F_OK 0
  37. # define W_OK 2
  38. # define R_OK 4
  39. #endif
  40. #ifndef PATH_MAX
  41. # define PATH_MAX 4096
  42. #endif
  43. #define BASE_SECTION "ca"
  44. #define ENV_DEFAULT_CA "default_ca"
  45. #define STRING_MASK "string_mask"
  46. #define UTF8_IN "utf8"
  47. #define ENV_NEW_CERTS_DIR "new_certs_dir"
  48. #define ENV_CERTIFICATE "certificate"
  49. #define ENV_SERIAL "serial"
  50. #define ENV_RAND_SERIAL "rand_serial"
  51. #define ENV_CRLNUMBER "crlnumber"
  52. #define ENV_PRIVATE_KEY "private_key"
  53. #define ENV_DEFAULT_DAYS "default_days"
  54. #define ENV_DEFAULT_STARTDATE "default_startdate"
  55. #define ENV_DEFAULT_ENDDATE "default_enddate"
  56. #define ENV_DEFAULT_CRL_DAYS "default_crl_days"
  57. #define ENV_DEFAULT_CRL_HOURS "default_crl_hours"
  58. #define ENV_DEFAULT_MD "default_md"
  59. #define ENV_DEFAULT_EMAIL_DN "email_in_dn"
  60. #define ENV_PRESERVE "preserve"
  61. #define ENV_POLICY "policy"
  62. #define ENV_EXTENSIONS "x509_extensions"
  63. #define ENV_CRLEXT "crl_extensions"
  64. #define ENV_MSIE_HACK "msie_hack"
  65. #define ENV_NAMEOPT "name_opt"
  66. #define ENV_CERTOPT "cert_opt"
  67. #define ENV_EXTCOPY "copy_extensions"
  68. #define ENV_UNIQUE_SUBJECT "unique_subject"
  69. #define ENV_DATABASE "database"
  70. /* Additional revocation information types */
  71. typedef enum {
  72. REV_VALID = -1, /* Valid (not-revoked) status */
  73. REV_NONE = 0, /* No additional information */
  74. REV_CRL_REASON = 1, /* Value is CRL reason code */
  75. REV_HOLD = 2, /* Value is hold instruction */
  76. REV_KEY_COMPROMISE = 3, /* Value is cert key compromise time */
  77. REV_CA_COMPROMISE = 4 /* Value is CA key compromise time */
  78. } REVINFO_TYPE;
  79. static char *lookup_conf(const CONF *conf, const char *group, const char *tag);
  80. static int certify(X509 **xret, const char *infile, int informat,
  81. EVP_PKEY *pkey, X509 *x509,
  82. const char *dgst,
  83. STACK_OF(OPENSSL_STRING) *sigopts,
  84. STACK_OF(OPENSSL_STRING) *vfyopts,
  85. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  86. BIGNUM *serial, const char *subj, unsigned long chtype,
  87. int multirdn, int email_dn, const char *startdate,
  88. const char *enddate,
  89. long days, int batch, const char *ext_sect, CONF *conf,
  90. int verbose, unsigned long certopt, unsigned long nameopt,
  91. int default_op, int ext_copy, int selfsign, unsigned long dateopt);
  92. static int certify_cert(X509 **xret, const char *infile, int certformat,
  93. const char *passin, EVP_PKEY *pkey, X509 *x509,
  94. const char *dgst,
  95. STACK_OF(OPENSSL_STRING) *sigopts,
  96. STACK_OF(OPENSSL_STRING) *vfyopts,
  97. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  98. BIGNUM *serial, const char *subj, unsigned long chtype,
  99. int multirdn, int email_dn, const char *startdate,
  100. const char *enddate, long days, int batch, const char *ext_sect,
  101. CONF *conf, int verbose, unsigned long certopt,
  102. unsigned long nameopt, int default_op, int ext_copy, unsigned long dateopt);
  103. static int certify_spkac(X509 **xret, const char *infile, EVP_PKEY *pkey,
  104. X509 *x509, const char *dgst,
  105. STACK_OF(OPENSSL_STRING) *sigopts,
  106. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  107. BIGNUM *serial, const char *subj, unsigned long chtype,
  108. int multirdn, int email_dn, const char *startdate,
  109. const char *enddate, long days, const char *ext_sect, CONF *conf,
  110. int verbose, unsigned long certopt,
  111. unsigned long nameopt, int default_op, int ext_copy, unsigned long dateopt);
  112. static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
  113. const char *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
  114. STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,
  115. const char *subj, unsigned long chtype, int multirdn,
  116. int email_dn, const char *startdate, const char *enddate, long days,
  117. int batch, int verbose, X509_REQ *req, const char *ext_sect,
  118. CONF *conf, unsigned long certopt, unsigned long nameopt,
  119. int default_op, int ext_copy, int selfsign, unsigned long dateopt);
  120. static int get_certificate_status(const char *ser_status, CA_DB *db);
  121. static int check_time_format(const char *str);
  122. static int do_revoke(X509 *x509, CA_DB *db, REVINFO_TYPE rev_type,
  123. const char *extval);
  124. static char *make_revocation_str(REVINFO_TYPE rev_type, const char *rev_arg);
  125. static int make_revoked(X509_REVOKED *rev, const char *str);
  126. static int old_entry_print(const ASN1_OBJECT *obj, const ASN1_STRING *str);
  127. static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext);
  128. static CONF *extfile_conf = NULL;
  129. static int preserve = 0;
  130. static int msie_hack = 0;
  131. typedef enum OPTION_choice {
  132. OPT_COMMON,
  133. OPT_ENGINE, OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SUBJ, OPT_UTF8,
  134. OPT_CREATE_SERIAL, OPT_MULTIVALUE_RDN, OPT_STARTDATE, OPT_ENDDATE,
  135. OPT_DAYS, OPT_MD, OPT_POLICY, OPT_KEYFILE, OPT_KEYFORM, OPT_PASSIN,
  136. OPT_KEY, OPT_CERT, OPT_CERTFORM, OPT_SELFSIGN,
  137. OPT_IN, OPT_INFORM, OPT_OUT, OPT_DATEOPT, OPT_OUTDIR, OPT_VFYOPT,
  138. OPT_SIGOPT, OPT_NOTEXT, OPT_BATCH, OPT_PRESERVEDN, OPT_NOEMAILDN,
  139. OPT_GENCRL, OPT_MSIE_HACK, OPT_CRL_LASTUPDATE, OPT_CRL_NEXTUPDATE,
  140. OPT_CRLDAYS, OPT_CRLHOURS, OPT_CRLSEC, OPT_NOT_BEFORE, OPT_NOT_AFTER,
  141. OPT_INFILES, OPT_SS_CERT, OPT_SPKAC, OPT_REVOKE, OPT_VALID,
  142. OPT_EXTENSIONS, OPT_EXTFILE, OPT_STATUS, OPT_UPDATEDB, OPT_CRLEXTS,
  143. OPT_RAND_SERIAL, OPT_QUIET,
  144. OPT_R_ENUM, OPT_PROV_ENUM,
  145. /* Do not change the order here; see related case statements below */
  146. OPT_CRL_REASON, OPT_CRL_HOLD, OPT_CRL_COMPROMISE, OPT_CRL_CA_COMPROMISE
  147. } OPTION_CHOICE;
  148. const OPTIONS ca_options[] = {
  149. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [certreq...]\n"},
  150. OPT_SECTION("General"),
  151. {"help", OPT_HELP, '-', "Display this summary"},
  152. {"verbose", OPT_VERBOSE, '-', "Verbose output during processing"},
  153. {"quiet", OPT_QUIET, '-', "Terse output during processing"},
  154. {"outdir", OPT_OUTDIR, '/', "Where to put output cert"},
  155. {"in", OPT_IN, '<', "The input cert request(s)"},
  156. {"inform", OPT_INFORM, 'F',
  157. "CSR input format to use (PEM or DER; by default try PEM first)"},
  158. {"infiles", OPT_INFILES, '-', "The last argument, requests to process"},
  159. {"out", OPT_OUT, '>', "Where to put the output file(s)"},
  160. {"dateopt", OPT_DATEOPT, 's', "Datetime format used for printing. (rfc_822/iso_8601). Default is rfc_822."},
  161. {"notext", OPT_NOTEXT, '-', "Do not print the generated certificate"},
  162. {"batch", OPT_BATCH, '-', "Don't ask questions"},
  163. {"msie_hack", OPT_MSIE_HACK, '-',
  164. "msie modifications to handle all Universal Strings"},
  165. {"ss_cert", OPT_SS_CERT, '<', "File contains a self signed cert to sign"},
  166. {"spkac", OPT_SPKAC, '<',
  167. "File contains DN and signed public key and challenge"},
  168. #ifndef OPENSSL_NO_ENGINE
  169. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  170. #endif
  171. OPT_SECTION("Configuration"),
  172. {"config", OPT_CONFIG, 's', "A config file"},
  173. {"name", OPT_NAME, 's', "The particular CA definition to use"},
  174. {"section", OPT_NAME, 's', "An alias for -name"},
  175. {"policy", OPT_POLICY, 's', "The CA 'policy' to support"},
  176. OPT_SECTION("Certificate"),
  177. {"subj", OPT_SUBJ, 's', "Use arg instead of request's subject"},
  178. {"utf8", OPT_UTF8, '-', "Input characters are UTF8; default ASCII"},
  179. {"create_serial", OPT_CREATE_SERIAL, '-',
  180. "If reading serial fails, create a new random serial"},
  181. {"rand_serial", OPT_RAND_SERIAL, '-',
  182. "Always create a random serial; do not store it"},
  183. {"multivalue-rdn", OPT_MULTIVALUE_RDN, '-',
  184. "Deprecated; multi-valued RDNs support is always on."},
  185. {"startdate", OPT_STARTDATE, 's',
  186. "[CC]YYMMDDHHMMSSZ value for notBefore certificate field"},
  187. {"not_before", OPT_NOT_BEFORE, 's', "An alias for -startdate"},
  188. {"enddate", OPT_ENDDATE, 's',
  189. "[CC]YYMMDDHHMMSSZ value for notAfter certificate field, overrides -days"},
  190. {"not_after", OPT_NOT_AFTER, 's', "An alias for -enddate"},
  191. {"days", OPT_DAYS, 'p', "Number of days from today to certify the cert for"},
  192. {"extensions", OPT_EXTENSIONS, 's',
  193. "Extension section (override value in config file)"},
  194. {"extfile", OPT_EXTFILE, '<',
  195. "Configuration file with X509v3 extensions to add"},
  196. {"preserveDN", OPT_PRESERVEDN, '-', "Don't re-order the DN"},
  197. {"noemailDN", OPT_NOEMAILDN, '-', "Don't add the EMAIL field to the DN"},
  198. OPT_SECTION("Signing"),
  199. {"md", OPT_MD, 's', "Digest to use, such as sha256"},
  200. {"keyfile", OPT_KEYFILE, 's', "The CA private key"},
  201. {"keyform", OPT_KEYFORM, 'f',
  202. "Private key file format (ENGINE, other values ignored)"},
  203. {"passin", OPT_PASSIN, 's', "Key and cert input file pass phrase source"},
  204. {"key", OPT_KEY, 's',
  205. "Key to decrypt the private key or cert files if encrypted. Better use -passin"},
  206. {"cert", OPT_CERT, '<', "The CA cert"},
  207. {"certform", OPT_CERTFORM, 'F',
  208. "Certificate input format (DER/PEM/P12); has no effect"},
  209. {"selfsign", OPT_SELFSIGN, '-',
  210. "Sign a cert with the key associated with it"},
  211. {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"},
  212. {"vfyopt", OPT_VFYOPT, 's', "Verification parameter in n:v form"},
  213. OPT_SECTION("Revocation"),
  214. {"gencrl", OPT_GENCRL, '-', "Generate a new CRL"},
  215. {"valid", OPT_VALID, 's',
  216. "Add a Valid(not-revoked) DB entry about a cert (given in file)"},
  217. {"status", OPT_STATUS, 's', "Shows cert status given the serial number"},
  218. {"updatedb", OPT_UPDATEDB, '-', "Updates db for expired cert"},
  219. {"crlexts", OPT_CRLEXTS, 's',
  220. "CRL extension section (override value in config file)"},
  221. {"crl_reason", OPT_CRL_REASON, 's', "revocation reason"},
  222. {"crl_hold", OPT_CRL_HOLD, 's',
  223. "the hold instruction, an OID. Sets revocation reason to certificateHold"},
  224. {"crl_compromise", OPT_CRL_COMPROMISE, 's',
  225. "sets compromise time to val and the revocation reason to keyCompromise"},
  226. {"crl_CA_compromise", OPT_CRL_CA_COMPROMISE, 's',
  227. "sets compromise time to val and the revocation reason to CACompromise"},
  228. {"crl_lastupdate", OPT_CRL_LASTUPDATE, 's',
  229. "Sets the CRL lastUpdate time to val (YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ)"},
  230. {"crl_nextupdate", OPT_CRL_NEXTUPDATE, 's',
  231. "Sets the CRL nextUpdate time to val (YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ)"},
  232. {"crldays", OPT_CRLDAYS, 'p', "Days until the next CRL is due"},
  233. {"crlhours", OPT_CRLHOURS, 'p', "Hours until the next CRL is due"},
  234. {"crlsec", OPT_CRLSEC, 'p', "Seconds until the next CRL is due"},
  235. {"revoke", OPT_REVOKE, '<', "Revoke a cert (given in file)"},
  236. OPT_R_OPTIONS,
  237. OPT_PROV_OPTIONS,
  238. OPT_PARAMETERS(),
  239. {"certreq", 0, 0, "Certificate requests to be signed (optional)"},
  240. {NULL}
  241. };
  242. int ca_main(int argc, char **argv)
  243. {
  244. CONF *conf = NULL;
  245. ENGINE *e = NULL;
  246. BIGNUM *crlnumber = NULL, *serial = NULL;
  247. EVP_PKEY *pkey = NULL;
  248. BIO *in = NULL, *out = NULL, *Sout = NULL;
  249. ASN1_INTEGER *tmpser;
  250. CA_DB *db = NULL;
  251. DB_ATTR db_attr;
  252. STACK_OF(CONF_VALUE) *attribs = NULL;
  253. STACK_OF(OPENSSL_STRING) *sigopts = NULL, *vfyopts = NULL;
  254. STACK_OF(X509) *cert_sk = NULL;
  255. X509_CRL *crl = NULL;
  256. char *configfile = default_config_file, *section = NULL;
  257. char def_dgst[80] = "";
  258. char *dgst = NULL, *policy = NULL, *keyfile = NULL;
  259. char *certfile = NULL, *crl_ext = NULL, *crlnumberfile = NULL;
  260. int certformat = FORMAT_UNDEF, informat = FORMAT_UNDEF;
  261. unsigned long dateopt = ASN1_DTFLGS_RFC822;
  262. const char *infile = NULL, *spkac_file = NULL, *ss_cert_file = NULL;
  263. const char *extensions = NULL, *extfile = NULL, *passinarg = NULL;
  264. char *passin = NULL;
  265. char *outdir = NULL, *outfile = NULL, *rev_arg = NULL, *ser_status = NULL;
  266. const char *serialfile = NULL, *subj = NULL;
  267. char *prog, *startdate = NULL, *enddate = NULL;
  268. char *dbfile = NULL, *f;
  269. char new_cert[PATH_MAX];
  270. char tmp[10 + 1] = "\0";
  271. char *const *pp;
  272. const char *p;
  273. size_t outdirlen = 0;
  274. int create_ser = 0, free_passin = 0, total = 0, total_done = 0;
  275. int batch = 0, default_op = 1, doupdatedb = 0, ext_copy = EXT_COPY_NONE;
  276. int keyformat = FORMAT_UNDEF, multirdn = 1, notext = 0, output_der = 0;
  277. int ret = 1, email_dn = 1, req = 0, verbose = 0, gencrl = 0, dorevoke = 0;
  278. int rand_ser = 0, i, j, selfsign = 0, def_ret;
  279. char *crl_lastupdate = NULL, *crl_nextupdate = NULL;
  280. long crldays = 0, crlhours = 0, crlsec = 0, days = 0;
  281. unsigned long chtype = MBSTRING_ASC, certopt = 0;
  282. X509 *x509 = NULL, *x509p = NULL, *x = NULL;
  283. REVINFO_TYPE rev_type = REV_NONE;
  284. X509_REVOKED *r = NULL;
  285. OPTION_CHOICE o;
  286. prog = opt_init(argc, argv, ca_options);
  287. while ((o = opt_next()) != OPT_EOF) {
  288. switch (o) {
  289. case OPT_EOF:
  290. case OPT_ERR:
  291. opthelp:
  292. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  293. goto end;
  294. case OPT_HELP:
  295. opt_help(ca_options);
  296. ret = 0;
  297. goto end;
  298. case OPT_IN:
  299. req = 1;
  300. infile = opt_arg();
  301. break;
  302. case OPT_INFORM:
  303. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  304. goto opthelp;
  305. break;
  306. case OPT_OUT:
  307. outfile = opt_arg();
  308. break;
  309. case OPT_DATEOPT:
  310. if (!set_dateopt(&dateopt, opt_arg()))
  311. goto opthelp;
  312. break;
  313. case OPT_VERBOSE:
  314. verbose = 1;
  315. break;
  316. case OPT_QUIET:
  317. verbose = 0;
  318. break;
  319. case OPT_CONFIG:
  320. configfile = opt_arg();
  321. break;
  322. case OPT_NAME:
  323. section = opt_arg();
  324. break;
  325. case OPT_SUBJ:
  326. subj = opt_arg();
  327. /* preserve=1; */
  328. break;
  329. case OPT_UTF8:
  330. chtype = MBSTRING_UTF8;
  331. break;
  332. case OPT_RAND_SERIAL:
  333. rand_ser = 1;
  334. break;
  335. case OPT_CREATE_SERIAL:
  336. create_ser = 1;
  337. break;
  338. case OPT_MULTIVALUE_RDN:
  339. /* obsolete */
  340. break;
  341. case OPT_STARTDATE:
  342. case OPT_NOT_BEFORE:
  343. startdate = opt_arg();
  344. break;
  345. case OPT_ENDDATE:
  346. case OPT_NOT_AFTER:
  347. enddate = opt_arg();
  348. break;
  349. case OPT_DAYS:
  350. days = atoi(opt_arg());
  351. break;
  352. case OPT_MD:
  353. dgst = opt_arg();
  354. break;
  355. case OPT_POLICY:
  356. policy = opt_arg();
  357. break;
  358. case OPT_KEYFILE:
  359. keyfile = opt_arg();
  360. break;
  361. case OPT_KEYFORM:
  362. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
  363. goto opthelp;
  364. break;
  365. case OPT_PASSIN:
  366. passinarg = opt_arg();
  367. break;
  368. case OPT_R_CASES:
  369. if (!opt_rand(o))
  370. goto end;
  371. break;
  372. case OPT_PROV_CASES:
  373. if (!opt_provider(o))
  374. goto end;
  375. break;
  376. case OPT_KEY:
  377. passin = opt_arg();
  378. break;
  379. case OPT_CERT:
  380. certfile = opt_arg();
  381. break;
  382. case OPT_CERTFORM:
  383. if (!opt_format(opt_arg(), OPT_FMT_ANY, &certformat))
  384. goto opthelp;
  385. break;
  386. case OPT_SELFSIGN:
  387. selfsign = 1;
  388. break;
  389. case OPT_OUTDIR:
  390. outdir = opt_arg();
  391. break;
  392. case OPT_SIGOPT:
  393. if (sigopts == NULL)
  394. sigopts = sk_OPENSSL_STRING_new_null();
  395. if (sigopts == NULL || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
  396. goto end;
  397. break;
  398. case OPT_VFYOPT:
  399. if (vfyopts == NULL)
  400. vfyopts = sk_OPENSSL_STRING_new_null();
  401. if (vfyopts == NULL || !sk_OPENSSL_STRING_push(vfyopts, opt_arg()))
  402. goto end;
  403. break;
  404. case OPT_NOTEXT:
  405. notext = 1;
  406. break;
  407. case OPT_BATCH:
  408. batch = 1;
  409. break;
  410. case OPT_PRESERVEDN:
  411. preserve = 1;
  412. break;
  413. case OPT_NOEMAILDN:
  414. email_dn = 0;
  415. break;
  416. case OPT_GENCRL:
  417. gencrl = 1;
  418. break;
  419. case OPT_MSIE_HACK:
  420. msie_hack = 1;
  421. break;
  422. case OPT_CRL_LASTUPDATE:
  423. crl_lastupdate = opt_arg();
  424. break;
  425. case OPT_CRL_NEXTUPDATE:
  426. crl_nextupdate = opt_arg();
  427. break;
  428. case OPT_CRLDAYS:
  429. crldays = atol(opt_arg());
  430. break;
  431. case OPT_CRLHOURS:
  432. crlhours = atol(opt_arg());
  433. break;
  434. case OPT_CRLSEC:
  435. crlsec = atol(opt_arg());
  436. break;
  437. case OPT_INFILES:
  438. req = 1;
  439. goto end_of_options;
  440. case OPT_SS_CERT:
  441. ss_cert_file = opt_arg();
  442. req = 1;
  443. break;
  444. case OPT_SPKAC:
  445. spkac_file = opt_arg();
  446. req = 1;
  447. break;
  448. case OPT_REVOKE:
  449. infile = opt_arg();
  450. dorevoke = 1;
  451. break;
  452. case OPT_VALID:
  453. infile = opt_arg();
  454. dorevoke = 2;
  455. break;
  456. case OPT_EXTENSIONS:
  457. extensions = opt_arg();
  458. break;
  459. case OPT_EXTFILE:
  460. extfile = opt_arg();
  461. break;
  462. case OPT_STATUS:
  463. ser_status = opt_arg();
  464. break;
  465. case OPT_UPDATEDB:
  466. doupdatedb = 1;
  467. break;
  468. case OPT_CRLEXTS:
  469. crl_ext = opt_arg();
  470. break;
  471. case OPT_CRL_REASON: /* := REV_CRL_REASON */
  472. case OPT_CRL_HOLD:
  473. case OPT_CRL_COMPROMISE:
  474. case OPT_CRL_CA_COMPROMISE:
  475. rev_arg = opt_arg();
  476. rev_type = (o - OPT_CRL_REASON) + REV_CRL_REASON;
  477. break;
  478. case OPT_ENGINE:
  479. e = setup_engine(opt_arg(), 0);
  480. break;
  481. }
  482. }
  483. end_of_options:
  484. /* Remaining args are files to certify. */
  485. argc = opt_num_rest();
  486. argv = opt_rest();
  487. if ((conf = app_load_config_verbose(configfile, 1)) == NULL)
  488. goto end;
  489. if (configfile != default_config_file && !app_load_modules(conf))
  490. goto end;
  491. /* Lets get the config section we are using */
  492. if (section == NULL
  493. && (section = lookup_conf(conf, BASE_SECTION, ENV_DEFAULT_CA)) == NULL)
  494. goto end;
  495. p = app_conf_try_string(conf, NULL, "oid_file");
  496. if (p != NULL) {
  497. BIO *oid_bio = BIO_new_file(p, "r");
  498. if (oid_bio == NULL) {
  499. ERR_clear_error();
  500. } else {
  501. OBJ_create_objects(oid_bio);
  502. BIO_free(oid_bio);
  503. }
  504. }
  505. if (!add_oid_section(conf))
  506. goto end;
  507. app_RAND_load_conf(conf, BASE_SECTION);
  508. if (!app_RAND_load())
  509. goto end;
  510. f = app_conf_try_string(conf, section, STRING_MASK);
  511. if (f != NULL && !ASN1_STRING_set_default_mask_asc(f)) {
  512. BIO_printf(bio_err, "Invalid global string mask setting %s\n", f);
  513. goto end;
  514. }
  515. if (chtype != MBSTRING_UTF8) {
  516. f = app_conf_try_string(conf, section, UTF8_IN);
  517. if (f != NULL && strcmp(f, "yes") == 0)
  518. chtype = MBSTRING_UTF8;
  519. }
  520. db_attr.unique_subject = 1;
  521. p = app_conf_try_string(conf, section, ENV_UNIQUE_SUBJECT);
  522. if (p != NULL)
  523. db_attr.unique_subject = parse_yesno(p, 1);
  524. /*****************************************************************/
  525. /* report status of cert with serial number given on command line */
  526. if (ser_status) {
  527. dbfile = lookup_conf(conf, section, ENV_DATABASE);
  528. if (dbfile == NULL)
  529. goto end;
  530. db = load_index(dbfile, &db_attr);
  531. if (db == NULL) {
  532. BIO_printf(bio_err, "Problem with index file: %s (could not load/parse file)\n", dbfile);
  533. goto end;
  534. }
  535. if (index_index(db) <= 0)
  536. goto end;
  537. if (get_certificate_status(ser_status, db) != 1)
  538. BIO_printf(bio_err, "Error verifying serial %s!\n", ser_status);
  539. goto end;
  540. }
  541. /*****************************************************************/
  542. /* we definitely need a private key, so let's get it */
  543. if (keyfile == NULL
  544. && (keyfile = lookup_conf(conf, section, ENV_PRIVATE_KEY)) == NULL)
  545. goto end;
  546. if (passin == NULL) {
  547. free_passin = 1;
  548. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  549. BIO_printf(bio_err, "Error getting password\n");
  550. goto end;
  551. }
  552. }
  553. pkey = load_key(keyfile, keyformat, 0, passin, e, "CA private key");
  554. cleanse(passin);
  555. if (pkey == NULL)
  556. /* load_key() has already printed an appropriate message */
  557. goto end;
  558. /*****************************************************************/
  559. /* we need a certificate */
  560. if (!selfsign || spkac_file || ss_cert_file || gencrl) {
  561. if (certfile == NULL
  562. && (certfile = lookup_conf(conf, section, ENV_CERTIFICATE)) == NULL)
  563. goto end;
  564. x509 = load_cert_pass(certfile, certformat, 1, passin, "CA certificate");
  565. if (x509 == NULL)
  566. goto end;
  567. if (!X509_check_private_key(x509, pkey)) {
  568. BIO_printf(bio_err,
  569. "CA certificate and CA private key do not match\n");
  570. goto end;
  571. }
  572. }
  573. if (!selfsign)
  574. x509p = x509;
  575. f = app_conf_try_string(conf, BASE_SECTION, ENV_PRESERVE);
  576. if (f != NULL && (*f == 'y' || *f == 'Y'))
  577. preserve = 1;
  578. f = app_conf_try_string(conf, BASE_SECTION, ENV_MSIE_HACK);
  579. if (f != NULL && (*f == 'y' || *f == 'Y'))
  580. msie_hack = 1;
  581. f = app_conf_try_string(conf, section, ENV_NAMEOPT);
  582. if (f != NULL) {
  583. if (!set_nameopt(f)) {
  584. BIO_printf(bio_err, "Invalid name options: \"%s\"\n", f);
  585. goto end;
  586. }
  587. default_op = 0;
  588. }
  589. f = app_conf_try_string(conf, section, ENV_CERTOPT);
  590. if (f != NULL) {
  591. if (!set_cert_ex(&certopt, f)) {
  592. BIO_printf(bio_err, "Invalid certificate options: \"%s\"\n", f);
  593. goto end;
  594. }
  595. default_op = 0;
  596. }
  597. f = app_conf_try_string(conf, section, ENV_EXTCOPY);
  598. if (f != NULL) {
  599. if (!set_ext_copy(&ext_copy, f)) {
  600. BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n", f);
  601. goto end;
  602. }
  603. }
  604. /*****************************************************************/
  605. /* lookup where to write new certificates */
  606. if ((outdir == NULL) && (req)) {
  607. outdir = NCONF_get_string(conf, section, ENV_NEW_CERTS_DIR);
  608. if (outdir == NULL) {
  609. BIO_printf(bio_err,
  610. "there needs to be defined a directory for new certificate to be placed in\n");
  611. goto end;
  612. }
  613. #ifndef OPENSSL_SYS_VMS
  614. /*
  615. * outdir is a directory spec, but access() for VMS demands a
  616. * filename. We could use the DEC C routine to convert the
  617. * directory syntax to Unix, and give that to app_isdir,
  618. * but for now the fopen will catch the error if it's not a
  619. * directory
  620. */
  621. if (app_isdir(outdir) <= 0) {
  622. BIO_printf(bio_err, "%s: %s is not a directory\n", prog, outdir);
  623. perror(outdir);
  624. goto end;
  625. }
  626. #endif
  627. }
  628. /*****************************************************************/
  629. /* we need to load the database file */
  630. dbfile = lookup_conf(conf, section, ENV_DATABASE);
  631. if (dbfile == NULL)
  632. goto end;
  633. db = load_index(dbfile, &db_attr);
  634. if (db == NULL) {
  635. BIO_printf(bio_err, "Problem with index file: %s (could not load/parse file)\n", dbfile);
  636. goto end;
  637. }
  638. /* Lets check some fields */
  639. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  640. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  641. if ((pp[DB_type][0] != DB_TYPE_REV) && (pp[DB_rev_date][0] != '\0')) {
  642. BIO_printf(bio_err,
  643. "entry %d: not revoked yet, but has a revocation date\n",
  644. i + 1);
  645. goto end;
  646. }
  647. if ((pp[DB_type][0] == DB_TYPE_REV) &&
  648. !make_revoked(NULL, pp[DB_rev_date])) {
  649. BIO_printf(bio_err, " in entry %d\n", i + 1);
  650. goto end;
  651. }
  652. if (!check_time_format((char *)pp[DB_exp_date])) {
  653. BIO_printf(bio_err, "entry %d: invalid expiry date\n", i + 1);
  654. goto end;
  655. }
  656. p = pp[DB_serial];
  657. j = strlen(p);
  658. if (*p == '-') {
  659. p++;
  660. j--;
  661. }
  662. if ((j & 1) || (j < 2)) {
  663. BIO_printf(bio_err, "entry %d: bad serial number length (%d)\n",
  664. i + 1, j);
  665. goto end;
  666. }
  667. for ( ; *p; p++) {
  668. if (!isxdigit(_UC(*p))) {
  669. BIO_printf(bio_err,
  670. "entry %d: bad char 0%o '%c' in serial number\n",
  671. i + 1, *p, *p);
  672. goto end;
  673. }
  674. }
  675. }
  676. if (verbose) {
  677. TXT_DB_write(bio_out, db->db);
  678. BIO_printf(bio_err, "%d entries loaded from the database\n",
  679. sk_OPENSSL_PSTRING_num(db->db->data));
  680. BIO_printf(bio_err, "generating index\n");
  681. }
  682. if (index_index(db) <= 0)
  683. goto end;
  684. /*****************************************************************/
  685. /* Update the db file for expired certificates */
  686. if (doupdatedb) {
  687. if (verbose)
  688. BIO_printf(bio_err, "Updating %s ...\n", dbfile);
  689. i = do_updatedb(db, NULL);
  690. if (i == -1) {
  691. BIO_printf(bio_err, "Malloc failure\n");
  692. goto end;
  693. } else if (i == 0) {
  694. if (verbose)
  695. BIO_printf(bio_err, "No entries found to mark expired\n");
  696. } else {
  697. if (!save_index(dbfile, "new", db))
  698. goto end;
  699. if (!rotate_index(dbfile, "new", "old"))
  700. goto end;
  701. if (verbose)
  702. BIO_printf(bio_err, "Done. %d entries marked as expired\n", i);
  703. }
  704. }
  705. /*****************************************************************/
  706. /* Read extensions config file */
  707. if (extfile) {
  708. if ((extfile_conf = app_load_config(extfile)) == NULL) {
  709. ret = 1;
  710. goto end;
  711. }
  712. if (verbose)
  713. BIO_printf(bio_err, "Successfully loaded extensions file %s\n",
  714. extfile);
  715. /* We can have sections in the ext file */
  716. if (extensions == NULL) {
  717. extensions =
  718. app_conf_try_string(extfile_conf, "default", "extensions");
  719. if (extensions == NULL)
  720. extensions = "default";
  721. }
  722. }
  723. /*****************************************************************/
  724. if (req || gencrl) {
  725. if (spkac_file != NULL && outfile != NULL) {
  726. output_der = 1;
  727. batch = 1;
  728. }
  729. }
  730. def_ret = EVP_PKEY_get_default_digest_name(pkey, def_dgst, sizeof(def_dgst));
  731. /*
  732. * EVP_PKEY_get_default_digest_name() returns 2 if the digest is
  733. * mandatory for this algorithm.
  734. *
  735. * That call may give back the name "UNDEF", which has these meanings:
  736. *
  737. * when def_ret == 2: the user MUST leave the digest unspecified
  738. * when def_ret == 1: the user MAY leave the digest unspecified
  739. */
  740. if (def_ret == 2 && strcmp(def_dgst, "UNDEF") == 0) {
  741. dgst = NULL;
  742. } else if (dgst == NULL
  743. && (dgst = lookup_conf(conf, section, ENV_DEFAULT_MD)) == NULL
  744. && strcmp(def_dgst, "UNDEF") != 0) {
  745. goto end;
  746. } else {
  747. if (strcmp(dgst, "default") == 0 || strcmp(def_dgst, "UNDEF") == 0) {
  748. if (def_ret <= 0) {
  749. BIO_puts(bio_err, "no default digest\n");
  750. goto end;
  751. }
  752. dgst = def_dgst;
  753. }
  754. }
  755. if (req) {
  756. if (email_dn == 1) {
  757. char *tmp_email_dn = NULL;
  758. tmp_email_dn =
  759. app_conf_try_string(conf, section, ENV_DEFAULT_EMAIL_DN);
  760. if (tmp_email_dn != NULL && strcmp(tmp_email_dn, "no") == 0)
  761. email_dn = 0;
  762. }
  763. if (verbose)
  764. BIO_printf(bio_err, "message digest is %s\n", dgst);
  765. if (policy == NULL
  766. && (policy = lookup_conf(conf, section, ENV_POLICY)) == NULL)
  767. goto end;
  768. if (verbose)
  769. BIO_printf(bio_err, "policy is %s\n", policy);
  770. if (app_conf_try_string(conf, section, ENV_RAND_SERIAL) != NULL) {
  771. rand_ser = 1;
  772. } else {
  773. serialfile = lookup_conf(conf, section, ENV_SERIAL);
  774. if (serialfile == NULL)
  775. goto end;
  776. }
  777. if (extfile_conf != NULL) {
  778. /* Check syntax of extfile */
  779. X509V3_CTX ctx;
  780. X509V3_set_ctx_test(&ctx);
  781. X509V3_set_nconf(&ctx, extfile_conf);
  782. if (!X509V3_EXT_add_nconf(extfile_conf, &ctx, extensions, NULL)) {
  783. BIO_printf(bio_err,
  784. "Error checking certificate extensions from extfile section %s\n",
  785. extensions);
  786. ret = 1;
  787. goto end;
  788. }
  789. } else {
  790. /*
  791. * no '-extfile' option, so we look for extensions in the main
  792. * configuration file
  793. */
  794. if (extensions == NULL)
  795. extensions = app_conf_try_string(conf, section, ENV_EXTENSIONS);
  796. if (extensions != NULL) {
  797. /* Check syntax of config file section */
  798. X509V3_CTX ctx;
  799. X509V3_set_ctx_test(&ctx);
  800. X509V3_set_nconf(&ctx, conf);
  801. if (!X509V3_EXT_add_nconf(conf, &ctx, extensions, NULL)) {
  802. BIO_printf(bio_err,
  803. "Error checking certificate extension config section %s\n",
  804. extensions);
  805. ret = 1;
  806. goto end;
  807. }
  808. }
  809. }
  810. if (startdate == NULL)
  811. startdate =
  812. app_conf_try_string(conf, section, ENV_DEFAULT_STARTDATE);
  813. if (enddate == NULL)
  814. enddate = app_conf_try_string(conf, section, ENV_DEFAULT_ENDDATE);
  815. if (days == 0) {
  816. if (!app_conf_try_number(conf, section, ENV_DEFAULT_DAYS, &days))
  817. days = 0;
  818. }
  819. if (enddate == NULL && days == 0) {
  820. BIO_printf(bio_err, "cannot lookup how many days to certify for\n");
  821. goto end;
  822. }
  823. if (days != 0 && enddate != NULL)
  824. BIO_printf(bio_err,
  825. "Warning: -enddate or -not_after option overriding -days option\n");
  826. if (rand_ser) {
  827. if ((serial = BN_new()) == NULL || !rand_serial(serial, NULL)) {
  828. BIO_printf(bio_err, "error generating serial number\n");
  829. goto end;
  830. }
  831. } else {
  832. serial = load_serial(serialfile, NULL, create_ser, NULL);
  833. if (serial == NULL) {
  834. BIO_printf(bio_err, "error while loading serial number\n");
  835. goto end;
  836. }
  837. if (verbose) {
  838. if (BN_is_zero(serial)) {
  839. BIO_printf(bio_err, "next serial number is 00\n");
  840. } else {
  841. if ((f = BN_bn2hex(serial)) == NULL)
  842. goto end;
  843. BIO_printf(bio_err, "next serial number is %s\n", f);
  844. OPENSSL_free(f);
  845. }
  846. }
  847. }
  848. if ((attribs = NCONF_get_section(conf, policy)) == NULL) {
  849. BIO_printf(bio_err, "unable to find 'section' for %s\n", policy);
  850. goto end;
  851. }
  852. if ((cert_sk = sk_X509_new_null()) == NULL) {
  853. BIO_printf(bio_err, "Memory allocation failure\n");
  854. goto end;
  855. }
  856. if (spkac_file != NULL) {
  857. total++;
  858. j = certify_spkac(&x, spkac_file, pkey, x509, dgst, sigopts,
  859. attribs, db, serial, subj, chtype, multirdn,
  860. email_dn, startdate, enddate, days, extensions,
  861. conf, verbose, certopt, get_nameopt(), default_op,
  862. ext_copy, dateopt);
  863. if (j < 0)
  864. goto end;
  865. if (j > 0) {
  866. total_done++;
  867. BIO_printf(bio_err, "\n");
  868. if (!BN_add_word(serial, 1))
  869. goto end;
  870. if (!sk_X509_push(cert_sk, x)) {
  871. BIO_printf(bio_err, "Memory allocation failure\n");
  872. goto end;
  873. }
  874. }
  875. }
  876. if (ss_cert_file != NULL) {
  877. total++;
  878. j = certify_cert(&x, ss_cert_file, certformat, passin, pkey,
  879. x509, dgst, sigopts, vfyopts, attribs,
  880. db, serial, subj, chtype, multirdn, email_dn,
  881. startdate, enddate, days, batch, extensions,
  882. conf, verbose, certopt, get_nameopt(), default_op,
  883. ext_copy, dateopt);
  884. if (j < 0)
  885. goto end;
  886. if (j > 0) {
  887. total_done++;
  888. BIO_printf(bio_err, "\n");
  889. if (!BN_add_word(serial, 1))
  890. goto end;
  891. if (!sk_X509_push(cert_sk, x)) {
  892. BIO_printf(bio_err, "Memory allocation failure\n");
  893. goto end;
  894. }
  895. }
  896. }
  897. if (infile != NULL) {
  898. total++;
  899. j = certify(&x, infile, informat, pkey, x509p, dgst,
  900. sigopts, vfyopts, attribs, db,
  901. serial, subj, chtype, multirdn, email_dn, startdate,
  902. enddate, days, batch, extensions, conf, verbose,
  903. certopt, get_nameopt(), default_op, ext_copy, selfsign, dateopt);
  904. if (j < 0)
  905. goto end;
  906. if (j > 0) {
  907. total_done++;
  908. BIO_printf(bio_err, "\n");
  909. if (!BN_add_word(serial, 1))
  910. goto end;
  911. if (!sk_X509_push(cert_sk, x)) {
  912. BIO_printf(bio_err, "Memory allocation failure\n");
  913. goto end;
  914. }
  915. }
  916. }
  917. for (i = 0; i < argc; i++) {
  918. total++;
  919. j = certify(&x, argv[i], informat, pkey, x509p, dgst,
  920. sigopts, vfyopts,
  921. attribs, db,
  922. serial, subj, chtype, multirdn, email_dn, startdate,
  923. enddate, days, batch, extensions, conf, verbose,
  924. certopt, get_nameopt(), default_op, ext_copy, selfsign, dateopt);
  925. if (j < 0)
  926. goto end;
  927. if (j > 0) {
  928. total_done++;
  929. BIO_printf(bio_err, "\n");
  930. if (!BN_add_word(serial, 1)) {
  931. X509_free(x);
  932. goto end;
  933. }
  934. if (!sk_X509_push(cert_sk, x)) {
  935. BIO_printf(bio_err, "Memory allocation failure\n");
  936. X509_free(x);
  937. goto end;
  938. }
  939. }
  940. }
  941. /*
  942. * we have a stack of newly certified certificates and a database
  943. * and serial number that need updating
  944. */
  945. if (sk_X509_num(cert_sk) > 0) {
  946. if (!batch) {
  947. BIO_printf(bio_err,
  948. "\n%d out of %d certificate requests certified, commit? [y/n]",
  949. total_done, total);
  950. (void)BIO_flush(bio_err);
  951. tmp[0] = '\0';
  952. if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
  953. BIO_printf(bio_err, "CERTIFICATION CANCELED: I/O error\n");
  954. ret = 0;
  955. goto end;
  956. }
  957. if (tmp[0] != 'y' && tmp[0] != 'Y') {
  958. BIO_printf(bio_err, "CERTIFICATION CANCELED\n");
  959. ret = 0;
  960. goto end;
  961. }
  962. }
  963. BIO_printf(bio_err, "Write out database with %d new entries\n",
  964. sk_X509_num(cert_sk));
  965. if (serialfile != NULL
  966. && !save_serial(serialfile, "new", serial, NULL))
  967. goto end;
  968. if (!save_index(dbfile, "new", db))
  969. goto end;
  970. }
  971. outdirlen = OPENSSL_strlcpy(new_cert, outdir, sizeof(new_cert));
  972. #ifndef OPENSSL_SYS_VMS
  973. outdirlen = OPENSSL_strlcat(new_cert, "/", sizeof(new_cert));
  974. #endif
  975. if (verbose)
  976. BIO_printf(bio_err, "writing new certificates\n");
  977. for (i = 0; i < sk_X509_num(cert_sk); i++) {
  978. BIO *Cout = NULL;
  979. X509 *xi = sk_X509_value(cert_sk, i);
  980. const ASN1_INTEGER *serialNumber = X509_get0_serialNumber(xi);
  981. const unsigned char *psn = ASN1_STRING_get0_data(serialNumber);
  982. const int snl = ASN1_STRING_length(serialNumber);
  983. const int filen_len = 2 * (snl > 0 ? snl : 1) + sizeof(".pem");
  984. char *n = new_cert + outdirlen;
  985. if (outdirlen + filen_len > PATH_MAX) {
  986. BIO_printf(bio_err, "certificate file name too long\n");
  987. goto end;
  988. }
  989. if (snl > 0) {
  990. static const char HEX_DIGITS[] = "0123456789ABCDEF";
  991. for (j = 0; j < snl; j++, psn++) {
  992. *n++ = HEX_DIGITS[*psn >> 4];
  993. *n++ = HEX_DIGITS[*psn & 0x0F];
  994. }
  995. } else {
  996. *(n++) = '0';
  997. *(n++) = '0';
  998. }
  999. *(n++) = '.';
  1000. *(n++) = 'p';
  1001. *(n++) = 'e';
  1002. *(n++) = 'm';
  1003. *n = '\0'; /* closing new_cert */
  1004. if (verbose)
  1005. BIO_printf(bio_err, "writing %s\n", new_cert);
  1006. Sout = bio_open_default(outfile, 'w',
  1007. output_der ? FORMAT_ASN1 : FORMAT_TEXT);
  1008. if (Sout == NULL)
  1009. goto end;
  1010. Cout = BIO_new_file(new_cert, "w");
  1011. if (Cout == NULL) {
  1012. perror(new_cert);
  1013. goto end;
  1014. }
  1015. write_new_certificate(Cout, xi, 0, notext);
  1016. write_new_certificate(Sout, xi, output_der, notext);
  1017. BIO_free_all(Cout);
  1018. BIO_free_all(Sout);
  1019. Sout = NULL;
  1020. }
  1021. if (sk_X509_num(cert_sk)) {
  1022. /* Rename the database and the serial file */
  1023. if (serialfile != NULL
  1024. && !rotate_serial(serialfile, "new", "old"))
  1025. goto end;
  1026. if (!rotate_index(dbfile, "new", "old"))
  1027. goto end;
  1028. BIO_printf(bio_err, "Database updated\n");
  1029. }
  1030. }
  1031. /*****************************************************************/
  1032. if (gencrl) {
  1033. int crl_v2 = 0;
  1034. if (crl_ext == NULL)
  1035. crl_ext = app_conf_try_string(conf, section, ENV_CRLEXT);
  1036. if (crl_ext != NULL) {
  1037. /* Check syntax of file */
  1038. X509V3_CTX ctx;
  1039. X509V3_set_ctx_test(&ctx);
  1040. X509V3_set_nconf(&ctx, conf);
  1041. if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL)) {
  1042. BIO_printf(bio_err,
  1043. "Error checking CRL extension section %s\n", crl_ext);
  1044. ret = 1;
  1045. goto end;
  1046. }
  1047. }
  1048. crlnumberfile = app_conf_try_string(conf, section, ENV_CRLNUMBER);
  1049. if (crlnumberfile != NULL) {
  1050. if ((crlnumber = load_serial(crlnumberfile, NULL, 0, NULL))
  1051. == NULL) {
  1052. BIO_printf(bio_err, "error while loading CRL number\n");
  1053. goto end;
  1054. }
  1055. }
  1056. if (!crldays && !crlhours && !crlsec) {
  1057. if (!app_conf_try_number(conf, section,
  1058. ENV_DEFAULT_CRL_DAYS, &crldays))
  1059. crldays = 0;
  1060. if (!app_conf_try_number(conf, section,
  1061. ENV_DEFAULT_CRL_HOURS, &crlhours))
  1062. crlhours = 0;
  1063. }
  1064. if ((crl_nextupdate == NULL) &&
  1065. (crldays == 0) && (crlhours == 0) && (crlsec == 0)) {
  1066. BIO_printf(bio_err,
  1067. "cannot lookup how long until the next CRL is issued\n");
  1068. goto end;
  1069. }
  1070. if (verbose)
  1071. BIO_printf(bio_err, "making CRL\n");
  1072. if ((crl = X509_CRL_new_ex(app_get0_libctx(), app_get0_propq())) == NULL)
  1073. goto end;
  1074. if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509)))
  1075. goto end;
  1076. if (!set_crl_lastupdate(crl, crl_lastupdate)) {
  1077. BIO_puts(bio_err, "error setting CRL lastUpdate\n");
  1078. ret = 1;
  1079. goto end;
  1080. }
  1081. if (!set_crl_nextupdate(crl, crl_nextupdate,
  1082. crldays, crlhours, crlsec)) {
  1083. BIO_puts(bio_err, "error setting CRL nextUpdate\n");
  1084. ret = 1;
  1085. goto end;
  1086. }
  1087. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  1088. pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
  1089. if (pp[DB_type][0] == DB_TYPE_REV) {
  1090. if ((r = X509_REVOKED_new()) == NULL)
  1091. goto end;
  1092. j = make_revoked(r, pp[DB_rev_date]);
  1093. if (!j)
  1094. goto end;
  1095. if (j == 2)
  1096. crl_v2 = 1;
  1097. if (!BN_hex2bn(&serial, pp[DB_serial]))
  1098. goto end;
  1099. tmpser = BN_to_ASN1_INTEGER(serial, NULL);
  1100. BN_free(serial);
  1101. serial = NULL;
  1102. if (!tmpser)
  1103. goto end;
  1104. X509_REVOKED_set_serialNumber(r, tmpser);
  1105. ASN1_INTEGER_free(tmpser);
  1106. X509_CRL_add0_revoked(crl, r);
  1107. }
  1108. }
  1109. /*
  1110. * sort the data so it will be written in serial number order
  1111. */
  1112. X509_CRL_sort(crl);
  1113. /* we now have a CRL */
  1114. if (verbose)
  1115. BIO_printf(bio_err, "signing CRL\n");
  1116. /* Add any extensions asked for */
  1117. if (crl_ext != NULL || crlnumberfile != NULL) {
  1118. X509V3_CTX crlctx;
  1119. X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
  1120. X509V3_set_nconf(&crlctx, conf);
  1121. if (crl_ext != NULL)
  1122. if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx, crl_ext, crl)) {
  1123. BIO_printf(bio_err,
  1124. "Error adding CRL extensions from section %s\n", crl_ext);
  1125. goto end;
  1126. }
  1127. if (crlnumberfile != NULL) {
  1128. tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);
  1129. if (!tmpser)
  1130. goto end;
  1131. X509_CRL_add1_ext_i2d(crl, NID_crl_number, tmpser, 0, 0);
  1132. ASN1_INTEGER_free(tmpser);
  1133. crl_v2 = 1;
  1134. if (!BN_add_word(crlnumber, 1))
  1135. goto end;
  1136. }
  1137. }
  1138. if (crl_ext != NULL || crl_v2) {
  1139. if (!X509_CRL_set_version(crl, X509_CRL_VERSION_2))
  1140. goto end;
  1141. }
  1142. /* we have a CRL number that need updating */
  1143. if (crlnumberfile != NULL
  1144. && !save_serial(crlnumberfile, "new", crlnumber, NULL))
  1145. goto end;
  1146. BN_free(crlnumber);
  1147. crlnumber = NULL;
  1148. if (!do_X509_CRL_sign(crl, pkey, dgst, sigopts))
  1149. goto end;
  1150. Sout = bio_open_default(outfile, 'w',
  1151. output_der ? FORMAT_ASN1 : FORMAT_TEXT);
  1152. if (Sout == NULL)
  1153. goto end;
  1154. PEM_write_bio_X509_CRL(Sout, crl);
  1155. /* Rename the crlnumber file */
  1156. if (crlnumberfile != NULL
  1157. && !rotate_serial(crlnumberfile, "new", "old"))
  1158. goto end;
  1159. }
  1160. /*****************************************************************/
  1161. if (dorevoke) {
  1162. if (infile == NULL) {
  1163. BIO_printf(bio_err, "no input files\n");
  1164. goto end;
  1165. } else {
  1166. X509 *revcert;
  1167. revcert = load_cert_pass(infile, informat, 1, passin,
  1168. "certificate to be revoked");
  1169. if (revcert == NULL)
  1170. goto end;
  1171. if (dorevoke == 2)
  1172. rev_type = REV_VALID;
  1173. j = do_revoke(revcert, db, rev_type, rev_arg);
  1174. if (j <= 0)
  1175. goto end;
  1176. X509_free(revcert);
  1177. if (!save_index(dbfile, "new", db))
  1178. goto end;
  1179. if (!rotate_index(dbfile, "new", "old"))
  1180. goto end;
  1181. BIO_printf(bio_err, "Database updated\n");
  1182. }
  1183. }
  1184. ret = 0;
  1185. end:
  1186. if (ret)
  1187. ERR_print_errors(bio_err);
  1188. BIO_free_all(Sout);
  1189. BIO_free_all(out);
  1190. BIO_free_all(in);
  1191. OSSL_STACK_OF_X509_free(cert_sk);
  1192. cleanse(passin);
  1193. if (free_passin)
  1194. OPENSSL_free(passin);
  1195. BN_free(serial);
  1196. BN_free(crlnumber);
  1197. free_index(db);
  1198. sk_OPENSSL_STRING_free(sigopts);
  1199. sk_OPENSSL_STRING_free(vfyopts);
  1200. EVP_PKEY_free(pkey);
  1201. X509_free(x509);
  1202. X509_CRL_free(crl);
  1203. NCONF_free(conf);
  1204. NCONF_free(extfile_conf);
  1205. release_engine(e);
  1206. return ret;
  1207. }
  1208. static char *lookup_conf(const CONF *conf, const char *section, const char *tag)
  1209. {
  1210. char *entry = NCONF_get_string(conf, section, tag);
  1211. if (entry == NULL)
  1212. BIO_printf(bio_err, "variable lookup failed for %s::%s\n", section, tag);
  1213. return entry;
  1214. }
  1215. static int certify(X509 **xret, const char *infile, int informat,
  1216. EVP_PKEY *pkey, X509 *x509,
  1217. const char *dgst,
  1218. STACK_OF(OPENSSL_STRING) *sigopts,
  1219. STACK_OF(OPENSSL_STRING) *vfyopts,
  1220. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  1221. BIGNUM *serial, const char *subj, unsigned long chtype,
  1222. int multirdn, int email_dn, const char *startdate,
  1223. const char *enddate,
  1224. long days, int batch, const char *ext_sect, CONF *lconf,
  1225. int verbose, unsigned long certopt, unsigned long nameopt,
  1226. int default_op, int ext_copy, int selfsign, unsigned long dateopt)
  1227. {
  1228. X509_REQ *req = NULL;
  1229. EVP_PKEY *pktmp = NULL;
  1230. int ok = -1, i;
  1231. req = load_csr_autofmt(infile, informat, vfyopts, "certificate request");
  1232. if (req == NULL)
  1233. goto end;
  1234. if ((pktmp = X509_REQ_get0_pubkey(req)) == NULL) {
  1235. BIO_printf(bio_err, "Error unpacking public key\n");
  1236. goto end;
  1237. }
  1238. if (verbose)
  1239. X509_REQ_print_ex(bio_err, req, nameopt, X509_FLAG_COMPAT);
  1240. BIO_printf(bio_err, "Check that the request matches the signature\n");
  1241. ok = 0;
  1242. if (selfsign && !X509_REQ_check_private_key(req, pkey)) {
  1243. BIO_printf(bio_err,
  1244. "Certificate request and CA private key do not match\n");
  1245. goto end;
  1246. }
  1247. i = do_X509_REQ_verify(req, pktmp, vfyopts);
  1248. if (i < 0) {
  1249. BIO_printf(bio_err, "Signature verification problems...\n");
  1250. goto end;
  1251. }
  1252. if (i == 0) {
  1253. BIO_printf(bio_err,
  1254. "Signature did not match the certificate request\n");
  1255. goto end;
  1256. }
  1257. BIO_printf(bio_err, "Signature ok\n");
  1258. ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
  1259. chtype, multirdn, email_dn, startdate, enddate, days, batch,
  1260. verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
  1261. ext_copy, selfsign, dateopt);
  1262. end:
  1263. ERR_print_errors(bio_err);
  1264. X509_REQ_free(req);
  1265. return ok;
  1266. }
  1267. static int certify_cert(X509 **xret, const char *infile, int certformat,
  1268. const char *passin, EVP_PKEY *pkey, X509 *x509,
  1269. const char *dgst,
  1270. STACK_OF(OPENSSL_STRING) *sigopts,
  1271. STACK_OF(OPENSSL_STRING) *vfyopts,
  1272. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  1273. BIGNUM *serial, const char *subj, unsigned long chtype,
  1274. int multirdn, int email_dn, const char *startdate,
  1275. const char *enddate, long days, int batch, const char *ext_sect,
  1276. CONF *lconf, int verbose, unsigned long certopt,
  1277. unsigned long nameopt, int default_op, int ext_copy, unsigned long dateopt)
  1278. {
  1279. X509 *template_cert = NULL;
  1280. X509_REQ *rreq = NULL;
  1281. EVP_PKEY *pktmp = NULL;
  1282. int ok = -1, i;
  1283. if ((template_cert = load_cert_pass(infile, certformat, 1, passin,
  1284. "template certificate")) == NULL)
  1285. goto end;
  1286. if (verbose)
  1287. X509_print(bio_err, template_cert);
  1288. BIO_printf(bio_err, "Check that the request matches the signature\n");
  1289. if ((pktmp = X509_get0_pubkey(template_cert)) == NULL) {
  1290. BIO_printf(bio_err, "error unpacking public key\n");
  1291. goto end;
  1292. }
  1293. i = do_X509_verify(template_cert, pktmp, vfyopts);
  1294. if (i < 0) {
  1295. ok = 0;
  1296. BIO_printf(bio_err, "Signature verification problems....\n");
  1297. goto end;
  1298. }
  1299. if (i == 0) {
  1300. ok = 0;
  1301. BIO_printf(bio_err, "Signature did not match the certificate\n");
  1302. goto end;
  1303. } else {
  1304. BIO_printf(bio_err, "Signature ok\n");
  1305. }
  1306. if ((rreq = X509_to_X509_REQ(template_cert, NULL, NULL)) == NULL)
  1307. goto end;
  1308. ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
  1309. chtype, multirdn, email_dn, startdate, enddate, days, batch,
  1310. verbose, rreq, ext_sect, lconf, certopt, nameopt, default_op,
  1311. ext_copy, 0, dateopt);
  1312. end:
  1313. X509_REQ_free(rreq);
  1314. X509_free(template_cert);
  1315. return ok;
  1316. }
  1317. static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
  1318. const char *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
  1319. STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,
  1320. const char *subj, unsigned long chtype, int multirdn,
  1321. int email_dn, const char *startdate, const char *enddate, long days,
  1322. int batch, int verbose, X509_REQ *req, const char *ext_sect,
  1323. CONF *lconf, unsigned long certopt, unsigned long nameopt,
  1324. int default_op, int ext_copy, int selfsign, unsigned long dateopt)
  1325. {
  1326. const X509_NAME *name = NULL;
  1327. X509_NAME *CAname = NULL, *subject = NULL;
  1328. const ASN1_TIME *tm;
  1329. ASN1_STRING *str, *str2;
  1330. ASN1_OBJECT *obj;
  1331. X509 *ret = NULL;
  1332. X509_NAME_ENTRY *ne, *tne;
  1333. EVP_PKEY *pktmp;
  1334. int ok = -1, i, j, last, nid;
  1335. const char *p;
  1336. CONF_VALUE *cv;
  1337. OPENSSL_STRING row[DB_NUMBER];
  1338. OPENSSL_STRING *irow = NULL;
  1339. OPENSSL_STRING *rrow = NULL;
  1340. char buf[25];
  1341. X509V3_CTX ext_ctx;
  1342. for (i = 0; i < DB_NUMBER; i++)
  1343. row[i] = NULL;
  1344. if (subj) {
  1345. X509_NAME *n = parse_name(subj, chtype, multirdn, "subject");
  1346. if (!n)
  1347. goto end;
  1348. X509_REQ_set_subject_name(req, n);
  1349. X509_NAME_free(n);
  1350. }
  1351. if (default_op)
  1352. BIO_printf(bio_err, "The Subject's Distinguished Name is as follows\n");
  1353. name = X509_REQ_get_subject_name(req);
  1354. for (i = 0; i < X509_NAME_entry_count(name); i++) {
  1355. ne = X509_NAME_get_entry(name, i);
  1356. str = X509_NAME_ENTRY_get_data(ne);
  1357. obj = X509_NAME_ENTRY_get_object(ne);
  1358. nid = OBJ_obj2nid(obj);
  1359. if (msie_hack) {
  1360. /* assume all type should be strings */
  1361. if (str->type == V_ASN1_UNIVERSALSTRING)
  1362. ASN1_UNIVERSALSTRING_to_string(str);
  1363. if (str->type == V_ASN1_IA5STRING && nid != NID_pkcs9_emailAddress)
  1364. str->type = V_ASN1_T61STRING;
  1365. if (nid == NID_pkcs9_emailAddress
  1366. && str->type == V_ASN1_PRINTABLESTRING)
  1367. str->type = V_ASN1_IA5STRING;
  1368. }
  1369. /* If no EMAIL is wanted in the subject */
  1370. if (nid == NID_pkcs9_emailAddress && !email_dn)
  1371. continue;
  1372. /* check some things */
  1373. if (nid == NID_pkcs9_emailAddress && str->type != V_ASN1_IA5STRING) {
  1374. BIO_printf(bio_err,
  1375. "\nemailAddress type needs to be of type IA5STRING\n");
  1376. goto end;
  1377. }
  1378. if (str->type != V_ASN1_BMPSTRING && str->type != V_ASN1_UTF8STRING) {
  1379. j = ASN1_PRINTABLE_type(str->data, str->length);
  1380. if ((j == V_ASN1_T61STRING && str->type != V_ASN1_T61STRING) ||
  1381. (j == V_ASN1_IA5STRING && str->type == V_ASN1_PRINTABLESTRING))
  1382. {
  1383. BIO_printf(bio_err,
  1384. "\nThe string contains characters that are illegal for the ASN.1 type\n");
  1385. goto end;
  1386. }
  1387. }
  1388. if (default_op)
  1389. old_entry_print(obj, str);
  1390. }
  1391. /* Ok, now we check the 'policy' stuff. */
  1392. if ((subject = X509_NAME_new()) == NULL) {
  1393. BIO_printf(bio_err, "Memory allocation failure\n");
  1394. goto end;
  1395. }
  1396. /* take a copy of the issuer name before we mess with it. */
  1397. if (selfsign)
  1398. CAname = X509_NAME_dup(name);
  1399. else
  1400. CAname = X509_NAME_dup(X509_get_subject_name(x509));
  1401. if (CAname == NULL)
  1402. goto end;
  1403. str = str2 = NULL;
  1404. for (i = 0; i < sk_CONF_VALUE_num(policy); i++) {
  1405. cv = sk_CONF_VALUE_value(policy, i); /* get the object id */
  1406. if ((j = OBJ_txt2nid(cv->name)) == NID_undef) {
  1407. BIO_printf(bio_err,
  1408. "%s:unknown object type in 'policy' configuration\n",
  1409. cv->name);
  1410. goto end;
  1411. }
  1412. obj = OBJ_nid2obj(j);
  1413. last = -1;
  1414. for (;;) {
  1415. X509_NAME_ENTRY *push = NULL;
  1416. /* lookup the object in the supplied name list */
  1417. j = X509_NAME_get_index_by_OBJ(name, obj, last);
  1418. if (j < 0) {
  1419. if (last != -1)
  1420. break;
  1421. tne = NULL;
  1422. } else {
  1423. tne = X509_NAME_get_entry(name, j);
  1424. }
  1425. last = j;
  1426. /* depending on the 'policy', decide what to do. */
  1427. if (strcmp(cv->value, "optional") == 0) {
  1428. if (tne != NULL)
  1429. push = tne;
  1430. } else if (strcmp(cv->value, "supplied") == 0) {
  1431. if (tne == NULL) {
  1432. BIO_printf(bio_err,
  1433. "The %s field needed to be supplied and was missing\n",
  1434. cv->name);
  1435. goto end;
  1436. } else {
  1437. push = tne;
  1438. }
  1439. } else if (strcmp(cv->value, "match") == 0) {
  1440. int last2;
  1441. if (tne == NULL) {
  1442. BIO_printf(bio_err,
  1443. "The mandatory %s field was missing\n",
  1444. cv->name);
  1445. goto end;
  1446. }
  1447. last2 = -1;
  1448. again2:
  1449. j = X509_NAME_get_index_by_OBJ(CAname, obj, last2);
  1450. if ((j < 0) && (last2 == -1)) {
  1451. BIO_printf(bio_err,
  1452. "The %s field does not exist in the CA certificate,\n"
  1453. "the 'policy' is misconfigured\n", cv->name);
  1454. goto end;
  1455. }
  1456. if (j >= 0) {
  1457. push = X509_NAME_get_entry(CAname, j);
  1458. str = X509_NAME_ENTRY_get_data(tne);
  1459. str2 = X509_NAME_ENTRY_get_data(push);
  1460. last2 = j;
  1461. if (ASN1_STRING_cmp(str, str2) != 0)
  1462. goto again2;
  1463. }
  1464. if (j < 0) {
  1465. BIO_printf(bio_err,
  1466. "The %s field is different between\n"
  1467. "CA certificate (%s) and the request (%s)\n",
  1468. cv->name,
  1469. ((str2 == NULL) ? "NULL" : (char *)str2->data),
  1470. ((str == NULL) ? "NULL" : (char *)str->data));
  1471. goto end;
  1472. }
  1473. } else {
  1474. BIO_printf(bio_err,
  1475. "%s:invalid type in 'policy' configuration\n",
  1476. cv->value);
  1477. goto end;
  1478. }
  1479. if (push != NULL) {
  1480. if (!X509_NAME_add_entry(subject, push, -1, 0)) {
  1481. BIO_printf(bio_err, "Memory allocation failure\n");
  1482. goto end;
  1483. }
  1484. }
  1485. if (j < 0)
  1486. break;
  1487. }
  1488. }
  1489. if (preserve) {
  1490. X509_NAME_free(subject);
  1491. /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
  1492. subject = X509_NAME_dup(name);
  1493. if (subject == NULL)
  1494. goto end;
  1495. }
  1496. /* We are now totally happy, lets make and sign the certificate */
  1497. if (verbose)
  1498. BIO_printf(bio_err,
  1499. "Everything appears to be ok, creating and signing the certificate\n");
  1500. if ((ret = X509_new_ex(app_get0_libctx(), app_get0_propq())) == NULL)
  1501. goto end;
  1502. if (BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(ret)) == NULL)
  1503. goto end;
  1504. if (selfsign) {
  1505. if (!X509_set_issuer_name(ret, subject))
  1506. goto end;
  1507. } else {
  1508. if (!X509_set_issuer_name(ret, X509_get_subject_name(x509)))
  1509. goto end;
  1510. }
  1511. if (!set_cert_times(ret, startdate, enddate, days, 0))
  1512. goto end;
  1513. if (enddate != NULL) {
  1514. int tdays;
  1515. if (!ASN1_TIME_diff(&tdays, NULL, NULL, X509_get0_notAfter(ret)))
  1516. goto end;
  1517. days = tdays;
  1518. }
  1519. if (!X509_set_subject_name(ret, subject))
  1520. goto end;
  1521. pktmp = X509_REQ_get0_pubkey(req);
  1522. i = X509_set_pubkey(ret, pktmp);
  1523. if (!i)
  1524. goto end;
  1525. /* Initialize the context structure */
  1526. X509V3_set_ctx(&ext_ctx, selfsign ? ret : x509,
  1527. ret, NULL /* no need to give req, needed info is in ret */,
  1528. NULL, X509V3_CTX_REPLACE);
  1529. /* prepare fallback for AKID, but only if issuer cert equals subject cert */
  1530. if (selfsign) {
  1531. if (!X509V3_set_issuer_pkey(&ext_ctx, pkey))
  1532. goto end;
  1533. if (!cert_matches_key(ret, pkey))
  1534. BIO_printf(bio_err,
  1535. "Warning: Signature key and public key of cert do not match\n");
  1536. }
  1537. /* Lets add the extensions, if there are any */
  1538. if (ext_sect) {
  1539. if (extfile_conf != NULL) {
  1540. if (verbose)
  1541. BIO_printf(bio_err, "Extra configuration file found\n");
  1542. /* Use the extfile_conf configuration db LHASH */
  1543. X509V3_set_nconf(&ext_ctx, extfile_conf);
  1544. /* Adds exts contained in the configuration file */
  1545. if (!X509V3_EXT_add_nconf(extfile_conf, &ext_ctx, ext_sect, ret)) {
  1546. BIO_printf(bio_err,
  1547. "Error adding certificate extensions from extfile section %s\n",
  1548. ext_sect);
  1549. goto end;
  1550. }
  1551. if (verbose)
  1552. BIO_printf(bio_err,
  1553. "Successfully added extensions from file.\n");
  1554. } else if (ext_sect) {
  1555. /* We found extensions to be set from config file */
  1556. X509V3_set_nconf(&ext_ctx, lconf);
  1557. if (!X509V3_EXT_add_nconf(lconf, &ext_ctx, ext_sect, ret)) {
  1558. BIO_printf(bio_err,
  1559. "Error adding certificate extensions from config section %s\n",
  1560. ext_sect);
  1561. goto end;
  1562. }
  1563. if (verbose)
  1564. BIO_printf(bio_err,
  1565. "Successfully added extensions from config\n");
  1566. }
  1567. }
  1568. /* Copy extensions from request (if any) */
  1569. if (!copy_extensions(ret, req, ext_copy)) {
  1570. BIO_printf(bio_err, "ERROR: adding extensions from request\n");
  1571. goto end;
  1572. }
  1573. if (verbose)
  1574. BIO_printf(bio_err,
  1575. "The subject name appears to be ok, checking database for clashes\n");
  1576. /* Build the correct Subject if no e-mail is wanted in the subject. */
  1577. if (!email_dn) {
  1578. X509_NAME_ENTRY *tmpne;
  1579. X509_NAME *dn_subject;
  1580. /*
  1581. * Its best to dup the subject DN and then delete any email addresses
  1582. * because this retains its structure.
  1583. */
  1584. if ((dn_subject = X509_NAME_dup(subject)) == NULL) {
  1585. BIO_printf(bio_err, "Memory allocation failure\n");
  1586. goto end;
  1587. }
  1588. i = -1;
  1589. while ((i = X509_NAME_get_index_by_NID(dn_subject,
  1590. NID_pkcs9_emailAddress,
  1591. i)) >= 0) {
  1592. tmpne = X509_NAME_delete_entry(dn_subject, i--);
  1593. X509_NAME_ENTRY_free(tmpne);
  1594. }
  1595. if (!X509_set_subject_name(ret, dn_subject)) {
  1596. X509_NAME_free(dn_subject);
  1597. goto end;
  1598. }
  1599. X509_NAME_free(dn_subject);
  1600. }
  1601. row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0);
  1602. if (row[DB_name] == NULL) {
  1603. BIO_printf(bio_err, "Memory allocation failure\n");
  1604. goto end;
  1605. }
  1606. if (BN_is_zero(serial))
  1607. row[DB_serial] = OPENSSL_strdup("00");
  1608. else
  1609. row[DB_serial] = BN_bn2hex(serial);
  1610. if (row[DB_serial] == NULL) {
  1611. BIO_printf(bio_err, "Memory allocation failure\n");
  1612. goto end;
  1613. }
  1614. if (row[DB_name][0] == '\0') {
  1615. /*
  1616. * An empty subject! We'll use the serial number instead. If
  1617. * unique_subject is in use then we don't want different entries with
  1618. * empty subjects matching each other.
  1619. */
  1620. OPENSSL_free(row[DB_name]);
  1621. row[DB_name] = OPENSSL_strdup(row[DB_serial]);
  1622. if (row[DB_name] == NULL) {
  1623. BIO_printf(bio_err, "Memory allocation failure\n");
  1624. goto end;
  1625. }
  1626. }
  1627. if (db->attributes.unique_subject) {
  1628. OPENSSL_STRING *crow = row;
  1629. rrow = TXT_DB_get_by_index(db->db, DB_name, crow);
  1630. if (rrow != NULL) {
  1631. BIO_printf(bio_err,
  1632. "ERROR:There is already a certificate for %s\n",
  1633. row[DB_name]);
  1634. }
  1635. }
  1636. if (rrow == NULL) {
  1637. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  1638. if (rrow != NULL) {
  1639. BIO_printf(bio_err,
  1640. "ERROR:Serial number %s has already been issued,\n",
  1641. row[DB_serial]);
  1642. BIO_printf(bio_err,
  1643. " check the database/serial_file for corruption\n");
  1644. }
  1645. }
  1646. if (rrow != NULL) {
  1647. BIO_printf(bio_err, "The matching entry has the following details\n");
  1648. if (rrow[DB_type][0] == DB_TYPE_EXP)
  1649. p = "Expired";
  1650. else if (rrow[DB_type][0] == DB_TYPE_REV)
  1651. p = "Revoked";
  1652. else if (rrow[DB_type][0] == DB_TYPE_VAL)
  1653. p = "Valid";
  1654. else
  1655. p = "\ninvalid type, Database error\n";
  1656. BIO_printf(bio_err, "Type :%s\n", p);
  1657. if (rrow[DB_type][0] == DB_TYPE_REV) {
  1658. p = rrow[DB_exp_date];
  1659. if (p == NULL)
  1660. p = "undef";
  1661. BIO_printf(bio_err, "Was revoked on:%s\n", p);
  1662. }
  1663. p = rrow[DB_exp_date];
  1664. if (p == NULL)
  1665. p = "undef";
  1666. BIO_printf(bio_err, "Expires on :%s\n", p);
  1667. p = rrow[DB_serial];
  1668. if (p == NULL)
  1669. p = "undef";
  1670. BIO_printf(bio_err, "Serial Number :%s\n", p);
  1671. p = rrow[DB_file];
  1672. if (p == NULL)
  1673. p = "undef";
  1674. BIO_printf(bio_err, "File name :%s\n", p);
  1675. p = rrow[DB_name];
  1676. if (p == NULL)
  1677. p = "undef";
  1678. BIO_printf(bio_err, "Subject Name :%s\n", p);
  1679. ok = -1; /* This is now a 'bad' error. */
  1680. goto end;
  1681. }
  1682. if (!default_op) {
  1683. BIO_printf(bio_err, "Certificate Details:\n");
  1684. /*
  1685. * Never print signature details because signature not present
  1686. */
  1687. certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;
  1688. X509_print_ex(bio_err, ret, nameopt, certopt);
  1689. }
  1690. BIO_printf(bio_err, "Certificate is to be certified until ");
  1691. ASN1_TIME_print_ex(bio_err, X509_get0_notAfter(ret), dateopt);
  1692. if (days)
  1693. BIO_printf(bio_err, " (%ld days)", days);
  1694. BIO_printf(bio_err, "\n");
  1695. if (!batch) {
  1696. BIO_printf(bio_err, "Sign the certificate? [y/n]:");
  1697. (void)BIO_flush(bio_err);
  1698. buf[0] = '\0';
  1699. if (fgets(buf, sizeof(buf), stdin) == NULL) {
  1700. BIO_printf(bio_err,
  1701. "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
  1702. ok = 0;
  1703. goto end;
  1704. }
  1705. if (!(buf[0] == 'y' || buf[0] == 'Y')) {
  1706. BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED\n");
  1707. ok = 0;
  1708. goto end;
  1709. }
  1710. }
  1711. pktmp = X509_get0_pubkey(ret);
  1712. if (EVP_PKEY_missing_parameters(pktmp) &&
  1713. !EVP_PKEY_missing_parameters(pkey))
  1714. EVP_PKEY_copy_parameters(pktmp, pkey);
  1715. if (!do_X509_sign(ret, 0, pkey, dgst, sigopts, &ext_ctx))
  1716. goto end;
  1717. /* We now just add it to the database as DB_TYPE_VAL('V') */
  1718. row[DB_type] = OPENSSL_strdup("V");
  1719. tm = X509_get0_notAfter(ret);
  1720. row[DB_exp_date] = app_malloc(tm->length + 1, "row expdate");
  1721. memcpy(row[DB_exp_date], tm->data, tm->length);
  1722. row[DB_exp_date][tm->length] = '\0';
  1723. row[DB_rev_date] = NULL;
  1724. row[DB_file] = OPENSSL_strdup("unknown");
  1725. if ((row[DB_type] == NULL) || (row[DB_file] == NULL)
  1726. || (row[DB_name] == NULL)) {
  1727. BIO_printf(bio_err, "Memory allocation failure\n");
  1728. goto end;
  1729. }
  1730. irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row space");
  1731. for (i = 0; i < DB_NUMBER; i++)
  1732. irow[i] = row[i];
  1733. irow[DB_NUMBER] = NULL;
  1734. if (!TXT_DB_insert(db->db, irow)) {
  1735. BIO_printf(bio_err, "failed to update database\n");
  1736. BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
  1737. goto end;
  1738. }
  1739. irow = NULL;
  1740. ok = 1;
  1741. end:
  1742. if (ok != 1) {
  1743. for (i = 0; i < DB_NUMBER; i++)
  1744. OPENSSL_free(row[i]);
  1745. }
  1746. OPENSSL_free(irow);
  1747. X509_NAME_free(CAname);
  1748. X509_NAME_free(subject);
  1749. if (ok <= 0)
  1750. X509_free(ret);
  1751. else
  1752. *xret = ret;
  1753. return ok;
  1754. }
  1755. static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext)
  1756. {
  1757. if (output_der) {
  1758. (void)i2d_X509_bio(bp, x);
  1759. return;
  1760. }
  1761. if (!notext)
  1762. X509_print(bp, x);
  1763. PEM_write_bio_X509(bp, x);
  1764. }
  1765. static int certify_spkac(X509 **xret, const char *infile, EVP_PKEY *pkey,
  1766. X509 *x509, const char *dgst,
  1767. STACK_OF(OPENSSL_STRING) *sigopts,
  1768. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  1769. BIGNUM *serial, const char *subj, unsigned long chtype,
  1770. int multirdn, int email_dn, const char *startdate,
  1771. const char *enddate, long days, const char *ext_sect,
  1772. CONF *lconf, int verbose, unsigned long certopt,
  1773. unsigned long nameopt, int default_op, int ext_copy, unsigned long dateopt)
  1774. {
  1775. STACK_OF(CONF_VALUE) *sk = NULL;
  1776. LHASH_OF(CONF_VALUE) *parms = NULL;
  1777. X509_REQ *req = NULL;
  1778. CONF_VALUE *cv = NULL;
  1779. NETSCAPE_SPKI *spki = NULL;
  1780. char *type, *buf;
  1781. EVP_PKEY *pktmp = NULL;
  1782. X509_NAME *n = NULL;
  1783. X509_NAME_ENTRY *ne = NULL;
  1784. int ok = -1, i, j;
  1785. long errline;
  1786. int nid;
  1787. /*
  1788. * Load input file into a hash table. (This is just an easy
  1789. * way to read and parse the file, then put it into a convenient
  1790. * STACK format).
  1791. */
  1792. parms = CONF_load(NULL, infile, &errline);
  1793. if (parms == NULL) {
  1794. BIO_printf(bio_err, "error on line %ld of %s\n", errline, infile);
  1795. goto end;
  1796. }
  1797. sk = CONF_get_section(parms, "default");
  1798. if (sk_CONF_VALUE_num(sk) == 0) {
  1799. BIO_printf(bio_err, "no name/value pairs found in %s\n", infile);
  1800. goto end;
  1801. }
  1802. /*
  1803. * Now create a dummy X509 request structure. We don't actually
  1804. * have an X509 request, but we have many of the components
  1805. * (a public key, various DN components). The idea is that we
  1806. * put these components into the right X509 request structure
  1807. * and we can use the same code as if you had a real X509 request.
  1808. */
  1809. req = X509_REQ_new();
  1810. if (req == NULL)
  1811. goto end;
  1812. /*
  1813. * Build up the subject name set.
  1814. */
  1815. n = X509_REQ_get_subject_name(req);
  1816. for (i = 0;; i++) {
  1817. if (sk_CONF_VALUE_num(sk) <= i)
  1818. break;
  1819. cv = sk_CONF_VALUE_value(sk, i);
  1820. type = cv->name;
  1821. /*
  1822. * Skip past any leading X. X: X, etc to allow for multiple instances
  1823. */
  1824. for (buf = cv->name; *buf; buf++)
  1825. if ((*buf == ':') || (*buf == ',') || (*buf == '.')) {
  1826. buf++;
  1827. if (*buf)
  1828. type = buf;
  1829. break;
  1830. }
  1831. buf = cv->value;
  1832. if ((nid = OBJ_txt2nid(type)) == NID_undef) {
  1833. if (strcmp(type, "SPKAC") == 0) {
  1834. spki = NETSCAPE_SPKI_b64_decode(cv->value, -1);
  1835. if (spki == NULL) {
  1836. BIO_printf(bio_err,
  1837. "unable to load Netscape SPKAC structure\n");
  1838. goto end;
  1839. }
  1840. }
  1841. continue;
  1842. }
  1843. if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
  1844. (unsigned char *)buf, -1, -1, 0))
  1845. goto end;
  1846. }
  1847. if (spki == NULL) {
  1848. BIO_printf(bio_err, "Netscape SPKAC structure not found in %s\n",
  1849. infile);
  1850. goto end;
  1851. }
  1852. /*
  1853. * Now extract the key from the SPKI structure.
  1854. */
  1855. BIO_printf(bio_err, "Check that the SPKAC request matches the signature\n");
  1856. if ((pktmp = NETSCAPE_SPKI_get_pubkey(spki)) == NULL) {
  1857. BIO_printf(bio_err, "error unpacking SPKAC public key\n");
  1858. goto end;
  1859. }
  1860. j = NETSCAPE_SPKI_verify(spki, pktmp);
  1861. if (j <= 0) {
  1862. EVP_PKEY_free(pktmp);
  1863. BIO_printf(bio_err,
  1864. "signature verification failed on SPKAC public key\n");
  1865. goto end;
  1866. }
  1867. BIO_printf(bio_err, "Signature ok\n");
  1868. X509_REQ_set_pubkey(req, pktmp);
  1869. EVP_PKEY_free(pktmp);
  1870. ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
  1871. chtype, multirdn, email_dn, startdate, enddate, days, 1,
  1872. verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
  1873. ext_copy, 0, dateopt);
  1874. end:
  1875. X509_REQ_free(req);
  1876. CONF_free(parms);
  1877. NETSCAPE_SPKI_free(spki);
  1878. X509_NAME_ENTRY_free(ne);
  1879. return ok;
  1880. }
  1881. static int check_time_format(const char *str)
  1882. {
  1883. return ASN1_TIME_set_string(NULL, str);
  1884. }
  1885. static int do_revoke(X509 *x509, CA_DB *db, REVINFO_TYPE rev_type,
  1886. const char *value)
  1887. {
  1888. const ASN1_TIME *tm = NULL;
  1889. char *row[DB_NUMBER], **rrow, **irow;
  1890. char *rev_str = NULL;
  1891. BIGNUM *bn = NULL;
  1892. int ok = -1, i;
  1893. for (i = 0; i < DB_NUMBER; i++)
  1894. row[i] = NULL;
  1895. row[DB_name] = X509_NAME_oneline(X509_get_subject_name(x509), NULL, 0);
  1896. bn = ASN1_INTEGER_to_BN(X509_get0_serialNumber(x509), NULL);
  1897. if (!bn)
  1898. goto end;
  1899. if (BN_is_zero(bn))
  1900. row[DB_serial] = OPENSSL_strdup("00");
  1901. else
  1902. row[DB_serial] = BN_bn2hex(bn);
  1903. BN_free(bn);
  1904. if (row[DB_name] != NULL && row[DB_name][0] == '\0') {
  1905. /* Entries with empty Subjects actually use the serial number instead */
  1906. OPENSSL_free(row[DB_name]);
  1907. row[DB_name] = OPENSSL_strdup(row[DB_serial]);
  1908. }
  1909. if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) {
  1910. BIO_printf(bio_err, "Memory allocation failure\n");
  1911. goto end;
  1912. }
  1913. /*
  1914. * We have to lookup by serial number because name lookup skips revoked
  1915. * certs
  1916. */
  1917. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  1918. if (rrow == NULL) {
  1919. BIO_printf(bio_err,
  1920. "Adding Entry with serial number %s to DB for %s\n",
  1921. row[DB_serial], row[DB_name]);
  1922. /* We now just add it to the database as DB_TYPE_REV('V') */
  1923. row[DB_type] = OPENSSL_strdup("V");
  1924. tm = X509_get0_notAfter(x509);
  1925. row[DB_exp_date] = app_malloc(tm->length + 1, "row exp_data");
  1926. memcpy(row[DB_exp_date], tm->data, tm->length);
  1927. row[DB_exp_date][tm->length] = '\0';
  1928. row[DB_rev_date] = NULL;
  1929. row[DB_file] = OPENSSL_strdup("unknown");
  1930. if (row[DB_type] == NULL || row[DB_file] == NULL) {
  1931. BIO_printf(bio_err, "Memory allocation failure\n");
  1932. goto end;
  1933. }
  1934. irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row ptr");
  1935. for (i = 0; i < DB_NUMBER; i++)
  1936. irow[i] = row[i];
  1937. irow[DB_NUMBER] = NULL;
  1938. if (!TXT_DB_insert(db->db, irow)) {
  1939. BIO_printf(bio_err, "failed to update database\n");
  1940. BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
  1941. OPENSSL_free(irow);
  1942. goto end;
  1943. }
  1944. for (i = 0; i < DB_NUMBER; i++)
  1945. row[i] = NULL;
  1946. /* Revoke Certificate */
  1947. if (rev_type == REV_VALID)
  1948. ok = 1;
  1949. else
  1950. /* Retry revocation after DB insertion */
  1951. ok = do_revoke(x509, db, rev_type, value);
  1952. goto end;
  1953. } else if (index_name_cmp_noconst(row, rrow)) {
  1954. BIO_printf(bio_err, "ERROR:name does not match %s\n", row[DB_name]);
  1955. goto end;
  1956. } else if (rev_type == REV_VALID) {
  1957. BIO_printf(bio_err, "ERROR:Already present, serial number %s\n",
  1958. row[DB_serial]);
  1959. goto end;
  1960. } else if (rrow[DB_type][0] == DB_TYPE_REV) {
  1961. BIO_printf(bio_err, "ERROR:Already revoked, serial number %s\n",
  1962. row[DB_serial]);
  1963. goto end;
  1964. } else {
  1965. BIO_printf(bio_err, "Revoking Certificate %s.\n", rrow[DB_serial]);
  1966. rev_str = make_revocation_str(rev_type, value);
  1967. if (!rev_str) {
  1968. BIO_printf(bio_err, "Error in revocation arguments\n");
  1969. goto end;
  1970. }
  1971. rrow[DB_type][0] = DB_TYPE_REV;
  1972. rrow[DB_type][1] = '\0';
  1973. rrow[DB_rev_date] = rev_str;
  1974. }
  1975. ok = 1;
  1976. end:
  1977. for (i = 0; i < DB_NUMBER; i++)
  1978. OPENSSL_free(row[i]);
  1979. return ok;
  1980. }
  1981. static int get_certificate_status(const char *serial, CA_DB *db)
  1982. {
  1983. char *row[DB_NUMBER], **rrow;
  1984. int ok = -1, i;
  1985. size_t serial_len = strlen(serial);
  1986. /* Free Resources */
  1987. for (i = 0; i < DB_NUMBER; i++)
  1988. row[i] = NULL;
  1989. /* Malloc needed char spaces */
  1990. row[DB_serial] = app_malloc(serial_len + 2, "row serial#");
  1991. if (serial_len % 2) {
  1992. /*
  1993. * Set the first char to 0
  1994. */
  1995. row[DB_serial][0] = '0';
  1996. /* Copy String from serial to row[DB_serial] */
  1997. memcpy(row[DB_serial] + 1, serial, serial_len);
  1998. row[DB_serial][serial_len + 1] = '\0';
  1999. } else {
  2000. /* Copy String from serial to row[DB_serial] */
  2001. memcpy(row[DB_serial], serial, serial_len);
  2002. row[DB_serial][serial_len] = '\0';
  2003. }
  2004. /* Make it Upper Case */
  2005. make_uppercase(row[DB_serial]);
  2006. ok = 1;
  2007. /* Search for the certificate */
  2008. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  2009. if (rrow == NULL) {
  2010. BIO_printf(bio_err, "Serial %s not present in db.\n", row[DB_serial]);
  2011. ok = -1;
  2012. goto end;
  2013. } else if (rrow[DB_type][0] == DB_TYPE_VAL) {
  2014. BIO_printf(bio_err, "%s=Valid (%c)\n",
  2015. row[DB_serial], rrow[DB_type][0]);
  2016. goto end;
  2017. } else if (rrow[DB_type][0] == DB_TYPE_REV) {
  2018. BIO_printf(bio_err, "%s=Revoked (%c)\n",
  2019. row[DB_serial], rrow[DB_type][0]);
  2020. goto end;
  2021. } else if (rrow[DB_type][0] == DB_TYPE_EXP) {
  2022. BIO_printf(bio_err, "%s=Expired (%c)\n",
  2023. row[DB_serial], rrow[DB_type][0]);
  2024. goto end;
  2025. } else if (rrow[DB_type][0] == DB_TYPE_SUSP) {
  2026. BIO_printf(bio_err, "%s=Suspended (%c)\n",
  2027. row[DB_serial], rrow[DB_type][0]);
  2028. goto end;
  2029. } else {
  2030. BIO_printf(bio_err, "%s=Unknown (%c).\n",
  2031. row[DB_serial], rrow[DB_type][0]);
  2032. ok = -1;
  2033. }
  2034. end:
  2035. for (i = 0; i < DB_NUMBER; i++) {
  2036. OPENSSL_free(row[i]);
  2037. }
  2038. return ok;
  2039. }
  2040. int do_updatedb(CA_DB *db, time_t *now)
  2041. {
  2042. ASN1_TIME *a_tm = NULL;
  2043. int i, cnt = 0;
  2044. char **rrow;
  2045. a_tm = ASN1_TIME_new();
  2046. if (a_tm == NULL)
  2047. return -1;
  2048. /* get actual time */
  2049. if (X509_time_adj(a_tm, 0, now) == NULL) {
  2050. ASN1_TIME_free(a_tm);
  2051. return -1;
  2052. }
  2053. for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
  2054. rrow = sk_OPENSSL_PSTRING_value(db->db->data, i);
  2055. if (rrow[DB_type][0] == DB_TYPE_VAL) {
  2056. /* ignore entries that are not valid */
  2057. ASN1_TIME *exp_date = NULL;
  2058. exp_date = ASN1_TIME_new();
  2059. if (exp_date == NULL) {
  2060. ASN1_TIME_free(a_tm);
  2061. return -1;
  2062. }
  2063. if (!ASN1_TIME_set_string(exp_date, rrow[DB_exp_date])) {
  2064. ASN1_TIME_free(a_tm);
  2065. ASN1_TIME_free(exp_date);
  2066. return -1;
  2067. }
  2068. if (ASN1_TIME_compare(exp_date, a_tm) <= 0) {
  2069. rrow[DB_type][0] = DB_TYPE_EXP;
  2070. rrow[DB_type][1] = '\0';
  2071. cnt++;
  2072. BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]);
  2073. }
  2074. ASN1_TIME_free(exp_date);
  2075. }
  2076. }
  2077. ASN1_TIME_free(a_tm);
  2078. return cnt;
  2079. }
  2080. static const char *crl_reasons[] = {
  2081. /* CRL reason strings */
  2082. "unspecified",
  2083. "keyCompromise",
  2084. "CACompromise",
  2085. "affiliationChanged",
  2086. "superseded",
  2087. "cessationOfOperation",
  2088. "certificateHold",
  2089. "removeFromCRL",
  2090. /* Additional pseudo reasons */
  2091. "holdInstruction",
  2092. "keyTime",
  2093. "CAkeyTime"
  2094. };
  2095. #define NUM_REASONS OSSL_NELEM(crl_reasons)
  2096. /*
  2097. * Given revocation information convert to a DB string. The format of the
  2098. * string is: revtime[,reason,extra]. Where 'revtime' is the revocation time
  2099. * (the current time). 'reason' is the optional CRL reason and 'extra' is any
  2100. * additional argument
  2101. */
  2102. static char *make_revocation_str(REVINFO_TYPE rev_type, const char *rev_arg)
  2103. {
  2104. char *str;
  2105. const char *reason = NULL, *other = NULL;
  2106. ASN1_OBJECT *otmp;
  2107. ASN1_UTCTIME *revtm = NULL;
  2108. int i;
  2109. switch (rev_type) {
  2110. case REV_NONE:
  2111. case REV_VALID:
  2112. break;
  2113. case REV_CRL_REASON:
  2114. for (i = 0; i < 8; i++) {
  2115. if (OPENSSL_strcasecmp(rev_arg, crl_reasons[i]) == 0) {
  2116. reason = crl_reasons[i];
  2117. break;
  2118. }
  2119. }
  2120. if (reason == NULL) {
  2121. BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg);
  2122. return NULL;
  2123. }
  2124. break;
  2125. case REV_HOLD:
  2126. /* Argument is an OID */
  2127. otmp = OBJ_txt2obj(rev_arg, 0);
  2128. ASN1_OBJECT_free(otmp);
  2129. if (otmp == NULL) {
  2130. BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg);
  2131. return NULL;
  2132. }
  2133. reason = "holdInstruction";
  2134. other = rev_arg;
  2135. break;
  2136. case REV_KEY_COMPROMISE:
  2137. case REV_CA_COMPROMISE:
  2138. /* Argument is the key compromise time */
  2139. if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg)) {
  2140. BIO_printf(bio_err,
  2141. "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
  2142. rev_arg);
  2143. return NULL;
  2144. }
  2145. other = rev_arg;
  2146. if (rev_type == REV_KEY_COMPROMISE)
  2147. reason = "keyTime";
  2148. else
  2149. reason = "CAkeyTime";
  2150. break;
  2151. }
  2152. revtm = X509_gmtime_adj(NULL, 0);
  2153. if (!revtm)
  2154. return NULL;
  2155. i = revtm->length + 1;
  2156. if (reason)
  2157. i += strlen(reason) + 1;
  2158. if (other)
  2159. i += strlen(other) + 1;
  2160. str = app_malloc(i, "revocation reason");
  2161. OPENSSL_strlcpy(str, (char *)revtm->data, i);
  2162. if (reason) {
  2163. OPENSSL_strlcat(str, ",", i);
  2164. OPENSSL_strlcat(str, reason, i);
  2165. }
  2166. if (other) {
  2167. OPENSSL_strlcat(str, ",", i);
  2168. OPENSSL_strlcat(str, other, i);
  2169. }
  2170. ASN1_UTCTIME_free(revtm);
  2171. return str;
  2172. }
  2173. /*-
  2174. * Convert revocation field to X509_REVOKED entry
  2175. * return code:
  2176. * 0 error
  2177. * 1 OK
  2178. * 2 OK and some extensions added (i.e. V2 CRL)
  2179. */
  2180. static int make_revoked(X509_REVOKED *rev, const char *str)
  2181. {
  2182. char *tmp = NULL;
  2183. int reason_code = -1;
  2184. int i, ret = 0;
  2185. ASN1_OBJECT *hold = NULL;
  2186. ASN1_GENERALIZEDTIME *comp_time = NULL;
  2187. ASN1_ENUMERATED *rtmp = NULL;
  2188. ASN1_TIME *revDate = NULL;
  2189. i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str);
  2190. if (i == 0)
  2191. goto end;
  2192. if (rev && !X509_REVOKED_set_revocationDate(rev, revDate))
  2193. goto end;
  2194. if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) {
  2195. rtmp = ASN1_ENUMERATED_new();
  2196. if (rtmp == NULL || !ASN1_ENUMERATED_set(rtmp, reason_code))
  2197. goto end;
  2198. if (X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0) <= 0)
  2199. goto end;
  2200. }
  2201. if (rev && comp_time) {
  2202. if (X509_REVOKED_add1_ext_i2d
  2203. (rev, NID_invalidity_date, comp_time, 0, 0) <= 0)
  2204. goto end;
  2205. }
  2206. if (rev && hold) {
  2207. if (X509_REVOKED_add1_ext_i2d
  2208. (rev, NID_hold_instruction_code, hold, 0, 0) <= 0)
  2209. goto end;
  2210. }
  2211. if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)
  2212. ret = 2;
  2213. else
  2214. ret = 1;
  2215. end:
  2216. OPENSSL_free(tmp);
  2217. ASN1_OBJECT_free(hold);
  2218. ASN1_GENERALIZEDTIME_free(comp_time);
  2219. ASN1_ENUMERATED_free(rtmp);
  2220. ASN1_TIME_free(revDate);
  2221. return ret;
  2222. }
  2223. static int old_entry_print(const ASN1_OBJECT *obj, const ASN1_STRING *str)
  2224. {
  2225. char buf[25], *pbuf;
  2226. const char *p;
  2227. int j;
  2228. j = i2a_ASN1_OBJECT(bio_err, obj);
  2229. pbuf = buf;
  2230. for (j = 22 - j; j > 0; j--)
  2231. *(pbuf++) = ' ';
  2232. *(pbuf++) = ':';
  2233. *(pbuf++) = '\0';
  2234. BIO_puts(bio_err, buf);
  2235. if (str->type == V_ASN1_PRINTABLESTRING)
  2236. BIO_printf(bio_err, "PRINTABLE:'");
  2237. else if (str->type == V_ASN1_T61STRING)
  2238. BIO_printf(bio_err, "T61STRING:'");
  2239. else if (str->type == V_ASN1_IA5STRING)
  2240. BIO_printf(bio_err, "IA5STRING:'");
  2241. else if (str->type == V_ASN1_UNIVERSALSTRING)
  2242. BIO_printf(bio_err, "UNIVERSALSTRING:'");
  2243. else
  2244. BIO_printf(bio_err, "ASN.1 %2d:'", str->type);
  2245. p = (const char *)str->data;
  2246. for (j = str->length; j > 0; j--) {
  2247. if ((*p >= ' ') && (*p <= '~'))
  2248. BIO_printf(bio_err, "%c", *p);
  2249. else if (*p & 0x80)
  2250. BIO_printf(bio_err, "\\0x%02X", *p);
  2251. else if ((unsigned char)*p == 0xf7)
  2252. BIO_printf(bio_err, "^?");
  2253. else
  2254. BIO_printf(bio_err, "^%c", *p + '@');
  2255. p++;
  2256. }
  2257. BIO_printf(bio_err, "'\n");
  2258. return 1;
  2259. }
  2260. int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold,
  2261. ASN1_GENERALIZEDTIME **pinvtm, const char *str)
  2262. {
  2263. char *tmp;
  2264. char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
  2265. int reason_code = -1;
  2266. int ret = 0;
  2267. unsigned int i;
  2268. ASN1_OBJECT *hold = NULL;
  2269. ASN1_GENERALIZEDTIME *comp_time = NULL;
  2270. tmp = OPENSSL_strdup(str);
  2271. if (!tmp) {
  2272. BIO_printf(bio_err, "memory allocation failure\n");
  2273. goto end;
  2274. }
  2275. p = strchr(tmp, ',');
  2276. rtime_str = tmp;
  2277. if (p) {
  2278. *p = '\0';
  2279. p++;
  2280. reason_str = p;
  2281. p = strchr(p, ',');
  2282. if (p) {
  2283. *p = '\0';
  2284. arg_str = p + 1;
  2285. }
  2286. }
  2287. if (prevtm) {
  2288. *prevtm = ASN1_UTCTIME_new();
  2289. if (*prevtm == NULL) {
  2290. BIO_printf(bio_err, "memory allocation failure\n");
  2291. goto end;
  2292. }
  2293. if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) {
  2294. BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
  2295. goto end;
  2296. }
  2297. }
  2298. if (reason_str) {
  2299. for (i = 0; i < NUM_REASONS; i++) {
  2300. if (OPENSSL_strcasecmp(reason_str, crl_reasons[i]) == 0) {
  2301. reason_code = i;
  2302. break;
  2303. }
  2304. }
  2305. if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) {
  2306. BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
  2307. goto end;
  2308. }
  2309. if (reason_code == 7) {
  2310. reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
  2311. } else if (reason_code == 8) { /* Hold instruction */
  2312. if (!arg_str) {
  2313. BIO_printf(bio_err, "missing hold instruction\n");
  2314. goto end;
  2315. }
  2316. reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
  2317. hold = OBJ_txt2obj(arg_str, 0);
  2318. if (!hold) {
  2319. BIO_printf(bio_err, "invalid object identifier %s\n", arg_str);
  2320. goto end;
  2321. }
  2322. if (phold)
  2323. *phold = hold;
  2324. else
  2325. ASN1_OBJECT_free(hold);
  2326. } else if ((reason_code == 9) || (reason_code == 10)) {
  2327. if (!arg_str) {
  2328. BIO_printf(bio_err, "missing compromised time\n");
  2329. goto end;
  2330. }
  2331. comp_time = ASN1_GENERALIZEDTIME_new();
  2332. if (comp_time == NULL) {
  2333. BIO_printf(bio_err, "memory allocation failure\n");
  2334. goto end;
  2335. }
  2336. if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) {
  2337. BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
  2338. goto end;
  2339. }
  2340. if (reason_code == 9)
  2341. reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
  2342. else
  2343. reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
  2344. }
  2345. }
  2346. if (preason)
  2347. *preason = reason_code;
  2348. if (pinvtm) {
  2349. *pinvtm = comp_time;
  2350. comp_time = NULL;
  2351. }
  2352. ret = 1;
  2353. end:
  2354. OPENSSL_free(tmp);
  2355. ASN1_GENERALIZEDTIME_free(comp_time);
  2356. return ret;
  2357. }