从私人SecKeyRef获取公钥

时间:2014-06-28 21:17:09

标签: c macos cryptography security.framework

如果使用来自RSA私钥的SecKeyRef加载SecItemImport,是否有办法仅为公钥组件获取或创建SecKeyRef?在OpenSSL中,这可以通过将模数和公共指数复制到新结构来完成,但SecKeyRef是不透明的,并且我无法找到执行此操作的函数。

2 个答案:

答案 0 :(得分:1)

从macOS 10.12,iOS / tvOS 10和watchOS 3开始,现在存在函数SecKeyCopyPublicKey

答案 1 :(得分:0)

老问题,但因为我今天和它斗争,我找到了办法。假设您有使用Java(X509证书公钥)创建的base64编码的公共RSA密钥(它不是证书,它不是der,pem,...),您可以用这种方式创建SecKeyRef

- (NSData *)stripPublicKeyHeader2:(NSData *)keyBits {
  // Skip ASN.1 public key header
  if (keyBits == nil) {
    return nil;
  }

  unsigned int len = [keyBits length];
  if (!len) {
    return nil;
  }

  unsigned char *c_key = (unsigned char *)[keyBits bytes];
  unsigned int  idx    = 0;

  if (c_key[idx++] != 0x30) {
    return nil;
  }

  if (c_key[idx] > 0x80) {
    idx += c_key[idx] - 0x80 + 1;
  }
  else {
    idx++;
  }

  if (idx >= len) {
    return nil;
  }

  if (c_key[idx] != 0x30) {
    return nil;
  }

  idx += 15;

  if (idx >= len - 2) {
    return nil;
  }

  if (c_key[idx++] != 0x03) {
    return nil;
  }

  if (c_key[idx] > 0x80) {
    idx += c_key[idx] - 0x80 + 1;
  }
  else {
    idx++;
  }

  if (idx >= len) {
    return nil;
  }

  if (c_key[idx++] != 0x00) {
    return nil;
  }

  if (idx >= len) {
    return nil;
  }

  // Now make a new NSData from this buffer
  return([NSData dataWithBytes:&c_key[idx] length:len - idx]);
}

- (SecKeyRef)publicKey:(NSData *)d_key withTag:(NSString *)tag
{
  NSData *d_tag = [NSData dataWithBytes:[tag UTF8String] length:[tag length]];

  NSDictionary *saveDict = @{
                             (__bridge id) kSecClass : (__bridge id) kSecClassKey,
                             (__bridge id) kSecAttrKeyType : (__bridge id) kSecAttrKeyTypeRSA,
                             (__bridge id) kSecAttrApplicationTag : d_tag,
                             (__bridge id) kSecAttrKeyClass : (__bridge id) kSecAttrKeyClassPublic,
                             (__bridge id) kSecValueData : d_key
                             };

  OSStatus secStatus = SecItemAdd((__bridge CFDictionaryRef)saveDict, NULL );
  if (secStatus == errSecDuplicateItem ) {
    SecItemDelete((__bridge CFDictionaryRef)saveDict);
    secStatus = SecItemAdd((__bridge CFDictionaryRef)saveDict, NULL );
  }

  if ( secStatus != noErr ) {
    return NULL;
  }

  NSDictionary *queryDict = @{
                              (__bridge id) kSecClass : (__bridge id) kSecClassKey,
                              (__bridge id) kSecAttrKeyType : (__bridge id) kSecAttrKeyTypeRSA,
                              (__bridge id) kSecAttrApplicationTag : tag,
                              (__bridge id) kSecAttrKeyClass : (__bridge id)kSecAttrKeyClassPublic,
                              (__bridge id) kSecReturnRef : (__bridge id) kCFBooleanTrue
                              };

  // Now fetch the SecKeyRef version of the key
  SecKeyRef keyRef = nil;
  secStatus = SecItemCopyMatching((__bridge CFDictionaryRef)queryDict,
                                  (CFTypeRef *)&keyRef);

  if ( secStatus != noErr ) {
    return NULL;
  }

  return keyRef;
}

NSData *keyData = [[NSData alloc] initWithBase64EncodedString:@"base64encoded X509 private key"
                                                              options:0];
certificateData = [self stripPublicKeyHeader2:keyData];
SecKeyRef key = [self publicKey:certificateData withTag:[[NSUUID UUID] UUIDString]];

有点尴尬,不是我的代码,谷歌搜索一整天,许多碎片粘在一起。必须清理它,...把它当作从互联网编译的黑客。

针对代码在Java中的服务器进行测试,这是如何执行此操作的唯一方法。我找到的唯一方法。也许还有其他方法,但只有这一个对我有用,我可以使用这个RSA公钥加密(我们的服务器代码(Java)可以解密它。它只能在没有填充(不推荐)或{{{ 1}}在iOS端填充,在Java端使用kSecPaddingOAEP