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