ca.c 86 KB

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