OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 } | 100 } |
101 | 101 |
102 // Helper function for deleting a vector of certificates. | 102 // Helper function for deleting a vector of certificates. |
103 static void DeleteCert(SSLCertificate* cert) { delete cert; } | 103 static void DeleteCert(SSLCertificate* cert) { delete cert; } |
104 | 104 |
105 std::vector<SSLCertificate*> certs_; | 105 std::vector<SSLCertificate*> certs_; |
106 | 106 |
107 RTC_DISALLOW_COPY_AND_ASSIGN(SSLCertChain); | 107 RTC_DISALLOW_COPY_AND_ASSIGN(SSLCertChain); |
108 }; | 108 }; |
109 | 109 |
| 110 // KT_DEFAULT is currently an alias for KT_RSA. This is likely to change. |
| 111 // KT_LAST is intended for vector declarations and loops over all key types; |
| 112 // it does not represent any key type in itself. |
110 // TODO(hbos,torbjorng): Don't change KT_DEFAULT without first updating | 113 // TODO(hbos,torbjorng): Don't change KT_DEFAULT without first updating |
111 // PeerConnectionFactory_nativeCreatePeerConnection's certificate generation | 114 // PeerConnectionFactory_nativeCreatePeerConnection's certificate generation |
112 // code. | 115 // code. |
113 enum KeyType { KT_RSA, KT_ECDSA, KT_LAST, KT_DEFAULT = KT_RSA }; | 116 enum KeyType { KT_RSA, KT_ECDSA, KT_LAST, KT_DEFAULT = KT_RSA }; |
114 | 117 |
| 118 static const int kRsaDefaultModSize = 1024; |
| 119 static const int kRsaDefaultExponent = 0x10001; // = 2^16+1 = 65537 |
| 120 static const int kRsaMinModSize = 1024; |
| 121 static const int kRsaMaxModSize = 8192; |
| 122 |
| 123 struct RSAParams { |
| 124 unsigned int mod_size; |
| 125 unsigned int pub_exp; |
| 126 }; |
| 127 |
| 128 enum ECCurve { EC_NIST_P256, /* EC_FANCY, */ EC_LAST }; |
| 129 |
| 130 class KeyParams { |
| 131 public: |
| 132 // Generate a KeyParams object from a simple KeyType, using default params. |
| 133 explicit KeyParams(KeyType key_type = KT_DEFAULT); |
| 134 |
| 135 // Generate a a KeyParams for RSA with explicit parameters. |
| 136 static KeyParams RSA(int mod_size = kRsaDefaultModSize, |
| 137 int pub_exp = kRsaDefaultExponent); |
| 138 |
| 139 // Generate a a KeyParams for ECDSA specifying the curve. |
| 140 static KeyParams ECDSA(ECCurve curve = EC_NIST_P256); |
| 141 |
| 142 // Check validity of a KeyParams object. Since the factory functions have |
| 143 // no way of returning errors, this function can be called after creation |
| 144 // to make sure the parameters are OK. |
| 145 bool IsValid() const; |
| 146 |
| 147 RSAParams rsa_params() const; |
| 148 |
| 149 ECCurve ec_curve() const; |
| 150 |
| 151 KeyType type() const { return type_; } |
| 152 |
| 153 private: |
| 154 KeyType type_; |
| 155 union { |
| 156 RSAParams rsa; |
| 157 ECCurve curve; |
| 158 } params_; |
| 159 }; |
| 160 |
115 // TODO(hbos): Remove once rtc::KeyType (to be modified) and | 161 // TODO(hbos): Remove once rtc::KeyType (to be modified) and |
116 // blink::WebRTCKeyType (to be landed) match. By using this function in Chromium | 162 // blink::WebRTCKeyType (to be landed) match. By using this function in Chromium |
117 // appropriately we can change KeyType enum -> class without breaking Chromium. | 163 // appropriately we can change KeyType enum -> class without breaking Chromium. |
118 KeyType IntKeyTypeFamilyToKeyType(int key_type_family); | 164 KeyType IntKeyTypeFamilyToKeyType(int key_type_family); |
119 | 165 |
120 // Parameters for generating an identity for testing. If common_name is | 166 // Parameters for generating a certificate. If |common_name| is non-empty, it |
121 // non-empty, it will be used for the certificate's subject and issuer name, | 167 // will be used for the certificate's subject and issuer name, otherwise a |
122 // otherwise a random string will be used. |not_before| and |not_after| are | 168 // random string will be used. |
123 // offsets to the current time in number of seconds. | |
124 struct SSLIdentityParams { | 169 struct SSLIdentityParams { |
125 std::string common_name; | 170 std::string common_name; |
126 int not_before; // in seconds. | 171 int not_before; // offset from current time in seconds. |
127 int not_after; // in seconds. | 172 int not_after; // offset from current time in seconds. |
128 KeyType key_type; | 173 KeyParams key_params; |
129 }; | 174 }; |
130 | 175 |
131 // Our identity in an SSL negotiation: a keypair and certificate (both | 176 // Our identity in an SSL negotiation: a keypair and certificate (both |
132 // with the same public key). | 177 // with the same public key). |
133 // This too is pretty much immutable once created. | 178 // This too is pretty much immutable once created. |
134 class SSLIdentity { | 179 class SSLIdentity { |
135 public: | 180 public: |
136 // Generates an identity (keypair and self-signed certificate). If | 181 // Generates an identity (keypair and self-signed certificate). If |
137 // common_name is non-empty, it will be used for the certificate's | 182 // common_name is non-empty, it will be used for the certificate's |
138 // subject and issuer name, otherwise a random string will be used. | 183 // subject and issuer name, otherwise a random string will be used. |
139 // Returns NULL on failure. | 184 // Returns NULL on failure. |
140 // Caller is responsible for freeing the returned object. | 185 // Caller is responsible for freeing the returned object. |
141 static SSLIdentity* Generate(const std::string& common_name, | 186 static SSLIdentity* Generate(const std::string& common_name, |
142 KeyType key_type); | 187 const KeyParams& key_param); |
| 188 static SSLIdentity* Generate(const std::string& common_name, |
| 189 KeyType key_type) { |
| 190 return Generate(common_name, KeyParams(key_type)); |
| 191 } |
143 | 192 |
144 // Generates an identity with the specified validity period. | 193 // Generates an identity with the specified validity period. |
145 static SSLIdentity* GenerateForTest(const SSLIdentityParams& params); | 194 static SSLIdentity* GenerateForTest(const SSLIdentityParams& params); |
146 | 195 |
147 // Construct an identity from a private key and a certificate. | 196 // Construct an identity from a private key and a certificate. |
148 static SSLIdentity* FromPEMStrings(const std::string& private_key, | 197 static SSLIdentity* FromPEMStrings(const std::string& private_key, |
149 const std::string& certificate); | 198 const std::string& certificate); |
150 | 199 |
151 virtual ~SSLIdentity() {} | 200 virtual ~SSLIdentity() {} |
152 | 201 |
(...skipping 15 matching lines...) Expand all Loading... |
168 size_t length); | 217 size_t length); |
169 }; | 218 }; |
170 | 219 |
171 extern const char kPemTypeCertificate[]; | 220 extern const char kPemTypeCertificate[]; |
172 extern const char kPemTypeRsaPrivateKey[]; | 221 extern const char kPemTypeRsaPrivateKey[]; |
173 extern const char kPemTypeEcPrivateKey[]; | 222 extern const char kPemTypeEcPrivateKey[]; |
174 | 223 |
175 } // namespace rtc | 224 } // namespace rtc |
176 | 225 |
177 #endif // WEBRTC_BASE_SSLIDENTITY_H_ | 226 #endif // WEBRTC_BASE_SSLIDENTITY_H_ |
OLD | NEW |