ca.c 84 KB

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