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++) { | |
tommi
2015/11/30 15:53:55
nit: no {}
torbjorng (webrtc)
2015/11/30 21:05:45
Done.
| |
187 x = 10 * x + p[i] - '0'; | |
188 } | |
189 *pp = p + n; | |
190 *np = *np - n; | |
191 return x; | |
192 } | |
193 | |
194 int64_t ASN1TimeToSec(const unsigned char* s, size_t length, bool long_format) { | |
195 std::tm tm; | |
tommi
2015/11/30 15:53:55
nit: define these variables where they're needed (
torbjorng (webrtc)
2015/11/30 21:05:45
Done.
| |
196 int year; | |
197 size_t bytes_left = length; | |
198 | |
199 // Make sure the string ends with Z. Doing it here protects the strspn call | |
200 // from running off the end of the string in Z's absense. | |
201 if (length == 0 || s[length - 1] != 'Z') | |
tommi
2015/11/30 15:53:55
Below, I thought you might have a problem if the s
torbjorng (webrtc)
2015/11/30 21:05:44
This is noted in the declaration of this method.
| |
202 return -1; | |
203 | |
204 // Make sure we only have ASCII digits so that we don't need to clutter the | |
205 // code below and ASN1ReadInt with error checking. | |
206 size_t n = strspn(reinterpret_cast<const char*>(s), "0123456789"); | |
207 if (n + 1 != length) | |
208 return -1; | |
209 | |
210 // Read out ASN1 year, in either 2-char "UTCTIME" or 4-char "GENERALIZEDTIME" | |
211 // format. Both format use UTC in this context. | |
212 if (long_format) { | |
213 // ASN1 format: yyyymmddhh[mm[ss[.fff]]]Z where the Z is literal, but | |
214 // RFC 5280 requires us to only support exactly yyyymmddhhmmssZ. | |
215 | |
216 if (bytes_left < 11) | |
217 return -1; | |
218 | |
219 year = ASN1ReadInt(&s, &bytes_left, 4); | |
220 year -= 1900; | |
221 } else { | |
222 // ASN1 format: yymmddhhmm[ss]Z where the Z is literal, but RFC 5280 | |
223 // requires us to only support exactly yymmddhhmmssZ. | |
224 | |
225 if (bytes_left < 9) | |
226 return -1; | |
227 | |
228 year = ASN1ReadInt(&s, &bytes_left, 2); | |
229 if (year < 70) | |
230 year += 100; | |
231 } | |
232 | |
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 |