xrootd
XrdCryptoX509Chain.hh
Go to the documentation of this file.
00001 #ifndef __CRYPTO_X509CHAIN_H__
00002 #define __CRYPTO_X509CHAIN_H__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                   X r d C r y p t o X 5 0 9 C h a i n . h h                */
00006 /*                                                                            */
00007 /* (c) 2005 G. Ganis , CERN                                                   */
00008 /*                                                                            */
00009 /* This file is part of the XRootD software suite.                            */
00010 /*                                                                            */
00011 /* XRootD is free software: you can redistribute it and/or modify it under    */
00012 /* the terms of the GNU Lesser General Public License as published by the     */
00013 /* Free Software Foundation, either version 3 of the License, or (at your     */
00014 /* option) any later version.                                                 */
00015 /*                                                                            */
00016 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00017 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00018 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00019 /* License for more details.                                                  */
00020 /*                                                                            */
00021 /* You should have received a copy of the GNU Lesser General Public License   */
00022 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00023 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00024 /*                                                                            */
00025 /* The copyright holder's institutional names and contributor's names may not */
00026 /* be used to endorse or promote products derived from this software without  */
00027 /* specific prior written permission of the institution or contributor.       */
00028 /*                                                                            */
00029 /******************************************************************************/
00030 
00031 /* ************************************************************************** */
00032 /*                                                                            */
00033 /* Chain of X509 certificates.                                                */
00034 /*                                                                            */
00035 /* ************************************************************************** */
00036 
00037 #include "XrdSut/XrdSutBucket.hh"
00038 #include "XrdCrypto/XrdCryptoX509.hh"
00039 #include "XrdCrypto/XrdCryptoX509Crl.hh"
00040 
00041 // ---------------------------------------------------------------------------//
00042 //                                                                            //
00043 // XrdCryptoX509Chain                                                         //
00044 //                                                                            //
00045 // Light single-linked list for managing stacks of XrdCryptoX509* objects     //
00046 //                                                                            //
00047 // ---------------------------------------------------------------------------//
00048 
00049 //
00050 // Description of options for verify
00051 typedef struct {
00052    int  opt;            // option container
00053    int  when;           // time of verification (UTC)
00054    int  pathlen;        // max allowed path length of chain
00055    XrdCryptoX509Crl *crl; // CRL
00056 } x509ChainVerifyOpt_t;
00057 
00058 const int kOptsCheckSelfSigned = 0x2;    // CA ckecking option
00059 
00060 //
00061 // Node definition
00062 //
00063 class XrdCryptoX509ChainNode {
00064 
00065 private:
00066    XrdCryptoX509          *cert;
00067    XrdCryptoX509ChainNode *next;
00068 public:
00069    XrdCryptoX509ChainNode(XrdCryptoX509 *c = 0, XrdCryptoX509ChainNode *n = 0)
00070         { cert = c; next = n;}
00071    virtual ~XrdCryptoX509ChainNode() { }
00072 
00073    XrdCryptoX509          *Cert() const { return cert; }
00074    XrdCryptoX509ChainNode *Next() const { return next; }
00075 
00076    void SetNext(XrdCryptoX509ChainNode *n) { next = n; }
00077 };
00078 
00079 class XrdCryptoX509Chain {
00080 
00081    friend class XrdCryptosslgsiX509Chain;
00082 
00083    enum ESearchMode { kExact = 0, kBegin = 1, kEnd = 2 };
00084 
00085 public:
00086    XrdCryptoX509Chain(XrdCryptoX509 *c = 0);
00087    XrdCryptoX509Chain(XrdCryptoX509Chain *ch);
00088    virtual ~XrdCryptoX509Chain();
00089 
00090    // CA status
00091    enum ECAStatus { kUnknown = 0, kAbsent, kInvalid, kValid};
00092 
00093    // Error codes
00094    enum EX509ChainErr { kNone = 0, kInconsistent, kTooMany, kNoCA,
00095                         kNoCertificate, kInvalidType, kInvalidNames,
00096                         kRevoked, kExpired, kMissingExtension,
00097                         kVerifyFail, kInvalidSign, kCANotAutoSigned };
00098 
00099    // In case or error
00100    const char         *X509ChainError(EX509ChainErr e);
00101    const char         *LastError() const { return lastError.c_str(); }
00102 
00103    // Dump content
00104    void Dump();
00105 
00106    // Access information
00107    int                 Size() const { return size; }
00108    XrdCryptoX509      *End() const { return end->Cert(); }
00109    ECAStatus           StatusCA() const { return statusCA; }
00110    const char         *CAname();
00111    const char         *EECname();
00112    const char         *CAhash();
00113    const char         *EEChash();
00114 
00115    // Modifiers
00116    void                InsertAfter(XrdCryptoX509 *c, XrdCryptoX509 *cp);
00117    void                PutInFront(XrdCryptoX509 *c);
00118    void                PushBack(XrdCryptoX509 *c);
00119    void                Remove(XrdCryptoX509 *c);
00120    bool                CheckCA(bool checkselfsigned = 1);
00121    void                Cleanup(bool keepCA = 0);
00122    void                SetStatusCA(ECAStatus st) { statusCA = st; }
00123 
00124    // Search
00125    XrdCryptoX509      *SearchByIssuer(const char *issuer,
00126                                       ESearchMode mode = kExact);
00127    XrdCryptoX509      *SearchBySubject(const char *subject,
00128                                        ESearchMode mode = kExact);
00129 
00130    // Check validity in time
00131    virtual int         CheckValidity(bool outatfirst = 1, int when = 0);
00132 
00133    // Reorder (C(n) issuer of C(n+1)) 
00134    virtual int         Reorder();
00135 
00136    // Verify chain
00137    virtual bool        Verify(EX509ChainErr &e, x509ChainVerifyOpt_t *vopt = 0);
00138 
00139    // Pseudo - iterator functionality
00140    XrdCryptoX509       *Begin();
00141    XrdCryptoX509       *Next();
00142 
00143 private:
00144 
00145 
00146    XrdCryptoX509ChainNode *begin;
00147    XrdCryptoX509ChainNode *current;
00148    XrdCryptoX509ChainNode *end;
00149    XrdCryptoX509ChainNode *previous;
00150    int                     size;
00151    XrdOucString            lastError;
00152    XrdOucString            caname;
00153    XrdOucString            eecname;
00154    XrdOucString            cahash;
00155    XrdOucString            eechash;
00156    ECAStatus               statusCA;
00157 
00158    XrdCryptoX509ChainNode *Find(XrdCryptoX509 *c);
00159    XrdCryptoX509ChainNode *FindIssuer(const char *issuer,
00160                                       ESearchMode mode = kExact,
00161                                       XrdCryptoX509ChainNode **p = 0);
00162    XrdCryptoX509ChainNode *FindSubject(const char *subject,
00163                                        ESearchMode mode = kExact,
00164                                        XrdCryptoX509ChainNode **p = 0);
00165    bool Verify(EX509ChainErr &e, const char *msg,
00166                XrdCryptoX509::EX509Type type, int when,
00167                XrdCryptoX509 *xcer, XrdCryptoX509 *xsig,
00168                XrdCryptoX509Crl *crl = 0);
00169 
00170 };
00171 
00172 #endif