ca.c 91 KB

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