ca.c 86 KB

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