Debian Codegraph @

src/objective-c/GRPCClient/private/GRPCCore/GRPCSecureChannelFactory.m

raw
1 /*
2 *
3 * Copyright 2018 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #import "GRPCSecureChannelFactory.h"
20
21 #import <GRPCClient/GRPCTypes.h>
22 #include <grpc/grpc_security.h>
23
24 #import "ChannelArgsUtil.h"
25 #import "GRPCChannel.h"
26
27 @implementation GRPCSecureChannelFactory {
28 grpc_channel_credentials *_channelCreds;
29 NSString *_sslRootPathStr;
30 }
31
32 + (instancetype)factoryWithPEMRootCertificates:(NSString *)rootCerts
33 privateKey:(NSString *)privateKey
34 certChain:(NSString *)certChain
35 error:(NSError *__autoreleasing *)errorPtr {
36 return [[self alloc] initWithPEMRootCerts:rootCerts
37 privateKey:privateKey
38 certChain:certChain
39 error:errorPtr];
40 }
41
42 - (NSData *)nullTerminatedDataWithString:(NSString *)string {
43 // dataUsingEncoding: does not return a null-terminated string.
44 NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
45 if (data == nil) {
46 return nil;
47 }
48 NSMutableData *nullTerminated = [NSMutableData dataWithData:data];
49 [nullTerminated appendBytes:"\0" length:1];
50 return nullTerminated;
51 }
52
53 - (instancetype)initWithPEMRootCerts:(NSString *)rootCerts
54 privateKey:(NSString *)privateKey
55 certChain:(NSString *)certChain
56 error:(NSError *__autoreleasing *)errorPtr {
57 static dispatch_once_t loading;
58 dispatch_once(&loading, ^{
59 NSString *rootsPEM = @"roots";
60 NSString *resourceBundlePath = @"gRPCCertificates.bundle"; // .pem
61 // Do not use NSBundle.mainBundle, as it's nil for tests of library projects.
62 NSBundle *bundle = [NSBundle bundleForClass:[self class]];
63 NSBundle *resourceBundle = [NSBundle
64 bundleWithURL:[[bundle resourceURL] URLByAppendingPathComponent:resourceBundlePath]];
65 _sslRootPathStr = [resourceBundle pathForResource:rootsPEM ofType:@"pem"];
66 const char *utf8Str = [_sslRootPathStr cStringUsingEncoding:NSUTF8StringEncoding];
67 if (utf8Str != NULL) {
68 setenv("GRPC_DEFAULT_SSL_ROOTS_FILE_PATH", utf8Str, 1);
69 } else {
70 if (errorPtr) {
71 *errorPtr = [NSError
72 errorWithDomain:kGRPCErrorDomain
73 code:GRPCErrorCodeInternal
74 userInfo:@{
75 NSLocalizedDescriptionKey : @"Failed to set default ssl root file path."
76 }];
77 }
78 }
79 });
80
81 NSData *rootsASCII = nil;
82 // if rootCerts is not provided, gRPC will use its own default certs
83 if (rootCerts != nil) {
84 rootsASCII = [self nullTerminatedDataWithString:rootCerts];
85 }
86
87 grpc_channel_credentials *creds = NULL;
88 if (privateKey.length == 0 && certChain.length == 0) {
89 creds = grpc_ssl_credentials_create((const char *)rootsASCII.bytes, NULL, NULL, NULL);
90 } else {
91 grpc_ssl_pem_key_cert_pair key_cert_pair;
92 NSData *privateKeyASCII = [self nullTerminatedDataWithString:privateKey];
93 NSData *certChainASCII = [self nullTerminatedDataWithString:certChain];
94 key_cert_pair.private_key = (const char *)privateKeyASCII.bytes;
95 key_cert_pair.cert_chain = (const char *)certChainASCII.bytes;
96 if (key_cert_pair.private_key == NULL || key_cert_pair.cert_chain == NULL) {
97 creds = grpc_ssl_credentials_create((const char *)rootsASCII.bytes, NULL, NULL, NULL);
98 } else {
99 creds =
100 grpc_ssl_credentials_create((const char *)rootsASCII.bytes, &key_cert_pair, NULL, NULL);
101 }
102 }
103
104 if ((self = [super init])) {
105 _channelCreds = creds;
106 }
107 return self;
108 }
109
110 - (grpc_channel *)createChannelWithHost:(NSString *)host channelArgs:(NSDictionary *)args {
111 NSAssert(host.length != 0, @"host cannot be empty");
112 if (host.length == 0) {
113 return NULL;
114 }
115 grpc_channel_args *coreChannelArgs = GRPCBuildChannelArgs(args);
116 grpc_channel *unmanagedChannel =
117 grpc_channel_create(host.UTF8String, _channelCreds, coreChannelArgs);
118 GRPCFreeChannelArgs(coreChannelArgs);
119 return unmanagedChannel;
120 }
121
122 - (void)dealloc {
123 if (_channelCreds != NULL) {
124 grpc_channel_credentials_release(_channelCreds);
125 }
126 }
127
128 @end