ca.c 86 KB

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