1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#ifndef ICESSL_PLUGIN_H
#define ICESSL_PLUGIN_H
#include <Ice/Plugin.h>
#include <IceSSL/Config.h>
#include <IceSSL/ConnectionInfoF.h>
#ifdef ICE_CPP11_MAPPING
# include <chrono>
#else
# include <IceUtil/Time.h>
#endif
#include <vector>
#include <list>
#ifndef ICESSL_API
# if defined(ICE_STATIC_LIBS)
# define ICESSL_API /**/
# elif defined(ICESSL_API_EXPORTS)
# define ICESSL_API ICE_DECLSPEC_EXPORT
# else
# define ICESSL_API ICE_DECLSPEC_IMPORT
# endif
#endif
namespace IceSSL
{
/**
* The reason for an IceSSL certificate verification failure.
*/
#ifdef ICE_CPP11_MAPPING
enum class TrustError : unsigned char
#else
enum TrustError
#endif
{
/** The certification verification succeed */
NoError = 0,
/** The certificate chain length is greater than the specified maximum depth **/
ChainTooLong,
/** The X509 chain is invalid because a certificate has excluded a name constraint **/
HasExcludedNameConstraint,
/** The certificate has an undefined name constraint **/
HasNonDefinedNameConstraint,
/** The certificate has a non permitted name constraint **/
HasNonPermittedNameConstraint,
/** The certificate does not support a critical extension **/
HasNonSupportedCriticalExtension,
/** The certificate does not have a supported name constraint or has a name constraint that is unsupported **/
HasNonSupportedNameConstraint,
/** A host name mismatch has occurred **/
HostNameMismatch,
/** The X509 chain is invalid due to invalid basic constraints **/
InvalidBasicConstraints,
/** The X509 chain is invalid due to an invalid extension **/
InvalidExtension,
/** The X509 chain is invalid due to invalid name constraints **/
InvalidNameConstraints,
/** The X509 chain is invalid due to invalid policy constraints **/
InvalidPolicyConstraints,
/** The supplied certificate cannot be used for the specified purpose **/
InvalidPurpose,
/** The X509 chain is invalid due to an invalid certificate signature **/
InvalidSignature,
/** The X509 chain is not valid due to an invalid time value, such as a value that indicates an expired
certificate **/
InvalidTime,
/** The certificate is explicitly not trusted **/
NotTrusted,
/** The X509 chain could not be built up to the root certificate **/
PartialChain,
/** It is not possible to determine whether the certificate has been revoked **/
RevocationStatusUnknown,
/** The X509 chain is invalid due to a revoked certificate **/
Revoked,
/** The X509 chain is invalid due to an untrusted root certificate **/
UntrustedRoot,
/** The X509 chain is invalid due to other unknown failure **/
UnknownTrustFailure,
};
ICESSL_API TrustError getTrustError(const IceSSL::ConnectionInfoPtr&);
ICESSL_API std::string getTrustErrorDescription(TrustError);
ICESSL_API std::string getHost(const IceSSL::ConnectionInfoPtr&);
/**
* The key usage "digitalSignature" bit is set
*/
const unsigned int KEY_USAGE_DIGITAL_SIGNATURE = 1u << 0;
/**
* The key usage "nonRepudiation" bit is set
*/
const unsigned int KEY_USAGE_NON_REPUDIATION = 1u << 1;
/**
* The key usage "keyEncipherment" bit is set
*/
const unsigned int KEY_USAGE_KEY_ENCIPHERMENT = 1u << 2;
/**
* The key usage "dataEncipherment" bit is set
*/
const unsigned int KEY_USAGE_DATA_ENCIPHERMENT = 1u << 3;
/**
* The key usage "keyAgreement" bit is set
*/
const unsigned int KEY_USAGE_KEY_AGREEMENT = 1u << 4;
/**
* The key usage "keyCertSign" bit is set
*/
const unsigned int KEY_USAGE_KEY_CERT_SIGN = 1u << 5;
/**
* The key usage "cRLSign" bit is set
*/
const unsigned int KEY_USAGE_CRL_SIGN = 1u << 6;
/**
* The key usage "encipherOnly" bit is set
*/
const unsigned int KEY_USAGE_ENCIPHER_ONLY = 1u << 7;
/**
* The key usage "decipherOnly" bit is set
*/
const unsigned int KEY_USAGE_DECIPHER_ONLY = 1u << 8;
/**
* The extended key usage "anyKeyUsage" bit is set
*/
const unsigned int EXTENDED_KEY_USAGE_ANY_KEY_USAGE = 1u << 0;
/**
* The extended key usage "serverAuth" bit is set
*/
const unsigned int EXTENDED_KEY_USAGE_SERVER_AUTH = 1u << 1;
/**
* The extended key usage "clientAuth" bit is set
*/
const unsigned int EXTENDED_KEY_USAGE_CLIENT_AUTH = 1u << 2;
/**
* The extended key usage "codeSigning" bit is set
*/
const unsigned int EXTENDED_KEY_USAGE_CODE_SIGNING = 1u << 3;
/**
* The extended key usage "emailProtection" bit is set
*/
const unsigned int EXTENDED_KEY_USAGE_EMAIL_PROTECTION = 1u << 4;
/**
* The extended key usage "timeStamping" bit is set
*/
const unsigned int EXTENDED_KEY_USAGE_TIME_STAMPING = 1u << 5;
/**
* The extended key usage "OCSPSigning" bit is set
*/
const unsigned int EXTENDED_KEY_USAGE_OCSP_SIGNING = 1u << 6;
/**
* Thrown if the certificate cannot be read.
* \headerfile IceSSL/IceSSL.h
*/
class ICESSL_API CertificateReadException : public IceUtil::ExceptionHelper<CertificateReadException>
{
public:
CertificateReadException(const char*, int, const std::string&);
#ifndef ICE_CPP11_COMPILER
virtual ~CertificateReadException() throw();
#endif
virtual std::string ice_id() const;
#ifndef ICE_CPP11_MAPPING
/**
* Creates a shallow copy of this exception.
* @return The new exception instance.
*/
virtual CertificateReadException* ice_clone() const;
#endif
/** The reason for the exception. */
std::string reason;
private:
static const char* _name;
};
/**
* Thrown if the certificate cannot be encoded.
* \headerfile IceSSL/IceSSL.h
*/
class ICESSL_API CertificateEncodingException : public IceUtil::ExceptionHelper<CertificateEncodingException>
{
public:
CertificateEncodingException(const char*, int, const std::string&);
#ifndef ICE_CPP11_COMPILER
virtual ~CertificateEncodingException() throw();
#endif
virtual std::string ice_id() const;
#ifndef ICE_CPP11_MAPPING
/**
* Creates a shallow copy of this exception.
* @return The new exception instance.
*/
virtual CertificateEncodingException* ice_clone() const;
#endif
/** The reason for the exception. */
std::string reason;
private:
static const char* _name;
};
/**
* This exception is thrown if a distinguished name cannot be parsed.
* \headerfile IceSSL/IceSSL.h
*/
class ICESSL_API ParseException : public IceUtil::ExceptionHelper<ParseException>
{
public:
ParseException(const char*, int, const std::string&);
#ifndef ICE_CPP11_COMPILER
virtual ~ParseException() throw();
#endif
virtual std::string ice_id() const;
#ifndef ICE_CPP11_MAPPING
/**
* Creates a shallow copy of this exception.
* @return The new exception instance.
*/
virtual ParseException* ice_clone() const;
#endif
/** The reason for the exception. */
std::string reason;
private:
static const char* _name;
};
/**
* This class represents a DistinguishedName, similar to the Java
* type X500Principal and the .NET type X500DistinguishedName.
*
* For comparison purposes, the value of a relative distinguished
* name (RDN) component is always unescaped before matching,
* therefore "ZeroC, Inc." will match ZeroC\, Inc.
*
* toString() always returns exactly the same information as was
* provided in the constructor (i.e., "ZeroC, Inc." will not turn
* into ZeroC\, Inc.).
* \headerfile IceSSL/IceSSL.h
*/
class ICESSL_API DistinguishedName
{
public:
/**
* Creates a DistinguishedName from a string encoded using the rules in RFC2253.
* @param name The encoded distinguished name.
* @throws ParseException if parsing fails.
*/
explicit DistinguishedName(const std::string& name);
/**
* Creates a DistinguishedName from a list of RDN pairs,
* where each pair consists of the RDN's type and value.
* For example, the RDN "O=ZeroC" is represented by the
* pair ("O", "ZeroC").
* @throws ParseException if parsing fails.
*/
explicit DistinguishedName(const std::list<std::pair<std::string, std::string> >&);
/**
* Performs an exact match. The order of the RDN components is important.
*/
friend ICESSL_API bool operator==(const DistinguishedName&, const DistinguishedName&);
/**
* Performs an exact match. The order of the RDN components is important.
*/
friend ICESSL_API bool operator<(const DistinguishedName&, const DistinguishedName&);
/**
* Performs a partial match with another DistinguishedName.
* @param dn The name to be matched.
* @return True if all of the RDNs in the argument are present in this
* DistinguishedName and they have the same values.
*/
bool match(const DistinguishedName& dn) const;
/**
* Performs a partial match with another DistinguishedName.
* @param dn The name to be matched.
* @return True if all of the RDNs in the argument are present in this
* DistinguishedName and they have the same values.
*/
bool match(const std::string& dn) const;
/**
* Encodes the DN in RFC2253 format.
* @return An encoded string.
*/
operator std::string() const;
protected:
/// \cond INTERNAL
void unescape();
/// \endcond
private:
std::list<std::pair<std::string, std::string> > _rdns;
std::list<std::pair<std::string, std::string> > _unescaped;
};
/**
* Performs an exact match. The order of the RDN components is important.
*/
inline bool
operator>(const DistinguishedName& lhs, const DistinguishedName& rhs)
{
return rhs < lhs;
}
/**
* Performs an exact match. The order of the RDN components is important.
*/
inline bool
operator<=(const DistinguishedName& lhs, const DistinguishedName& rhs)
{
return !(lhs > rhs);
}
/**
* Performs an exact match. The order of the RDN components is important.
*/
inline bool
operator>=(const DistinguishedName& lhs, const DistinguishedName& rhs)
{
return !(lhs < rhs);
}
/**
* Performs an exact match. The order of the RDN components is important.
*/
inline bool
operator!=(const DistinguishedName& lhs, const DistinguishedName& rhs)
{
return !(lhs == rhs);
}
/**
* Represents an X509 Certificate extension.
* \headerfile IceSSL/IceSSL.h
*/
class ICESSL_API X509Extension
#ifndef ICE_CPP11_MAPPING
: public virtual IceUtil::Shared
#endif
{
public:
/**
* Determines whether the information in this extension is important.
* @return True if if information is important, false otherwise.
*/
virtual bool isCritical() const = 0;
/**
* Obtains the object ID of this extension.
* @return The object ID.
*/
virtual std::string getOID() const = 0;
/**
* Obtains the data associated with this extension.
* @return The extension data.
*/
virtual std::vector<Ice::Byte> getData() const = 0;
};
ICE_DEFINE_PTR(X509ExtensionPtr, X509Extension);
class Certificate;
ICE_DEFINE_PTR(CertificatePtr, Certificate);
/**
* This convenience class is a wrapper around a native certificate.
* The interface is inspired by java.security.cert.X509Certificate.
* \headerfile IceSSL/IceSSL.h
*/
class ICESSL_API Certificate :
#ifdef ICE_CPP11_MAPPING
public std::enable_shared_from_this<Certificate>
#else
public virtual IceUtil::Shared
#endif
{
public:
/**
* Compares the certificates for equality using the native certificate comparison method.
*/
virtual bool operator==(const Certificate&) const = 0;
/**
* Compares the certificates for equality using the native certificate comparison method.
*/
virtual bool operator!=(const Certificate&) const = 0;
/**
* Obtains the authority key identifier.
* @return The identifier.
*/
virtual std::vector<Ice::Byte> getAuthorityKeyIdentifier() const = 0;
/**
* Obtains the subject key identifier.
* @return The identifier.
*/
virtual std::vector<Ice::Byte> getSubjectKeyIdentifier() const = 0;
/**
* Verifies that this certificate was signed by the given certificate
* public key.
* @param cert A certificate containing the public key.
* @return True if signed, false otherwise.
*/
virtual bool verify(const CertificatePtr& cert) const = 0;
/**
* Obtains a string encoding of the certificate in PEM format.
* @return The encoded certificate.
* @throws CertificateEncodingException if an error occurs.
*/
virtual std::string encode() const = 0;
/**
* Checks that the certificate is currently valid, that is, the current
* date falls between the validity period given in the certificate.
* @return True if the certificate is valid, false otherwise.
*/
virtual bool checkValidity() const = 0;
/**
* Checks that the certificate is valid at the given time.
* @param t The target time.
* @return True if the certificate is valid, false otherwise.
*/
#ifdef ICE_CPP11_MAPPING
virtual bool checkValidity(const std::chrono::system_clock::time_point& t) const = 0;
#else
virtual bool checkValidity(const IceUtil::Time& t) const = 0;
#endif
/**
* Returns the value of the key usage extension. The flags <b>KEY_USAGE_DIGITAL_SIGNATURE</b>,
* <b>KEY_USAGE_NON_REPUDIATION</b>, <b>KEY_USAGE_KEY_ENCIPHERMENT</b>, <b>KEY_USAGE_DATA_ENCIPHERMENT</b>
* <b>KEY_USAGE_KEY_AGREEMENT</b>, <b>KEY_USAGE_KEY_CERT_SIGN</b>, <b>KEY_USAGE_CRL_SIGN</b>,
* <b>KEY_USAGE_ENCIPHER_ONLY</b> and <b>KEY_USAGE_DECIPHER_ONLY</b> can be used to check what
* key usage bits are set.
*/
unsigned int getKeyUsage() const;
/**
* Returns the value of the extended key usage extension. The flags <b>EXTENDED_KEY_USAGE_ANY_KEY_USAGE</b>,
* <b>EXTENDED_KEY_USAGE_SERVER_AUTH</b>, <b>EXTENDED_KEY_USAGE_CLIENT_AUTH</b>,
* <b>EXTENDED_KEY_USAGE_CODE_SIGNING</b>, <b>EXTENDED_KEY_USAGE_EMAIL_PROTECTION</b>,
* <b>EXTENDED_KEY_USAGE_TIME_STAMPING</b> and <b>EXTENDED_KEY_USAGE_OCSP_SIGNING</b> can be used to check what
* extended key usage bits are set.
*/
unsigned int getExtendedKeyUsage() const;
/**
* Obtains the not-after validity time.
* @return The time after which this certificate is invalid.
*/
#ifdef ICE_CPP11_MAPPING
virtual std::chrono::system_clock::time_point getNotAfter() const = 0;
#else
virtual IceUtil::Time getNotAfter() const = 0;
#endif
/**
* Obtains the not-before validity time.
* @return The time at which this certificate is valid.
*/
#ifdef ICE_CPP11_MAPPING
virtual std::chrono::system_clock::time_point getNotBefore() const = 0;
#else
virtual IceUtil::Time getNotBefore() const = 0;
#endif
/**
* Obtains the serial number. This is an arbitrarily large number.
* @return The certificate's serial number.
*/
virtual std::string getSerialNumber() const = 0;
/**
* Obtains the issuer's distinguished name (DN).
* @return The distinguished name.
*/
virtual DistinguishedName getIssuerDN() const = 0;
/**
* Obtains the values in the issuer's alternative names extension.
*
* The returned list contains a pair of int, string.
*
* otherName [0] OtherName
* rfc822Name [1] IA5String
* dNSName [2] IA5String
* x400Address [3] ORAddress
* directoryName [4] Name
* ediPartyName [5] EDIPartyName
* uniformResourceIdentifier [6] IA5String
* iPAddress [7] OCTET STRING
* registeredID [8] OBJECT IDENTIFIER
*
* rfc822Name, dNSName, directoryName and
* uniformResourceIdentifier data is returned as a string.
*
* iPAddress is returned in dotted quad notation. IPv6 is not
* currently supported.
*
* All distinguished names are encoded in RFC2253 format.
*
* The remainder of the data will result in an empty string. Use the raw
* X509* certificate to obtain these values.
*
* @return The issuer's alternative names.
*/
virtual std::vector<std::pair<int, std::string> > getIssuerAlternativeNames() const = 0;
/**
* Obtains the subject's distinguished name (DN).
* @return The distinguished name.
*/
virtual DistinguishedName getSubjectDN() const = 0;
/**
* See the comment for Plugin::getIssuerAlternativeNames.
* @return The subject's alternative names.
*/
virtual std::vector<std::pair<int, std::string> > getSubjectAlternativeNames() const = 0;
/**
* Obtains the certificate version number.
* @return The version number.
*/
virtual int getVersion() const = 0;
/**
* Stringifies the certificate. This is a human readable version of
* the certificate, not a DER or PEM encoding.
* @return A string version of the certificate.
*/
virtual std::string toString() const = 0;
/**
* Obtains a list of the X509v3 extensions contained in the certificate.
* @return The extensions.
*/
virtual std::vector<X509ExtensionPtr> getX509Extensions() const = 0;
/**
* Obtains the extension with the given OID.
* @return The extension, or null if the certificate
* does not contain a extension with the given OID.
*/
virtual X509ExtensionPtr getX509Extension(const std::string& oid) const = 0;
/**
* Loads the certificate from a file. The certificate must use the
* PEM encoding format.
* @param file The certificate file.
* @return The new certificate instance.
* @throws CertificateReadException if the file cannot be read.
*/
static CertificatePtr load(const std::string& file);
/**
* Decodes a certificate from a string that uses the PEM encoding format.
* @param str A string containing the encoded certificate.
* @throws CertificateEncodingException if an error occurs.
*/
static CertificatePtr decode(const std::string& str);
};
#ifndef ICE_CPP11_MAPPING // C++98 mapping
/**
* An application can customize the certificate verification process
* by implementing the CertificateVerifier interface.
* \headerfile IceSSL/IceSSL.h
*/
class ICESSL_API CertificateVerifier : public IceUtil::Shared
{
public:
virtual ~CertificateVerifier();
/**
* Determines whether to accept a certificate.
* @param info Information about the connection.
* @return False if the connection should be rejected, or true to allow it.
*/
virtual bool verify(const ConnectionInfoPtr& info) = 0;
};
typedef IceUtil::Handle<CertificateVerifier> CertificateVerifierPtr;
/**
* In order to read an encrypted file, such as one containing a
* private key, OpenSSL requests a password from IceSSL. The password
* can be defined using an IceSSL configuration property, but a
* plain-text password is a security risk. If a password is not
* supplied via configuration, IceSSL allows OpenSSL to prompt the
* user interactively. This may not be desirable (or even possible),
* so the application can supply an implementation of PasswordPrompt
* to take responsibility for obtaining the password.
*
* Note that the password is needed during plug-in initialization, so
* in general you will need to delay initialization (by defining
* IceSSL.DelayInit=1), configure the PasswordPrompt, then manually
* initialize the plug-in.
* \headerfile IceSSL/IceSSL.h
*/
class ICESSL_API PasswordPrompt : public IceUtil::Shared
{
public:
virtual ~PasswordPrompt();
/**
* Obtains the password. This method may be invoked repeatedly, such as when
* several encrypted files are opened, or when multiple password
* attempts are allowed.
* @return The clear-text password.
*/
virtual std::string getPassword() = 0;
};
typedef IceUtil::Handle<PasswordPrompt> PasswordPromptPtr;
#endif
/**
* Represents the IceSSL plug-in object.
* \headerfile IceSSL/IceSSL.h
*/
class ICESSL_API Plugin : public Ice::Plugin
{
public:
virtual ~Plugin();
/**
* Establish the certificate verifier object. This should be done
* before any connections are established.
* @param v The verifier.
*/
#ifdef ICE_CPP11_MAPPING
virtual void setCertificateVerifier(std::function<bool(const std::shared_ptr<ConnectionInfo>&)> v) = 0;
#else
virtual void setCertificateVerifier(const CertificateVerifierPtr& v) = 0;
#endif
/**
* Establish the password prompt object. This must be done before
* the plug-in is initialized.
* @param p The password prompt.
*/
#ifdef ICE_CPP11_MAPPING
virtual void setPasswordPrompt(std::function<std::string()> p) = 0;
#else
virtual void setPasswordPrompt(const PasswordPromptPtr& p) = 0;
#endif
/**
* Load the certificate from a file. The certificate must use the
* PEM encoding format.
* @param file The certificate file.
* @throws CertificateReadException if the file cannot be read.
*/
virtual CertificatePtr load(const std::string& file) const = 0;
/**
* Decode a certificate from a string that uses the PEM encoding
* format.
* @param str A string containing the encoded certificate.
* @throws CertificateEncodingException if an error occurs.
*/
virtual CertificatePtr decode(const std::string& str) const = 0;
};
ICE_DEFINE_PTR(PluginPtr, Plugin);
}
#endif
|