No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

x509_vfy.c 71 KiB

Fix build when using Visual Studio 2015 Update 1. Many of the compatibility issues are described at https://msdn.microsoft.com/en-us/library/mt612856.aspx. The macros that suppressed warnings on a per-function basis no longer work in Update 1, so replace them with #pragmas. Update 1 warns when |size_t| arguments to |printf| are casted, so stop doing that casting. Unfortunately, this requires an ugly hack to continue working in MSVC 2013 as MSVC 2013 doesn't support "%zu". Finally, Update 1 has new warnings, some of which need to be suppressed. --- Updated by davidben to give up on suppressing warnings in crypto/x509 and crypto/x509v3 as those directories aren't changed much from upstream. In each of these cases, upstream opted just blindly initialize the variable, so do the same. Also switch C4265 to level 4, per Microsoft's recommendation and work around a bug in limits.h that happens to get fixed by Google include order style. (limits.h is sensitive to whether corecrt.h, pulled in by stddef.h and some other headers, is included before it. The reason it affected just one file is we often put the file's header first, which means base.h is pulling in stddef.h. Relying on this is ugly, but it's no worse than what everything else is doing and this doesn't seem worth making something as tame as limits.h so messy to use.) Change-Id: I02d1f935356899f424d3525d03eca401bfa3e6cd Reviewed-on: https://boringssl-review.googlesource.com/7480 Reviewed-by: David Benjamin <davidben@google.com>
hace 8 años
Add some sanity checks when checking CRL scores and tests Note: this was accidentally omitted from OpenSSL 1.0.2 branch. Without this fix any attempt to use CRLs will crash. CVE-2016-7052 (Imported from upstream's 6e629b5be45face20b4ca71c4fcbfed78b864a2e) Test CRL Root Key: -----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAo16WiLWZuaymsD8n5SKPmxV1y6jjgr3BS/dUBpbrzd1aeFzN lI8l2jfAnzUyp+I21RQ+nh/MhqjGElkTtK9xMn1Y+S9GMRh+5R/Du0iCb1tCZIPY 07Tgrb0KMNWe0v2QKVVruuYSgxIWodBfxlKO64Z8AJ5IbnWpuRqO6rctN9qUoMlT IAB6dL4G0tDJ/PGFWOJYwOMEIX54bly2wgyYJVBKiRRt4f7n8H922qmvPNA9idmX 9G1VAtgV6x97XXi7ULORIQvn9lVQF6nTYDBJhyuPB+mLThbLP2o9orxGx7aCtnnB ZUIxUvHNOI0FaSaZH7Fi0xsZ/GkG2HZe7ImPJwIDAQABAoIBAQCJF9MTHfHGkk+/ DwCXlA0Wg0e6hBuHl10iNobYkMWIl/xXjOknhYiqOqb181py76472SVC5ERprC+r Lf0PXzqKuA117mnkwT2bYLCL9Skf8WEhoFLQNbVlloF6wYjqXcYgKYKh8HgQbZl4 aLg2YQl2NADTNABsUWj/4H2WEelsODVviqfFs725lFg9KHDI8zxAZXLzDt/M9uVL GxJiX12tr0AwaeAFZ1oPM/y+LznM3N3+Ht3jHHw3jZ/u8Z1RdAmdpu3bZ6tbwGBr 9edsH5rKkm9aBvMrY7eX5VHqaqyRNFyG152ZOJh4XiiFG7EmgTPCpaHo50Y018Re grVtk+FBAoGBANY3lY+V8ZOwMxSHes+kTnoimHO5Ob7nxrOC71i27x+4HHsYUeAr /zOOghiDIn+oNkuiX5CIOWZKx159Bp65CPpCbTb/fh+HYnSgXFgCw7XptycO7LXM 5GwR5jSfpfzBFdYxjxoUzDMFBwTEYRTm0HkUHkH+s+ajjw5wqqbcGLcfAoGBAMM8 DKW6Tb66xsf708f0jonAjKYTLZ+WOcwsBEWSFHoY8dUjvW5gqx5acHTEsc5ZTeh4 BCFLa+Mn9cuJWVJNs09k7Xb2PNl92HQ4GN2vbdkJhExbkT6oLDHg1hVD0w8KLfz1 lTAW6pS+6CdOHMEJpvqx89EgU/1GgIQ1fXYczE75AoGAKeJoXdDFkUjsU+FBhAPu TDcjc80Nm2QaF9NMFR5/lsYa236f06MGnQAKM9zADBHJu/Qdl1brUjLg1HrBppsr RDNkw1IlSOjhuUf5hkPUHGd8Jijm440SRIcjabqla8wdBupdvo2+d2NOQgJbsQiI ToQ+fkzcxAXK3Nnuo/1436UCgYBjLH7UNOZHS8OsVM0I1r8NVKVdu4JCfeJQR8/H s2P5ffBir+wLRMnH+nMDreMQiibcPxMCArkERAlE4jlgaJ38Z62E76KLbLTmnJRt EC9Bv+bXjvAiHvWMRMUbOj/ddPNVez7Uld+FvdBaHwDWQlvzHzBWfBCOKSEhh7Z6 qDhUqQKBgQDPMDx2i5rfmQp3imV9xUcCkIRsyYQVf8Eo7NV07IdUy/otmksgn4Zt Lbf3v2dvxOpTNTONWjp2c+iUQo8QxJCZr5Sfb21oQ9Ktcrmc/CY7LeBVDibXwxdM vRG8kBzvslFWh7REzC3u06GSVhyKDfW93kN2cKVwGoahRlhj7oHuZQ== -----END RSA PRIVATE KEY----- Change-Id: Icc58811c78d4682591f5bb460cdd219bd41566d8 Reviewed-on: https://boringssl-review.googlesource.com/11246 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
hace 8 años
Add some sanity checks when checking CRL scores and tests Note: this was accidentally omitted from OpenSSL 1.0.2 branch. Without this fix any attempt to use CRLs will crash. CVE-2016-7052 (Imported from upstream's 6e629b5be45face20b4ca71c4fcbfed78b864a2e) Test CRL Root Key: -----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAo16WiLWZuaymsD8n5SKPmxV1y6jjgr3BS/dUBpbrzd1aeFzN lI8l2jfAnzUyp+I21RQ+nh/MhqjGElkTtK9xMn1Y+S9GMRh+5R/Du0iCb1tCZIPY 07Tgrb0KMNWe0v2QKVVruuYSgxIWodBfxlKO64Z8AJ5IbnWpuRqO6rctN9qUoMlT IAB6dL4G0tDJ/PGFWOJYwOMEIX54bly2wgyYJVBKiRRt4f7n8H922qmvPNA9idmX 9G1VAtgV6x97XXi7ULORIQvn9lVQF6nTYDBJhyuPB+mLThbLP2o9orxGx7aCtnnB ZUIxUvHNOI0FaSaZH7Fi0xsZ/GkG2HZe7ImPJwIDAQABAoIBAQCJF9MTHfHGkk+/ DwCXlA0Wg0e6hBuHl10iNobYkMWIl/xXjOknhYiqOqb181py76472SVC5ERprC+r Lf0PXzqKuA117mnkwT2bYLCL9Skf8WEhoFLQNbVlloF6wYjqXcYgKYKh8HgQbZl4 aLg2YQl2NADTNABsUWj/4H2WEelsODVviqfFs725lFg9KHDI8zxAZXLzDt/M9uVL GxJiX12tr0AwaeAFZ1oPM/y+LznM3N3+Ht3jHHw3jZ/u8Z1RdAmdpu3bZ6tbwGBr 9edsH5rKkm9aBvMrY7eX5VHqaqyRNFyG152ZOJh4XiiFG7EmgTPCpaHo50Y018Re grVtk+FBAoGBANY3lY+V8ZOwMxSHes+kTnoimHO5Ob7nxrOC71i27x+4HHsYUeAr /zOOghiDIn+oNkuiX5CIOWZKx159Bp65CPpCbTb/fh+HYnSgXFgCw7XptycO7LXM 5GwR5jSfpfzBFdYxjxoUzDMFBwTEYRTm0HkUHkH+s+ajjw5wqqbcGLcfAoGBAMM8 DKW6Tb66xsf708f0jonAjKYTLZ+WOcwsBEWSFHoY8dUjvW5gqx5acHTEsc5ZTeh4 BCFLa+Mn9cuJWVJNs09k7Xb2PNl92HQ4GN2vbdkJhExbkT6oLDHg1hVD0w8KLfz1 lTAW6pS+6CdOHMEJpvqx89EgU/1GgIQ1fXYczE75AoGAKeJoXdDFkUjsU+FBhAPu TDcjc80Nm2QaF9NMFR5/lsYa236f06MGnQAKM9zADBHJu/Qdl1brUjLg1HrBppsr RDNkw1IlSOjhuUf5hkPUHGd8Jijm440SRIcjabqla8wdBupdvo2+d2NOQgJbsQiI ToQ+fkzcxAXK3Nnuo/1436UCgYBjLH7UNOZHS8OsVM0I1r8NVKVdu4JCfeJQR8/H s2P5ffBir+wLRMnH+nMDreMQiibcPxMCArkERAlE4jlgaJ38Z62E76KLbLTmnJRt EC9Bv+bXjvAiHvWMRMUbOj/ddPNVez7Uld+FvdBaHwDWQlvzHzBWfBCOKSEhh7Z6 qDhUqQKBgQDPMDx2i5rfmQp3imV9xUcCkIRsyYQVf8Eo7NV07IdUy/otmksgn4Zt Lbf3v2dvxOpTNTONWjp2c+iUQo8QxJCZr5Sfb21oQ9Ktcrmc/CY7LeBVDibXwxdM vRG8kBzvslFWh7REzC3u06GSVhyKDfW93kN2cKVwGoahRlhj7oHuZQ== -----END RSA PRIVATE KEY----- Change-Id: Icc58811c78d4682591f5bb460cdd219bd41566d8 Reviewed-on: https://boringssl-review.googlesource.com/11246 Reviewed-by: Steven Valdez <svaldez@google.com> Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
hace 8 años
Switch OPENSSL_VERSION_NUMBER to 1.1.0. Although we are derived from 1.0.2, we mimic 1.1.0 in some ways around our FOO_up_ref functions and opaque libssl types. This causes some difficulties when porting third-party code as any OPENSSL_VERSION_NUMBER checks for 1.1.0 APIs we have will be wrong. Moreover, adding accessors without changing OPENSSL_VERSION_NUMBER can break external projects. It is common to implement a compatibility version of an accessor under #ifdef as a static function. This then conflicts with our headers if we, unlike OpenSSL 1.0.2, have this function. This change switches OPENSSL_VERSION_NUMBER to 1.1.0 and atomically adds enough accessors for software with 1.1.0 support already. The hope is this will unblock hiding SSL_CTX and SSL_SESSION, which will be especially useful with C++-ficiation. The cost is we will hit some growing pains as more 1.1.0 consumers enter the ecosystem and we converge on the right set of APIs to import from upstream. It does not remove any 1.0.2 APIs, so we will not require that all projects support 1.1.0. The exception is APIs which changed in 1.1.0 but did not change the function signature. Those are breaking changes. Specifically: - SSL_CTX_sess_set_get_cb is now const-correct. - X509_get0_signature is now const-correct. For C++ consumers only, this change temporarily includes an overload hack for SSL_CTX_sess_set_get_cb that keeps the old callback working. This is a workaround for Node not yet supporting OpenSSL 1.1.0. The version number is set at (the as yet unreleased) 1.1.0g to denote that this change includes https://github.com/openssl/openssl/pull/4384. Bug: 91 Change-Id: I5eeb27448a6db4c25c244afac37f9604d9608a76 Reviewed-on: https://boringssl-review.googlesource.com/10340 Commit-Queue: David Benjamin <davidben@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org> Reviewed-by: Adam Langley <agl@google.com>
hace 8 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424
  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. #include <ctype.h>
  57. #include <string.h>
  58. #include <time.h>
  59. #include <openssl/asn1.h>
  60. #include <openssl/buf.h>
  61. #include <openssl/err.h>
  62. #include <openssl/evp.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/obj.h>
  65. #include <openssl/thread.h>
  66. #include <openssl/x509.h>
  67. #include <openssl/x509v3.h>
  68. #include "vpm_int.h"
  69. #include "../internal.h"
  70. static CRYPTO_EX_DATA_CLASS g_ex_data_class =
  71. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  72. /* CRL score values */
  73. /* No unhandled critical extensions */
  74. #define CRL_SCORE_NOCRITICAL 0x100
  75. /* certificate is within CRL scope */
  76. #define CRL_SCORE_SCOPE 0x080
  77. /* CRL times valid */
  78. #define CRL_SCORE_TIME 0x040
  79. /* Issuer name matches certificate */
  80. #define CRL_SCORE_ISSUER_NAME 0x020
  81. /* If this score or above CRL is probably valid */
  82. #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
  83. /* CRL issuer is certificate issuer */
  84. #define CRL_SCORE_ISSUER_CERT 0x018
  85. /* CRL issuer is on certificate path */
  86. #define CRL_SCORE_SAME_PATH 0x008
  87. /* CRL issuer matches CRL AKID */
  88. #define CRL_SCORE_AKID 0x004
  89. /* Have a delta CRL with valid times */
  90. #define CRL_SCORE_TIME_DELTA 0x002
  91. static int null_callback(int ok, X509_STORE_CTX *e);
  92. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
  93. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
  94. static int check_chain_extensions(X509_STORE_CTX *ctx);
  95. static int check_name_constraints(X509_STORE_CTX *ctx);
  96. static int check_id(X509_STORE_CTX *ctx);
  97. static int check_trust(X509_STORE_CTX *ctx);
  98. static int check_revocation(X509_STORE_CTX *ctx);
  99. static int check_cert(X509_STORE_CTX *ctx);
  100. static int check_policy(X509_STORE_CTX *ctx);
  101. static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
  102. unsigned int *preasons, X509_CRL *crl, X509 *x);
  103. static int get_crl_delta(X509_STORE_CTX *ctx,
  104. X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
  105. static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,
  106. int *pcrl_score, X509_CRL *base,
  107. STACK_OF(X509_CRL) *crls);
  108. static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
  109. int *pcrl_score);
  110. static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
  111. unsigned int *preasons);
  112. static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
  113. static int check_crl_chain(X509_STORE_CTX *ctx,
  114. STACK_OF(X509) *cert_path,
  115. STACK_OF(X509) *crl_path);
  116. static int internal_verify(X509_STORE_CTX *ctx);
  117. static int null_callback(int ok, X509_STORE_CTX *e)
  118. {
  119. return ok;
  120. }
  121. /* Return 1 is a certificate is self signed */
  122. static int cert_self_signed(X509 *x)
  123. {
  124. X509_check_purpose(x, -1, 0);
  125. if (x->ex_flags & EXFLAG_SS)
  126. return 1;
  127. else
  128. return 0;
  129. }
  130. /* Given a certificate try and find an exact match in the store */
  131. static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
  132. {
  133. STACK_OF(X509) *certs;
  134. X509 *xtmp = NULL;
  135. size_t i;
  136. /* Lookup all certs with matching subject name */
  137. certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
  138. if (certs == NULL)
  139. return NULL;
  140. /* Look for exact match */
  141. for (i = 0; i < sk_X509_num(certs); i++) {
  142. xtmp = sk_X509_value(certs, i);
  143. if (!X509_cmp(xtmp, x))
  144. break;
  145. }
  146. if (i < sk_X509_num(certs))
  147. X509_up_ref(xtmp);
  148. else
  149. xtmp = NULL;
  150. sk_X509_pop_free(certs, X509_free);
  151. return xtmp;
  152. }
  153. int X509_verify_cert(X509_STORE_CTX *ctx)
  154. {
  155. X509 *x, *xtmp, *xtmp2, *chain_ss = NULL;
  156. int bad_chain = 0;
  157. X509_VERIFY_PARAM *param = ctx->param;
  158. int depth, i, ok = 0;
  159. int num, j, retry, trust;
  160. int (*cb) (int xok, X509_STORE_CTX *xctx);
  161. STACK_OF(X509) *sktmp = NULL;
  162. if (ctx->cert == NULL) {
  163. OPENSSL_PUT_ERROR(X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
  164. ctx->error = X509_V_ERR_INVALID_CALL;
  165. return -1;
  166. }
  167. if (ctx->chain != NULL) {
  168. /*
  169. * This X509_STORE_CTX has already been used to verify a cert. We
  170. * cannot do another one.
  171. */
  172. OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  173. ctx->error = X509_V_ERR_INVALID_CALL;
  174. return -1;
  175. }
  176. cb = ctx->verify_cb;
  177. /*
  178. * first we make sure the chain we are going to build is present and that
  179. * the first entry is in place
  180. */
  181. ctx->chain = sk_X509_new_null();
  182. if (ctx->chain == NULL || !sk_X509_push(ctx->chain, ctx->cert)) {
  183. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  184. ctx->error = X509_V_ERR_OUT_OF_MEM;
  185. goto end;
  186. }
  187. X509_up_ref(ctx->cert);
  188. ctx->last_untrusted = 1;
  189. /* We use a temporary STACK so we can chop and hack at it.
  190. * sktmp = ctx->untrusted ++ ctx->ctx->additional_untrusted */
  191. if (ctx->untrusted != NULL
  192. && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
  193. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  194. ctx->error = X509_V_ERR_OUT_OF_MEM;
  195. goto end;
  196. }
  197. if (ctx->ctx->additional_untrusted != NULL) {
  198. if (sktmp == NULL) {
  199. sktmp = sk_X509_new_null();
  200. if (sktmp == NULL) {
  201. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  202. ctx->error = X509_V_ERR_OUT_OF_MEM;
  203. goto end;
  204. }
  205. }
  206. for (size_t k = 0; k < sk_X509_num(ctx->ctx->additional_untrusted);
  207. k++) {
  208. if (!sk_X509_push(sktmp,
  209. sk_X509_value(ctx->ctx->additional_untrusted,
  210. k))) {
  211. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  212. ctx->error = X509_V_ERR_OUT_OF_MEM;
  213. goto end;
  214. }
  215. }
  216. }
  217. num = sk_X509_num(ctx->chain);
  218. x = sk_X509_value(ctx->chain, num - 1);
  219. depth = param->depth;
  220. for (;;) {
  221. /* If we have enough, we break */
  222. if (depth < num)
  223. break; /* FIXME: If this happens, we should take
  224. * note of it and, if appropriate, use the
  225. * X509_V_ERR_CERT_CHAIN_TOO_LONG error code
  226. * later. */
  227. /* If we are self signed, we break */
  228. if (cert_self_signed(x))
  229. break;
  230. /*
  231. * If asked see if we can find issuer in trusted store first
  232. */
  233. if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) {
  234. ok = ctx->get_issuer(&xtmp, ctx, x);
  235. if (ok < 0) {
  236. ctx->error = X509_V_ERR_STORE_LOOKUP;
  237. goto end;
  238. }
  239. /*
  240. * If successful for now free up cert so it will be picked up
  241. * again later.
  242. */
  243. if (ok > 0) {
  244. X509_free(xtmp);
  245. break;
  246. }
  247. }
  248. /* If we were passed a cert chain, use it first */
  249. if (sktmp != NULL) {
  250. xtmp = find_issuer(ctx, sktmp, x);
  251. if (xtmp != NULL) {
  252. if (!sk_X509_push(ctx->chain, xtmp)) {
  253. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  254. ctx->error = X509_V_ERR_OUT_OF_MEM;
  255. ok = 0;
  256. goto end;
  257. }
  258. X509_up_ref(xtmp);
  259. (void)sk_X509_delete_ptr(sktmp, xtmp);
  260. ctx->last_untrusted++;
  261. x = xtmp;
  262. num++;
  263. /*
  264. * reparse the full chain for the next one
  265. */
  266. continue;
  267. }
  268. }
  269. break;
  270. }
  271. /* Remember how many untrusted certs we have */
  272. j = num;
  273. /*
  274. * at this point, chain should contain a list of untrusted certificates.
  275. * We now need to add at least one trusted one, if possible, otherwise we
  276. * complain.
  277. */
  278. do {
  279. /*
  280. * Examine last certificate in chain and see if it is self signed.
  281. */
  282. i = sk_X509_num(ctx->chain);
  283. x = sk_X509_value(ctx->chain, i - 1);
  284. if (cert_self_signed(x)) {
  285. /* we have a self signed certificate */
  286. if (sk_X509_num(ctx->chain) == 1) {
  287. /*
  288. * We have a single self signed certificate: see if we can
  289. * find it in the store. We must have an exact match to avoid
  290. * possible impersonation.
  291. */
  292. ok = ctx->get_issuer(&xtmp, ctx, x);
  293. if ((ok <= 0) || X509_cmp(x, xtmp)) {
  294. ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
  295. ctx->current_cert = x;
  296. ctx->error_depth = i - 1;
  297. if (ok == 1)
  298. X509_free(xtmp);
  299. bad_chain = 1;
  300. ok = cb(0, ctx);
  301. if (!ok)
  302. goto end;
  303. } else {
  304. /*
  305. * We have a match: replace certificate with store
  306. * version so we get any trust settings.
  307. */
  308. X509_free(x);
  309. x = xtmp;
  310. (void)sk_X509_set(ctx->chain, i - 1, x);
  311. ctx->last_untrusted = 0;
  312. }
  313. } else {
  314. /*
  315. * extract and save self signed certificate for later use
  316. */
  317. chain_ss = sk_X509_pop(ctx->chain);
  318. ctx->last_untrusted--;
  319. num--;
  320. j--;
  321. x = sk_X509_value(ctx->chain, num - 1);
  322. }
  323. }
  324. /* We now lookup certs from the certificate store */
  325. for (;;) {
  326. /* If we have enough, we break */
  327. if (depth < num)
  328. break;
  329. /* If we are self signed, we break */
  330. if (cert_self_signed(x))
  331. break;
  332. ok = ctx->get_issuer(&xtmp, ctx, x);
  333. if (ok < 0) {
  334. ctx->error = X509_V_ERR_STORE_LOOKUP;
  335. goto end;
  336. }
  337. if (ok == 0)
  338. break;
  339. x = xtmp;
  340. if (!sk_X509_push(ctx->chain, x)) {
  341. X509_free(xtmp);
  342. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  343. ctx->error = X509_V_ERR_OUT_OF_MEM;
  344. ok = 0;
  345. goto end;
  346. }
  347. num++;
  348. }
  349. /* we now have our chain, lets check it... */
  350. trust = check_trust(ctx);
  351. /* If explicitly rejected error */
  352. if (trust == X509_TRUST_REJECTED) {
  353. ok = 0;
  354. goto end;
  355. }
  356. /*
  357. * If it's not explicitly trusted then check if there is an alternative
  358. * chain that could be used. We only do this if we haven't already
  359. * checked via TRUSTED_FIRST and the user hasn't switched off alternate
  360. * chain checking
  361. */
  362. retry = 0;
  363. if (trust != X509_TRUST_TRUSTED
  364. && !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
  365. && !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
  366. while (j-- > 1) {
  367. xtmp2 = sk_X509_value(ctx->chain, j - 1);
  368. ok = ctx->get_issuer(&xtmp, ctx, xtmp2);
  369. if (ok < 0)
  370. goto end;
  371. /* Check if we found an alternate chain */
  372. if (ok > 0) {
  373. /*
  374. * Free up the found cert we'll add it again later
  375. */
  376. X509_free(xtmp);
  377. /*
  378. * Dump all the certs above this point - we've found an
  379. * alternate chain
  380. */
  381. while (num > j) {
  382. xtmp = sk_X509_pop(ctx->chain);
  383. X509_free(xtmp);
  384. num--;
  385. }
  386. ctx->last_untrusted = sk_X509_num(ctx->chain);
  387. retry = 1;
  388. break;
  389. }
  390. }
  391. }
  392. } while (retry);
  393. /*
  394. * If not explicitly trusted then indicate error unless it's a single
  395. * self signed certificate in which case we've indicated an error already
  396. * and set bad_chain == 1
  397. */
  398. if (trust != X509_TRUST_TRUSTED && !bad_chain) {
  399. if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) {
  400. if (ctx->last_untrusted >= num)
  401. ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
  402. else
  403. ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
  404. ctx->current_cert = x;
  405. } else {
  406. sk_X509_push(ctx->chain, chain_ss);
  407. num++;
  408. ctx->last_untrusted = num;
  409. ctx->current_cert = chain_ss;
  410. ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
  411. chain_ss = NULL;
  412. }
  413. ctx->error_depth = num - 1;
  414. bad_chain = 1;
  415. ok = cb(0, ctx);
  416. if (!ok)
  417. goto end;
  418. }
  419. /* We have the chain complete: now we need to check its purpose */
  420. ok = check_chain_extensions(ctx);
  421. if (!ok)
  422. goto end;
  423. ok = check_id(ctx);
  424. if (!ok)
  425. goto end;
  426. /*
  427. * Check revocation status: we do this after copying parameters because
  428. * they may be needed for CRL signature verification.
  429. */
  430. ok = ctx->check_revocation(ctx);
  431. if (!ok)
  432. goto end;
  433. int err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
  434. ctx->param->flags);
  435. if (err != X509_V_OK) {
  436. ctx->error = err;
  437. ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
  438. ok = cb(0, ctx);
  439. if (!ok)
  440. goto end;
  441. }
  442. /* At this point, we have a chain and need to verify it */
  443. if (ctx->verify != NULL)
  444. ok = ctx->verify(ctx);
  445. else
  446. ok = internal_verify(ctx);
  447. if (!ok)
  448. goto end;
  449. /* Check name constraints */
  450. ok = check_name_constraints(ctx);
  451. if (!ok)
  452. goto end;
  453. /* If we get this far evaluate policies */
  454. if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
  455. ok = ctx->check_policy(ctx);
  456. end:
  457. if (sktmp != NULL)
  458. sk_X509_free(sktmp);
  459. if (chain_ss != NULL)
  460. X509_free(chain_ss);
  461. /* Safety net, error returns must set ctx->error */
  462. if (ok <= 0 && ctx->error == X509_V_OK)
  463. ctx->error = X509_V_ERR_UNSPECIFIED;
  464. return ok;
  465. }
  466. /*
  467. * Given a STACK_OF(X509) find the issuer of cert (if any)
  468. */
  469. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
  470. {
  471. size_t i;
  472. X509 *issuer;
  473. for (i = 0; i < sk_X509_num(sk); i++) {
  474. issuer = sk_X509_value(sk, i);
  475. if (ctx->check_issued(ctx, x, issuer))
  476. return issuer;
  477. }
  478. return NULL;
  479. }
  480. /* Given a possible certificate and issuer check them */
  481. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
  482. {
  483. int ret;
  484. ret = X509_check_issued(issuer, x);
  485. if (ret == X509_V_OK)
  486. return 1;
  487. /* If we haven't asked for issuer errors don't set ctx */
  488. if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
  489. return 0;
  490. ctx->error = ret;
  491. ctx->current_cert = x;
  492. ctx->current_issuer = issuer;
  493. return ctx->verify_cb(0, ctx);
  494. }
  495. /* Alternative lookup method: look from a STACK stored in other_ctx */
  496. static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
  497. {
  498. *issuer = find_issuer(ctx, ctx->other_ctx, x);
  499. if (*issuer) {
  500. X509_up_ref(*issuer);
  501. return 1;
  502. } else
  503. return 0;
  504. }
  505. /*
  506. * Check a certificate chains extensions for consistency with the supplied
  507. * purpose
  508. */
  509. static int check_chain_extensions(X509_STORE_CTX *ctx)
  510. {
  511. int i, ok = 0, plen = 0;
  512. X509 *x;
  513. int (*cb) (int xok, X509_STORE_CTX *xctx);
  514. int proxy_path_length = 0;
  515. int purpose;
  516. int allow_proxy_certs;
  517. cb = ctx->verify_cb;
  518. enum {
  519. // ca_or_leaf allows either type of certificate so that direct use of
  520. // self-signed certificates works.
  521. ca_or_leaf,
  522. must_be_ca,
  523. must_not_be_ca,
  524. } ca_requirement;
  525. /* CRL path validation */
  526. if (ctx->parent) {
  527. allow_proxy_certs = 0;
  528. purpose = X509_PURPOSE_CRL_SIGN;
  529. } else {
  530. allow_proxy_certs =
  531. ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
  532. purpose = ctx->param->purpose;
  533. }
  534. ca_requirement = ca_or_leaf;
  535. /* Check all untrusted certificates */
  536. for (i = 0; i < ctx->last_untrusted; i++) {
  537. int ret;
  538. x = sk_X509_value(ctx->chain, i);
  539. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  540. && (x->ex_flags & EXFLAG_CRITICAL)) {
  541. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
  542. ctx->error_depth = i;
  543. ctx->current_cert = x;
  544. ok = cb(0, ctx);
  545. if (!ok)
  546. goto end;
  547. }
  548. if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
  549. ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
  550. ctx->error_depth = i;
  551. ctx->current_cert = x;
  552. ok = cb(0, ctx);
  553. if (!ok)
  554. goto end;
  555. }
  556. switch (ca_requirement) {
  557. case ca_or_leaf:
  558. ret = 1;
  559. break;
  560. case must_not_be_ca:
  561. if (X509_check_ca(x)) {
  562. ret = 0;
  563. ctx->error = X509_V_ERR_INVALID_NON_CA;
  564. } else
  565. ret = 1;
  566. break;
  567. case must_be_ca:
  568. if (!X509_check_ca(x)) {
  569. ret = 0;
  570. ctx->error = X509_V_ERR_INVALID_CA;
  571. } else
  572. ret = 1;
  573. break;
  574. default:
  575. // impossible.
  576. ret = 0;
  577. }
  578. if (ret == 0) {
  579. ctx->error_depth = i;
  580. ctx->current_cert = x;
  581. ok = cb(0, ctx);
  582. if (!ok)
  583. goto end;
  584. }
  585. if (ctx->param->purpose > 0) {
  586. ret = X509_check_purpose(x, purpose, ca_requirement == must_be_ca);
  587. if (ret != 1) {
  588. ret = 0;
  589. ctx->error = X509_V_ERR_INVALID_PURPOSE;
  590. ctx->error_depth = i;
  591. ctx->current_cert = x;
  592. ok = cb(0, ctx);
  593. if (!ok)
  594. goto end;
  595. }
  596. }
  597. /* Check pathlen if not self issued */
  598. if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
  599. && (x->ex_pathlen != -1)
  600. && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
  601. ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
  602. ctx->error_depth = i;
  603. ctx->current_cert = x;
  604. ok = cb(0, ctx);
  605. if (!ok)
  606. goto end;
  607. }
  608. /* Increment path length if not self issued */
  609. if (!(x->ex_flags & EXFLAG_SI))
  610. plen++;
  611. /*
  612. * If this certificate is a proxy certificate, the next certificate
  613. * must be another proxy certificate or a EE certificate. If not,
  614. * the next certificate must be a CA certificate.
  615. */
  616. if (x->ex_flags & EXFLAG_PROXY) {
  617. if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
  618. ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
  619. ctx->error_depth = i;
  620. ctx->current_cert = x;
  621. ok = cb(0, ctx);
  622. if (!ok)
  623. goto end;
  624. }
  625. proxy_path_length++;
  626. ca_requirement = must_not_be_ca;
  627. } else {
  628. ca_requirement = must_be_ca;
  629. }
  630. }
  631. ok = 1;
  632. end:
  633. return ok;
  634. }
  635. static int check_name_constraints(X509_STORE_CTX *ctx)
  636. {
  637. X509 *x;
  638. int i, j, rv;
  639. /* Check name constraints for all certificates */
  640. for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
  641. x = sk_X509_value(ctx->chain, i);
  642. /* Ignore self issued certs unless last in chain */
  643. if (i && (x->ex_flags & EXFLAG_SI))
  644. continue;
  645. /*
  646. * Check against constraints for all certificates higher in chain
  647. * including trust anchor. Trust anchor not strictly speaking needed
  648. * but if it includes constraints it is to be assumed it expects them
  649. * to be obeyed.
  650. */
  651. for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
  652. NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
  653. if (nc) {
  654. rv = NAME_CONSTRAINTS_check(x, nc);
  655. switch (rv) {
  656. case X509_V_OK:
  657. continue;
  658. case X509_V_ERR_OUT_OF_MEM:
  659. ctx->error = rv;
  660. return 0;
  661. default:
  662. ctx->error = rv;
  663. ctx->error_depth = i;
  664. ctx->current_cert = x;
  665. if (!ctx->verify_cb(0, ctx))
  666. return 0;
  667. break;
  668. }
  669. }
  670. }
  671. }
  672. return 1;
  673. }
  674. static int check_id_error(X509_STORE_CTX *ctx, int errcode)
  675. {
  676. ctx->error = errcode;
  677. ctx->current_cert = ctx->cert;
  678. ctx->error_depth = 0;
  679. return ctx->verify_cb(0, ctx);
  680. }
  681. static int check_hosts(X509 *x, X509_VERIFY_PARAM_ID *id)
  682. {
  683. size_t i;
  684. size_t n = sk_OPENSSL_STRING_num(id->hosts);
  685. char *name;
  686. if (id->peername != NULL) {
  687. OPENSSL_free(id->peername);
  688. id->peername = NULL;
  689. }
  690. for (i = 0; i < n; ++i) {
  691. name = sk_OPENSSL_STRING_value(id->hosts, i);
  692. if (X509_check_host(x, name, strlen(name), id->hostflags,
  693. &id->peername) > 0)
  694. return 1;
  695. }
  696. return n == 0;
  697. }
  698. static int check_id(X509_STORE_CTX *ctx)
  699. {
  700. X509_VERIFY_PARAM *vpm = ctx->param;
  701. X509_VERIFY_PARAM_ID *id = vpm->id;
  702. X509 *x = ctx->cert;
  703. if (id->poison) {
  704. if (!check_id_error(ctx, X509_V_ERR_INVALID_CALL))
  705. return 0;
  706. }
  707. if (id->hosts && check_hosts(x, id) <= 0) {
  708. if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
  709. return 0;
  710. }
  711. if (id->email && X509_check_email(x, id->email, id->emaillen, 0) <= 0) {
  712. if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
  713. return 0;
  714. }
  715. if (id->ip && X509_check_ip(x, id->ip, id->iplen, 0) <= 0) {
  716. if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
  717. return 0;
  718. }
  719. return 1;
  720. }
  721. static int check_trust(X509_STORE_CTX *ctx)
  722. {
  723. size_t i;
  724. int ok;
  725. X509 *x = NULL;
  726. int (*cb) (int xok, X509_STORE_CTX *xctx);
  727. cb = ctx->verify_cb;
  728. /* Check all trusted certificates in chain */
  729. for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) {
  730. x = sk_X509_value(ctx->chain, i);
  731. ok = X509_check_trust(x, ctx->param->trust, 0);
  732. /* If explicitly trusted return trusted */
  733. if (ok == X509_TRUST_TRUSTED)
  734. return X509_TRUST_TRUSTED;
  735. /*
  736. * If explicitly rejected notify callback and reject if not
  737. * overridden.
  738. */
  739. if (ok == X509_TRUST_REJECTED) {
  740. ctx->error_depth = i;
  741. ctx->current_cert = x;
  742. ctx->error = X509_V_ERR_CERT_REJECTED;
  743. ok = cb(0, ctx);
  744. if (!ok)
  745. return X509_TRUST_REJECTED;
  746. }
  747. }
  748. /*
  749. * If we accept partial chains and have at least one trusted certificate
  750. * return success.
  751. */
  752. if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
  753. X509 *mx;
  754. if (ctx->last_untrusted < (int)sk_X509_num(ctx->chain))
  755. return X509_TRUST_TRUSTED;
  756. x = sk_X509_value(ctx->chain, 0);
  757. mx = lookup_cert_match(ctx, x);
  758. if (mx) {
  759. (void)sk_X509_set(ctx->chain, 0, mx);
  760. X509_free(x);
  761. ctx->last_untrusted = 0;
  762. return X509_TRUST_TRUSTED;
  763. }
  764. }
  765. /*
  766. * If no trusted certs in chain at all return untrusted and allow
  767. * standard (no issuer cert) etc errors to be indicated.
  768. */
  769. return X509_TRUST_UNTRUSTED;
  770. }
  771. static int check_revocation(X509_STORE_CTX *ctx)
  772. {
  773. int i, last, ok;
  774. if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
  775. return 1;
  776. if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
  777. last = sk_X509_num(ctx->chain) - 1;
  778. else {
  779. /* If checking CRL paths this isn't the EE certificate */
  780. if (ctx->parent)
  781. return 1;
  782. last = 0;
  783. }
  784. for (i = 0; i <= last; i++) {
  785. ctx->error_depth = i;
  786. ok = check_cert(ctx);
  787. if (!ok)
  788. return ok;
  789. }
  790. return 1;
  791. }
  792. static int check_cert(X509_STORE_CTX *ctx)
  793. {
  794. X509_CRL *crl = NULL, *dcrl = NULL;
  795. X509 *x;
  796. int ok = 0, cnum;
  797. unsigned int last_reasons;
  798. cnum = ctx->error_depth;
  799. x = sk_X509_value(ctx->chain, cnum);
  800. ctx->current_cert = x;
  801. ctx->current_issuer = NULL;
  802. ctx->current_crl_score = 0;
  803. ctx->current_reasons = 0;
  804. while (ctx->current_reasons != CRLDP_ALL_REASONS) {
  805. last_reasons = ctx->current_reasons;
  806. /* Try to retrieve relevant CRL */
  807. if (ctx->get_crl)
  808. ok = ctx->get_crl(ctx, &crl, x);
  809. else
  810. ok = get_crl_delta(ctx, &crl, &dcrl, x);
  811. /*
  812. * If error looking up CRL, nothing we can do except notify callback
  813. */
  814. if (!ok) {
  815. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  816. ok = ctx->verify_cb(0, ctx);
  817. goto err;
  818. }
  819. ctx->current_crl = crl;
  820. ok = ctx->check_crl(ctx, crl);
  821. if (!ok)
  822. goto err;
  823. if (dcrl) {
  824. ok = ctx->check_crl(ctx, dcrl);
  825. if (!ok)
  826. goto err;
  827. ok = ctx->cert_crl(ctx, dcrl, x);
  828. if (!ok)
  829. goto err;
  830. } else
  831. ok = 1;
  832. /* Don't look in full CRL if delta reason is removefromCRL */
  833. if (ok != 2) {
  834. ok = ctx->cert_crl(ctx, crl, x);
  835. if (!ok)
  836. goto err;
  837. }
  838. X509_CRL_free(crl);
  839. X509_CRL_free(dcrl);
  840. crl = NULL;
  841. dcrl = NULL;
  842. /*
  843. * If reasons not updated we wont get anywhere by another iteration,
  844. * so exit loop.
  845. */
  846. if (last_reasons == ctx->current_reasons) {
  847. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  848. ok = ctx->verify_cb(0, ctx);
  849. goto err;
  850. }
  851. }
  852. err:
  853. X509_CRL_free(crl);
  854. X509_CRL_free(dcrl);
  855. ctx->current_crl = NULL;
  856. return ok;
  857. }
  858. /* Check CRL times against values in X509_STORE_CTX */
  859. static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
  860. {
  861. time_t *ptime;
  862. int i;
  863. if (notify)
  864. ctx->current_crl = crl;
  865. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  866. ptime = &ctx->param->check_time;
  867. else
  868. ptime = NULL;
  869. i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
  870. if (i == 0) {
  871. if (!notify)
  872. return 0;
  873. ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
  874. if (!ctx->verify_cb(0, ctx))
  875. return 0;
  876. }
  877. if (i > 0) {
  878. if (!notify)
  879. return 0;
  880. ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
  881. if (!ctx->verify_cb(0, ctx))
  882. return 0;
  883. }
  884. if (X509_CRL_get_nextUpdate(crl)) {
  885. i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
  886. if (i == 0) {
  887. if (!notify)
  888. return 0;
  889. ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
  890. if (!ctx->verify_cb(0, ctx))
  891. return 0;
  892. }
  893. /* Ignore expiry of base CRL is delta is valid */
  894. if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
  895. if (!notify)
  896. return 0;
  897. ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
  898. if (!ctx->verify_cb(0, ctx))
  899. return 0;
  900. }
  901. }
  902. if (notify)
  903. ctx->current_crl = NULL;
  904. return 1;
  905. }
  906. static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
  907. X509 **pissuer, int *pscore, unsigned int *preasons,
  908. STACK_OF(X509_CRL) *crls)
  909. {
  910. int crl_score, best_score = *pscore;
  911. size_t i;
  912. unsigned int reasons, best_reasons = 0;
  913. X509 *x = ctx->current_cert;
  914. X509_CRL *crl, *best_crl = NULL;
  915. X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
  916. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  917. crl = sk_X509_CRL_value(crls, i);
  918. reasons = *preasons;
  919. crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
  920. if (crl_score < best_score || crl_score == 0)
  921. continue;
  922. /* If current CRL is equivalent use it if it is newer */
  923. if (crl_score == best_score && best_crl != NULL) {
  924. int day, sec;
  925. if (ASN1_TIME_diff(&day, &sec, X509_CRL_get_lastUpdate(best_crl),
  926. X509_CRL_get_lastUpdate(crl)) == 0)
  927. continue;
  928. /*
  929. * ASN1_TIME_diff never returns inconsistent signs for |day|
  930. * and |sec|.
  931. */
  932. if (day <= 0 && sec <= 0)
  933. continue;
  934. }
  935. best_crl = crl;
  936. best_crl_issuer = crl_issuer;
  937. best_score = crl_score;
  938. best_reasons = reasons;
  939. }
  940. if (best_crl) {
  941. if (*pcrl)
  942. X509_CRL_free(*pcrl);
  943. *pcrl = best_crl;
  944. *pissuer = best_crl_issuer;
  945. *pscore = best_score;
  946. *preasons = best_reasons;
  947. X509_CRL_up_ref(best_crl);
  948. if (*pdcrl) {
  949. X509_CRL_free(*pdcrl);
  950. *pdcrl = NULL;
  951. }
  952. get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
  953. }
  954. if (best_score >= CRL_SCORE_VALID)
  955. return 1;
  956. return 0;
  957. }
  958. /*
  959. * Compare two CRL extensions for delta checking purposes. They should be
  960. * both present or both absent. If both present all fields must be identical.
  961. */
  962. static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
  963. {
  964. ASN1_OCTET_STRING *exta, *extb;
  965. int i;
  966. i = X509_CRL_get_ext_by_NID(a, nid, -1);
  967. if (i >= 0) {
  968. /* Can't have multiple occurrences */
  969. if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
  970. return 0;
  971. exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
  972. } else
  973. exta = NULL;
  974. i = X509_CRL_get_ext_by_NID(b, nid, -1);
  975. if (i >= 0) {
  976. if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
  977. return 0;
  978. extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
  979. } else
  980. extb = NULL;
  981. if (!exta && !extb)
  982. return 1;
  983. if (!exta || !extb)
  984. return 0;
  985. if (ASN1_OCTET_STRING_cmp(exta, extb))
  986. return 0;
  987. return 1;
  988. }
  989. /* See if a base and delta are compatible */
  990. static int check_delta_base(X509_CRL *delta, X509_CRL *base)
  991. {
  992. /* Delta CRL must be a delta */
  993. if (!delta->base_crl_number)
  994. return 0;
  995. /* Base must have a CRL number */
  996. if (!base->crl_number)
  997. return 0;
  998. /* Issuer names must match */
  999. if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
  1000. return 0;
  1001. /* AKID and IDP must match */
  1002. if (!crl_extension_match(delta, base, NID_authority_key_identifier))
  1003. return 0;
  1004. if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
  1005. return 0;
  1006. /* Delta CRL base number must not exceed Full CRL number. */
  1007. if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
  1008. return 0;
  1009. /* Delta CRL number must exceed full CRL number */
  1010. if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
  1011. return 1;
  1012. return 0;
  1013. }
  1014. /*
  1015. * For a given base CRL find a delta... maybe extend to delta scoring or
  1016. * retrieve a chain of deltas...
  1017. */
  1018. static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
  1019. X509_CRL *base, STACK_OF(X509_CRL) *crls)
  1020. {
  1021. X509_CRL *delta;
  1022. size_t i;
  1023. if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
  1024. return;
  1025. if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
  1026. return;
  1027. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  1028. delta = sk_X509_CRL_value(crls, i);
  1029. if (check_delta_base(delta, base)) {
  1030. if (check_crl_time(ctx, delta, 0))
  1031. *pscore |= CRL_SCORE_TIME_DELTA;
  1032. X509_CRL_up_ref(delta);
  1033. *dcrl = delta;
  1034. return;
  1035. }
  1036. }
  1037. *dcrl = NULL;
  1038. }
  1039. /*
  1040. * For a given CRL return how suitable it is for the supplied certificate
  1041. * 'x'. The return value is a mask of several criteria. If the issuer is not
  1042. * the certificate issuer this is returned in *pissuer. The reasons mask is
  1043. * also used to determine if the CRL is suitable: if no new reasons the CRL
  1044. * is rejected, otherwise reasons is updated.
  1045. */
  1046. static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
  1047. unsigned int *preasons, X509_CRL *crl, X509 *x)
  1048. {
  1049. int crl_score = 0;
  1050. unsigned int tmp_reasons = *preasons, crl_reasons;
  1051. /* First see if we can reject CRL straight away */
  1052. /* Invalid IDP cannot be processed */
  1053. if (crl->idp_flags & IDP_INVALID)
  1054. return 0;
  1055. /* Reason codes or indirect CRLs need extended CRL support */
  1056. if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
  1057. if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
  1058. return 0;
  1059. } else if (crl->idp_flags & IDP_REASONS) {
  1060. /* If no new reasons reject */
  1061. if (!(crl->idp_reasons & ~tmp_reasons))
  1062. return 0;
  1063. }
  1064. /* Don't process deltas at this stage */
  1065. else if (crl->base_crl_number)
  1066. return 0;
  1067. /* If issuer name doesn't match certificate need indirect CRL */
  1068. if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
  1069. if (!(crl->idp_flags & IDP_INDIRECT))
  1070. return 0;
  1071. } else
  1072. crl_score |= CRL_SCORE_ISSUER_NAME;
  1073. if (!(crl->flags & EXFLAG_CRITICAL))
  1074. crl_score |= CRL_SCORE_NOCRITICAL;
  1075. /* Check expiry */
  1076. if (check_crl_time(ctx, crl, 0))
  1077. crl_score |= CRL_SCORE_TIME;
  1078. /* Check authority key ID and locate certificate issuer */
  1079. crl_akid_check(ctx, crl, pissuer, &crl_score);
  1080. /* If we can't locate certificate issuer at this point forget it */
  1081. if (!(crl_score & CRL_SCORE_AKID))
  1082. return 0;
  1083. /* Check cert for matching CRL distribution points */
  1084. if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
  1085. /* If no new reasons reject */
  1086. if (!(crl_reasons & ~tmp_reasons))
  1087. return 0;
  1088. tmp_reasons |= crl_reasons;
  1089. crl_score |= CRL_SCORE_SCOPE;
  1090. }
  1091. *preasons = tmp_reasons;
  1092. return crl_score;
  1093. }
  1094. static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
  1095. X509 **pissuer, int *pcrl_score)
  1096. {
  1097. X509 *crl_issuer = NULL;
  1098. X509_NAME *cnm = X509_CRL_get_issuer(crl);
  1099. int cidx = ctx->error_depth;
  1100. size_t i;
  1101. if ((size_t)cidx != sk_X509_num(ctx->chain) - 1)
  1102. cidx++;
  1103. crl_issuer = sk_X509_value(ctx->chain, cidx);
  1104. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1105. if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
  1106. *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
  1107. *pissuer = crl_issuer;
  1108. return;
  1109. }
  1110. }
  1111. for (cidx++; cidx < (int)sk_X509_num(ctx->chain); cidx++) {
  1112. crl_issuer = sk_X509_value(ctx->chain, cidx);
  1113. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  1114. continue;
  1115. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1116. *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
  1117. *pissuer = crl_issuer;
  1118. return;
  1119. }
  1120. }
  1121. /* Anything else needs extended CRL support */
  1122. if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
  1123. return;
  1124. /*
  1125. * Otherwise the CRL issuer is not on the path. Look for it in the set of
  1126. * untrusted certificates.
  1127. */
  1128. for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
  1129. crl_issuer = sk_X509_value(ctx->untrusted, i);
  1130. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  1131. continue;
  1132. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1133. *pissuer = crl_issuer;
  1134. *pcrl_score |= CRL_SCORE_AKID;
  1135. return;
  1136. }
  1137. }
  1138. for (i = 0; i < sk_X509_num(ctx->ctx->additional_untrusted); i++) {
  1139. crl_issuer = sk_X509_value(ctx->ctx->additional_untrusted, i);
  1140. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  1141. continue;
  1142. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1143. *pissuer = crl_issuer;
  1144. *pcrl_score |= CRL_SCORE_AKID;
  1145. return;
  1146. }
  1147. }
  1148. }
  1149. /*
  1150. * Check the path of a CRL issuer certificate. This creates a new
  1151. * X509_STORE_CTX and populates it with most of the parameters from the
  1152. * parent. This could be optimised somewhat since a lot of path checking will
  1153. * be duplicated by the parent, but this will rarely be used in practice.
  1154. */
  1155. static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
  1156. {
  1157. X509_STORE_CTX crl_ctx;
  1158. int ret;
  1159. /* Don't allow recursive CRL path validation */
  1160. if (ctx->parent)
  1161. return 0;
  1162. if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
  1163. return -1;
  1164. crl_ctx.crls = ctx->crls;
  1165. /* Copy verify params across */
  1166. X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
  1167. crl_ctx.parent = ctx;
  1168. crl_ctx.verify_cb = ctx->verify_cb;
  1169. /* Verify CRL issuer */
  1170. ret = X509_verify_cert(&crl_ctx);
  1171. if (ret <= 0)
  1172. goto err;
  1173. /* Check chain is acceptable */
  1174. ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
  1175. err:
  1176. X509_STORE_CTX_cleanup(&crl_ctx);
  1177. return ret;
  1178. }
  1179. /*
  1180. * RFC3280 says nothing about the relationship between CRL path and
  1181. * certificate path, which could lead to situations where a certificate could
  1182. * be revoked or validated by a CA not authorised to do so. RFC5280 is more
  1183. * strict and states that the two paths must end in the same trust anchor,
  1184. * though some discussions remain... until this is resolved we use the
  1185. * RFC5280 version
  1186. */
  1187. static int check_crl_chain(X509_STORE_CTX *ctx,
  1188. STACK_OF(X509) *cert_path,
  1189. STACK_OF(X509) *crl_path)
  1190. {
  1191. X509 *cert_ta, *crl_ta;
  1192. cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
  1193. crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
  1194. if (!X509_cmp(cert_ta, crl_ta))
  1195. return 1;
  1196. return 0;
  1197. }
  1198. /*
  1199. * Check for match between two dist point names: three separate cases. 1.
  1200. * Both are relative names and compare X509_NAME types. 2. One full, one
  1201. * relative. Compare X509_NAME to GENERAL_NAMES. 3. Both are full names and
  1202. * compare two GENERAL_NAMES. 4. One is NULL: automatic match.
  1203. */
  1204. static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
  1205. {
  1206. X509_NAME *nm = NULL;
  1207. GENERAL_NAMES *gens = NULL;
  1208. GENERAL_NAME *gena, *genb;
  1209. size_t i, j;
  1210. if (!a || !b)
  1211. return 1;
  1212. if (a->type == 1) {
  1213. if (!a->dpname)
  1214. return 0;
  1215. /* Case 1: two X509_NAME */
  1216. if (b->type == 1) {
  1217. if (!b->dpname)
  1218. return 0;
  1219. if (!X509_NAME_cmp(a->dpname, b->dpname))
  1220. return 1;
  1221. else
  1222. return 0;
  1223. }
  1224. /* Case 2: set name and GENERAL_NAMES appropriately */
  1225. nm = a->dpname;
  1226. gens = b->name.fullname;
  1227. } else if (b->type == 1) {
  1228. if (!b->dpname)
  1229. return 0;
  1230. /* Case 2: set name and GENERAL_NAMES appropriately */
  1231. gens = a->name.fullname;
  1232. nm = b->dpname;
  1233. }
  1234. /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
  1235. if (nm) {
  1236. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  1237. gena = sk_GENERAL_NAME_value(gens, i);
  1238. if (gena->type != GEN_DIRNAME)
  1239. continue;
  1240. if (!X509_NAME_cmp(nm, gena->d.directoryName))
  1241. return 1;
  1242. }
  1243. return 0;
  1244. }
  1245. /* Else case 3: two GENERAL_NAMES */
  1246. for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
  1247. gena = sk_GENERAL_NAME_value(a->name.fullname, i);
  1248. for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
  1249. genb = sk_GENERAL_NAME_value(b->name.fullname, j);
  1250. if (!GENERAL_NAME_cmp(gena, genb))
  1251. return 1;
  1252. }
  1253. }
  1254. return 0;
  1255. }
  1256. static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
  1257. {
  1258. size_t i;
  1259. X509_NAME *nm = X509_CRL_get_issuer(crl);
  1260. /* If no CRLissuer return is successful iff don't need a match */
  1261. if (!dp->CRLissuer)
  1262. return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
  1263. for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
  1264. GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
  1265. if (gen->type != GEN_DIRNAME)
  1266. continue;
  1267. if (!X509_NAME_cmp(gen->d.directoryName, nm))
  1268. return 1;
  1269. }
  1270. return 0;
  1271. }
  1272. /* Check CRLDP and IDP */
  1273. static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
  1274. unsigned int *preasons)
  1275. {
  1276. size_t i;
  1277. if (crl->idp_flags & IDP_ONLYATTR)
  1278. return 0;
  1279. if (x->ex_flags & EXFLAG_CA) {
  1280. if (crl->idp_flags & IDP_ONLYUSER)
  1281. return 0;
  1282. } else {
  1283. if (crl->idp_flags & IDP_ONLYCA)
  1284. return 0;
  1285. }
  1286. *preasons = crl->idp_reasons;
  1287. for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
  1288. DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
  1289. if (crldp_check_crlissuer(dp, crl, crl_score)) {
  1290. if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
  1291. *preasons &= dp->dp_reasons;
  1292. return 1;
  1293. }
  1294. }
  1295. }
  1296. if ((!crl->idp || !crl->idp->distpoint)
  1297. && (crl_score & CRL_SCORE_ISSUER_NAME))
  1298. return 1;
  1299. return 0;
  1300. }
  1301. /*
  1302. * Retrieve CRL corresponding to current certificate. If deltas enabled try
  1303. * to find a delta CRL too
  1304. */
  1305. static int get_crl_delta(X509_STORE_CTX *ctx,
  1306. X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
  1307. {
  1308. int ok;
  1309. X509 *issuer = NULL;
  1310. int crl_score = 0;
  1311. unsigned int reasons;
  1312. X509_CRL *crl = NULL, *dcrl = NULL;
  1313. STACK_OF(X509_CRL) *skcrl;
  1314. X509_NAME *nm = X509_get_issuer_name(x);
  1315. reasons = ctx->current_reasons;
  1316. ok = get_crl_sk(ctx, &crl, &dcrl,
  1317. &issuer, &crl_score, &reasons, ctx->crls);
  1318. if (ok)
  1319. goto done;
  1320. /* Lookup CRLs from store */
  1321. skcrl = ctx->lookup_crls(ctx, nm);
  1322. /* If no CRLs found and a near match from get_crl_sk use that */
  1323. if (!skcrl && crl)
  1324. goto done;
  1325. get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
  1326. sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
  1327. done:
  1328. /* If we got any kind of CRL use it and return success */
  1329. if (crl) {
  1330. ctx->current_issuer = issuer;
  1331. ctx->current_crl_score = crl_score;
  1332. ctx->current_reasons = reasons;
  1333. *pcrl = crl;
  1334. *pdcrl = dcrl;
  1335. return 1;
  1336. }
  1337. return 0;
  1338. }
  1339. /* Check CRL validity */
  1340. static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
  1341. {
  1342. X509 *issuer = NULL;
  1343. EVP_PKEY *ikey = NULL;
  1344. int ok = 0, chnum, cnum;
  1345. cnum = ctx->error_depth;
  1346. chnum = sk_X509_num(ctx->chain) - 1;
  1347. /* if we have an alternative CRL issuer cert use that */
  1348. if (ctx->current_issuer)
  1349. issuer = ctx->current_issuer;
  1350. /*
  1351. * Else find CRL issuer: if not last certificate then issuer is next
  1352. * certificate in chain.
  1353. */
  1354. else if (cnum < chnum)
  1355. issuer = sk_X509_value(ctx->chain, cnum + 1);
  1356. else {
  1357. issuer = sk_X509_value(ctx->chain, chnum);
  1358. /* If not self signed, can't check signature */
  1359. if (!ctx->check_issued(ctx, issuer, issuer)) {
  1360. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
  1361. ok = ctx->verify_cb(0, ctx);
  1362. if (!ok)
  1363. goto err;
  1364. }
  1365. }
  1366. if (issuer) {
  1367. /*
  1368. * Skip most tests for deltas because they have already been done
  1369. */
  1370. if (!crl->base_crl_number) {
  1371. /* Check for cRLSign bit if keyUsage present */
  1372. if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
  1373. !(issuer->ex_kusage & KU_CRL_SIGN)) {
  1374. ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
  1375. ok = ctx->verify_cb(0, ctx);
  1376. if (!ok)
  1377. goto err;
  1378. }
  1379. if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
  1380. ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
  1381. ok = ctx->verify_cb(0, ctx);
  1382. if (!ok)
  1383. goto err;
  1384. }
  1385. if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
  1386. if (check_crl_path(ctx, ctx->current_issuer) <= 0) {
  1387. ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
  1388. ok = ctx->verify_cb(0, ctx);
  1389. if (!ok)
  1390. goto err;
  1391. }
  1392. }
  1393. if (crl->idp_flags & IDP_INVALID) {
  1394. ctx->error = X509_V_ERR_INVALID_EXTENSION;
  1395. ok = ctx->verify_cb(0, ctx);
  1396. if (!ok)
  1397. goto err;
  1398. }
  1399. }
  1400. if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
  1401. ok = check_crl_time(ctx, crl, 1);
  1402. if (!ok)
  1403. goto err;
  1404. }
  1405. /* Attempt to get issuer certificate public key */
  1406. ikey = X509_get_pubkey(issuer);
  1407. if (!ikey) {
  1408. ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  1409. ok = ctx->verify_cb(0, ctx);
  1410. if (!ok)
  1411. goto err;
  1412. } else {
  1413. int rv;
  1414. rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
  1415. if (rv != X509_V_OK) {
  1416. ctx->error = rv;
  1417. ok = ctx->verify_cb(0, ctx);
  1418. if (!ok)
  1419. goto err;
  1420. }
  1421. /* Verify CRL signature */
  1422. if (X509_CRL_verify(crl, ikey) <= 0) {
  1423. ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
  1424. ok = ctx->verify_cb(0, ctx);
  1425. if (!ok)
  1426. goto err;
  1427. }
  1428. }
  1429. }
  1430. ok = 1;
  1431. err:
  1432. EVP_PKEY_free(ikey);
  1433. return ok;
  1434. }
  1435. /* Check certificate against CRL */
  1436. static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
  1437. {
  1438. int ok;
  1439. X509_REVOKED *rev;
  1440. /*
  1441. * The rules changed for this... previously if a CRL contained unhandled
  1442. * critical extensions it could still be used to indicate a certificate
  1443. * was revoked. This has since been changed since critical extension can
  1444. * change the meaning of CRL entries.
  1445. */
  1446. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  1447. && (crl->flags & EXFLAG_CRITICAL)) {
  1448. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
  1449. ok = ctx->verify_cb(0, ctx);
  1450. if (!ok)
  1451. return 0;
  1452. }
  1453. /*
  1454. * Look for serial number of certificate in CRL If found make sure reason
  1455. * is not removeFromCRL.
  1456. */
  1457. if (X509_CRL_get0_by_cert(crl, &rev, x)) {
  1458. if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
  1459. return 2;
  1460. ctx->error = X509_V_ERR_CERT_REVOKED;
  1461. ok = ctx->verify_cb(0, ctx);
  1462. if (!ok)
  1463. return 0;
  1464. }
  1465. return 1;
  1466. }
  1467. static int check_policy(X509_STORE_CTX *ctx)
  1468. {
  1469. int ret;
  1470. if (ctx->parent)
  1471. return 1;
  1472. ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
  1473. ctx->param->policies, ctx->param->flags);
  1474. if (ret == 0) {
  1475. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  1476. ctx->error = X509_V_ERR_OUT_OF_MEM;
  1477. return 0;
  1478. }
  1479. /* Invalid or inconsistent extensions */
  1480. if (ret == -1) {
  1481. /*
  1482. * Locate certificates with bad extensions and notify callback.
  1483. */
  1484. X509 *x;
  1485. size_t i;
  1486. for (i = 1; i < sk_X509_num(ctx->chain); i++) {
  1487. x = sk_X509_value(ctx->chain, i);
  1488. if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
  1489. continue;
  1490. ctx->current_cert = x;
  1491. ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
  1492. if (!ctx->verify_cb(0, ctx))
  1493. return 0;
  1494. }
  1495. return 1;
  1496. }
  1497. if (ret == -2) {
  1498. ctx->current_cert = NULL;
  1499. ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
  1500. return ctx->verify_cb(0, ctx);
  1501. }
  1502. if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
  1503. ctx->current_cert = NULL;
  1504. /*
  1505. * Verification errors need to be "sticky", a callback may have allowed
  1506. * an SSL handshake to continue despite an error, and we must then
  1507. * remain in an error state. Therefore, we MUST NOT clear earlier
  1508. * verification errors by setting the error to X509_V_OK.
  1509. */
  1510. if (!ctx->verify_cb(2, ctx))
  1511. return 0;
  1512. }
  1513. return 1;
  1514. }
  1515. static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
  1516. {
  1517. time_t *ptime;
  1518. int i;
  1519. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  1520. ptime = &ctx->param->check_time;
  1521. else
  1522. ptime = NULL;
  1523. i = X509_cmp_time(X509_get_notBefore(x), ptime);
  1524. if (i == 0) {
  1525. ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
  1526. ctx->current_cert = x;
  1527. if (!ctx->verify_cb(0, ctx))
  1528. return 0;
  1529. }
  1530. if (i > 0) {
  1531. ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
  1532. ctx->current_cert = x;
  1533. if (!ctx->verify_cb(0, ctx))
  1534. return 0;
  1535. }
  1536. i = X509_cmp_time(X509_get_notAfter(x), ptime);
  1537. if (i == 0) {
  1538. ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
  1539. ctx->current_cert = x;
  1540. if (!ctx->verify_cb(0, ctx))
  1541. return 0;
  1542. }
  1543. if (i < 0) {
  1544. ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
  1545. ctx->current_cert = x;
  1546. if (!ctx->verify_cb(0, ctx))
  1547. return 0;
  1548. }
  1549. return 1;
  1550. }
  1551. static int internal_verify(X509_STORE_CTX *ctx)
  1552. {
  1553. int ok = 0, n;
  1554. X509 *xs, *xi;
  1555. EVP_PKEY *pkey = NULL;
  1556. int (*cb) (int xok, X509_STORE_CTX *xctx);
  1557. cb = ctx->verify_cb;
  1558. n = sk_X509_num(ctx->chain);
  1559. ctx->error_depth = n - 1;
  1560. n--;
  1561. xi = sk_X509_value(ctx->chain, n);
  1562. if (ctx->check_issued(ctx, xi, xi))
  1563. xs = xi;
  1564. else {
  1565. if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
  1566. xs = xi;
  1567. goto check_cert;
  1568. }
  1569. if (n <= 0) {
  1570. ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
  1571. ctx->current_cert = xi;
  1572. ok = cb(0, ctx);
  1573. goto end;
  1574. } else {
  1575. n--;
  1576. ctx->error_depth = n;
  1577. xs = sk_X509_value(ctx->chain, n);
  1578. }
  1579. }
  1580. /* ctx->error=0; not needed */
  1581. while (n >= 0) {
  1582. ctx->error_depth = n;
  1583. /*
  1584. * Skip signature check for self signed certificates unless
  1585. * explicitly asked for. It doesn't add any security and just wastes
  1586. * time.
  1587. */
  1588. if (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)) {
  1589. if ((pkey = X509_get_pubkey(xi)) == NULL) {
  1590. ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  1591. ctx->current_cert = xi;
  1592. ok = (*cb) (0, ctx);
  1593. if (!ok)
  1594. goto end;
  1595. } else if (X509_verify(xs, pkey) <= 0) {
  1596. ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
  1597. ctx->current_cert = xs;
  1598. ok = (*cb) (0, ctx);
  1599. if (!ok) {
  1600. EVP_PKEY_free(pkey);
  1601. goto end;
  1602. }
  1603. }
  1604. EVP_PKEY_free(pkey);
  1605. pkey = NULL;
  1606. }
  1607. check_cert:
  1608. ok = check_cert_time(ctx, xs);
  1609. if (!ok)
  1610. goto end;
  1611. /* The last error (if any) is still in the error value */
  1612. ctx->current_issuer = xi;
  1613. ctx->current_cert = xs;
  1614. ok = (*cb) (1, ctx);
  1615. if (!ok)
  1616. goto end;
  1617. n--;
  1618. if (n >= 0) {
  1619. xi = xs;
  1620. xs = sk_X509_value(ctx->chain, n);
  1621. }
  1622. }
  1623. ok = 1;
  1624. end:
  1625. return ok;
  1626. }
  1627. int X509_cmp_current_time(const ASN1_TIME *ctm)
  1628. {
  1629. return X509_cmp_time(ctm, NULL);
  1630. }
  1631. int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
  1632. {
  1633. static const size_t utctime_length = sizeof("YYMMDDHHMMSSZ") - 1;
  1634. static const size_t generalizedtime_length = sizeof("YYYYMMDDHHMMSSZ") - 1;
  1635. ASN1_TIME *asn1_cmp_time = NULL;
  1636. int i, day, sec, ret = 0;
  1637. /*
  1638. * Note that ASN.1 allows much more slack in the time format than RFC5280.
  1639. * In RFC5280, the representation is fixed:
  1640. * UTCTime: YYMMDDHHMMSSZ
  1641. * GeneralizedTime: YYYYMMDDHHMMSSZ
  1642. *
  1643. * We do NOT currently enforce the following RFC 5280 requirement:
  1644. * "CAs conforming to this profile MUST always encode certificate
  1645. * validity dates through the year 2049 as UTCTime; certificate validity
  1646. * dates in 2050 or later MUST be encoded as GeneralizedTime."
  1647. */
  1648. switch (ctm->type) {
  1649. case V_ASN1_UTCTIME:
  1650. if (ctm->length != (int)(utctime_length))
  1651. return 0;
  1652. break;
  1653. case V_ASN1_GENERALIZEDTIME:
  1654. if (ctm->length != (int)(generalizedtime_length))
  1655. return 0;
  1656. break;
  1657. default:
  1658. return 0;
  1659. }
  1660. /**
  1661. * Verify the format: the ASN.1 functions we use below allow a more
  1662. * flexible format than what's mandated by RFC 5280.
  1663. * Digit and date ranges will be verified in the conversion methods.
  1664. */
  1665. for (i = 0; i < ctm->length - 1; i++) {
  1666. if (!isdigit(ctm->data[i]))
  1667. return 0;
  1668. }
  1669. if (ctm->data[ctm->length - 1] != 'Z')
  1670. return 0;
  1671. /*
  1672. * There is ASN1_UTCTIME_cmp_time_t but no
  1673. * ASN1_GENERALIZEDTIME_cmp_time_t or ASN1_TIME_cmp_time_t,
  1674. * so we go through ASN.1
  1675. */
  1676. asn1_cmp_time = X509_time_adj(NULL, 0, cmp_time);
  1677. if (asn1_cmp_time == NULL)
  1678. goto err;
  1679. if (!ASN1_TIME_diff(&day, &sec, ctm, asn1_cmp_time))
  1680. goto err;
  1681. /*
  1682. * X509_cmp_time comparison is <=.
  1683. * The return value 0 is reserved for errors.
  1684. */
  1685. ret = (day >= 0 && sec >= 0) ? -1 : 1;
  1686. err:
  1687. ASN1_TIME_free(asn1_cmp_time);
  1688. return ret;
  1689. }
  1690. ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
  1691. {
  1692. return X509_time_adj(s, adj, NULL);
  1693. }
  1694. ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
  1695. {
  1696. return X509_time_adj_ex(s, 0, offset_sec, in_tm);
  1697. }
  1698. ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
  1699. int offset_day, long offset_sec, time_t *in_tm)
  1700. {
  1701. time_t t = 0;
  1702. if (in_tm)
  1703. t = *in_tm;
  1704. else
  1705. time(&t);
  1706. if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
  1707. if (s->type == V_ASN1_UTCTIME)
  1708. return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
  1709. if (s->type == V_ASN1_GENERALIZEDTIME)
  1710. return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
  1711. }
  1712. return ASN1_TIME_adj(s, t, offset_day, offset_sec);
  1713. }
  1714. /* Make a delta CRL as the diff between two full CRLs */
  1715. X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
  1716. EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)
  1717. {
  1718. X509_CRL *crl = NULL;
  1719. int i;
  1720. size_t j;
  1721. STACK_OF(X509_REVOKED) *revs = NULL;
  1722. /* CRLs can't be delta already */
  1723. if (base->base_crl_number || newer->base_crl_number) {
  1724. OPENSSL_PUT_ERROR(X509, X509_R_CRL_ALREADY_DELTA);
  1725. return NULL;
  1726. }
  1727. /* Base and new CRL must have a CRL number */
  1728. if (!base->crl_number || !newer->crl_number) {
  1729. OPENSSL_PUT_ERROR(X509, X509_R_NO_CRL_NUMBER);
  1730. return NULL;
  1731. }
  1732. /* Issuer names must match */
  1733. if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) {
  1734. OPENSSL_PUT_ERROR(X509, X509_R_ISSUER_MISMATCH);
  1735. return NULL;
  1736. }
  1737. /* AKID and IDP must match */
  1738. if (!crl_extension_match(base, newer, NID_authority_key_identifier)) {
  1739. OPENSSL_PUT_ERROR(X509, X509_R_AKID_MISMATCH);
  1740. return NULL;
  1741. }
  1742. if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) {
  1743. OPENSSL_PUT_ERROR(X509, X509_R_IDP_MISMATCH);
  1744. return NULL;
  1745. }
  1746. /* Newer CRL number must exceed full CRL number */
  1747. if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) {
  1748. OPENSSL_PUT_ERROR(X509, X509_R_NEWER_CRL_NOT_NEWER);
  1749. return NULL;
  1750. }
  1751. /* CRLs must verify */
  1752. if (skey && (X509_CRL_verify(base, skey) <= 0 ||
  1753. X509_CRL_verify(newer, skey) <= 0)) {
  1754. OPENSSL_PUT_ERROR(X509, X509_R_CRL_VERIFY_FAILURE);
  1755. return NULL;
  1756. }
  1757. /* Create new CRL */
  1758. crl = X509_CRL_new();
  1759. if (!crl || !X509_CRL_set_version(crl, 1))
  1760. goto memerr;
  1761. /* Set issuer name */
  1762. if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer)))
  1763. goto memerr;
  1764. if (!X509_CRL_set_lastUpdate(crl, X509_CRL_get_lastUpdate(newer)))
  1765. goto memerr;
  1766. if (!X509_CRL_set_nextUpdate(crl, X509_CRL_get_nextUpdate(newer)))
  1767. goto memerr;
  1768. /* Set base CRL number: must be critical */
  1769. if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0))
  1770. goto memerr;
  1771. /*
  1772. * Copy extensions across from newest CRL to delta: this will set CRL
  1773. * number to correct value too.
  1774. */
  1775. for (i = 0; i < X509_CRL_get_ext_count(newer); i++) {
  1776. X509_EXTENSION *ext;
  1777. ext = X509_CRL_get_ext(newer, i);
  1778. if (!X509_CRL_add_ext(crl, ext, -1))
  1779. goto memerr;
  1780. }
  1781. /* Go through revoked entries, copying as needed */
  1782. revs = X509_CRL_get_REVOKED(newer);
  1783. for (j = 0; j < sk_X509_REVOKED_num(revs); j++) {
  1784. X509_REVOKED *rvn, *rvtmp;
  1785. rvn = sk_X509_REVOKED_value(revs, j);
  1786. /*
  1787. * Add only if not also in base. TODO: need something cleverer here
  1788. * for some more complex CRLs covering multiple CAs.
  1789. */
  1790. if (!X509_CRL_get0_by_serial(base, &rvtmp, rvn->serialNumber)) {
  1791. rvtmp = X509_REVOKED_dup(rvn);
  1792. if (!rvtmp)
  1793. goto memerr;
  1794. if (!X509_CRL_add0_revoked(crl, rvtmp)) {
  1795. X509_REVOKED_free(rvtmp);
  1796. goto memerr;
  1797. }
  1798. }
  1799. }
  1800. /* TODO: optionally prune deleted entries */
  1801. if (skey && md && !X509_CRL_sign(crl, skey, md))
  1802. goto memerr;
  1803. return crl;
  1804. memerr:
  1805. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  1806. if (crl)
  1807. X509_CRL_free(crl);
  1808. return NULL;
  1809. }
  1810. int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
  1811. CRYPTO_EX_unused * unused,
  1812. CRYPTO_EX_dup *dup_unused,
  1813. CRYPTO_EX_free *free_func)
  1814. {
  1815. /*
  1816. * This function is (usually) called only once, by
  1817. * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
  1818. */
  1819. int index;
  1820. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
  1821. free_func)) {
  1822. return -1;
  1823. }
  1824. return index;
  1825. }
  1826. int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
  1827. {
  1828. return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  1829. }
  1830. void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
  1831. {
  1832. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1833. }
  1834. int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
  1835. {
  1836. return ctx->error;
  1837. }
  1838. void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
  1839. {
  1840. ctx->error = err;
  1841. }
  1842. int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
  1843. {
  1844. return ctx->error_depth;
  1845. }
  1846. X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
  1847. {
  1848. return ctx->current_cert;
  1849. }
  1850. STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
  1851. {
  1852. return ctx->chain;
  1853. }
  1854. STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
  1855. {
  1856. if (!ctx->chain)
  1857. return NULL;
  1858. return X509_chain_up_ref(ctx->chain);
  1859. }
  1860. X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
  1861. {
  1862. return ctx->current_issuer;
  1863. }
  1864. X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
  1865. {
  1866. return ctx->current_crl;
  1867. }
  1868. X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
  1869. {
  1870. return ctx->parent;
  1871. }
  1872. void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
  1873. {
  1874. ctx->cert = x;
  1875. }
  1876. void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1877. {
  1878. ctx->untrusted = sk;
  1879. }
  1880. STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(X509_STORE_CTX *ctx)
  1881. {
  1882. return ctx->untrusted;
  1883. }
  1884. void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
  1885. {
  1886. ctx->crls = sk;
  1887. }
  1888. int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
  1889. {
  1890. return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
  1891. }
  1892. int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
  1893. {
  1894. return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
  1895. }
  1896. /*
  1897. * This function is used to set the X509_STORE_CTX purpose and trust values.
  1898. * This is intended to be used when another structure has its own trust and
  1899. * purpose values which (if set) will be inherited by the ctx. If they aren't
  1900. * set then we will usually have a default purpose in mind which should then
  1901. * be used to set the trust value. An example of this is SSL use: an SSL
  1902. * structure will have its own purpose and trust settings which the
  1903. * application can set: if they aren't set then we use the default of SSL
  1904. * client/server.
  1905. */
  1906. int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
  1907. int purpose, int trust)
  1908. {
  1909. int idx;
  1910. /* If purpose not set use default */
  1911. if (!purpose)
  1912. purpose = def_purpose;
  1913. /* If we have a purpose then check it is valid */
  1914. if (purpose) {
  1915. X509_PURPOSE *ptmp;
  1916. idx = X509_PURPOSE_get_by_id(purpose);
  1917. if (idx == -1) {
  1918. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
  1919. return 0;
  1920. }
  1921. ptmp = X509_PURPOSE_get0(idx);
  1922. if (ptmp->trust == X509_TRUST_DEFAULT) {
  1923. idx = X509_PURPOSE_get_by_id(def_purpose);
  1924. if (idx == -1) {
  1925. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
  1926. return 0;
  1927. }
  1928. ptmp = X509_PURPOSE_get0(idx);
  1929. }
  1930. /* If trust not set then get from purpose default */
  1931. if (!trust)
  1932. trust = ptmp->trust;
  1933. }
  1934. if (trust) {
  1935. idx = X509_TRUST_get_by_id(trust);
  1936. if (idx == -1) {
  1937. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_TRUST_ID);
  1938. return 0;
  1939. }
  1940. }
  1941. if (purpose && !ctx->param->purpose)
  1942. ctx->param->purpose = purpose;
  1943. if (trust && !ctx->param->trust)
  1944. ctx->param->trust = trust;
  1945. return 1;
  1946. }
  1947. X509_STORE_CTX *X509_STORE_CTX_new(void)
  1948. {
  1949. X509_STORE_CTX *ctx;
  1950. ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
  1951. if (!ctx) {
  1952. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  1953. return NULL;
  1954. }
  1955. X509_STORE_CTX_zero(ctx);
  1956. return ctx;
  1957. }
  1958. void X509_STORE_CTX_zero(X509_STORE_CTX *ctx)
  1959. {
  1960. OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX));
  1961. }
  1962. void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
  1963. {
  1964. if (ctx == NULL) {
  1965. return;
  1966. }
  1967. X509_STORE_CTX_cleanup(ctx);
  1968. OPENSSL_free(ctx);
  1969. }
  1970. int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
  1971. STACK_OF(X509) *chain)
  1972. {
  1973. int ret = 1;
  1974. X509_STORE_CTX_zero(ctx);
  1975. ctx->ctx = store;
  1976. ctx->cert = x509;
  1977. ctx->untrusted = chain;
  1978. CRYPTO_new_ex_data(&ctx->ex_data);
  1979. ctx->param = X509_VERIFY_PARAM_new();
  1980. if (!ctx->param)
  1981. goto err;
  1982. /*
  1983. * Inherit callbacks and flags from X509_STORE if not set use defaults.
  1984. */
  1985. if (store)
  1986. ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
  1987. else
  1988. ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
  1989. if (store) {
  1990. ctx->verify_cb = store->verify_cb;
  1991. ctx->cleanup = store->cleanup;
  1992. } else
  1993. ctx->cleanup = 0;
  1994. if (ret)
  1995. ret = X509_VERIFY_PARAM_inherit(ctx->param,
  1996. X509_VERIFY_PARAM_lookup("default"));
  1997. if (ret == 0)
  1998. goto err;
  1999. if (store && store->check_issued)
  2000. ctx->check_issued = store->check_issued;
  2001. else
  2002. ctx->check_issued = check_issued;
  2003. if (store && store->get_issuer)
  2004. ctx->get_issuer = store->get_issuer;
  2005. else
  2006. ctx->get_issuer = X509_STORE_CTX_get1_issuer;
  2007. if (store && store->verify_cb)
  2008. ctx->verify_cb = store->verify_cb;
  2009. else
  2010. ctx->verify_cb = null_callback;
  2011. if (store && store->verify)
  2012. ctx->verify = store->verify;
  2013. else
  2014. ctx->verify = internal_verify;
  2015. if (store && store->check_revocation)
  2016. ctx->check_revocation = store->check_revocation;
  2017. else
  2018. ctx->check_revocation = check_revocation;
  2019. if (store && store->get_crl)
  2020. ctx->get_crl = store->get_crl;
  2021. else
  2022. ctx->get_crl = NULL;
  2023. if (store && store->check_crl)
  2024. ctx->check_crl = store->check_crl;
  2025. else
  2026. ctx->check_crl = check_crl;
  2027. if (store && store->cert_crl)
  2028. ctx->cert_crl = store->cert_crl;
  2029. else
  2030. ctx->cert_crl = cert_crl;
  2031. if (store && store->lookup_certs)
  2032. ctx->lookup_certs = store->lookup_certs;
  2033. else
  2034. ctx->lookup_certs = X509_STORE_get1_certs;
  2035. if (store && store->lookup_crls)
  2036. ctx->lookup_crls = store->lookup_crls;
  2037. else
  2038. ctx->lookup_crls = X509_STORE_get1_crls;
  2039. ctx->check_policy = check_policy;
  2040. return 1;
  2041. err:
  2042. CRYPTO_free_ex_data(&g_ex_data_class, ctx, &ctx->ex_data);
  2043. if (ctx->param != NULL) {
  2044. X509_VERIFY_PARAM_free(ctx->param);
  2045. }
  2046. OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX));
  2047. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  2048. return 0;
  2049. }
  2050. /*
  2051. * Set alternative lookup method: just a STACK of trusted certificates. This
  2052. * avoids X509_STORE nastiness where it isn't needed.
  2053. */
  2054. void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  2055. {
  2056. ctx->other_ctx = sk;
  2057. ctx->get_issuer = get_issuer_sk;
  2058. }
  2059. void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
  2060. {
  2061. /* We need to be idempotent because, unfortunately, |X509_STORE_CTX_free|
  2062. * also calls this function. */
  2063. if (ctx->cleanup != NULL) {
  2064. ctx->cleanup(ctx);
  2065. ctx->cleanup = NULL;
  2066. }
  2067. if (ctx->param != NULL) {
  2068. if (ctx->parent == NULL)
  2069. X509_VERIFY_PARAM_free(ctx->param);
  2070. ctx->param = NULL;
  2071. }
  2072. if (ctx->tree != NULL) {
  2073. X509_policy_tree_free(ctx->tree);
  2074. ctx->tree = NULL;
  2075. }
  2076. if (ctx->chain != NULL) {
  2077. sk_X509_pop_free(ctx->chain, X509_free);
  2078. ctx->chain = NULL;
  2079. }
  2080. CRYPTO_free_ex_data(&g_ex_data_class, ctx, &(ctx->ex_data));
  2081. OPENSSL_memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
  2082. }
  2083. void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
  2084. {
  2085. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  2086. }
  2087. void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
  2088. {
  2089. X509_VERIFY_PARAM_set_flags(ctx->param, flags);
  2090. }
  2091. void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
  2092. time_t t)
  2093. {
  2094. X509_VERIFY_PARAM_set_time(ctx->param, t);
  2095. }
  2096. X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx)
  2097. {
  2098. return ctx->cert;
  2099. }
  2100. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
  2101. int (*verify_cb) (int, X509_STORE_CTX *))
  2102. {
  2103. ctx->verify_cb = verify_cb;
  2104. }
  2105. X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
  2106. {
  2107. return ctx->tree;
  2108. }
  2109. int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
  2110. {
  2111. return ctx->explicit_policy;
  2112. }
  2113. int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
  2114. {
  2115. const X509_VERIFY_PARAM *param;
  2116. param = X509_VERIFY_PARAM_lookup(name);
  2117. if (!param)
  2118. return 0;
  2119. return X509_VERIFY_PARAM_inherit(ctx->param, param);
  2120. }
  2121. X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
  2122. {
  2123. return ctx->param;
  2124. }
  2125. void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
  2126. {
  2127. if (ctx->param)
  2128. X509_VERIFY_PARAM_free(ctx->param);
  2129. ctx->param = param;
  2130. }
  2131. IMPLEMENT_ASN1_SET_OF(X509)
  2132. IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)