home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / ksslcertificate.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  9.5 KB  |  377 lines

  1. /* This file is part of the KDE project
  2.  *
  3.  * Copyright (C) 2000-2003 George Staikos <staikos@kde.org>
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Library General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * Library General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Library General Public License
  16.  * along with this library; see the file COPYING.LIB.  If not, write to
  17.  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18.  * Boston, MA 02110-1301, USA.
  19.  */
  20.  
  21. #ifndef _KSSLCERTIFICATE_H
  22. #define _KSSLCERTIFICATE_H
  23.  
  24.  
  25. // UPDATE: I like the structure of this class less and less every time I look
  26. //         at it.  I think it needs to change.
  27. //
  28. //
  29. //  The biggest reason for making everything protected here is so that
  30. //  the class can have all it's methods available even if openssl is not
  31. //  available.  Also, to create a new certificate you should use the
  32. //  KSSLCertificateFactory, and to manage the user's database of certificates,
  33. //  you should go through the KSSLCertificateHome.
  34. //
  35. //  There should be no reason to touch the X509 stuff directly.
  36. //
  37.  
  38. #include <qcstring.h>
  39. #include <qvaluelist.h>
  40.  
  41. class QString;
  42. class QStringList;
  43. class QCString;
  44. class KSSL;
  45. class KSSLCertificatePrivate;
  46. class QDateTime;
  47. class KSSLCertChain;
  48. class KSSLX509V3;
  49.  
  50. #include <kdelibs_export.h>
  51.  
  52. #ifdef Q_WS_WIN
  53. #include "ksslconfig_win.h"
  54. #else
  55. #include "ksslconfig.h"
  56. #endif
  57.  
  58. #ifdef KSSL_HAVE_SSL
  59. typedef struct x509_st X509;
  60. #else
  61. class X509;
  62. #endif
  63.  
  64. /**
  65.  * KDE X.509 Certificate
  66.  *
  67.  * This class represents an X.509 (SSL) certificate.
  68.  * Note: this object is VERY HEAVY TO COPY.  Please try to use reference
  69.  *       or pointer whenever possible
  70.  *
  71.  * @author George Staikos <staikos@kde.org>
  72.  * @see KSSL
  73.  * @short KDE X.509 Certificate
  74.  */
  75. class KIO_EXPORT KSSLCertificate {
  76. friend class KSSL;
  77. friend class KSSLCertificateHome;
  78. friend class KSSLCertificateFactory;
  79. friend class KSSLCertificateCache;
  80. friend class KSSLCertChain;
  81. friend class KSSLPeerInfo;
  82. friend class KSSLPKCS12;
  83. friend class KSSLD;
  84. friend class KSMIMECryptoPrivate;
  85.  
  86.  
  87. public:
  88.     /**
  89.      *  Destroy this X.509 certificate.
  90.      */
  91.     ~KSSLCertificate();
  92.  
  93.     /**
  94.      *  Create an X.509 certificate from a base64 encoded string.
  95.      *  @param cert the certificate in base64 form
  96.      *  @return the X.509 certificate, or NULL
  97.      */
  98.     static KSSLCertificate *fromString(QCString cert);
  99.  
  100.     /**
  101.      *  Create an X.509 certificate from the internal representation.
  102.      *  This one duplicates the X509 object for itself.
  103.      *  @param x5 the OpenSSL representation of the certificate
  104.      *  @return the X.509 certificate, or NULL
  105.      *  @internal
  106.      */
  107.     static KSSLCertificate *fromX509(X509 *x5);
  108.  
  109.         /**
  110.          * A CA certificate can be validated as Irrelevant when it was
  111.          * not used to sign any other relevant certificate.
  112.          */
  113.     enum KSSLValidation {   Unknown, Ok, NoCARoot, InvalidPurpose,
  114.                 PathLengthExceeded, InvalidCA, Expired,
  115.                 SelfSigned, ErrorReadingRoot, NoSSL,
  116.                 Revoked, Untrusted, SignatureFailed,
  117.                 Rejected, PrivateKeyFailed, InvalidHost,
  118.                 Irrelevant, SelfSignedChain
  119.                 };
  120.  
  121.     enum KSSLPurpose {      None=0, SSLServer=1, SSLClient=2,
  122.                 SMIMESign=3, SMIMEEncrypt=4, Any=5 };
  123.  
  124.         typedef QValueList<KSSLValidation> KSSLValidationList;
  125.  
  126.     /**
  127.      *  Convert this certificate to a string.
  128.      *  @return the certificate in base64 format
  129.      */
  130.     QString toString();
  131.  
  132.     /**
  133.      *  Get the subject of the certificate (X.509 map).
  134.      *  @return the subject
  135.      */
  136.     QString getSubject() const;
  137.  
  138.     /**
  139.      *  Get the issuer of the certificate (X.509 map).
  140.      *  @return the issuer
  141.      */
  142.     QString getIssuer() const;
  143.  
  144.     /**
  145.      *  Get the date that the certificate becomes valid on.
  146.      *  @return the date as a string, localised
  147.      */
  148.     QString getNotBefore() const;
  149.  
  150.     /**
  151.      *  Get the date that the certificate is valid until.
  152.      *  @return the date as a string, localised
  153.      */
  154.     QString getNotAfter() const;
  155.  
  156.     /**
  157.      *  Get the date that the certificate becomes valid on.
  158.      *  @return the date
  159.      */
  160.     QDateTime getQDTNotBefore() const;
  161.  
  162.     /**
  163.      *  Get the date that the certificate is valid until.
  164.      *  @return the date
  165.      */
  166.     QDateTime getQDTNotAfter() const;
  167.  
  168.     /**
  169.      *  Convert the certificate to DER (ASN.1) format.
  170.      *  @return the binary data of the DER encoding
  171.      */
  172.     QByteArray toDer();
  173.  
  174.     /**
  175.      *  Convert the certificate to PEM (base64) format.
  176.      *  @return the binary data of the PEM encoding
  177.      */
  178.     QByteArray toPem();
  179.  
  180.     /**
  181.      *  Convert the certificate to Netscape format.
  182.      *  @return the binary data of the Netscape encoding
  183.      */
  184.     QByteArray toNetscape();
  185.  
  186.     /**
  187.      *  Convert the certificate to OpenSSL plain text format.
  188.      *  @return the OpenSSL text encoding
  189.      */
  190.     QString toText();
  191.  
  192.     /**
  193.      *  Get the serial number of the certificate.
  194.      *  @return the serial number as a string
  195.      */
  196.     QString getSerialNumber() const;
  197.  
  198.     /**
  199.      *  Get the key type (RSA, DSA, etc).
  200.      *  @return the key type as a string
  201.      */
  202.     QString getKeyType() const;
  203.  
  204.     /**
  205.      *  Get the public key.
  206.      *  @return the public key as a hexidecimal string
  207.      */
  208.     QString getPublicKeyText() const;
  209.  
  210.     /**
  211.      *  Get the MD5 digest of the certificate.
  212.      *  Result is padded with : to separate bytes - it's a text version!
  213.      *  @return the MD5 digest in a hexidecimal string
  214.      */
  215.     QString getMD5DigestText() const;
  216.  
  217.     /**
  218.      *  Get the MD5 digest of the certificate.
  219.      *  @return the MD5 digest in a hexidecimal string
  220.      */
  221.     QString getMD5Digest() const;
  222.  
  223.     /**
  224.      *  Get the signature.
  225.      *  @return the signature in text format
  226.      */
  227.     QString getSignatureText() const;
  228.  
  229.     /**
  230.      *  Check if this is a valid certificate.  Will use cached data.
  231.      *  @return true if it is valid
  232.      */
  233.     bool isValid();
  234.  
  235.     /**
  236.      *  Check if this is a valid certificate.  Will use cached data.
  237.      *  @param p the purpose to validate for
  238.      *  @return true if it is valid
  239.      */
  240.     bool isValid(KSSLPurpose p);
  241.  
  242.     /**
  243.      *  The alternate subject name.
  244.      *  @return string list with subjectAltName
  245.      */
  246.     QStringList subjAltNames() const;
  247.  
  248.     /**
  249.      *  Check if this is a valid certificate.  Will use cached data.
  250.      *  @return the result of the validation
  251.      */
  252.     KSSLValidation validate();
  253.  
  254.     /**
  255.      *  Check if this is a valid certificate.  Will use cached data.
  256.      *  @param p the purpose to validate for
  257.      *  @return the result of the validation
  258.      */
  259.     KSSLValidation validate(KSSLPurpose p);
  260.  
  261.     /**
  262.      *  Check if this is a valid certificate.  Will use cached data.
  263.      *  @param p the purpose to validate for
  264.      *  @return all problems encountered during validation
  265.      */
  266.     KSSLValidationList validateVerbose(KSSLPurpose p);
  267.  
  268.     /**
  269.      *  Check if the certificate ca is a proper CA for this
  270.      *  certificate.
  271.      *  @param p the purpose to validate for
  272.      *  @param ca the certificate to check
  273.      *  @return all problems encountered during validation
  274.      */
  275.     KSSLValidationList validateVerbose(KSSLPurpose p, KSSLCertificate *ca);
  276.  
  277.     /**
  278.      *  Check if this is a valid certificate.  Will NOT use cached data.
  279.      *  @return the result of the validation
  280.      */
  281.     KSSLValidation revalidate();
  282.  
  283.     /**
  284.      *  Check if this is a valid certificate.  Will NOT use cached data.
  285.      *  @param p the purpose to validate for
  286.      *  @return the result of the validation
  287.      */
  288.     KSSLValidation revalidate(KSSLPurpose p);
  289.  
  290.     /**
  291.      *  Get a reference to the certificate chain.
  292.      *  @return reference to the chain
  293.      */
  294.     KSSLCertChain& chain();
  295.  
  296.     /**
  297.      *  Obtain the localized message that corresponds to a validation result.
  298.      *  @param x the code to look up
  299.      *  @return the message text corresponding to the validation code
  300.      */
  301.     static QString verifyText(KSSLValidation x);
  302.  
  303.     /**
  304.      *  Explicitly make a copy of this certificate.
  305.      *  @return a copy of the certificate
  306.      */
  307.     KSSLCertificate *replicate();
  308.  
  309.     /**
  310.      *  Copy constructor.  Beware, this is very expensive.
  311.      *  @param x the object to copy from
  312.      */
  313.     KSSLCertificate(const KSSLCertificate& x); // copy constructor
  314.  
  315.     /**
  316.      *  Re-set the certificate from a base64 string.
  317.      *  @param cert the certificate to set to
  318.      *  @return true on success
  319.      */
  320.     bool setCert(QString& cert);
  321.  
  322.     /**
  323.      *  Access the X.509v3 parameters.
  324.      *  @return reference to the extension object
  325.      *  @see KSSLX509V3
  326.      */
  327.     KSSLX509V3& x509V3Extensions();
  328.  
  329.     /**
  330.      *  Check if this is a signer certificate.
  331.      *  @return true if this is a signer certificate
  332.      */
  333.     bool isSigner();
  334.  
  335.     /**
  336.      *  FIXME: document
  337.      */
  338.     void getEmails(QStringList& to) const;
  339.  
  340.     /**
  341.      * KDEKey is a concatenation "Subject (MD5)", mostly needed for SMIME.
  342.      * The result of getKDEKey might change and should not be used for
  343.      * persistant storage.
  344.      */
  345.     QString getKDEKey() const;
  346.  
  347.     /**
  348.      * Aegypten semantics force us to search by MD5Digest only.
  349.      */
  350.     static QString getMD5DigestFromKDEKey(const QString& k);
  351.  
  352. private:
  353.     KIO_EXPORT friend int operator!=(KSSLCertificate& x, KSSLCertificate& y);
  354.     KIO_EXPORT friend int operator==(KSSLCertificate& x, KSSLCertificate& y);
  355.  
  356.     KSSLCertificatePrivate *d;
  357.     int purposeToOpenSSL(KSSLPurpose p) const;
  358.  
  359. protected:
  360.     KSSLCertificate();
  361.  
  362.     void setCert(X509 *c);
  363.     void setChain(void *c);
  364.     X509 *getCert();
  365.     KSSLValidation processError(int ec);
  366. };
  367.  
  368. KIO_EXPORT QDataStream& operator<<(QDataStream& s, const KSSLCertificate& r);
  369. KIO_EXPORT QDataStream& operator>>(QDataStream& s, KSSLCertificate& r);
  370.  
  371. KIO_EXPORT int operator==(KSSLCertificate& x, KSSLCertificate& y);
  372. KIO_EXPORT inline int operator!=(KSSLCertificate& x, KSSLCertificate& y)
  373. { return !(x == y); }
  374.  
  375. #endif
  376.  
  377.