ca.c 87 KB

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