ca.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879
  1. /* apps/ca.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* The PPKI stuff has been donated by Jeff Barber <jeffb@issl.atl.hp.com> */
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <ctype.h>
  63. #include <sys/types.h>
  64. #include <sys/stat.h>
  65. #include <openssl/conf.h>
  66. #include <openssl/bio.h>
  67. #include <openssl/err.h>
  68. #include <openssl/bn.h>
  69. #include <openssl/txt_db.h>
  70. #include <openssl/evp.h>
  71. #include <openssl/x509.h>
  72. #include <openssl/x509v3.h>
  73. #include <openssl/objects.h>
  74. #include <openssl/ocsp.h>
  75. #include <openssl/pem.h>
  76. #ifndef W_OK
  77. # ifdef OPENSSL_SYS_VMS
  78. # if defined(__DECC)
  79. # include <unistd.h>
  80. # else
  81. # include <unixlib.h>
  82. # endif
  83. # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_NETWARE) && !defined(__TANDEM)
  84. # include <sys/file.h>
  85. # endif
  86. #endif
  87. #include "apps.h"
  88. #ifndef W_OK
  89. # define F_OK 0
  90. # define X_OK 1
  91. # define W_OK 2
  92. # define R_OK 4
  93. #endif
  94. #undef PROG
  95. #define PROG ca_main
  96. #define BASE_SECTION "ca"
  97. #define CONFIG_FILE "openssl.cnf"
  98. #define ENV_DEFAULT_CA "default_ca"
  99. #define STRING_MASK "string_mask"
  100. #define UTF8_IN "utf8"
  101. #define ENV_DIR "dir"
  102. #define ENV_CERTS "certs"
  103. #define ENV_CRL_DIR "crl_dir"
  104. #define ENV_CA_DB "CA_DB"
  105. #define ENV_NEW_CERTS_DIR "new_certs_dir"
  106. #define ENV_CERTIFICATE "certificate"
  107. #define ENV_SERIAL "serial"
  108. #define ENV_CRLNUMBER "crlnumber"
  109. #define ENV_CRL "crl"
  110. #define ENV_PRIVATE_KEY "private_key"
  111. #define ENV_RANDFILE "RANDFILE"
  112. #define ENV_DEFAULT_DAYS "default_days"
  113. #define ENV_DEFAULT_STARTDATE "default_startdate"
  114. #define ENV_DEFAULT_ENDDATE "default_enddate"
  115. #define ENV_DEFAULT_CRL_DAYS "default_crl_days"
  116. #define ENV_DEFAULT_CRL_HOURS "default_crl_hours"
  117. #define ENV_DEFAULT_MD "default_md"
  118. #define ENV_DEFAULT_EMAIL_DN "email_in_dn"
  119. #define ENV_PRESERVE "preserve"
  120. #define ENV_POLICY "policy"
  121. #define ENV_EXTENSIONS "x509_extensions"
  122. #define ENV_CRLEXT "crl_extensions"
  123. #define ENV_MSIE_HACK "msie_hack"
  124. #define ENV_NAMEOPT "name_opt"
  125. #define ENV_CERTOPT "cert_opt"
  126. #define ENV_EXTCOPY "copy_extensions"
  127. #define ENV_UNIQUE_SUBJECT "unique_subject"
  128. #define ENV_DATABASE "database"
  129. /* Additional revocation information types */
  130. #define REV_NONE 0 /* No addditional information */
  131. #define REV_CRL_REASON 1 /* Value is CRL reason code */
  132. #define REV_HOLD 2 /* Value is hold instruction */
  133. #define REV_KEY_COMPROMISE 3 /* Value is cert key compromise time */
  134. #define REV_CA_COMPROMISE 4 /* Value is CA key compromise time */
  135. static const char *ca_usage[] = {
  136. "usage: ca args\n",
  137. "\n",
  138. " -verbose - Talk alot while doing things\n",
  139. " -config file - A config file\n",
  140. " -name arg - The particular CA definition to use\n",
  141. " -gencrl - Generate a new CRL\n",
  142. " -crldays days - Days is when the next CRL is due\n",
  143. " -crlhours hours - Hours is when the next CRL is due\n",
  144. " -startdate YYMMDDHHMMSSZ - certificate validity notBefore\n",
  145. " -enddate YYMMDDHHMMSSZ - certificate validity notAfter (overrides -days)\n",
  146. " -days arg - number of days to certify the certificate for\n",
  147. " -md arg - md to use, one of md2, md5, sha or sha1\n",
  148. " -policy arg - The CA 'policy' to support\n",
  149. " -keyfile arg - private key file\n",
  150. " -keyform arg - private key file format (PEM or ENGINE)\n",
  151. " -key arg - key to decode the private key if it is encrypted\n",
  152. " -cert file - The CA certificate\n",
  153. " -selfsign - sign a certificate with the key associated with it\n",
  154. " -in file - The input PEM encoded certificate request(s)\n",
  155. " -out file - Where to put the output file(s)\n",
  156. " -outdir dir - Where to put output certificates\n",
  157. " -infiles .... - The last argument, requests to process\n",
  158. " -spkac file - File contains DN and signed public key and challenge\n",
  159. " -ss_cert file - File contains a self signed cert to sign\n",
  160. " -preserveDN - Don't re-order the DN\n",
  161. " -noemailDN - Don't add the EMAIL field into certificate' subject\n",
  162. " -batch - Don't ask questions\n",
  163. " -msie_hack - msie modifications to handle all those universal strings\n",
  164. " -revoke file - Revoke a certificate (given in file)\n",
  165. " -subj arg - Use arg instead of request's subject\n",
  166. " -utf8 - input characters are UTF8 (default ASCII)\n",
  167. " -multivalue-rdn - enable support for multivalued RDNs\n",
  168. " -extensions .. - Extension section (override value in config file)\n",
  169. " -extfile file - Configuration file with X509v3 extentions to add\n",
  170. " -crlexts .. - CRL extension section (override value in config file)\n",
  171. #ifndef OPENSSL_NO_ENGINE
  172. " -engine e - use engine e, possibly a hardware device.\n",
  173. #endif
  174. " -status serial - Shows certificate status given the serial number\n",
  175. " -updatedb - Updates db for expired certificates\n",
  176. NULL
  177. };
  178. #ifdef EFENCE
  179. extern int EF_PROTECT_FREE;
  180. extern int EF_PROTECT_BELOW;
  181. extern int EF_ALIGNMENT;
  182. #endif
  183. static void lookup_fail(const char *name, const char *tag);
  184. static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
  185. const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy,
  186. CA_DB *db, BIGNUM *serial, char *subj,
  187. unsigned long chtype, int multirdn, int email_dn,
  188. char *startdate, char *enddate, long days, int batch,
  189. char *ext_sect, CONF *conf, int verbose,
  190. unsigned long certopt, unsigned long nameopt,
  191. int default_op, int ext_copy, int selfsign);
  192. static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
  193. const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy,
  194. CA_DB *db, BIGNUM *serial, char *subj,
  195. unsigned long chtype, int multirdn, int email_dn,
  196. char *startdate, char *enddate, long days, int batch,
  197. char *ext_sect, CONF *conf, int verbose,
  198. unsigned long certopt, unsigned long nameopt,
  199. int default_op, int ext_copy, ENGINE *e);
  200. static int certify_spkac(X509 **xret, char *infile, EVP_PKEY *pkey,
  201. X509 *x509, const EVP_MD *dgst,
  202. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  203. BIGNUM *serial, char *subj, unsigned long chtype,
  204. int multirdn, int email_dn, char *startdate,
  205. char *enddate, long days, char *ext_sect, CONF *conf,
  206. int verbose, unsigned long certopt,
  207. unsigned long nameopt, int default_op, int ext_copy);
  208. static void write_new_certificate(BIO *bp, X509 *x, int output_der,
  209. int notext);
  210. static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
  211. const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy,
  212. CA_DB *db, BIGNUM *serial, char *subj,
  213. unsigned long chtype, int multirdn, int email_dn,
  214. char *startdate, char *enddate, long days, int batch,
  215. int verbose, X509_REQ *req, char *ext_sect, CONF *conf,
  216. unsigned long certopt, unsigned long nameopt,
  217. int default_op, int ext_copy, int selfsign);
  218. static int do_revoke(X509 *x509, CA_DB *db, int ext, char *extval);
  219. static int get_certificate_status(const char *ser_status, CA_DB *db);
  220. static int do_updatedb(CA_DB *db);
  221. static int check_time_format(const char *str);
  222. char *make_revocation_str(int rev_type, char *rev_arg);
  223. int make_revoked(X509_REVOKED *rev, const char *str);
  224. int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str);
  225. static CONF *conf = NULL;
  226. static CONF *extconf = NULL;
  227. static char *section = NULL;
  228. static int preserve = 0;
  229. static int msie_hack = 0;
  230. int MAIN(int, char **);
  231. int MAIN(int argc, char **argv)
  232. {
  233. ENGINE *e = NULL;
  234. char *key = NULL, *passargin = NULL;
  235. int create_ser = 0;
  236. int free_key = 0;
  237. int total = 0;
  238. int total_done = 0;
  239. int badops = 0;
  240. int ret = 1;
  241. int email_dn = 1;
  242. int req = 0;
  243. int verbose = 0;
  244. int gencrl = 0;
  245. int dorevoke = 0;
  246. int doupdatedb = 0;
  247. long crldays = 0;
  248. long crlhours = 0;
  249. long errorline = -1;
  250. char *configfile = NULL;
  251. char *md = NULL;
  252. char *policy = NULL;
  253. char *keyfile = NULL;
  254. char *certfile = NULL;
  255. int keyform = FORMAT_PEM;
  256. char *infile = NULL;
  257. char *spkac_file = NULL;
  258. char *ss_cert_file = NULL;
  259. char *ser_status = NULL;
  260. EVP_PKEY *pkey = NULL;
  261. int output_der = 0;
  262. char *outfile = NULL;
  263. char *outdir = NULL;
  264. char *serialfile = NULL;
  265. char *crlnumberfile = NULL;
  266. char *extensions = NULL;
  267. char *extfile = NULL;
  268. char *subj = NULL;
  269. unsigned long chtype = MBSTRING_ASC;
  270. int multirdn = 0;
  271. char *tmp_email_dn = NULL;
  272. char *crl_ext = NULL;
  273. int rev_type = REV_NONE;
  274. char *rev_arg = NULL;
  275. BIGNUM *serial = NULL;
  276. BIGNUM *crlnumber = NULL;
  277. char *startdate = NULL;
  278. char *enddate = NULL;
  279. long days = 0;
  280. int batch = 0;
  281. int notext = 0;
  282. unsigned long nameopt = 0, certopt = 0;
  283. int default_op = 1;
  284. int ext_copy = EXT_COPY_NONE;
  285. int selfsign = 0;
  286. X509 *x509 = NULL, *x509p = NULL;
  287. X509 *x = NULL;
  288. BIO *in = NULL, *out = NULL, *Sout = NULL, *Cout = NULL;
  289. char *dbfile = NULL;
  290. CA_DB *db = NULL;
  291. X509_CRL *crl = NULL;
  292. X509_REVOKED *r = NULL;
  293. ASN1_TIME *tmptm;
  294. ASN1_INTEGER *tmpser;
  295. char *f;
  296. const char *p, **pp;
  297. int i, j;
  298. const EVP_MD *dgst = NULL;
  299. STACK_OF(CONF_VALUE) *attribs = NULL;
  300. STACK_OF(X509) *cert_sk = NULL;
  301. #undef BSIZE
  302. #define BSIZE 256
  303. MS_STATIC char buf[3][BSIZE];
  304. char *randfile = NULL;
  305. #ifndef OPENSSL_NO_ENGINE
  306. char *engine = NULL;
  307. #endif
  308. char *tofree = NULL;
  309. DB_ATTR db_attr;
  310. #ifdef EFENCE
  311. EF_PROTECT_FREE = 1;
  312. EF_PROTECT_BELOW = 1;
  313. EF_ALIGNMENT = 0;
  314. #endif
  315. apps_startup();
  316. conf = NULL;
  317. key = NULL;
  318. section = NULL;
  319. preserve = 0;
  320. msie_hack = 0;
  321. if (bio_err == NULL)
  322. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  323. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  324. argc--;
  325. argv++;
  326. while (argc >= 1) {
  327. if (strcmp(*argv, "-verbose") == 0)
  328. verbose = 1;
  329. else if (strcmp(*argv, "-config") == 0) {
  330. if (--argc < 1)
  331. goto bad;
  332. configfile = *(++argv);
  333. } else if (strcmp(*argv, "-name") == 0) {
  334. if (--argc < 1)
  335. goto bad;
  336. section = *(++argv);
  337. } else if (strcmp(*argv, "-subj") == 0) {
  338. if (--argc < 1)
  339. goto bad;
  340. subj = *(++argv);
  341. /* preserve=1; */
  342. } else if (strcmp(*argv, "-utf8") == 0)
  343. chtype = MBSTRING_UTF8;
  344. else if (strcmp(*argv, "-create_serial") == 0)
  345. create_ser = 1;
  346. else if (strcmp(*argv, "-multivalue-rdn") == 0)
  347. multirdn = 1;
  348. else if (strcmp(*argv, "-startdate") == 0) {
  349. if (--argc < 1)
  350. goto bad;
  351. startdate = *(++argv);
  352. } else if (strcmp(*argv, "-enddate") == 0) {
  353. if (--argc < 1)
  354. goto bad;
  355. enddate = *(++argv);
  356. } else if (strcmp(*argv, "-days") == 0) {
  357. if (--argc < 1)
  358. goto bad;
  359. days = atoi(*(++argv));
  360. } else if (strcmp(*argv, "-md") == 0) {
  361. if (--argc < 1)
  362. goto bad;
  363. md = *(++argv);
  364. } else if (strcmp(*argv, "-policy") == 0) {
  365. if (--argc < 1)
  366. goto bad;
  367. policy = *(++argv);
  368. } else if (strcmp(*argv, "-keyfile") == 0) {
  369. if (--argc < 1)
  370. goto bad;
  371. keyfile = *(++argv);
  372. } else if (strcmp(*argv, "-keyform") == 0) {
  373. if (--argc < 1)
  374. goto bad;
  375. keyform = str2fmt(*(++argv));
  376. } else if (strcmp(*argv, "-passin") == 0) {
  377. if (--argc < 1)
  378. goto bad;
  379. passargin = *(++argv);
  380. } else if (strcmp(*argv, "-key") == 0) {
  381. if (--argc < 1)
  382. goto bad;
  383. key = *(++argv);
  384. } else if (strcmp(*argv, "-cert") == 0) {
  385. if (--argc < 1)
  386. goto bad;
  387. certfile = *(++argv);
  388. } else if (strcmp(*argv, "-selfsign") == 0)
  389. selfsign = 1;
  390. else if (strcmp(*argv, "-in") == 0) {
  391. if (--argc < 1)
  392. goto bad;
  393. infile = *(++argv);
  394. req = 1;
  395. } else if (strcmp(*argv, "-out") == 0) {
  396. if (--argc < 1)
  397. goto bad;
  398. outfile = *(++argv);
  399. } else if (strcmp(*argv, "-outdir") == 0) {
  400. if (--argc < 1)
  401. goto bad;
  402. outdir = *(++argv);
  403. } else if (strcmp(*argv, "-notext") == 0)
  404. notext = 1;
  405. else if (strcmp(*argv, "-batch") == 0)
  406. batch = 1;
  407. else if (strcmp(*argv, "-preserveDN") == 0)
  408. preserve = 1;
  409. else if (strcmp(*argv, "-noemailDN") == 0)
  410. email_dn = 0;
  411. else if (strcmp(*argv, "-gencrl") == 0)
  412. gencrl = 1;
  413. else if (strcmp(*argv, "-msie_hack") == 0)
  414. msie_hack = 1;
  415. else if (strcmp(*argv, "-crldays") == 0) {
  416. if (--argc < 1)
  417. goto bad;
  418. crldays = atol(*(++argv));
  419. } else if (strcmp(*argv, "-crlhours") == 0) {
  420. if (--argc < 1)
  421. goto bad;
  422. crlhours = atol(*(++argv));
  423. } else if (strcmp(*argv, "-infiles") == 0) {
  424. argc--;
  425. argv++;
  426. req = 1;
  427. break;
  428. } else if (strcmp(*argv, "-ss_cert") == 0) {
  429. if (--argc < 1)
  430. goto bad;
  431. ss_cert_file = *(++argv);
  432. req = 1;
  433. } else if (strcmp(*argv, "-spkac") == 0) {
  434. if (--argc < 1)
  435. goto bad;
  436. spkac_file = *(++argv);
  437. req = 1;
  438. } else if (strcmp(*argv, "-revoke") == 0) {
  439. if (--argc < 1)
  440. goto bad;
  441. infile = *(++argv);
  442. dorevoke = 1;
  443. } else if (strcmp(*argv, "-extensions") == 0) {
  444. if (--argc < 1)
  445. goto bad;
  446. extensions = *(++argv);
  447. } else if (strcmp(*argv, "-extfile") == 0) {
  448. if (--argc < 1)
  449. goto bad;
  450. extfile = *(++argv);
  451. } else if (strcmp(*argv, "-status") == 0) {
  452. if (--argc < 1)
  453. goto bad;
  454. ser_status = *(++argv);
  455. } else if (strcmp(*argv, "-updatedb") == 0) {
  456. doupdatedb = 1;
  457. } else if (strcmp(*argv, "-crlexts") == 0) {
  458. if (--argc < 1)
  459. goto bad;
  460. crl_ext = *(++argv);
  461. } else if (strcmp(*argv, "-crl_reason") == 0) {
  462. if (--argc < 1)
  463. goto bad;
  464. rev_arg = *(++argv);
  465. rev_type = REV_CRL_REASON;
  466. } else if (strcmp(*argv, "-crl_hold") == 0) {
  467. if (--argc < 1)
  468. goto bad;
  469. rev_arg = *(++argv);
  470. rev_type = REV_HOLD;
  471. } else if (strcmp(*argv, "-crl_compromise") == 0) {
  472. if (--argc < 1)
  473. goto bad;
  474. rev_arg = *(++argv);
  475. rev_type = REV_KEY_COMPROMISE;
  476. } else if (strcmp(*argv, "-crl_CA_compromise") == 0) {
  477. if (--argc < 1)
  478. goto bad;
  479. rev_arg = *(++argv);
  480. rev_type = REV_CA_COMPROMISE;
  481. }
  482. #ifndef OPENSSL_NO_ENGINE
  483. else if (strcmp(*argv, "-engine") == 0) {
  484. if (--argc < 1)
  485. goto bad;
  486. engine = *(++argv);
  487. }
  488. #endif
  489. else {
  490. bad:
  491. BIO_printf(bio_err, "unknown option %s\n", *argv);
  492. badops = 1;
  493. break;
  494. }
  495. argc--;
  496. argv++;
  497. }
  498. if (badops) {
  499. for (pp = ca_usage; (*pp != NULL); pp++)
  500. BIO_printf(bio_err, "%s", *pp);
  501. goto err;
  502. }
  503. ERR_load_crypto_strings();
  504. /*****************************************************************/
  505. tofree = NULL;
  506. if (configfile == NULL)
  507. configfile = getenv("OPENSSL_CONF");
  508. if (configfile == NULL)
  509. configfile = getenv("SSLEAY_CONF");
  510. if (configfile == NULL) {
  511. const char *s = X509_get_default_cert_area();
  512. size_t len;
  513. #ifdef OPENSSL_SYS_VMS
  514. len = strlen(s) + sizeof(CONFIG_FILE);
  515. tofree = OPENSSL_malloc(len);
  516. strcpy(tofree, s);
  517. #else
  518. len = strlen(s) + sizeof(CONFIG_FILE) + 1;
  519. tofree = OPENSSL_malloc(len);
  520. BUF_strlcpy(tofree, s, len);
  521. BUF_strlcat(tofree, "/", len);
  522. #endif
  523. BUF_strlcat(tofree, CONFIG_FILE, len);
  524. configfile = tofree;
  525. }
  526. BIO_printf(bio_err, "Using configuration from %s\n", configfile);
  527. conf = NCONF_new(NULL);
  528. if (NCONF_load(conf, configfile, &errorline) <= 0) {
  529. if (errorline <= 0)
  530. BIO_printf(bio_err, "error loading the config file '%s'\n",
  531. configfile);
  532. else
  533. BIO_printf(bio_err, "error on line %ld of config file '%s'\n",
  534. errorline, configfile);
  535. goto err;
  536. }
  537. if (tofree) {
  538. OPENSSL_free(tofree);
  539. tofree = NULL;
  540. }
  541. if (!load_config(bio_err, conf))
  542. goto err;
  543. #ifndef OPENSSL_NO_ENGINE
  544. e = setup_engine(bio_err, engine, 0);
  545. #endif
  546. /* Lets get the config section we are using */
  547. if (section == NULL) {
  548. section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_CA);
  549. if (section == NULL) {
  550. lookup_fail(BASE_SECTION, ENV_DEFAULT_CA);
  551. goto err;
  552. }
  553. }
  554. if (conf != NULL) {
  555. p = NCONF_get_string(conf, NULL, "oid_file");
  556. if (p == NULL)
  557. ERR_clear_error();
  558. if (p != NULL) {
  559. BIO *oid_bio;
  560. oid_bio = BIO_new_file(p, "r");
  561. if (oid_bio == NULL) {
  562. /*-
  563. BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
  564. ERR_print_errors(bio_err);
  565. */
  566. ERR_clear_error();
  567. } else {
  568. OBJ_create_objects(oid_bio);
  569. BIO_free(oid_bio);
  570. }
  571. }
  572. if (!add_oid_section(bio_err, conf)) {
  573. ERR_print_errors(bio_err);
  574. goto err;
  575. }
  576. }
  577. randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE");
  578. if (randfile == NULL)
  579. ERR_clear_error();
  580. app_RAND_load_file(randfile, bio_err, 0);
  581. f = NCONF_get_string(conf, section, STRING_MASK);
  582. if (!f)
  583. ERR_clear_error();
  584. if (f && !ASN1_STRING_set_default_mask_asc(f)) {
  585. BIO_printf(bio_err, "Invalid global string mask setting %s\n", f);
  586. goto err;
  587. }
  588. if (chtype != MBSTRING_UTF8) {
  589. f = NCONF_get_string(conf, section, UTF8_IN);
  590. if (!f)
  591. ERR_clear_error();
  592. else if (!strcmp(f, "yes"))
  593. chtype = MBSTRING_UTF8;
  594. }
  595. db_attr.unique_subject = 1;
  596. p = NCONF_get_string(conf, section, ENV_UNIQUE_SUBJECT);
  597. if (p) {
  598. #ifdef RL_DEBUG
  599. BIO_printf(bio_err, "DEBUG: unique_subject = \"%s\"\n", p);
  600. #endif
  601. db_attr.unique_subject = parse_yesno(p, 1);
  602. } else
  603. ERR_clear_error();
  604. #ifdef RL_DEBUG
  605. if (!p)
  606. BIO_printf(bio_err, "DEBUG: unique_subject undefined\n", p);
  607. #endif
  608. #ifdef RL_DEBUG
  609. BIO_printf(bio_err, "DEBUG: configured unique_subject is %d\n",
  610. db_attr.unique_subject);
  611. #endif
  612. in = BIO_new(BIO_s_file());
  613. out = BIO_new(BIO_s_file());
  614. Sout = BIO_new(BIO_s_file());
  615. Cout = BIO_new(BIO_s_file());
  616. if ((in == NULL) || (out == NULL) || (Sout == NULL) || (Cout == NULL)) {
  617. ERR_print_errors(bio_err);
  618. goto err;
  619. }
  620. /*****************************************************************/
  621. /* report status of cert with serial number given on command line */
  622. if (ser_status) {
  623. if ((dbfile = NCONF_get_string(conf, section, ENV_DATABASE)) == NULL) {
  624. lookup_fail(section, ENV_DATABASE);
  625. goto err;
  626. }
  627. db = load_index(dbfile, &db_attr);
  628. if (db == NULL)
  629. goto err;
  630. if (!index_index(db))
  631. goto err;
  632. if (get_certificate_status(ser_status, db) != 1)
  633. BIO_printf(bio_err, "Error verifying serial %s!\n", ser_status);
  634. goto err;
  635. }
  636. /*****************************************************************/
  637. /* we definitely need a private key, so let's get it */
  638. if ((keyfile == NULL) && ((keyfile = NCONF_get_string(conf,
  639. section,
  640. ENV_PRIVATE_KEY)) ==
  641. NULL)) {
  642. lookup_fail(section, ENV_PRIVATE_KEY);
  643. goto err;
  644. }
  645. if (!key) {
  646. free_key = 1;
  647. if (!app_passwd(bio_err, passargin, NULL, &key, NULL)) {
  648. BIO_printf(bio_err, "Error getting password\n");
  649. goto err;
  650. }
  651. }
  652. pkey = load_key(bio_err, keyfile, keyform, 0, key, e, "CA private key");
  653. if (key)
  654. OPENSSL_cleanse(key, strlen(key));
  655. if (pkey == NULL) {
  656. /* load_key() has already printed an appropriate message */
  657. goto err;
  658. }
  659. /*****************************************************************/
  660. /* we need a certificate */
  661. if (!selfsign || spkac_file || ss_cert_file || gencrl) {
  662. if ((certfile == NULL)
  663. && ((certfile = NCONF_get_string(conf,
  664. section,
  665. ENV_CERTIFICATE)) == NULL)) {
  666. lookup_fail(section, ENV_CERTIFICATE);
  667. goto err;
  668. }
  669. x509 = load_cert(bio_err, certfile, FORMAT_PEM, NULL, e,
  670. "CA certificate");
  671. if (x509 == NULL)
  672. goto err;
  673. if (!X509_check_private_key(x509, pkey)) {
  674. BIO_printf(bio_err,
  675. "CA certificate and CA private key do not match\n");
  676. goto err;
  677. }
  678. }
  679. if (!selfsign)
  680. x509p = x509;
  681. f = NCONF_get_string(conf, BASE_SECTION, ENV_PRESERVE);
  682. if (f == NULL)
  683. ERR_clear_error();
  684. if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
  685. preserve = 1;
  686. f = NCONF_get_string(conf, BASE_SECTION, ENV_MSIE_HACK);
  687. if (f == NULL)
  688. ERR_clear_error();
  689. if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
  690. msie_hack = 1;
  691. f = NCONF_get_string(conf, section, ENV_NAMEOPT);
  692. if (f) {
  693. if (!set_name_ex(&nameopt, f)) {
  694. BIO_printf(bio_err, "Invalid name options: \"%s\"\n", f);
  695. goto err;
  696. }
  697. default_op = 0;
  698. } else
  699. ERR_clear_error();
  700. f = NCONF_get_string(conf, section, ENV_CERTOPT);
  701. if (f) {
  702. if (!set_cert_ex(&certopt, f)) {
  703. BIO_printf(bio_err, "Invalid certificate options: \"%s\"\n", f);
  704. goto err;
  705. }
  706. default_op = 0;
  707. } else
  708. ERR_clear_error();
  709. f = NCONF_get_string(conf, section, ENV_EXTCOPY);
  710. if (f) {
  711. if (!set_ext_copy(&ext_copy, f)) {
  712. BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n", f);
  713. goto err;
  714. }
  715. } else
  716. ERR_clear_error();
  717. /*****************************************************************/
  718. /* lookup where to write new certificates */
  719. if ((outdir == NULL) && (req)) {
  720. struct stat sb;
  721. if ((outdir = NCONF_get_string(conf, section, ENV_NEW_CERTS_DIR))
  722. == NULL) {
  723. BIO_printf(bio_err,
  724. "there needs to be defined a directory for new certificate to be placed in\n");
  725. goto err;
  726. }
  727. #ifndef OPENSSL_SYS_VMS
  728. /*
  729. * outdir is a directory spec, but access() for VMS demands a
  730. * filename. In any case, stat(), below, will catch the problem if
  731. * outdir is not a directory spec, and the fopen() or open() will
  732. * catch an error if there is no write access.
  733. *
  734. * Presumably, this problem could also be solved by using the DEC C
  735. * routines to convert the directory syntax to Unixly, and give that
  736. * to access(). However, time's too short to do that just now.
  737. */
  738. if (access(outdir, R_OK | W_OK | X_OK) != 0) {
  739. BIO_printf(bio_err, "I am unable to access the %s directory\n",
  740. outdir);
  741. perror(outdir);
  742. goto err;
  743. }
  744. if (stat(outdir, &sb) != 0) {
  745. BIO_printf(bio_err, "unable to stat(%s)\n", outdir);
  746. perror(outdir);
  747. goto err;
  748. }
  749. # ifdef S_ISDIR
  750. if (!S_ISDIR(sb.st_mode)) {
  751. BIO_printf(bio_err, "%s need to be a directory\n", outdir);
  752. perror(outdir);
  753. goto err;
  754. }
  755. # endif
  756. #endif
  757. }
  758. /*****************************************************************/
  759. /* we need to load the database file */
  760. if ((dbfile = NCONF_get_string(conf, section, ENV_DATABASE)) == NULL) {
  761. lookup_fail(section, ENV_DATABASE);
  762. goto err;
  763. }
  764. db = load_index(dbfile, &db_attr);
  765. if (db == NULL)
  766. goto err;
  767. /* Lets check some fields */
  768. for (i = 0; i < sk_num(db->db->data); i++) {
  769. pp = (const char **)sk_value(db->db->data, i);
  770. if ((pp[DB_type][0] != DB_TYPE_REV) && (pp[DB_rev_date][0] != '\0')) {
  771. BIO_printf(bio_err,
  772. "entry %d: not revoked yet, but has a revocation date\n",
  773. i + 1);
  774. goto err;
  775. }
  776. if ((pp[DB_type][0] == DB_TYPE_REV) &&
  777. !make_revoked(NULL, pp[DB_rev_date])) {
  778. BIO_printf(bio_err, " in entry %d\n", i + 1);
  779. goto err;
  780. }
  781. if (!check_time_format(pp[DB_exp_date])) {
  782. BIO_printf(bio_err, "entry %d: invalid expiry date\n", i + 1);
  783. goto err;
  784. }
  785. p = pp[DB_serial];
  786. j = strlen(p);
  787. if (*p == '-') {
  788. p++;
  789. j--;
  790. }
  791. if ((j & 1) || (j < 2)) {
  792. BIO_printf(bio_err, "entry %d: bad serial number length (%d)\n",
  793. i + 1, j);
  794. goto err;
  795. }
  796. while (*p) {
  797. if (!(((*p >= '0') && (*p <= '9')) ||
  798. ((*p >= 'A') && (*p <= 'F')) ||
  799. ((*p >= 'a') && (*p <= 'f')))) {
  800. BIO_printf(bio_err,
  801. "entry %d: bad serial number characters, char pos %ld, char is '%c'\n",
  802. i + 1, (long)(p - pp[DB_serial]), *p);
  803. goto err;
  804. }
  805. p++;
  806. }
  807. }
  808. if (verbose) {
  809. BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT); /* cannot fail */
  810. #ifdef OPENSSL_SYS_VMS
  811. {
  812. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  813. out = BIO_push(tmpbio, out);
  814. }
  815. #endif
  816. TXT_DB_write(out, db->db);
  817. BIO_printf(bio_err, "%d entries loaded from the database\n",
  818. db->db->data->num);
  819. BIO_printf(bio_err, "generating index\n");
  820. }
  821. if (!index_index(db))
  822. goto err;
  823. /*****************************************************************/
  824. /* Update the db file for expired certificates */
  825. if (doupdatedb) {
  826. if (verbose)
  827. BIO_printf(bio_err, "Updating %s ...\n", dbfile);
  828. i = do_updatedb(db);
  829. if (i == -1) {
  830. BIO_printf(bio_err, "Malloc failure\n");
  831. goto err;
  832. } else if (i == 0) {
  833. if (verbose)
  834. BIO_printf(bio_err, "No entries found to mark expired\n");
  835. } else {
  836. if (!save_index(dbfile, "new", db))
  837. goto err;
  838. if (!rotate_index(dbfile, "new", "old"))
  839. goto err;
  840. if (verbose)
  841. BIO_printf(bio_err,
  842. "Done. %d entries marked as expired\n", i);
  843. }
  844. }
  845. /*****************************************************************/
  846. /* Read extentions config file */
  847. if (extfile) {
  848. extconf = NCONF_new(NULL);
  849. if (NCONF_load(extconf, extfile, &errorline) <= 0) {
  850. if (errorline <= 0)
  851. BIO_printf(bio_err, "ERROR: loading the config file '%s'\n",
  852. extfile);
  853. else
  854. BIO_printf(bio_err,
  855. "ERROR: on line %ld of config file '%s'\n",
  856. errorline, extfile);
  857. ret = 1;
  858. goto err;
  859. }
  860. if (verbose)
  861. BIO_printf(bio_err, "Successfully loaded extensions file %s\n",
  862. extfile);
  863. /* We can have sections in the ext file */
  864. if (!extensions
  865. && !(extensions =
  866. NCONF_get_string(extconf, "default", "extensions")))
  867. extensions = "default";
  868. }
  869. /*****************************************************************/
  870. if (req || gencrl) {
  871. if (outfile != NULL) {
  872. if (BIO_write_filename(Sout, outfile) <= 0) {
  873. perror(outfile);
  874. goto err;
  875. }
  876. } else {
  877. BIO_set_fp(Sout, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
  878. #ifdef OPENSSL_SYS_VMS
  879. {
  880. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  881. Sout = BIO_push(tmpbio, Sout);
  882. }
  883. #endif
  884. }
  885. }
  886. if ((md == NULL) && ((md = NCONF_get_string(conf,
  887. section,
  888. ENV_DEFAULT_MD)) == NULL)) {
  889. lookup_fail(section, ENV_DEFAULT_MD);
  890. goto err;
  891. }
  892. if ((dgst = EVP_get_digestbyname(md)) == NULL) {
  893. BIO_printf(bio_err, "%s is an unsupported message digest type\n", md);
  894. goto err;
  895. }
  896. if (req) {
  897. if ((email_dn == 1) && ((tmp_email_dn = NCONF_get_string(conf,
  898. section,
  899. ENV_DEFAULT_EMAIL_DN))
  900. != NULL)) {
  901. if (strcmp(tmp_email_dn, "no") == 0)
  902. email_dn = 0;
  903. }
  904. if (verbose)
  905. BIO_printf(bio_err, "message digest is %s\n",
  906. OBJ_nid2ln(dgst->type));
  907. if ((policy == NULL) && ((policy = NCONF_get_string(conf,
  908. section,
  909. ENV_POLICY)) ==
  910. NULL)) {
  911. lookup_fail(section, ENV_POLICY);
  912. goto err;
  913. }
  914. if (verbose)
  915. BIO_printf(bio_err, "policy is %s\n", policy);
  916. if ((serialfile = NCONF_get_string(conf, section, ENV_SERIAL))
  917. == NULL) {
  918. lookup_fail(section, ENV_SERIAL);
  919. goto err;
  920. }
  921. if (!extconf) {
  922. /*
  923. * no '-extfile' option, so we look for extensions in the main
  924. * configuration file
  925. */
  926. if (!extensions) {
  927. extensions = NCONF_get_string(conf, section, ENV_EXTENSIONS);
  928. if (!extensions)
  929. ERR_clear_error();
  930. }
  931. if (extensions) {
  932. /* Check syntax of file */
  933. X509V3_CTX ctx;
  934. X509V3_set_ctx_test(&ctx);
  935. X509V3_set_nconf(&ctx, conf);
  936. if (!X509V3_EXT_add_nconf(conf, &ctx, extensions, NULL)) {
  937. BIO_printf(bio_err,
  938. "Error Loading extension section %s\n",
  939. extensions);
  940. ret = 1;
  941. goto err;
  942. }
  943. }
  944. }
  945. if (startdate == NULL) {
  946. startdate = NCONF_get_string(conf, section,
  947. ENV_DEFAULT_STARTDATE);
  948. if (startdate == NULL)
  949. ERR_clear_error();
  950. }
  951. if (startdate && !ASN1_UTCTIME_set_string(NULL, startdate)) {
  952. BIO_printf(bio_err,
  953. "start date is invalid, it should be YYMMDDHHMMSSZ\n");
  954. goto err;
  955. }
  956. if (startdate == NULL)
  957. startdate = "today";
  958. if (enddate == NULL) {
  959. enddate = NCONF_get_string(conf, section, ENV_DEFAULT_ENDDATE);
  960. if (enddate == NULL)
  961. ERR_clear_error();
  962. }
  963. if (enddate && !ASN1_UTCTIME_set_string(NULL, enddate)) {
  964. BIO_printf(bio_err,
  965. "end date is invalid, it should be YYMMDDHHMMSSZ\n");
  966. goto err;
  967. }
  968. if (days == 0) {
  969. if (!NCONF_get_number(conf, section, ENV_DEFAULT_DAYS, &days))
  970. days = 0;
  971. }
  972. if (!enddate && (days == 0)) {
  973. BIO_printf(bio_err,
  974. "cannot lookup how many days to certify for\n");
  975. goto err;
  976. }
  977. if ((serial = load_serial(serialfile, create_ser, NULL)) == NULL) {
  978. BIO_printf(bio_err, "error while loading serial number\n");
  979. goto err;
  980. }
  981. if (verbose) {
  982. if (BN_is_zero(serial))
  983. BIO_printf(bio_err, "next serial number is 00\n");
  984. else {
  985. if ((f = BN_bn2hex(serial)) == NULL)
  986. goto err;
  987. BIO_printf(bio_err, "next serial number is %s\n", f);
  988. OPENSSL_free(f);
  989. }
  990. }
  991. if ((attribs = NCONF_get_section(conf, policy)) == NULL) {
  992. BIO_printf(bio_err, "unable to find 'section' for %s\n", policy);
  993. goto err;
  994. }
  995. if ((cert_sk = sk_X509_new_null()) == NULL) {
  996. BIO_printf(bio_err, "Memory allocation failure\n");
  997. goto err;
  998. }
  999. if (spkac_file != NULL) {
  1000. total++;
  1001. j = certify_spkac(&x, spkac_file, pkey, x509, dgst, attribs, db,
  1002. serial, subj, chtype, multirdn, email_dn,
  1003. startdate, enddate, days, extensions, conf,
  1004. verbose, certopt, nameopt, default_op,
  1005. ext_copy);
  1006. if (j < 0)
  1007. goto err;
  1008. if (j > 0) {
  1009. total_done++;
  1010. BIO_printf(bio_err, "\n");
  1011. if (!BN_add_word(serial, 1))
  1012. goto err;
  1013. if (!sk_X509_push(cert_sk, x)) {
  1014. BIO_printf(bio_err, "Memory allocation failure\n");
  1015. goto err;
  1016. }
  1017. if (outfile) {
  1018. output_der = 1;
  1019. batch = 1;
  1020. }
  1021. }
  1022. }
  1023. if (ss_cert_file != NULL) {
  1024. total++;
  1025. j = certify_cert(&x, ss_cert_file, pkey, x509, dgst, attribs,
  1026. db, serial, subj, chtype, multirdn, email_dn,
  1027. startdate, enddate, days, batch, extensions,
  1028. conf, verbose, certopt, nameopt, default_op,
  1029. ext_copy, e);
  1030. if (j < 0)
  1031. goto err;
  1032. if (j > 0) {
  1033. total_done++;
  1034. BIO_printf(bio_err, "\n");
  1035. if (!BN_add_word(serial, 1))
  1036. goto err;
  1037. if (!sk_X509_push(cert_sk, x)) {
  1038. BIO_printf(bio_err, "Memory allocation failure\n");
  1039. goto err;
  1040. }
  1041. }
  1042. }
  1043. if (infile != NULL) {
  1044. total++;
  1045. j = certify(&x, infile, pkey, x509p, dgst, attribs, db,
  1046. serial, subj, chtype, multirdn, email_dn, startdate,
  1047. enddate, days, batch, extensions, conf, verbose,
  1048. certopt, nameopt, default_op, ext_copy, selfsign);
  1049. if (j < 0)
  1050. goto err;
  1051. if (j > 0) {
  1052. total_done++;
  1053. BIO_printf(bio_err, "\n");
  1054. if (!BN_add_word(serial, 1))
  1055. goto err;
  1056. if (!sk_X509_push(cert_sk, x)) {
  1057. BIO_printf(bio_err, "Memory allocation failure\n");
  1058. goto err;
  1059. }
  1060. }
  1061. }
  1062. for (i = 0; i < argc; i++) {
  1063. total++;
  1064. j = certify(&x, argv[i], pkey, x509p, dgst, attribs, db,
  1065. serial, subj, chtype, multirdn, email_dn, startdate,
  1066. enddate, days, batch, extensions, conf, verbose,
  1067. certopt, nameopt, default_op, ext_copy, selfsign);
  1068. if (j < 0)
  1069. goto err;
  1070. if (j > 0) {
  1071. total_done++;
  1072. BIO_printf(bio_err, "\n");
  1073. if (!BN_add_word(serial, 1))
  1074. goto err;
  1075. if (!sk_X509_push(cert_sk, x)) {
  1076. BIO_printf(bio_err, "Memory allocation failure\n");
  1077. goto err;
  1078. }
  1079. }
  1080. }
  1081. /*
  1082. * we have a stack of newly certified certificates and a data base
  1083. * and serial number that need updating
  1084. */
  1085. if (sk_X509_num(cert_sk) > 0) {
  1086. if (!batch) {
  1087. BIO_printf(bio_err,
  1088. "\n%d out of %d certificate requests certified, commit? [y/n]",
  1089. total_done, total);
  1090. (void)BIO_flush(bio_err);
  1091. buf[0][0] = '\0';
  1092. if (!fgets(buf[0], 10, stdin)) {
  1093. BIO_printf(bio_err,
  1094. "CERTIFICATION CANCELED: I/O error\n");
  1095. ret = 0;
  1096. goto err;
  1097. }
  1098. if ((buf[0][0] != 'y') && (buf[0][0] != 'Y')) {
  1099. BIO_printf(bio_err, "CERTIFICATION CANCELED\n");
  1100. ret = 0;
  1101. goto err;
  1102. }
  1103. }
  1104. BIO_printf(bio_err, "Write out database with %d new entries\n",
  1105. sk_X509_num(cert_sk));
  1106. if (!save_serial(serialfile, "new", serial, NULL))
  1107. goto err;
  1108. if (!save_index(dbfile, "new", db))
  1109. goto err;
  1110. }
  1111. if (verbose)
  1112. BIO_printf(bio_err, "writing new certificates\n");
  1113. for (i = 0; i < sk_X509_num(cert_sk); i++) {
  1114. int k;
  1115. char *n;
  1116. x = sk_X509_value(cert_sk, i);
  1117. j = x->cert_info->serialNumber->length;
  1118. p = (const char *)x->cert_info->serialNumber->data;
  1119. if (strlen(outdir) >= (size_t)(j ? BSIZE - j * 2 - 6 : BSIZE - 8)) {
  1120. BIO_printf(bio_err, "certificate file name too long\n");
  1121. goto err;
  1122. }
  1123. strcpy(buf[2], outdir);
  1124. #ifndef OPENSSL_SYS_VMS
  1125. BUF_strlcat(buf[2], "/", sizeof(buf[2]));
  1126. #endif
  1127. n = (char *)&(buf[2][strlen(buf[2])]);
  1128. if (j > 0) {
  1129. for (k = 0; k < j; k++) {
  1130. if (n >= &(buf[2][sizeof(buf[2])]))
  1131. break;
  1132. BIO_snprintf(n,
  1133. &buf[2][0] + sizeof(buf[2]) - n,
  1134. "%02X", (unsigned char)*(p++));
  1135. n += 2;
  1136. }
  1137. } else {
  1138. *(n++) = '0';
  1139. *(n++) = '0';
  1140. }
  1141. *(n++) = '.';
  1142. *(n++) = 'p';
  1143. *(n++) = 'e';
  1144. *(n++) = 'm';
  1145. *n = '\0';
  1146. if (verbose)
  1147. BIO_printf(bio_err, "writing %s\n", buf[2]);
  1148. if (BIO_write_filename(Cout, buf[2]) <= 0) {
  1149. perror(buf[2]);
  1150. goto err;
  1151. }
  1152. write_new_certificate(Cout, x, 0, notext);
  1153. write_new_certificate(Sout, x, output_der, notext);
  1154. }
  1155. if (sk_X509_num(cert_sk)) {
  1156. /* Rename the database and the serial file */
  1157. if (!rotate_serial(serialfile, "new", "old"))
  1158. goto err;
  1159. if (!rotate_index(dbfile, "new", "old"))
  1160. goto err;
  1161. BIO_printf(bio_err, "Data Base Updated\n");
  1162. }
  1163. }
  1164. /*****************************************************************/
  1165. if (gencrl) {
  1166. int crl_v2 = 0;
  1167. if (!crl_ext) {
  1168. crl_ext = NCONF_get_string(conf, section, ENV_CRLEXT);
  1169. if (!crl_ext)
  1170. ERR_clear_error();
  1171. }
  1172. if (crl_ext) {
  1173. /* Check syntax of file */
  1174. X509V3_CTX ctx;
  1175. X509V3_set_ctx_test(&ctx);
  1176. X509V3_set_nconf(&ctx, conf);
  1177. if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL)) {
  1178. BIO_printf(bio_err,
  1179. "Error Loading CRL extension section %s\n",
  1180. crl_ext);
  1181. ret = 1;
  1182. goto err;
  1183. }
  1184. }
  1185. if ((crlnumberfile = NCONF_get_string(conf, section, ENV_CRLNUMBER))
  1186. != NULL)
  1187. if ((crlnumber = load_serial(crlnumberfile, 0, NULL)) == NULL) {
  1188. BIO_printf(bio_err, "error while loading CRL number\n");
  1189. goto err;
  1190. }
  1191. if (!crldays && !crlhours) {
  1192. if (!NCONF_get_number(conf, section,
  1193. ENV_DEFAULT_CRL_DAYS, &crldays))
  1194. crldays = 0;
  1195. if (!NCONF_get_number(conf, section,
  1196. ENV_DEFAULT_CRL_HOURS, &crlhours))
  1197. crlhours = 0;
  1198. }
  1199. if ((crldays == 0) && (crlhours == 0)) {
  1200. BIO_printf(bio_err,
  1201. "cannot lookup how long until the next CRL is issued\n");
  1202. goto err;
  1203. }
  1204. if (verbose)
  1205. BIO_printf(bio_err, "making CRL\n");
  1206. if ((crl = X509_CRL_new()) == NULL)
  1207. goto err;
  1208. if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509)))
  1209. goto err;
  1210. tmptm = ASN1_TIME_new();
  1211. if (!tmptm)
  1212. goto err;
  1213. X509_gmtime_adj(tmptm, 0);
  1214. X509_CRL_set_lastUpdate(crl, tmptm);
  1215. X509_gmtime_adj(tmptm, (crldays * 24 + crlhours) * 60 * 60);
  1216. X509_CRL_set_nextUpdate(crl, tmptm);
  1217. ASN1_TIME_free(tmptm);
  1218. for (i = 0; i < sk_num(db->db->data); i++) {
  1219. pp = (const char **)sk_value(db->db->data, i);
  1220. if (pp[DB_type][0] == DB_TYPE_REV) {
  1221. if ((r = X509_REVOKED_new()) == NULL)
  1222. goto err;
  1223. j = make_revoked(r, pp[DB_rev_date]);
  1224. if (!j)
  1225. goto err;
  1226. if (j == 2)
  1227. crl_v2 = 1;
  1228. if (!BN_hex2bn(&serial, pp[DB_serial]))
  1229. goto err;
  1230. tmpser = BN_to_ASN1_INTEGER(serial, NULL);
  1231. BN_free(serial);
  1232. serial = NULL;
  1233. if (!tmpser)
  1234. goto err;
  1235. X509_REVOKED_set_serialNumber(r, tmpser);
  1236. ASN1_INTEGER_free(tmpser);
  1237. X509_CRL_add0_revoked(crl, r);
  1238. }
  1239. }
  1240. /*
  1241. * sort the data so it will be written in serial number order
  1242. */
  1243. X509_CRL_sort(crl);
  1244. /* we now have a CRL */
  1245. if (verbose)
  1246. BIO_printf(bio_err, "signing CRL\n");
  1247. #ifndef OPENSSL_NO_DSA
  1248. if (pkey->type == EVP_PKEY_DSA)
  1249. dgst = EVP_dss1();
  1250. else
  1251. #endif
  1252. #ifndef OPENSSL_NO_ECDSA
  1253. if (pkey->type == EVP_PKEY_EC)
  1254. dgst = EVP_ecdsa();
  1255. #endif
  1256. /* Add any extensions asked for */
  1257. if (crl_ext || crlnumberfile != NULL) {
  1258. X509V3_CTX crlctx;
  1259. X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
  1260. X509V3_set_nconf(&crlctx, conf);
  1261. if (crl_ext)
  1262. if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx, crl_ext, crl))
  1263. goto err;
  1264. if (crlnumberfile != NULL) {
  1265. tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);
  1266. if (!tmpser)
  1267. goto err;
  1268. X509_CRL_add1_ext_i2d(crl, NID_crl_number, tmpser, 0, 0);
  1269. ASN1_INTEGER_free(tmpser);
  1270. crl_v2 = 1;
  1271. if (!BN_add_word(crlnumber, 1))
  1272. goto err;
  1273. }
  1274. }
  1275. if (crl_ext || crl_v2) {
  1276. if (!X509_CRL_set_version(crl, 1))
  1277. goto err; /* version 2 CRL */
  1278. }
  1279. /* we have a CRL number that need updating */
  1280. if (crlnumberfile != NULL)
  1281. if (!save_serial(crlnumberfile, "new", crlnumber, NULL))
  1282. goto err;
  1283. if (!X509_CRL_sign(crl, pkey, dgst))
  1284. goto err;
  1285. PEM_write_bio_X509_CRL(Sout, crl);
  1286. if (crlnumberfile != NULL) /* Rename the crlnumber file */
  1287. if (!rotate_serial(crlnumberfile, "new", "old"))
  1288. goto err;
  1289. }
  1290. /*****************************************************************/
  1291. if (dorevoke) {
  1292. if (infile == NULL) {
  1293. BIO_printf(bio_err, "no input files\n");
  1294. goto err;
  1295. } else {
  1296. X509 *revcert;
  1297. revcert = load_cert(bio_err, infile, FORMAT_PEM, NULL, e, infile);
  1298. if (revcert == NULL)
  1299. goto err;
  1300. j = do_revoke(revcert, db, rev_type, rev_arg);
  1301. if (j <= 0)
  1302. goto err;
  1303. X509_free(revcert);
  1304. if (!save_index(dbfile, "new", db))
  1305. goto err;
  1306. if (!rotate_index(dbfile, "new", "old"))
  1307. goto err;
  1308. BIO_printf(bio_err, "Data Base Updated\n");
  1309. }
  1310. }
  1311. /*****************************************************************/
  1312. ret = 0;
  1313. err:
  1314. if (tofree)
  1315. OPENSSL_free(tofree);
  1316. BIO_free_all(Cout);
  1317. BIO_free_all(Sout);
  1318. BIO_free_all(out);
  1319. BIO_free_all(in);
  1320. if (cert_sk)
  1321. sk_X509_pop_free(cert_sk, X509_free);
  1322. if (ret)
  1323. ERR_print_errors(bio_err);
  1324. app_RAND_write_file(randfile, bio_err);
  1325. if (free_key && key)
  1326. OPENSSL_free(key);
  1327. BN_free(serial);
  1328. free_index(db);
  1329. EVP_PKEY_free(pkey);
  1330. if (x509)
  1331. X509_free(x509);
  1332. X509_CRL_free(crl);
  1333. NCONF_free(conf);
  1334. NCONF_free(extconf);
  1335. OBJ_cleanup();
  1336. apps_shutdown();
  1337. OPENSSL_EXIT(ret);
  1338. }
  1339. static void lookup_fail(const char *name, const char *tag)
  1340. {
  1341. BIO_printf(bio_err, "variable lookup failed for %s::%s\n", name, tag);
  1342. }
  1343. static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
  1344. const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy,
  1345. CA_DB *db, BIGNUM *serial, char *subj,
  1346. unsigned long chtype, int multirdn, int email_dn,
  1347. char *startdate, char *enddate, long days, int batch,
  1348. char *ext_sect, CONF *lconf, int verbose,
  1349. unsigned long certopt, unsigned long nameopt,
  1350. int default_op, int ext_copy, int selfsign)
  1351. {
  1352. X509_REQ *req = NULL;
  1353. BIO *in = NULL;
  1354. EVP_PKEY *pktmp = NULL;
  1355. int ok = -1, i;
  1356. in = BIO_new(BIO_s_file());
  1357. if (BIO_read_filename(in, infile) <= 0) {
  1358. perror(infile);
  1359. goto err;
  1360. }
  1361. if ((req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL)) == NULL) {
  1362. BIO_printf(bio_err, "Error reading certificate request in %s\n",
  1363. infile);
  1364. goto err;
  1365. }
  1366. if (verbose)
  1367. X509_REQ_print(bio_err, req);
  1368. BIO_printf(bio_err, "Check that the request matches the signature\n");
  1369. if (selfsign && !X509_REQ_check_private_key(req, pkey)) {
  1370. BIO_printf(bio_err,
  1371. "Certificate request and CA private key do not match\n");
  1372. ok = 0;
  1373. goto err;
  1374. }
  1375. if ((pktmp = X509_REQ_get_pubkey(req)) == NULL) {
  1376. BIO_printf(bio_err, "error unpacking public key\n");
  1377. goto err;
  1378. }
  1379. i = X509_REQ_verify(req, pktmp);
  1380. EVP_PKEY_free(pktmp);
  1381. if (i < 0) {
  1382. ok = 0;
  1383. BIO_printf(bio_err, "Signature verification problems....\n");
  1384. ERR_print_errors(bio_err);
  1385. goto err;
  1386. }
  1387. if (i == 0) {
  1388. ok = 0;
  1389. BIO_printf(bio_err,
  1390. "Signature did not match the certificate request\n");
  1391. ERR_print_errors(bio_err);
  1392. goto err;
  1393. } else
  1394. BIO_printf(bio_err, "Signature ok\n");
  1395. ok = do_body(xret, pkey, x509, dgst, policy, db, serial, subj, chtype,
  1396. multirdn, email_dn, startdate, enddate, days, batch, verbose,
  1397. req, ext_sect, lconf, certopt, nameopt, default_op, ext_copy,
  1398. selfsign);
  1399. err:
  1400. if (req != NULL)
  1401. X509_REQ_free(req);
  1402. if (in != NULL)
  1403. BIO_free(in);
  1404. return (ok);
  1405. }
  1406. static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
  1407. const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy,
  1408. CA_DB *db, BIGNUM *serial, char *subj,
  1409. unsigned long chtype, int multirdn, int email_dn,
  1410. char *startdate, char *enddate, long days, int batch,
  1411. char *ext_sect, CONF *lconf, int verbose,
  1412. unsigned long certopt, unsigned long nameopt,
  1413. int default_op, int ext_copy, ENGINE *e)
  1414. {
  1415. X509 *req = NULL;
  1416. X509_REQ *rreq = NULL;
  1417. EVP_PKEY *pktmp = NULL;
  1418. int ok = -1, i;
  1419. if ((req =
  1420. load_cert(bio_err, infile, FORMAT_PEM, NULL, e, infile)) == NULL)
  1421. goto err;
  1422. if (verbose)
  1423. X509_print(bio_err, req);
  1424. BIO_printf(bio_err, "Check that the request matches the signature\n");
  1425. if ((pktmp = X509_get_pubkey(req)) == NULL) {
  1426. BIO_printf(bio_err, "error unpacking public key\n");
  1427. goto err;
  1428. }
  1429. i = X509_verify(req, pktmp);
  1430. EVP_PKEY_free(pktmp);
  1431. if (i < 0) {
  1432. ok = 0;
  1433. BIO_printf(bio_err, "Signature verification problems....\n");
  1434. goto err;
  1435. }
  1436. if (i == 0) {
  1437. ok = 0;
  1438. BIO_printf(bio_err, "Signature did not match the certificate\n");
  1439. goto err;
  1440. } else
  1441. BIO_printf(bio_err, "Signature ok\n");
  1442. if ((rreq = X509_to_X509_REQ(req, NULL, EVP_md5())) == NULL)
  1443. goto err;
  1444. ok = do_body(xret, pkey, x509, dgst, policy, db, serial, subj, chtype,
  1445. multirdn, email_dn, startdate, enddate, days, batch, verbose,
  1446. rreq, ext_sect, lconf, certopt, nameopt, default_op,
  1447. ext_copy, 0);
  1448. err:
  1449. if (rreq != NULL)
  1450. X509_REQ_free(rreq);
  1451. if (req != NULL)
  1452. X509_free(req);
  1453. return (ok);
  1454. }
  1455. static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
  1456. const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy,
  1457. CA_DB *db, BIGNUM *serial, char *subj,
  1458. unsigned long chtype, int multirdn, int email_dn,
  1459. char *startdate, char *enddate, long days, int batch,
  1460. int verbose, X509_REQ *req, char *ext_sect, CONF *lconf,
  1461. unsigned long certopt, unsigned long nameopt,
  1462. int default_op, int ext_copy, int selfsign)
  1463. {
  1464. X509_NAME *name = NULL, *CAname = NULL, *subject = NULL, *dn_subject =
  1465. NULL;
  1466. ASN1_UTCTIME *tm, *tmptm;
  1467. ASN1_STRING *str, *str2;
  1468. ASN1_OBJECT *obj;
  1469. X509 *ret = NULL;
  1470. X509_CINF *ci;
  1471. X509_NAME_ENTRY *ne;
  1472. X509_NAME_ENTRY *tne, *push;
  1473. EVP_PKEY *pktmp;
  1474. int ok = -1, i, j, last, nid;
  1475. const char *p;
  1476. CONF_VALUE *cv;
  1477. char *row[DB_NUMBER], **rrow = NULL, **irow = NULL;
  1478. char buf[25];
  1479. tmptm = ASN1_UTCTIME_new();
  1480. if (tmptm == NULL) {
  1481. BIO_printf(bio_err, "malloc error\n");
  1482. return (0);
  1483. }
  1484. for (i = 0; i < DB_NUMBER; i++)
  1485. row[i] = NULL;
  1486. if (subj) {
  1487. X509_NAME *n = parse_name(subj, chtype, multirdn);
  1488. if (!n) {
  1489. ERR_print_errors(bio_err);
  1490. goto err;
  1491. }
  1492. X509_REQ_set_subject_name(req, n);
  1493. req->req_info->enc.modified = 1;
  1494. X509_NAME_free(n);
  1495. }
  1496. if (default_op)
  1497. BIO_printf(bio_err,
  1498. "The Subject's Distinguished Name is as follows\n");
  1499. name = X509_REQ_get_subject_name(req);
  1500. for (i = 0; i < X509_NAME_entry_count(name); i++) {
  1501. ne = X509_NAME_get_entry(name, i);
  1502. str = X509_NAME_ENTRY_get_data(ne);
  1503. obj = X509_NAME_ENTRY_get_object(ne);
  1504. if (msie_hack) {
  1505. /* assume all type should be strings */
  1506. nid = OBJ_obj2nid(ne->object);
  1507. if (str->type == V_ASN1_UNIVERSALSTRING)
  1508. ASN1_UNIVERSALSTRING_to_string(str);
  1509. if ((str->type == V_ASN1_IA5STRING) &&
  1510. (nid != NID_pkcs9_emailAddress))
  1511. str->type = V_ASN1_T61STRING;
  1512. if ((nid == NID_pkcs9_emailAddress) &&
  1513. (str->type == V_ASN1_PRINTABLESTRING))
  1514. str->type = V_ASN1_IA5STRING;
  1515. }
  1516. /* If no EMAIL is wanted in the subject */
  1517. if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (!email_dn))
  1518. continue;
  1519. /* check some things */
  1520. if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) &&
  1521. (str->type != V_ASN1_IA5STRING)) {
  1522. BIO_printf(bio_err,
  1523. "\nemailAddress type needs to be of type IA5STRING\n");
  1524. goto err;
  1525. }
  1526. if ((str->type != V_ASN1_BMPSTRING)
  1527. && (str->type != V_ASN1_UTF8STRING)) {
  1528. j = ASN1_PRINTABLE_type(str->data, str->length);
  1529. if (((j == V_ASN1_T61STRING) &&
  1530. (str->type != V_ASN1_T61STRING)) ||
  1531. ((j == V_ASN1_IA5STRING) &&
  1532. (str->type == V_ASN1_PRINTABLESTRING))) {
  1533. BIO_printf(bio_err,
  1534. "\nThe string contains characters that are illegal for the ASN.1 type\n");
  1535. goto err;
  1536. }
  1537. }
  1538. if (default_op)
  1539. old_entry_print(bio_err, obj, str);
  1540. }
  1541. /* Ok, now we check the 'policy' stuff. */
  1542. if ((subject = X509_NAME_new()) == NULL) {
  1543. BIO_printf(bio_err, "Memory allocation failure\n");
  1544. goto err;
  1545. }
  1546. /* take a copy of the issuer name before we mess with it. */
  1547. if (selfsign)
  1548. CAname = X509_NAME_dup(name);
  1549. else
  1550. CAname = X509_NAME_dup(x509->cert_info->subject);
  1551. if (CAname == NULL)
  1552. goto err;
  1553. str = str2 = NULL;
  1554. for (i = 0; i < sk_CONF_VALUE_num(policy); i++) {
  1555. cv = sk_CONF_VALUE_value(policy, i); /* get the object id */
  1556. if ((j = OBJ_txt2nid(cv->name)) == NID_undef) {
  1557. BIO_printf(bio_err,
  1558. "%s:unknown object type in 'policy' configuration\n",
  1559. cv->name);
  1560. goto err;
  1561. }
  1562. obj = OBJ_nid2obj(j);
  1563. last = -1;
  1564. for (;;) {
  1565. /* lookup the object in the supplied name list */
  1566. j = X509_NAME_get_index_by_OBJ(name, obj, last);
  1567. if (j < 0) {
  1568. if (last != -1)
  1569. break;
  1570. tne = NULL;
  1571. } else {
  1572. tne = X509_NAME_get_entry(name, j);
  1573. }
  1574. last = j;
  1575. /* depending on the 'policy', decide what to do. */
  1576. push = NULL;
  1577. if (strcmp(cv->value, "optional") == 0) {
  1578. if (tne != NULL)
  1579. push = tne;
  1580. } else if (strcmp(cv->value, "supplied") == 0) {
  1581. if (tne == NULL) {
  1582. BIO_printf(bio_err,
  1583. "The %s field needed to be supplied and was missing\n",
  1584. cv->name);
  1585. goto err;
  1586. } else
  1587. push = tne;
  1588. } else if (strcmp(cv->value, "match") == 0) {
  1589. int last2;
  1590. if (tne == NULL) {
  1591. BIO_printf(bio_err,
  1592. "The mandatory %s field was missing\n",
  1593. cv->name);
  1594. goto err;
  1595. }
  1596. last2 = -1;
  1597. again2:
  1598. j = X509_NAME_get_index_by_OBJ(CAname, obj, last2);
  1599. if ((j < 0) && (last2 == -1)) {
  1600. BIO_printf(bio_err,
  1601. "The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n",
  1602. cv->name);
  1603. goto err;
  1604. }
  1605. if (j >= 0) {
  1606. push = X509_NAME_get_entry(CAname, j);
  1607. str = X509_NAME_ENTRY_get_data(tne);
  1608. str2 = X509_NAME_ENTRY_get_data(push);
  1609. last2 = j;
  1610. if (ASN1_STRING_cmp(str, str2) != 0)
  1611. goto again2;
  1612. }
  1613. if (j < 0) {
  1614. BIO_printf(bio_err,
  1615. "The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n",
  1616. cv->name,
  1617. ((str2 == NULL) ? "NULL" : (char *)str2->data),
  1618. ((str == NULL) ? "NULL" : (char *)str->data));
  1619. goto err;
  1620. }
  1621. } else {
  1622. BIO_printf(bio_err,
  1623. "%s:invalid type in 'policy' configuration\n",
  1624. cv->value);
  1625. goto err;
  1626. }
  1627. if (push != NULL) {
  1628. if (!X509_NAME_add_entry(subject, push, -1, 0)) {
  1629. if (push != NULL)
  1630. X509_NAME_ENTRY_free(push);
  1631. BIO_printf(bio_err, "Memory allocation failure\n");
  1632. goto err;
  1633. }
  1634. }
  1635. if (j < 0)
  1636. break;
  1637. }
  1638. }
  1639. if (preserve) {
  1640. X509_NAME_free(subject);
  1641. /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
  1642. subject = X509_NAME_dup(name);
  1643. if (subject == NULL)
  1644. goto err;
  1645. }
  1646. if (verbose)
  1647. BIO_printf(bio_err,
  1648. "The subject name appears to be ok, checking data base for clashes\n");
  1649. /* Build the correct Subject if no e-mail is wanted in the subject */
  1650. /*
  1651. * and add it later on because of the method extensions are added
  1652. * (altName)
  1653. */
  1654. if (email_dn)
  1655. dn_subject = subject;
  1656. else {
  1657. X509_NAME_ENTRY *tmpne;
  1658. /*
  1659. * Its best to dup the subject DN and then delete any email addresses
  1660. * because this retains its structure.
  1661. */
  1662. if (!(dn_subject = X509_NAME_dup(subject))) {
  1663. BIO_printf(bio_err, "Memory allocation failure\n");
  1664. goto err;
  1665. }
  1666. while ((i = X509_NAME_get_index_by_NID(dn_subject,
  1667. NID_pkcs9_emailAddress,
  1668. -1)) >= 0) {
  1669. tmpne = X509_NAME_get_entry(dn_subject, i);
  1670. X509_NAME_delete_entry(dn_subject, i);
  1671. X509_NAME_ENTRY_free(tmpne);
  1672. }
  1673. }
  1674. if (BN_is_zero(serial))
  1675. row[DB_serial] = BUF_strdup("00");
  1676. else
  1677. row[DB_serial] = BN_bn2hex(serial);
  1678. if (row[DB_serial] == NULL) {
  1679. BIO_printf(bio_err, "Memory allocation failure\n");
  1680. goto err;
  1681. }
  1682. if (db->attributes.unique_subject) {
  1683. rrow = TXT_DB_get_by_index(db->db, DB_name, row);
  1684. if (rrow != NULL) {
  1685. BIO_printf(bio_err,
  1686. "ERROR:There is already a certificate for %s\n",
  1687. row[DB_name]);
  1688. }
  1689. }
  1690. if (rrow == NULL) {
  1691. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  1692. if (rrow != NULL) {
  1693. BIO_printf(bio_err,
  1694. "ERROR:Serial number %s has already been issued,\n",
  1695. row[DB_serial]);
  1696. BIO_printf(bio_err,
  1697. " check the database/serial_file for corruption\n");
  1698. }
  1699. }
  1700. if (rrow != NULL) {
  1701. BIO_printf(bio_err, "The matching entry has the following details\n");
  1702. if (rrow[DB_type][0] == 'E')
  1703. p = "Expired";
  1704. else if (rrow[DB_type][0] == 'R')
  1705. p = "Revoked";
  1706. else if (rrow[DB_type][0] == 'V')
  1707. p = "Valid";
  1708. else
  1709. p = "\ninvalid type, Data base error\n";
  1710. BIO_printf(bio_err, "Type :%s\n", p);;
  1711. if (rrow[DB_type][0] == 'R') {
  1712. p = rrow[DB_exp_date];
  1713. if (p == NULL)
  1714. p = "undef";
  1715. BIO_printf(bio_err, "Was revoked on:%s\n", p);
  1716. }
  1717. p = rrow[DB_exp_date];
  1718. if (p == NULL)
  1719. p = "undef";
  1720. BIO_printf(bio_err, "Expires on :%s\n", p);
  1721. p = rrow[DB_serial];
  1722. if (p == NULL)
  1723. p = "undef";
  1724. BIO_printf(bio_err, "Serial Number :%s\n", p);
  1725. p = rrow[DB_file];
  1726. if (p == NULL)
  1727. p = "undef";
  1728. BIO_printf(bio_err, "File name :%s\n", p);
  1729. p = rrow[DB_name];
  1730. if (p == NULL)
  1731. p = "undef";
  1732. BIO_printf(bio_err, "Subject Name :%s\n", p);
  1733. ok = -1; /* This is now a 'bad' error. */
  1734. goto err;
  1735. }
  1736. /* We are now totally happy, lets make and sign the certificate */
  1737. if (verbose)
  1738. BIO_printf(bio_err,
  1739. "Everything appears to be ok, creating and signing the certificate\n");
  1740. if ((ret = X509_new()) == NULL)
  1741. goto err;
  1742. ci = ret->cert_info;
  1743. #ifdef X509_V3
  1744. /* Make it an X509 v3 certificate. */
  1745. if (!X509_set_version(ret, 2))
  1746. goto err;
  1747. #endif
  1748. if (BN_to_ASN1_INTEGER(serial, ci->serialNumber) == NULL)
  1749. goto err;
  1750. if (selfsign) {
  1751. if (!X509_set_issuer_name(ret, subject))
  1752. goto err;
  1753. } else {
  1754. if (!X509_set_issuer_name(ret, X509_get_subject_name(x509)))
  1755. goto err;
  1756. }
  1757. if (strcmp(startdate, "today") == 0)
  1758. X509_gmtime_adj(X509_get_notBefore(ret), 0);
  1759. else
  1760. ASN1_UTCTIME_set_string(X509_get_notBefore(ret), startdate);
  1761. if (enddate == NULL)
  1762. X509_gmtime_adj(X509_get_notAfter(ret), (long)60 * 60 * 24 * days);
  1763. else
  1764. ASN1_UTCTIME_set_string(X509_get_notAfter(ret), enddate);
  1765. if (!X509_set_subject_name(ret, subject))
  1766. goto err;
  1767. pktmp = X509_REQ_get_pubkey(req);
  1768. i = X509_set_pubkey(ret, pktmp);
  1769. EVP_PKEY_free(pktmp);
  1770. if (!i)
  1771. goto err;
  1772. /* Lets add the extensions, if there are any */
  1773. if (ext_sect) {
  1774. X509V3_CTX ctx;
  1775. if (ci->version == NULL)
  1776. if ((ci->version = ASN1_INTEGER_new()) == NULL)
  1777. goto err;
  1778. ASN1_INTEGER_set(ci->version, 2); /* version 3 certificate */
  1779. /*
  1780. * Free the current entries if any, there should not be any I believe
  1781. */
  1782. if (ci->extensions != NULL)
  1783. sk_X509_EXTENSION_pop_free(ci->extensions, X509_EXTENSION_free);
  1784. ci->extensions = NULL;
  1785. /* Initialize the context structure */
  1786. if (selfsign)
  1787. X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0);
  1788. else
  1789. X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);
  1790. if (extconf) {
  1791. if (verbose)
  1792. BIO_printf(bio_err, "Extra configuration file found\n");
  1793. /* Use the extconf configuration db LHASH */
  1794. X509V3_set_nconf(&ctx, extconf);
  1795. /* Test the structure (needed?) */
  1796. /* X509V3_set_ctx_test(&ctx); */
  1797. /* Adds exts contained in the configuration file */
  1798. if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect, ret)) {
  1799. BIO_printf(bio_err,
  1800. "ERROR: adding extensions in section %s\n",
  1801. ext_sect);
  1802. ERR_print_errors(bio_err);
  1803. goto err;
  1804. }
  1805. if (verbose)
  1806. BIO_printf(bio_err,
  1807. "Successfully added extensions from file.\n");
  1808. } else if (ext_sect) {
  1809. /* We found extensions to be set from config file */
  1810. X509V3_set_nconf(&ctx, lconf);
  1811. if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) {
  1812. BIO_printf(bio_err,
  1813. "ERROR: adding extensions in section %s\n",
  1814. ext_sect);
  1815. ERR_print_errors(bio_err);
  1816. goto err;
  1817. }
  1818. if (verbose)
  1819. BIO_printf(bio_err,
  1820. "Successfully added extensions from config\n");
  1821. }
  1822. }
  1823. /* Copy extensions from request (if any) */
  1824. if (!copy_extensions(ret, req, ext_copy)) {
  1825. BIO_printf(bio_err, "ERROR: adding extensions from request\n");
  1826. ERR_print_errors(bio_err);
  1827. goto err;
  1828. }
  1829. /* Set the right value for the noemailDN option */
  1830. if (email_dn == 0) {
  1831. if (!X509_set_subject_name(ret, dn_subject))
  1832. goto err;
  1833. }
  1834. if (!default_op) {
  1835. BIO_printf(bio_err, "Certificate Details:\n");
  1836. /*
  1837. * Never print signature details because signature not present
  1838. */
  1839. certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;
  1840. X509_print_ex(bio_err, ret, nameopt, certopt);
  1841. }
  1842. BIO_printf(bio_err, "Certificate is to be certified until ");
  1843. ASN1_TIME_print(bio_err, X509_get_notAfter(ret));
  1844. if (days)
  1845. BIO_printf(bio_err, " (%ld days)", days);
  1846. BIO_printf(bio_err, "\n");
  1847. if (!batch) {
  1848. BIO_printf(bio_err, "Sign the certificate? [y/n]:");
  1849. (void)BIO_flush(bio_err);
  1850. buf[0] = '\0';
  1851. if (!fgets(buf, sizeof(buf) - 1, stdin)) {
  1852. BIO_printf(bio_err,
  1853. "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
  1854. ok = 0;
  1855. goto err;
  1856. }
  1857. if (!((buf[0] == 'y') || (buf[0] == 'Y'))) {
  1858. BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED\n");
  1859. ok = 0;
  1860. goto err;
  1861. }
  1862. }
  1863. #ifndef OPENSSL_NO_DSA
  1864. if (pkey->type == EVP_PKEY_DSA)
  1865. dgst = EVP_dss1();
  1866. pktmp = X509_get_pubkey(ret);
  1867. if (EVP_PKEY_missing_parameters(pktmp) &&
  1868. !EVP_PKEY_missing_parameters(pkey))
  1869. EVP_PKEY_copy_parameters(pktmp, pkey);
  1870. EVP_PKEY_free(pktmp);
  1871. #endif
  1872. #ifndef OPENSSL_NO_ECDSA
  1873. if (pkey->type == EVP_PKEY_EC)
  1874. dgst = EVP_ecdsa();
  1875. pktmp = X509_get_pubkey(ret);
  1876. if (EVP_PKEY_missing_parameters(pktmp) &&
  1877. !EVP_PKEY_missing_parameters(pkey))
  1878. EVP_PKEY_copy_parameters(pktmp, pkey);
  1879. EVP_PKEY_free(pktmp);
  1880. #endif
  1881. if (!X509_sign(ret, pkey, dgst))
  1882. goto err;
  1883. /* We now just add it to the database */
  1884. row[DB_type] = (char *)OPENSSL_malloc(2);
  1885. tm = X509_get_notAfter(ret);
  1886. row[DB_exp_date] = (char *)OPENSSL_malloc(tm->length + 1);
  1887. memcpy(row[DB_exp_date], tm->data, tm->length);
  1888. row[DB_exp_date][tm->length] = '\0';
  1889. row[DB_rev_date] = NULL;
  1890. /* row[DB_serial] done already */
  1891. row[DB_file] = (char *)OPENSSL_malloc(8);
  1892. row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0);
  1893. if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
  1894. (row[DB_file] == NULL) || (row[DB_name] == NULL)) {
  1895. BIO_printf(bio_err, "Memory allocation failure\n");
  1896. goto err;
  1897. }
  1898. BUF_strlcpy(row[DB_file], "unknown", 8);
  1899. row[DB_type][0] = 'V';
  1900. row[DB_type][1] = '\0';
  1901. if ((irow =
  1902. (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) == NULL) {
  1903. BIO_printf(bio_err, "Memory allocation failure\n");
  1904. goto err;
  1905. }
  1906. for (i = 0; i < DB_NUMBER; i++) {
  1907. irow[i] = row[i];
  1908. row[i] = NULL;
  1909. }
  1910. irow[DB_NUMBER] = NULL;
  1911. if (!TXT_DB_insert(db->db, irow)) {
  1912. BIO_printf(bio_err, "failed to update database\n");
  1913. BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
  1914. goto err;
  1915. }
  1916. ok = 1;
  1917. err:
  1918. for (i = 0; i < DB_NUMBER; i++)
  1919. if (row[i] != NULL)
  1920. OPENSSL_free(row[i]);
  1921. if (CAname != NULL)
  1922. X509_NAME_free(CAname);
  1923. if (subject != NULL)
  1924. X509_NAME_free(subject);
  1925. if ((dn_subject != NULL) && !email_dn)
  1926. X509_NAME_free(dn_subject);
  1927. if (tmptm != NULL)
  1928. ASN1_UTCTIME_free(tmptm);
  1929. if (ok <= 0) {
  1930. if (ret != NULL)
  1931. X509_free(ret);
  1932. ret = NULL;
  1933. } else
  1934. *xret = ret;
  1935. return (ok);
  1936. }
  1937. static void write_new_certificate(BIO *bp, X509 *x, int output_der,
  1938. int notext)
  1939. {
  1940. if (output_der) {
  1941. (void)i2d_X509_bio(bp, x);
  1942. return;
  1943. }
  1944. #if 0
  1945. /* ??? Not needed since X509_print prints all this stuff anyway */
  1946. f = X509_NAME_oneline(X509_get_issuer_name(x), buf, 256);
  1947. BIO_printf(bp, "issuer :%s\n", f);
  1948. f = X509_NAME_oneline(X509_get_subject_name(x), buf, 256);
  1949. BIO_printf(bp, "subject:%s\n", f);
  1950. BIO_puts(bp, "serial :");
  1951. i2a_ASN1_INTEGER(bp, x->cert_info->serialNumber);
  1952. BIO_puts(bp, "\n\n");
  1953. #endif
  1954. if (!notext)
  1955. X509_print(bp, x);
  1956. PEM_write_bio_X509(bp, x);
  1957. }
  1958. static int certify_spkac(X509 **xret, char *infile, EVP_PKEY *pkey,
  1959. X509 *x509, const EVP_MD *dgst,
  1960. STACK_OF(CONF_VALUE) *policy, CA_DB *db,
  1961. BIGNUM *serial, char *subj, unsigned long chtype,
  1962. int multirdn, int email_dn, char *startdate,
  1963. char *enddate, long days, char *ext_sect,
  1964. CONF *lconf, int verbose, unsigned long certopt,
  1965. unsigned long nameopt, int default_op, int ext_copy)
  1966. {
  1967. STACK_OF(CONF_VALUE) *sk = NULL;
  1968. LHASH *parms = NULL;
  1969. X509_REQ *req = NULL;
  1970. CONF_VALUE *cv = NULL;
  1971. NETSCAPE_SPKI *spki = NULL;
  1972. X509_REQ_INFO *ri;
  1973. char *type, *buf;
  1974. EVP_PKEY *pktmp = NULL;
  1975. X509_NAME *n = NULL;
  1976. X509_NAME_ENTRY *ne = NULL;
  1977. int ok = -1, i, j;
  1978. long errline;
  1979. int nid;
  1980. /*
  1981. * Load input file into a hash table. (This is just an easy
  1982. * way to read and parse the file, then put it into a convenient
  1983. * STACK format).
  1984. */
  1985. parms = CONF_load(NULL, infile, &errline);
  1986. if (parms == NULL) {
  1987. BIO_printf(bio_err, "error on line %ld of %s\n", errline, infile);
  1988. ERR_print_errors(bio_err);
  1989. goto err;
  1990. }
  1991. sk = CONF_get_section(parms, "default");
  1992. if (sk_CONF_VALUE_num(sk) == 0) {
  1993. BIO_printf(bio_err, "no name/value pairs found in %s\n", infile);
  1994. CONF_free(parms);
  1995. goto err;
  1996. }
  1997. /*
  1998. * Now create a dummy X509 request structure. We don't actually
  1999. * have an X509 request, but we have many of the components
  2000. * (a public key, various DN components). The idea is that we
  2001. * put these components into the right X509 request structure
  2002. * and we can use the same code as if you had a real X509 request.
  2003. */
  2004. req = X509_REQ_new();
  2005. if (req == NULL) {
  2006. ERR_print_errors(bio_err);
  2007. goto err;
  2008. }
  2009. /*
  2010. * Build up the subject name set.
  2011. */
  2012. ri = req->req_info;
  2013. n = ri->subject;
  2014. for (i = 0;; i++) {
  2015. if (sk_CONF_VALUE_num(sk) <= i)
  2016. break;
  2017. cv = sk_CONF_VALUE_value(sk, i);
  2018. type = cv->name;
  2019. /*
  2020. * Skip past any leading X. X: X, etc to allow for multiple instances
  2021. */
  2022. for (buf = cv->name; *buf; buf++)
  2023. if ((*buf == ':') || (*buf == ',') || (*buf == '.')) {
  2024. buf++;
  2025. if (*buf)
  2026. type = buf;
  2027. break;
  2028. }
  2029. buf = cv->value;
  2030. if ((nid = OBJ_txt2nid(type)) == NID_undef) {
  2031. if (strcmp(type, "SPKAC") == 0) {
  2032. spki = NETSCAPE_SPKI_b64_decode(cv->value, -1);
  2033. if (spki == NULL) {
  2034. BIO_printf(bio_err,
  2035. "unable to load Netscape SPKAC structure\n");
  2036. ERR_print_errors(bio_err);
  2037. goto err;
  2038. }
  2039. }
  2040. continue;
  2041. }
  2042. if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
  2043. (unsigned char *)buf, -1, -1, 0))
  2044. goto err;
  2045. }
  2046. if (spki == NULL) {
  2047. BIO_printf(bio_err, "Netscape SPKAC structure not found in %s\n",
  2048. infile);
  2049. goto err;
  2050. }
  2051. /*
  2052. * Now extract the key from the SPKI structure.
  2053. */
  2054. BIO_printf(bio_err,
  2055. "Check that the SPKAC request matches the signature\n");
  2056. if ((pktmp = NETSCAPE_SPKI_get_pubkey(spki)) == NULL) {
  2057. BIO_printf(bio_err, "error unpacking SPKAC public key\n");
  2058. goto err;
  2059. }
  2060. j = NETSCAPE_SPKI_verify(spki, pktmp);
  2061. if (j <= 0) {
  2062. BIO_printf(bio_err,
  2063. "signature verification failed on SPKAC public key\n");
  2064. goto err;
  2065. }
  2066. BIO_printf(bio_err, "Signature ok\n");
  2067. X509_REQ_set_pubkey(req, pktmp);
  2068. EVP_PKEY_free(pktmp);
  2069. ok = do_body(xret, pkey, x509, dgst, policy, db, serial, subj, chtype,
  2070. multirdn, email_dn, startdate, enddate, days, 1, verbose,
  2071. req, ext_sect, lconf, certopt, nameopt, default_op, ext_copy,
  2072. 0);
  2073. err:
  2074. if (req != NULL)
  2075. X509_REQ_free(req);
  2076. if (parms != NULL)
  2077. CONF_free(parms);
  2078. if (spki != NULL)
  2079. NETSCAPE_SPKI_free(spki);
  2080. if (ne != NULL)
  2081. X509_NAME_ENTRY_free(ne);
  2082. return (ok);
  2083. }
  2084. static int check_time_format(const char *str)
  2085. {
  2086. ASN1_TIME tm;
  2087. tm.data = (unsigned char *)str;
  2088. tm.length = strlen(str);
  2089. tm.type = V_ASN1_UTCTIME;
  2090. if (ASN1_TIME_check(&tm))
  2091. return 1;
  2092. tm.type = V_ASN1_GENERALIZEDTIME;
  2093. return ASN1_TIME_check(&tm);
  2094. }
  2095. static int do_revoke(X509 *x509, CA_DB *db, int type, char *value)
  2096. {
  2097. ASN1_UTCTIME *tm = NULL;
  2098. char *row[DB_NUMBER], **rrow, **irow;
  2099. char *rev_str = NULL;
  2100. BIGNUM *bn = NULL;
  2101. int ok = -1, i;
  2102. for (i = 0; i < DB_NUMBER; i++)
  2103. row[i] = NULL;
  2104. row[DB_name] = X509_NAME_oneline(X509_get_subject_name(x509), NULL, 0);
  2105. bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509), NULL);
  2106. if (BN_is_zero(bn))
  2107. row[DB_serial] = BUF_strdup("00");
  2108. else
  2109. row[DB_serial] = BN_bn2hex(bn);
  2110. BN_free(bn);
  2111. if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) {
  2112. BIO_printf(bio_err, "Memory allocation failure\n");
  2113. goto err;
  2114. }
  2115. /*
  2116. * We have to lookup by serial number because name lookup skips revoked
  2117. * certs
  2118. */
  2119. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  2120. if (rrow == NULL) {
  2121. BIO_printf(bio_err,
  2122. "Adding Entry with serial number %s to DB for %s\n",
  2123. row[DB_serial], row[DB_name]);
  2124. /* We now just add it to the database */
  2125. row[DB_type] = (char *)OPENSSL_malloc(2);
  2126. tm = X509_get_notAfter(x509);
  2127. row[DB_exp_date] = (char *)OPENSSL_malloc(tm->length + 1);
  2128. memcpy(row[DB_exp_date], tm->data, tm->length);
  2129. row[DB_exp_date][tm->length] = '\0';
  2130. row[DB_rev_date] = NULL;
  2131. /* row[DB_serial] done already */
  2132. row[DB_file] = (char *)OPENSSL_malloc(8);
  2133. /* row[DB_name] done already */
  2134. if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
  2135. (row[DB_file] == NULL)) {
  2136. BIO_printf(bio_err, "Memory allocation failure\n");
  2137. goto err;
  2138. }
  2139. BUF_strlcpy(row[DB_file], "unknown", 8);
  2140. row[DB_type][0] = 'V';
  2141. row[DB_type][1] = '\0';
  2142. if ((irow =
  2143. (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) ==
  2144. NULL) {
  2145. BIO_printf(bio_err, "Memory allocation failure\n");
  2146. goto err;
  2147. }
  2148. for (i = 0; i < DB_NUMBER; i++) {
  2149. irow[i] = row[i];
  2150. row[i] = NULL;
  2151. }
  2152. irow[DB_NUMBER] = NULL;
  2153. if (!TXT_DB_insert(db->db, irow)) {
  2154. BIO_printf(bio_err, "failed to update database\n");
  2155. BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
  2156. goto err;
  2157. }
  2158. /* Revoke Certificate */
  2159. ok = do_revoke(x509, db, type, value);
  2160. goto err;
  2161. } else if (index_name_cmp((const char **)row, (const char **)rrow)) {
  2162. BIO_printf(bio_err, "ERROR:name does not match %s\n", row[DB_name]);
  2163. goto err;
  2164. } else if (rrow[DB_type][0] == 'R') {
  2165. BIO_printf(bio_err, "ERROR:Already revoked, serial number %s\n",
  2166. row[DB_serial]);
  2167. goto err;
  2168. } else {
  2169. BIO_printf(bio_err, "Revoking Certificate %s.\n", rrow[DB_serial]);
  2170. rev_str = make_revocation_str(type, value);
  2171. if (!rev_str) {
  2172. BIO_printf(bio_err, "Error in revocation arguments\n");
  2173. goto err;
  2174. }
  2175. rrow[DB_type][0] = 'R';
  2176. rrow[DB_type][1] = '\0';
  2177. rrow[DB_rev_date] = rev_str;
  2178. }
  2179. ok = 1;
  2180. err:
  2181. for (i = 0; i < DB_NUMBER; i++) {
  2182. if (row[i] != NULL)
  2183. OPENSSL_free(row[i]);
  2184. }
  2185. return (ok);
  2186. }
  2187. static int get_certificate_status(const char *serial, CA_DB *db)
  2188. {
  2189. char *row[DB_NUMBER], **rrow;
  2190. int ok = -1, i;
  2191. /* Free Resources */
  2192. for (i = 0; i < DB_NUMBER; i++)
  2193. row[i] = NULL;
  2194. /* Malloc needed char spaces */
  2195. row[DB_serial] = OPENSSL_malloc(strlen(serial) + 2);
  2196. if (row[DB_serial] == NULL) {
  2197. BIO_printf(bio_err, "Malloc failure\n");
  2198. goto err;
  2199. }
  2200. if (strlen(serial) % 2) {
  2201. /*
  2202. * Set the first char to 0
  2203. */ ;
  2204. row[DB_serial][0] = '0';
  2205. /* Copy String from serial to row[DB_serial] */
  2206. memcpy(row[DB_serial] + 1, serial, strlen(serial));
  2207. row[DB_serial][strlen(serial) + 1] = '\0';
  2208. } else {
  2209. /* Copy String from serial to row[DB_serial] */
  2210. memcpy(row[DB_serial], serial, strlen(serial));
  2211. row[DB_serial][strlen(serial)] = '\0';
  2212. }
  2213. /* Make it Upper Case */
  2214. for (i = 0; row[DB_serial][i] != '\0'; i++)
  2215. row[DB_serial][i] = toupper(row[DB_serial][i]);
  2216. ok = 1;
  2217. /* Search for the certificate */
  2218. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  2219. if (rrow == NULL) {
  2220. BIO_printf(bio_err, "Serial %s not present in db.\n", row[DB_serial]);
  2221. ok = -1;
  2222. goto err;
  2223. } else if (rrow[DB_type][0] == 'V') {
  2224. BIO_printf(bio_err, "%s=Valid (%c)\n",
  2225. row[DB_serial], rrow[DB_type][0]);
  2226. goto err;
  2227. } else if (rrow[DB_type][0] == 'R') {
  2228. BIO_printf(bio_err, "%s=Revoked (%c)\n",
  2229. row[DB_serial], rrow[DB_type][0]);
  2230. goto err;
  2231. } else if (rrow[DB_type][0] == 'E') {
  2232. BIO_printf(bio_err, "%s=Expired (%c)\n",
  2233. row[DB_serial], rrow[DB_type][0]);
  2234. goto err;
  2235. } else if (rrow[DB_type][0] == 'S') {
  2236. BIO_printf(bio_err, "%s=Suspended (%c)\n",
  2237. row[DB_serial], rrow[DB_type][0]);
  2238. goto err;
  2239. } else {
  2240. BIO_printf(bio_err, "%s=Unknown (%c).\n",
  2241. row[DB_serial], rrow[DB_type][0]);
  2242. ok = -1;
  2243. }
  2244. err:
  2245. for (i = 0; i < DB_NUMBER; i++) {
  2246. if (row[i] != NULL)
  2247. OPENSSL_free(row[i]);
  2248. }
  2249. return (ok);
  2250. }
  2251. static int do_updatedb(CA_DB *db)
  2252. {
  2253. ASN1_UTCTIME *a_tm = NULL;
  2254. int i, cnt = 0;
  2255. int db_y2k, a_y2k; /* flags = 1 if y >= 2000 */
  2256. char **rrow, *a_tm_s;
  2257. a_tm = ASN1_UTCTIME_new();
  2258. /* get actual time and make a string */
  2259. a_tm = X509_gmtime_adj(a_tm, 0);
  2260. a_tm_s = (char *)OPENSSL_malloc(a_tm->length + 1);
  2261. if (a_tm_s == NULL) {
  2262. cnt = -1;
  2263. goto err;
  2264. }
  2265. memcpy(a_tm_s, a_tm->data, a_tm->length);
  2266. a_tm_s[a_tm->length] = '\0';
  2267. if (strncmp(a_tm_s, "49", 2) <= 0)
  2268. a_y2k = 1;
  2269. else
  2270. a_y2k = 0;
  2271. for (i = 0; i < sk_num(db->db->data); i++) {
  2272. rrow = (char **)sk_value(db->db->data, i);
  2273. if (rrow[DB_type][0] == 'V') {
  2274. /* ignore entries that are not valid */
  2275. if (strncmp(rrow[DB_exp_date], "49", 2) <= 0)
  2276. db_y2k = 1;
  2277. else
  2278. db_y2k = 0;
  2279. if (db_y2k == a_y2k) {
  2280. /* all on the same y2k side */
  2281. if (strcmp(rrow[DB_exp_date], a_tm_s) <= 0) {
  2282. rrow[DB_type][0] = 'E';
  2283. rrow[DB_type][1] = '\0';
  2284. cnt++;
  2285. BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]);
  2286. }
  2287. } else if (db_y2k < a_y2k) {
  2288. rrow[DB_type][0] = 'E';
  2289. rrow[DB_type][1] = '\0';
  2290. cnt++;
  2291. BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]);
  2292. }
  2293. }
  2294. }
  2295. err:
  2296. ASN1_UTCTIME_free(a_tm);
  2297. OPENSSL_free(a_tm_s);
  2298. return (cnt);
  2299. }
  2300. static const char *crl_reasons[] = {
  2301. /* CRL reason strings */
  2302. "unspecified",
  2303. "keyCompromise",
  2304. "CACompromise",
  2305. "affiliationChanged",
  2306. "superseded",
  2307. "cessationOfOperation",
  2308. "certificateHold",
  2309. "removeFromCRL",
  2310. /* Additional pseudo reasons */
  2311. "holdInstruction",
  2312. "keyTime",
  2313. "CAkeyTime"
  2314. };
  2315. #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))
  2316. /*
  2317. * Given revocation information convert to a DB string. The format of the
  2318. * string is: revtime[,reason,extra]. Where 'revtime' is the revocation time
  2319. * (the current time). 'reason' is the optional CRL reason and 'extra' is any
  2320. * additional argument
  2321. */
  2322. char *make_revocation_str(int rev_type, char *rev_arg)
  2323. {
  2324. char *other = NULL, *str;
  2325. const char *reason = NULL;
  2326. ASN1_OBJECT *otmp;
  2327. ASN1_UTCTIME *revtm = NULL;
  2328. int i;
  2329. switch (rev_type) {
  2330. case REV_NONE:
  2331. break;
  2332. case REV_CRL_REASON:
  2333. for (i = 0; i < 8; i++) {
  2334. if (!strcasecmp(rev_arg, crl_reasons[i])) {
  2335. reason = crl_reasons[i];
  2336. break;
  2337. }
  2338. }
  2339. if (reason == NULL) {
  2340. BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg);
  2341. return NULL;
  2342. }
  2343. break;
  2344. case REV_HOLD:
  2345. /* Argument is an OID */
  2346. otmp = OBJ_txt2obj(rev_arg, 0);
  2347. ASN1_OBJECT_free(otmp);
  2348. if (otmp == NULL) {
  2349. BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg);
  2350. return NULL;
  2351. }
  2352. reason = "holdInstruction";
  2353. other = rev_arg;
  2354. break;
  2355. case REV_KEY_COMPROMISE:
  2356. case REV_CA_COMPROMISE:
  2357. /* Argument is the key compromise time */
  2358. if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg)) {
  2359. BIO_printf(bio_err,
  2360. "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
  2361. rev_arg);
  2362. return NULL;
  2363. }
  2364. other = rev_arg;
  2365. if (rev_type == REV_KEY_COMPROMISE)
  2366. reason = "keyTime";
  2367. else
  2368. reason = "CAkeyTime";
  2369. break;
  2370. }
  2371. revtm = X509_gmtime_adj(NULL, 0);
  2372. if (!revtm)
  2373. return NULL;
  2374. i = revtm->length + 1;
  2375. if (reason)
  2376. i += strlen(reason) + 1;
  2377. if (other)
  2378. i += strlen(other) + 1;
  2379. str = OPENSSL_malloc(i);
  2380. if (!str)
  2381. return NULL;
  2382. BUF_strlcpy(str, (char *)revtm->data, i);
  2383. if (reason) {
  2384. BUF_strlcat(str, ",", i);
  2385. BUF_strlcat(str, reason, i);
  2386. }
  2387. if (other) {
  2388. BUF_strlcat(str, ",", i);
  2389. BUF_strlcat(str, other, i);
  2390. }
  2391. ASN1_UTCTIME_free(revtm);
  2392. return str;
  2393. }
  2394. /*-
  2395. * Convert revocation field to X509_REVOKED entry
  2396. * return code:
  2397. * 0 error
  2398. * 1 OK
  2399. * 2 OK and some extensions added (i.e. V2 CRL)
  2400. */
  2401. int make_revoked(X509_REVOKED *rev, const char *str)
  2402. {
  2403. char *tmp = NULL;
  2404. int reason_code = -1;
  2405. int i, ret = 0;
  2406. ASN1_OBJECT *hold = NULL;
  2407. ASN1_GENERALIZEDTIME *comp_time = NULL;
  2408. ASN1_ENUMERATED *rtmp = NULL;
  2409. ASN1_TIME *revDate = NULL;
  2410. i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str);
  2411. if (i == 0)
  2412. goto err;
  2413. if (rev && !X509_REVOKED_set_revocationDate(rev, revDate))
  2414. goto err;
  2415. if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) {
  2416. rtmp = ASN1_ENUMERATED_new();
  2417. if (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code))
  2418. goto err;
  2419. if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))
  2420. goto err;
  2421. }
  2422. if (rev && comp_time) {
  2423. if (!X509_REVOKED_add1_ext_i2d
  2424. (rev, NID_invalidity_date, comp_time, 0, 0))
  2425. goto err;
  2426. }
  2427. if (rev && hold) {
  2428. if (!X509_REVOKED_add1_ext_i2d
  2429. (rev, NID_hold_instruction_code, hold, 0, 0))
  2430. goto err;
  2431. }
  2432. if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)
  2433. ret = 2;
  2434. else
  2435. ret = 1;
  2436. err:
  2437. if (tmp)
  2438. OPENSSL_free(tmp);
  2439. ASN1_OBJECT_free(hold);
  2440. ASN1_GENERALIZEDTIME_free(comp_time);
  2441. ASN1_ENUMERATED_free(rtmp);
  2442. ASN1_TIME_free(revDate);
  2443. return ret;
  2444. }
  2445. int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str)
  2446. {
  2447. char buf[25], *pbuf, *p;
  2448. int j;
  2449. j = i2a_ASN1_OBJECT(bp, obj);
  2450. pbuf = buf;
  2451. for (j = 22 - j; j > 0; j--)
  2452. *(pbuf++) = ' ';
  2453. *(pbuf++) = ':';
  2454. *(pbuf++) = '\0';
  2455. BIO_puts(bp, buf);
  2456. if (str->type == V_ASN1_PRINTABLESTRING)
  2457. BIO_printf(bp, "PRINTABLE:'");
  2458. else if (str->type == V_ASN1_T61STRING)
  2459. BIO_printf(bp, "T61STRING:'");
  2460. else if (str->type == V_ASN1_IA5STRING)
  2461. BIO_printf(bp, "IA5STRING:'");
  2462. else if (str->type == V_ASN1_UNIVERSALSTRING)
  2463. BIO_printf(bp, "UNIVERSALSTRING:'");
  2464. else
  2465. BIO_printf(bp, "ASN.1 %2d:'", str->type);
  2466. p = (char *)str->data;
  2467. for (j = str->length; j > 0; j--) {
  2468. #ifdef CHARSET_EBCDIC
  2469. if ((*p >= 0x20) && (*p <= 0x7e))
  2470. BIO_printf(bp, "%c", os_toebcdic[*p]);
  2471. #else
  2472. if ((*p >= ' ') && (*p <= '~'))
  2473. BIO_printf(bp, "%c", *p);
  2474. #endif
  2475. else if (*p & 0x80)
  2476. BIO_printf(bp, "\\0x%02X", *p);
  2477. else if ((unsigned char)*p == 0xf7)
  2478. BIO_printf(bp, "^?");
  2479. #ifdef CHARSET_EBCDIC
  2480. else
  2481. BIO_printf(bp, "^%c", os_toebcdic[*p + 0x40]);
  2482. #else
  2483. else
  2484. BIO_printf(bp, "^%c", *p + '@');
  2485. #endif
  2486. p++;
  2487. }
  2488. BIO_printf(bp, "'\n");
  2489. return 1;
  2490. }
  2491. int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold,
  2492. ASN1_GENERALIZEDTIME **pinvtm, const char *str)
  2493. {
  2494. char *tmp = NULL;
  2495. char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
  2496. int reason_code = -1;
  2497. int ret = 0;
  2498. unsigned int i;
  2499. ASN1_OBJECT *hold = NULL;
  2500. ASN1_GENERALIZEDTIME *comp_time = NULL;
  2501. tmp = BUF_strdup(str);
  2502. p = strchr(tmp, ',');
  2503. rtime_str = tmp;
  2504. if (p) {
  2505. *p = '\0';
  2506. p++;
  2507. reason_str = p;
  2508. p = strchr(p, ',');
  2509. if (p) {
  2510. *p = '\0';
  2511. arg_str = p + 1;
  2512. }
  2513. }
  2514. if (prevtm) {
  2515. *prevtm = ASN1_UTCTIME_new();
  2516. if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) {
  2517. BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
  2518. goto err;
  2519. }
  2520. }
  2521. if (reason_str) {
  2522. for (i = 0; i < NUM_REASONS; i++) {
  2523. if (!strcasecmp(reason_str, crl_reasons[i])) {
  2524. reason_code = i;
  2525. break;
  2526. }
  2527. }
  2528. if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) {
  2529. BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
  2530. goto err;
  2531. }
  2532. if (reason_code == 7)
  2533. reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
  2534. else if (reason_code == 8) { /* Hold instruction */
  2535. if (!arg_str) {
  2536. BIO_printf(bio_err, "missing hold instruction\n");
  2537. goto err;
  2538. }
  2539. reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
  2540. hold = OBJ_txt2obj(arg_str, 0);
  2541. if (!hold) {
  2542. BIO_printf(bio_err, "invalid object identifier %s\n",
  2543. arg_str);
  2544. goto err;
  2545. }
  2546. if (phold)
  2547. *phold = hold;
  2548. } else if ((reason_code == 9) || (reason_code == 10)) {
  2549. if (!arg_str) {
  2550. BIO_printf(bio_err, "missing compromised time\n");
  2551. goto err;
  2552. }
  2553. comp_time = ASN1_GENERALIZEDTIME_new();
  2554. if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) {
  2555. BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
  2556. goto err;
  2557. }
  2558. if (reason_code == 9)
  2559. reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
  2560. else
  2561. reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
  2562. }
  2563. }
  2564. if (preason)
  2565. *preason = reason_code;
  2566. if (pinvtm)
  2567. *pinvtm = comp_time;
  2568. else
  2569. ASN1_GENERALIZEDTIME_free(comp_time);
  2570. ret = 1;
  2571. err:
  2572. if (tmp)
  2573. OPENSSL_free(tmp);
  2574. if (!phold)
  2575. ASN1_OBJECT_free(hold);
  2576. if (!pinvtm)
  2577. ASN1_GENERALIZEDTIME_free(comp_time);
  2578. return ret;
  2579. }