ca.c 87 KB

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