ca.c 73 KB

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