ca.c 87 KB

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