ca.c 93 KB

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