ca.c 93 KB

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