ca.c 88 KB

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