From 2eacf7b780a56919d5ec48afd5f1c6032450943d Mon Sep 17 00:00:00 2001
From: Jorge Canizales <jcanizales@google.com>
Date: Sun, 9 Aug 2015 15:18:23 -0700
Subject: [PATCH] Allow UTF8 in comments of root certificates files

---
 .../GRPCClient/private/GRPCSecureChannel.m          | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/objective-c/GRPCClient/private/GRPCSecureChannel.m b/src/objective-c/GRPCClient/private/GRPCSecureChannel.m
index 9b4b6768f8..0a54804bb2 100644
--- a/src/objective-c/GRPCClient/private/GRPCSecureChannel.m
+++ b/src/objective-c/GRPCClient/private/GRPCSecureChannel.m
@@ -38,15 +38,18 @@
 // Returns NULL if the file at path couldn't be read. In that case, if errorPtr isn't NULL,
 // *errorPtr will be an object describing what went wrong.
 static grpc_credentials *CertificatesAtPath(NSString *path, NSError **errorPtr) {
-  NSString *certsContent = [NSString stringWithContentsOfFile:path
-                                                     encoding:NSASCIIStringEncoding
+  // Files in PEM format can have non-ASCII characters in their comments (e.g. for the name of the
+  // issuer). Load them as UTF8 and produce an ASCII equivalent.
+  NSString *contentInUTF8 = [NSString stringWithContentsOfFile:path
+                                                     encoding:NSUTF8StringEncoding
                                                         error:errorPtr];
-  if (!certsContent) {
+  NSData *contentInASCII = [contentInUTF8 dataUsingEncoding:NSASCIIStringEncoding
+                                       allowLossyConversion:YES];
+  if (!contentInASCII.bytes) {
     // Passing NULL to grpc_ssl_credentials_create produces behavior we don't want, so return.
     return NULL;
   }
-  const char * asCString = [certsContent cStringUsingEncoding:NSASCIIStringEncoding];
-  return grpc_ssl_credentials_create(asCString, NULL);
+  return grpc_ssl_credentials_create(contentInASCII.bytes, NULL);
 }
 
 @implementation GRPCSecureChannel
-- 
GitLab