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 |
11 // Handling of certificates and keypairs for SSLStreamAdapter's peer mode. | 11 // Handling of certificates and keypairs for SSLStreamAdapter's peer mode. |
12 #if HAVE_CONFIG_H | 12 #if HAVE_CONFIG_H |
13 #include "config.h" | 13 #include "config.h" |
14 #endif // HAVE_CONFIG_H | 14 #endif // HAVE_CONFIG_H |
15 | 15 |
16 #include "webrtc/base/sslidentity.h" | 16 #include "webrtc/base/sslidentity.h" |
17 | 17 |
| 18 #include <ctime> |
18 #include <string> | 19 #include <string> |
19 | 20 |
20 #include "webrtc/base/base64.h" | 21 #include "webrtc/base/base64.h" |
21 #include "webrtc/base/checks.h" | 22 #include "webrtc/base/checks.h" |
22 #include "webrtc/base/logging.h" | 23 #include "webrtc/base/logging.h" |
23 #include "webrtc/base/sslconfig.h" | 24 #include "webrtc/base/sslconfig.h" |
24 | 25 |
25 #if SSL_USE_OPENSSL | 26 #if SSL_USE_OPENSSL |
26 | 27 |
27 #include "webrtc/base/opensslidentity.h" | 28 #include "webrtc/base/opensslidentity.h" |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 const std::string& certificate) { | 171 const std::string& certificate) { |
171 return OpenSSLIdentity::FromPEMStrings(private_key, certificate); | 172 return OpenSSLIdentity::FromPEMStrings(private_key, certificate); |
172 } | 173 } |
173 | 174 |
174 #else // !SSL_USE_OPENSSL | 175 #else // !SSL_USE_OPENSSL |
175 | 176 |
176 #error "No SSL implementation" | 177 #error "No SSL implementation" |
177 | 178 |
178 #endif // SSL_USE_OPENSSL | 179 #endif // SSL_USE_OPENSSL |
179 | 180 |
| 181 // Read |n| bytes from ASN1 number string at *|pp| and return the numeric value. |
| 182 // Update *|pp| and *|np| to reflect number of read bytes. |
| 183 static inline int ASN1ReadInt(const unsigned char** pp, size_t* np, size_t n) { |
| 184 const unsigned char* p = *pp; |
| 185 int x = 0; |
| 186 for (size_t i = 0; i < n; i++) |
| 187 x = 10 * x + p[i] - '0'; |
| 188 *pp = p + n; |
| 189 *np = *np - n; |
| 190 return x; |
| 191 } |
| 192 |
| 193 int64_t ASN1TimeToSec(const unsigned char* s, size_t length, bool long_format) { |
| 194 size_t bytes_left = length; |
| 195 |
| 196 // Make sure the string ends with Z. Doing it here protects the strspn call |
| 197 // from running off the end of the string in Z's absense. |
| 198 if (length == 0 || s[length - 1] != 'Z') |
| 199 return -1; |
| 200 |
| 201 // Make sure we only have ASCII digits so that we don't need to clutter the |
| 202 // code below and ASN1ReadInt with error checking. |
| 203 size_t n = strspn(reinterpret_cast<const char*>(s), "0123456789"); |
| 204 if (n + 1 != length) |
| 205 return -1; |
| 206 |
| 207 int year; |
| 208 |
| 209 // Read out ASN1 year, in either 2-char "UTCTIME" or 4-char "GENERALIZEDTIME" |
| 210 // format. Both format use UTC in this context. |
| 211 if (long_format) { |
| 212 // ASN1 format: yyyymmddhh[mm[ss[.fff]]]Z where the Z is literal, but |
| 213 // RFC 5280 requires us to only support exactly yyyymmddhhmmssZ. |
| 214 |
| 215 if (bytes_left < 11) |
| 216 return -1; |
| 217 |
| 218 year = ASN1ReadInt(&s, &bytes_left, 4); |
| 219 year -= 1900; |
| 220 } else { |
| 221 // ASN1 format: yymmddhhmm[ss]Z where the Z is literal, but RFC 5280 |
| 222 // requires us to only support exactly yymmddhhmmssZ. |
| 223 |
| 224 if (bytes_left < 9) |
| 225 return -1; |
| 226 |
| 227 year = ASN1ReadInt(&s, &bytes_left, 2); |
| 228 if (year < 50) // Per RFC 5280 4.1.2.5.1 |
| 229 year += 100; |
| 230 } |
| 231 |
| 232 std::tm tm; |
| 233 tm.tm_year = year; |
| 234 |
| 235 // Read out remaining ASN1 time data and store it in |tm| in documented |
| 236 // std::tm format. |
| 237 tm.tm_mon = ASN1ReadInt(&s, &bytes_left, 2) - 1; |
| 238 tm.tm_mday = ASN1ReadInt(&s, &bytes_left, 2); |
| 239 tm.tm_hour = ASN1ReadInt(&s, &bytes_left, 2); |
| 240 tm.tm_min = ASN1ReadInt(&s, &bytes_left, 2); |
| 241 tm.tm_sec = ASN1ReadInt(&s, &bytes_left, 2); |
| 242 |
| 243 if (bytes_left != 1) { |
| 244 // Now just Z should remain. Its existence was asserted above. |
| 245 return -1; |
| 246 } |
| 247 |
| 248 return TmToSeconds(tm); |
| 249 } |
| 250 |
180 } // namespace rtc | 251 } // namespace rtc |
OLD | NEW |