FIRAppDistributionMachO.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <mach-o/fat.h>
  17. #import <mach-o/loader.h>
  18. #include <mach-o/arch.h>
  19. #import <CommonCrypto/CommonHMAC.h>
  20. #import "FIRAppDistributionMachO.h"
  21. #import "FIRAppDistributionMachOSlice.h"
  22. @implementation FIRAppDistributionMachO
  23. - (instancetype)initWithPath:(NSString *)path {
  24. self = [super init];
  25. if (self) {
  26. _file = [NSFileHandle fileHandleForReadingAtPath:path];
  27. [_file seekToFileOffset:0];
  28. _slices = [NSMutableArray new];
  29. }
  30. [self extractSlices];
  31. return self;
  32. }
  33. - (void)extractSlices {
  34. uint32_t magicValue;
  35. struct fat_header fheader;
  36. NSData* data = [_file readDataOfLength:sizeof(fheader)];
  37. [data getBytes:&fheader length:sizeof(fheader)];
  38. magicValue = CFSwapInt32HostToBig(fheader.magic);
  39. if (magicValue == FAT_MAGIC || magicValue == FAT_CIGAM) {
  40. uint32_t archCount = CFSwapInt32HostToBig(fheader.nfat_arch);
  41. NSUInteger archOffsets[archCount];
  42. // Gather the offsets
  43. for (uint32_t i = 0; i < archCount; i++) {
  44. struct fat_arch arch;
  45. data = [_file readDataOfLength:sizeof(arch)];
  46. [data getBytes:&arch length:sizeof(arch)];
  47. archOffsets[i] = CFSwapInt32HostToBig(arch.offset);
  48. }
  49. // Iterate the slices
  50. for (uint32_t i = 0; i < archCount; i++) {
  51. FIRAppDistributionMachOSlice* slice = [self extractSliceAtOffset:archOffsets[i]];
  52. [_slices addObject:slice];
  53. }
  54. } else {
  55. FIRAppDistributionMachOSlice* slice = [self extractSliceAtOffset:0];
  56. [_slices addObject:slice];
  57. }
  58. }
  59. -(FIRAppDistributionMachOSlice*)extractSliceAtOffset:(NSUInteger)offset {
  60. [_file seekToFileOffset:offset];
  61. struct mach_header header;
  62. NSData *data = [_file readDataOfLength:sizeof(header)];
  63. [data getBytes:&header length:sizeof(header)];
  64. uint32_t magicValue = CFSwapInt32HostToBig(header.magic);
  65. // TODO: Verify Mach-O
  66. // If binary is 64-bit, read reserved bit and discard it
  67. if (magicValue == MH_CIGAM_64) {
  68. uint32_t reserved;
  69. [_file readDataOfLength:sizeof(reserved)];
  70. }
  71. for (uint32_t i = 0; i < header.ncmds; i++) {
  72. struct load_command lc;
  73. data = [_file readDataOfLength:sizeof(lc)];
  74. [data getBytes:&lc length:sizeof(lc)];
  75. if (lc.cmd == LC_UUID) {
  76. [_file seekToFileOffset:[_file offsetInFile] - sizeof(lc)];
  77. struct uuid_command uc;
  78. data = [_file readDataOfLength:sizeof(uc)];
  79. [data getBytes:&uc length:sizeof(uc)];
  80. NSUUID* uuid = [[NSUUID alloc] initWithUUIDBytes:uc.uuid];
  81. const NXArchInfo* arch = NXGetArchInfoFromCpuType(header.cputype, header.cpusubtype);
  82. NSLog(@"Got Architecture %s UUID: %@", arch->name, [uuid UUIDString]);
  83. return [[FIRAppDistributionMachOSlice alloc]initWithArch:arch uuid:uuid];
  84. } else {
  85. [_file seekToFileOffset:[_file offsetInFile] + lc.cmdsize - sizeof(lc)];
  86. }
  87. }
  88. return nil;
  89. }
  90. -(NSString*)codeHash {
  91. NSMutableString* prehashedString = [NSMutableString new];
  92. NSArray* sortedSlices = [_slices sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  93. return [[obj1 architectureName] compare:[obj2 architectureName]];
  94. }];
  95. for (FIRAppDistributionMachOSlice* slice in sortedSlices) {
  96. [prehashedString appendString:[slice uuidString]];
  97. }
  98. return [self sha1:prehashedString];;
  99. }
  100. -(NSString*)sha1:(NSString*)prehashedString {
  101. NSData *data = [prehashedString dataUsingEncoding:NSUTF8StringEncoding];
  102. uint8_t digest[CC_SHA1_DIGEST_LENGTH];
  103. CC_SHA1(data.bytes, (int)data.length, digest);
  104. NSMutableString *hashedString = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
  105. for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
  106. [hashedString appendFormat:@"%02x", digest[i]];
  107. return hashedString;
  108. }
  109. @end