You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.5 KiB

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