You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
6.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. // Handling of certificates and keypairs for SSLStreamAdapter's peer mode.
  11. #ifndef RTC_BASE_SSL_IDENTITY_H_
  12. #define RTC_BASE_SSL_IDENTITY_H_
  13. #include <stdint.h>
  14. #include <ctime>
  15. #include <string>
  16. #include "rtc_base/system/rtc_export.h"
  17. namespace rtc {
  18. class SSLCertChain;
  19. class SSLCertificate;
  20. // KT_LAST is intended for vector declarations and loops over all key types;
  21. // it does not represent any key type in itself.
  22. // KT_DEFAULT is used as the default KeyType for KeyParams.
  23. enum KeyType { KT_RSA, KT_ECDSA, KT_LAST, KT_DEFAULT = KT_ECDSA };
  24. static const int kRsaDefaultModSize = 1024;
  25. static const int kRsaDefaultExponent = 0x10001; // = 2^16+1 = 65537
  26. static const int kRsaMinModSize = 1024;
  27. static const int kRsaMaxModSize = 8192;
  28. // Certificate default validity lifetime.
  29. static const int kDefaultCertificateLifetimeInSeconds =
  30. 60 * 60 * 24 * 30; // 30 days
  31. // Certificate validity window.
  32. // This is to compensate for slightly incorrect system clocks.
  33. static const int kCertificateWindowInSeconds = -60 * 60 * 24;
  34. struct RSAParams {
  35. unsigned int mod_size;
  36. unsigned int pub_exp;
  37. };
  38. enum ECCurve { EC_NIST_P256, /* EC_FANCY, */ EC_LAST };
  39. class RTC_EXPORT KeyParams {
  40. public:
  41. // Generate a KeyParams object from a simple KeyType, using default params.
  42. explicit KeyParams(KeyType key_type = KT_DEFAULT);
  43. // Generate a a KeyParams for RSA with explicit parameters.
  44. static KeyParams RSA(int mod_size = kRsaDefaultModSize,
  45. int pub_exp = kRsaDefaultExponent);
  46. // Generate a a KeyParams for ECDSA specifying the curve.
  47. static KeyParams ECDSA(ECCurve curve = EC_NIST_P256);
  48. // Check validity of a KeyParams object. Since the factory functions have
  49. // no way of returning errors, this function can be called after creation
  50. // to make sure the parameters are OK.
  51. bool IsValid() const;
  52. RSAParams rsa_params() const;
  53. ECCurve ec_curve() const;
  54. KeyType type() const { return type_; }
  55. private:
  56. KeyType type_;
  57. union {
  58. RSAParams rsa;
  59. ECCurve curve;
  60. } params_;
  61. };
  62. // TODO(hbos): Remove once rtc::KeyType (to be modified) and
  63. // blink::WebRTCKeyType (to be landed) match. By using this function in Chromium
  64. // appropriately we can change KeyType enum -> class without breaking Chromium.
  65. KeyType IntKeyTypeFamilyToKeyType(int key_type_family);
  66. // Parameters for generating a certificate. If |common_name| is non-empty, it
  67. // will be used for the certificate's subject and issuer name, otherwise a
  68. // random string will be used.
  69. struct SSLIdentityParams {
  70. std::string common_name;
  71. time_t not_before; // Absolute time since epoch in seconds.
  72. time_t not_after; // Absolute time since epoch in seconds.
  73. KeyParams key_params;
  74. };
  75. // Our identity in an SSL negotiation: a keypair and certificate (both
  76. // with the same public key).
  77. // This too is pretty much immutable once created.
  78. class RTC_EXPORT SSLIdentity {
  79. public:
  80. // Generates an identity (keypair and self-signed certificate). If
  81. // |common_name| is non-empty, it will be used for the certificate's subject
  82. // and issuer name, otherwise a random string will be used. The key type and
  83. // parameters are defined in |key_param|. The certificate's lifetime in
  84. // seconds from the current time is defined in |certificate_lifetime|; it
  85. // should be a non-negative number.
  86. // Returns null on failure.
  87. // Caller is responsible for freeing the returned object.
  88. static SSLIdentity* GenerateWithExpiration(const std::string& common_name,
  89. const KeyParams& key_param,
  90. time_t certificate_lifetime);
  91. static SSLIdentity* Generate(const std::string& common_name,
  92. const KeyParams& key_param);
  93. static SSLIdentity* Generate(const std::string& common_name,
  94. KeyType key_type);
  95. // Generates an identity with the specified validity period.
  96. // TODO(torbjorng): Now that Generate() accepts relevant params, make tests
  97. // use that instead of this function.
  98. static SSLIdentity* GenerateForTest(const SSLIdentityParams& params);
  99. // Construct an identity from a private key and a certificate.
  100. static SSLIdentity* FromPEMStrings(const std::string& private_key,
  101. const std::string& certificate);
  102. // Construct an identity from a private key and a certificate chain.
  103. static SSLIdentity* FromPEMChainStrings(const std::string& private_key,
  104. const std::string& certificate_chain);
  105. virtual ~SSLIdentity() {}
  106. // Returns a new SSLIdentity object instance wrapping the same
  107. // identity information.
  108. // Caller is responsible for freeing the returned object.
  109. // TODO(hbos,torbjorng): Rename to a less confusing name.
  110. virtual SSLIdentity* GetReference() const = 0;
  111. // Returns a temporary reference to the end-entity (leaf) certificate.
  112. virtual const SSLCertificate& certificate() const = 0;
  113. // Returns a temporary reference to the entire certificate chain.
  114. virtual const SSLCertChain& cert_chain() const = 0;
  115. virtual std::string PrivateKeyToPEMString() const = 0;
  116. virtual std::string PublicKeyToPEMString() const = 0;
  117. // Helpers for parsing converting between PEM and DER format.
  118. static bool PemToDer(const std::string& pem_type,
  119. const std::string& pem_string,
  120. std::string* der);
  121. static std::string DerToPem(const std::string& pem_type,
  122. const unsigned char* data,
  123. size_t length);
  124. };
  125. bool operator==(const SSLIdentity& a, const SSLIdentity& b);
  126. bool operator!=(const SSLIdentity& a, const SSLIdentity& b);
  127. // Convert from ASN1 time as restricted by RFC 5280 to seconds from 1970-01-01
  128. // 00.00 ("epoch"). If the ASN1 time cannot be read, return -1. The data at
  129. // |s| is not 0-terminated; its char count is defined by |length|.
  130. int64_t ASN1TimeToSec(const unsigned char* s, size_t length, bool long_format);
  131. extern const char kPemTypeCertificate[];
  132. extern const char kPemTypeRsaPrivateKey[];
  133. extern const char kPemTypeEcPrivateKey[];
  134. } // namespace rtc
  135. #endif // RTC_BASE_SSL_IDENTITY_H_