Michael Iedema | ccee56b | 2018-07-05 15:28:24 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018 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 | |
| 11 | #import "WebRTC/RTCCertificate.h" |
| 12 | #import "WebRTC/RTCLogging.h" |
| 13 | |
| 14 | #include "rtc_base/logging.h" |
| 15 | #include "rtc_base/rtccertificategenerator.h" |
| 16 | #include "rtc_base/sslidentity.h" |
| 17 | |
| 18 | @implementation RTCCertificate |
| 19 | |
| 20 | @synthesize private_key = _private_key; |
| 21 | @synthesize certificate = _certificate; |
| 22 | |
| 23 | - (id)copyWithZone:(NSZone *)zone { |
| 24 | id copy = [[[self class] alloc] initWithPrivateKey:[self.private_key copyWithZone:zone] |
| 25 | certificate:[self.certificate copyWithZone:zone]]; |
| 26 | return copy; |
| 27 | } |
| 28 | |
| 29 | - (instancetype)initWithPrivateKey:(NSString *)private_key certificate:(NSString *)certificate { |
| 30 | if (self = [super init]) { |
| 31 | _private_key = [private_key copy]; |
| 32 | _certificate = [certificate copy]; |
| 33 | } |
| 34 | return self; |
| 35 | } |
| 36 | |
| 37 | + (nullable RTCCertificate *)generateCertificateWithParams:(NSDictionary *)params { |
| 38 | rtc::KeyType keyType = rtc::KT_ECDSA; |
| 39 | NSString *keyTypeString = [params valueForKey:@"name"]; |
| 40 | if (keyTypeString && [keyTypeString isEqualToString:@"RSASSA-PKCS1-v1_5"]) { |
| 41 | keyType = rtc::KT_RSA; |
| 42 | } |
| 43 | |
| 44 | NSNumber *expires = [params valueForKey:@"expires"]; |
| 45 | rtc::scoped_refptr<rtc::RTCCertificate> cc_certificate = nullptr; |
| 46 | if (expires != nil) { |
| 47 | uint64_t expirationTimestamp = [expires unsignedLongLongValue]; |
| 48 | cc_certificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(keyType), |
| 49 | expirationTimestamp); |
| 50 | } else { |
| 51 | cc_certificate = |
| 52 | rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(keyType), absl::nullopt); |
| 53 | } |
| 54 | if (!cc_certificate) { |
| 55 | RTCLogError(@"Failed to generate certificate."); |
| 56 | return nullptr; |
| 57 | } |
| 58 | // grab PEMs and create an NS RTCCerticicate |
| 59 | rtc::RTCCertificatePEM pem = cc_certificate->ToPEM(); |
| 60 | std::string pem_private_key = pem.private_key(); |
| 61 | std::string pem_certificate = pem.certificate(); |
| 62 | RTC_LOG(LS_INFO) << "CERT PEM "; |
| 63 | RTC_LOG(LS_INFO) << pem_certificate; |
| 64 | |
| 65 | RTCCertificate *cert = [[RTCCertificate alloc] initWithPrivateKey:@(pem_private_key.c_str()) |
| 66 | certificate:@(pem_certificate.c_str())]; |
| 67 | return cert; |
| 68 | } |
| 69 | |
| 70 | @end |