Açıklama Yok

FIRInstanceIDKeyPairUtilities.m 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "FIRInstanceIDKeyPairUtilities.h"
  17. #import <CommonCrypto/CommonDigest.h>
  18. #import "FIRInstanceIDKeyPair.h"
  19. #import "FIRInstanceIDLogger.h"
  20. #import "FIRInstanceIDStringEncoding.h"
  21. NSString *FIRInstanceIDWebSafeBase64(NSData *data) {
  22. // Websafe encoding with no padding.
  23. FIRInstanceIDStringEncoding *encoding =
  24. [FIRInstanceIDStringEncoding rfc4648Base64WebsafeStringEncoding];
  25. [encoding setDoPad:NO];
  26. return [encoding encode:data];
  27. }
  28. NSData *FIRInstanceIDSHA1(NSData *data) {
  29. unsigned int outputLength = CC_SHA1_DIGEST_LENGTH;
  30. unsigned char output[outputLength];
  31. unsigned int length = (unsigned int)[data length];
  32. CC_SHA1(data.bytes, length, output);
  33. return [NSMutableData dataWithBytes:output length:outputLength];
  34. }
  35. NSDictionary *FIRInstanceIDKeyPairQuery(NSString *tag, BOOL addReturnAttr, BOOL returnData) {
  36. NSMutableDictionary *queryKey = [NSMutableDictionary dictionary];
  37. NSData *tagData = [tag dataUsingEncoding:NSUTF8StringEncoding];
  38. queryKey[(__bridge id)kSecClass] = (__bridge id)kSecClassKey;
  39. queryKey[(__bridge id)kSecAttrApplicationTag] = tagData;
  40. queryKey[(__bridge id)kSecAttrKeyType] = (__bridge id)kSecAttrKeyTypeRSA;
  41. if (addReturnAttr) {
  42. if (returnData) {
  43. queryKey[(__bridge id)kSecReturnData] = @(YES);
  44. } else {
  45. queryKey[(__bridge id)kSecReturnRef] = @(YES);
  46. }
  47. }
  48. return queryKey;
  49. }
  50. NSString *FIRInstanceIDAppIdentity(FIRInstanceIDKeyPair *keyPair) {
  51. // An Instance-ID is a 64 bit (8 byte) integer with a fixed 4-bit header of 0111 (=^ 0x7).
  52. // The variable 60 bits are obtained by truncating the SHA1 of the app-instance's public key.
  53. SecKeyRef publicKeyRef = [keyPair publicKey];
  54. if (!publicKeyRef) {
  55. FIRInstanceIDLoggerError(kFIRInstanceIDMessageCodeKeyPair002,
  56. @"Unable to create a valid asymmetric crypto key");
  57. return nil;
  58. }
  59. NSData *publicKeyData = keyPair.publicKeyData;
  60. NSData *publicKeySHA1 = FIRInstanceIDSHA1(publicKeyData);
  61. const uint8_t *bytes = publicKeySHA1.bytes;
  62. NSMutableData *identityData = [NSMutableData dataWithData:publicKeySHA1];
  63. uint8_t b0 = bytes[0];
  64. // Take the first byte and make the initial four 7 by initially making the initial 4 bits 0
  65. // and then adding 0x70 to it.
  66. b0 = 0x70 + (0xF & b0);
  67. // failsafe should give you back b0 itself
  68. b0 = (b0 & 0xFF);
  69. [identityData replaceBytesInRange:NSMakeRange(0, 1) withBytes:&b0];
  70. NSData *data = [identityData subdataWithRange:NSMakeRange(0, 8 * sizeof(Byte))];
  71. return FIRInstanceIDWebSafeBase64(data);
  72. }