RSA公钥在iOS/Swift中创建,并导出为Java中无法识别的base64

xkftehaa  于 2023-11-16  发布在  Swift
关注(0)|答案(3)|浏览(188)
  • TL;DR:在iOS中生成并存储在密钥链中的RSA公钥,导出为base64并发送到java后端,无法识别。*

我正在iOS应用中实现聊天加密功能,我使用对称+非对称密钥来处理它。
在后端,我使用用户的公钥对用于加密和解密消息的对称密钥进行加密,而不对细节进行过多的讨论。
我创建了两个框架,分别在Swift和Java(后端)中处理密钥生成,加密,解密等,我也对它们进行了测试,所以我100%一切都按预期工作。
然而,后端似乎无法识别从iOS传递的公钥的格式。使用RSA双方,这是我在Swift中用于生成密钥的代码:

// private key parameters
static let privateKeyParams: [String : Any] = [
        kSecAttrIsPermanent as String: true,
        kSecAttrApplicationTag as String: "..." // I have a proper unique tag here
]

// public  key parameters
static let publicKeyParams: [String : Any] = [
        kSecAttrIsPermanent as String: true,
        kSecAttrApplicationTag as String: "..." // I have a proper unique tag here
]

// global parameters for our key generation
static let keyCreationParameters: [String : Any] = [
        kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
        kSecAttrKeySizeInBits as String: 2048,
        kSecPublicKeyAttrs as String: publicKeyParams,
        kSecPrivateKeyAttrs as String: privateKeyParams
]

...

var publicKey, privateKey: SecKey?
let status = SecKeyGeneratePair(Constants.keyCreationParameters as CFDictionary, &publicKey, &privateKey)

字符串
我使用镜面代码从钥匙链中读取钥匙。
这是我用来将公钥导出为base64字符串的代码:

extension SecKey {
  func asBase64() throws -> String {
    var dataPtr: CFTypeRef?
    let query: [String:Any] = [
      kSecClass as String: kSecClassKey,
      kSecAttrApplicationTag as String: "...", // Same unique tag here
      kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
      kSecReturnData as String: kCFBooleanTrue
    ]
    let result = SecItemCopyMatching(query as CFDictionary, &dataPtr)

    switch (result, dataPtr) {
    case (errSecSuccess, .some(let data)):
      // convert to Base64 string
      let base64PublicKey = data.base64EncodedString(options: [])
      return base64PublicKey
    default:
      throw CryptoError.keyConversionError
    }
  }
}


在后端级别,我使用以下Java代码将base64字符串转换为公钥:

public PublicKey publicKeyFrom(String data) throws NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] publicBytes = Base64.decodeBase64(data);
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    return keyFactory.generatePublic(keySpec);
}


但这在最后一行失败了,除了这个例外:

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: algid parse error, not a sequence


做了一些手动调试,我注意到公钥的格式是不同的-当我在iOS中生成一个密钥,然后导出为base 64时,它看起来像这样:

MIIBCgKCAQEA4M/bRDdH0f6qFIXxOg13RHka+g4Yv8u9PpPp1IR6pSwrM1aq8B6cyKRwnLe/MOkvODvDfJzvGXGQ01zSTxYWAW1B4uc/NCEemCmZqMosSB/VUJdNxxWtt2hJxpz06hAawqV+6HmweAB2dUn9tDEsQLsNHdwYouOKpyRZGimcF9qRFn1RjR0Q54sUh1tQAj/EwmgY2S2bI5TqtZnZw7X7Waji7wWi6Gz88IkuzLAzB9VBNDeV1cfJFiWsZ/MIixSvhpW3dMNCrJShvBouIG8nS+vykBlbFVRGy3gJr8+OcmIq5vuHVhqrWwHNOs+WR87K/qTFO/CB7MiyiIV1b1x5DQIDAQAB


总共360个字符,而在Java中做同样的事情(仍然使用RSA)就像:

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAAnWO4BXUGP0qM3Op36YXkWNxb4I2pPZuZ7jJtfUO7v+IO1mq43WzNaxLqqLPkTnMrv2ACRDK55vin+leQlL1z0LzVxjtZ9F6pajQo1r7PqBlL5N8bzBFKpagEf0QfyHPw0/0kG9DMnvQ+Im881QyN2zdl33wp5Fi+jRT7cunFQIDAQAB


长度为216个字符。
我无法找出问题所在--显然,如果iOS用不同的键处理键,并且需要特殊的处理才能与其他人交谈,我不会感到惊讶。
你知道吗?

vq8itlhq

vq8itlhq1#

我们在将iOS应用程序连接到Java后端时遇到了完全相同的问题。pedrofb 提到的CryptoExportImportManager也帮了我们的忙,这很棒。但是,CryptoExportImportManager类中的代码有点复杂,可能很难维护。这是因为在DER编码中添加新组件时使用了自顶向下的方法。因此,长度字段中包含的数字必须提前计算(即在定义长度所适用的内容之前)。因此,我创建了一个新类,现在我们使用它来转换RSA公钥的DER编码:

class RSAKeyEncoding: NSObject {

  // ASN.1 identifiers
  private let bitStringIdentifier: UInt8 = 0x03
  private let sequenceIdentifier: UInt8 = 0x30

  // ASN.1 AlgorithmIdentfier for RSA encryption: OID 1 2 840 113549 1 1 1 and NULL
  private let algorithmIdentifierForRSAEncryption: [UInt8] = [0x30, 0x0d, 0x06,
    0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00]

  /// Converts the DER encoding of an RSA public key that is either fetched from the
  /// keychain (e.g. by using `SecItemCopyMatching(_:_:)`) or retrieved in another way
  /// (e.g. by using `SecKeyCopyExternalRepresentation(_:_:)`), to a format typically
  /// used by tools and programming languages outside the Apple ecosystem (such as
  /// OpenSSL, Java, PHP and Perl). The DER encoding of an RSA public key created by
  /// iOS is represented with the ASN.1 RSAPublicKey type as defined by PKCS #1.
  /// However, many systems outside the Apple ecosystem expect the DER encoding of a
  /// key to be represented with the ASN.1 SubjectPublicKeyInfo type as defined by
  /// X.509. The two types are related in a way that if the SubjectPublicKeyInfo’s
  /// algorithm field contains the rsaEncryption object identifier as defined by
  /// PKCS #1, the subjectPublicKey field shall contain the DER encoding of an
  /// RSAPublicKey type.
  ///
  /// - Parameter rsaPublicKeyData: A data object containing the DER encoding of an
  ///     RSA public key, which is represented with the ASN.1 RSAPublicKey type.
  /// - Returns: A data object containing the DER encoding of an RSA public key, which
  ///     is represented with the ASN.1 SubjectPublicKeyInfo type.
  func convertToX509EncodedKey(_ rsaPublicKeyData: Data) -> Data {
    var derEncodedKeyBytes = [UInt8](rsaPublicKeyData)

    // Insert ASN.1 BIT STRING bytes at the beginning of the array
    derEncodedKeyBytes.insert(0x00, at: 0)
    derEncodedKeyBytes.insert(contentsOf: lengthField(of: derEncodedKeyBytes), at: 0)
    derEncodedKeyBytes.insert(bitStringIdentifier, at: 0)

    // Insert ASN.1 AlgorithmIdentifier bytes at the beginning of the array
    derEncodedKeyBytes.insert(contentsOf: algorithmIdentifierForRSAEncryption, at: 0)

    // Insert ASN.1 SEQUENCE bytes at the beginning of the array
    derEncodedKeyBytes.insert(contentsOf: lengthField(of: derEncodedKeyBytes), at: 0)
    derEncodedKeyBytes.insert(sequenceIdentifier, at: 0)

    return Data(derEncodedKeyBytes)
  }

  private func lengthField(of valueField: [UInt8]) -> [UInt8] {
    var length = valueField.count

    if length < 128 {
      return [ UInt8(length) ]
    }

    // Number of bytes needed to encode the length
    let lengthBytesCount = Int((log2(Double(length)) / 8) + 1)

    // First byte encodes the number of remaining bytes in this field
    let firstLengthFieldByte = UInt8(128 + lengthBytesCount)

    var lengthField: [UInt8] = []
    for _ in 0..<lengthBytesCount {
      // Take the last 8 bits of length
      let lengthByte = UInt8(length & 0xff)
      // Insert them at the beginning of the array
      lengthField.insert(lengthByte, at: 0)
      // Delete the last 8 bits of length
      length = length >> 8
    }

    // Insert firstLengthFieldByte at the beginning of the array
    lengthField.insert(firstLengthFieldByte, at: 0)

    return lengthField
  }
}

字符串

用法

您可以在函数asBase64()中使用这个类别,如下所示:

extension SecKey {
  func asBase64() throws -> String {
    var dataPtr: CFTypeRef?
    let query: [String:Any] = [
      kSecClass as String: kSecClassKey,
      kSecAttrApplicationTag as String: "...", // Same unique tag here
      kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
      kSecReturnData as String: kCFBooleanTrue
    ]
    let result = SecItemCopyMatching(query as CFDictionary, &dataPtr)

    switch (result, dataPtr) {
    case (errSecSuccess, .some(let data)):

      // convert to X509 encoded key
      let convertedData = RSAKeyEncoding().convertToX509EncodedKey(data)

      // convert to Base64 string
      let base64PublicKey = convertedData.base64EncodedString(options: [])
      return base64PublicKey
    default:
      throw CryptoError.keyConversionError
    }
  }
}

更新-其他问题

在使用了上面的类一段时间之后,我们偶然发现了另一个问题。有时候,从钥匙串中获取的公钥似乎是无效的,因为出于某种原因,它的大小已经增大了。这种行为与问题中描述的发现相匹配(尽管在我们的例子中Base64编码的密钥已经增长到392个字符而不是360个字符)。不幸的是,我们没有找到这种奇怪行为的确切原因,但我们找到了两个解决方案。第一个解决方案是在定义查询时指定kSecAttrKeySizeInBits沿着kSecAttrEffectiveKeySize,如下面的代码片段所示:

let keySize = ... // Key size specified when storing the key, for example: 2048

let query: [String: Any] = [
    kSecAttrKeySizeInBits as String: keySize,
    kSecAttrEffectiveKeySize as String: keySize,
    ... // More attributes
]

var dataPtr: CFTypeRef?

let result = SecItemCopyMatching(query as CFDictionary, &dataPtr)


第二个解决方案是在添加具有相同标记的新钥匙之前,始终从钥匙串(如果有)中删除旧钥匙。

更新-替代解决方案

我在GitHub上发布了this project,它可以作为上述类的替代。

引用

A Layman’s Guide to a Subset of ASN.1, BER, and DER
RFC 5280(X.509 v3)的扩展名
RFC 8017(PKCS #1版本2.2)
在创建lengthField(...)函数时,我发现here的一些代码给了我灵感。

zzlelutf

zzlelutf2#

Java需要一个以DER格式编码的公钥。不幸的是,iOS不支持这种标准格式,需要额外的转换(我不知道这是否会在最新版本的Swift中有所改进)
您可以使用CryptoExportImportManager转换密钥

func exportPublicKeyToDER(keyId:String) -> NSData?{

    let publicKey = loadKeyStringFromKeyChainAsNSData(PUBLIC_KEY + keyId)
    let keyType = kSecAttrKeyTypeRSA
    let keySize = 2048
    let exportImportManager = CryptoExportImportManager()
    if let exportableDERKey = exportImportManager.exportPublicKeyToDER(publicKey, keyType: keyType as String, keySize: keySize) {
        return exportableDERKey
    } else {
        return nil
    }
}

字符串

jdg4fx2g

jdg4fx2g3#

如果您使用的是2048位RSA密钥,导出X509 PEM公钥就像在iOS生成的PKCS#1 PEM公钥前添加字符串MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A一样简单。下面是我用J2EE-C编写的代码,它生成了PKCS#1(RSA PUBLIC KEY)和X509(PUBLIC KEY)版本的PEM文件:

NSString* alias = @"MyKeyAlias";
NSData* tag = [alias dataUsingEncoding:NSUTF8StringEncoding];
// delete any existing key pair with the same tag name
NSDictionary *deleteQuery = @{
  (id)kSecClass: (id)kSecClassKey,
  (id)kSecAttrApplicationTag: tag,
  (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA
};
SecItemDelete((__bridge CFDictionaryRef)deleteQuery);
// create a new key pair
NSDictionary* attributes = @{
  (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
  (id)kSecAttrKeySizeInBits: @2048,
  (id)kSecPrivateKeyAttrs: @{(id)kSecAttrIsPermanent: @YES, (id)kSecAttrApplicationTag: tag}
};
CFErrorRef error = NULL;
SecKeyRef privateKey = SecKeyCreateRandomKey((__bridge CFDictionaryRef)attributes, &error);
if (!privateKey) {
  NSError *err = CFBridgingRelease(error);
  NSString *message = [NSString stringWithFormat:@"Cannot generate key pair: %@", [err localizedDescription]];
  NSLog(message);
} else {
  SecKeyRef publicKey = SecKeyCopyPublicKey(privateKey);
  if (!publicKey)
    NSLog(@"Cannot export public key");
  else {
    NSData* keyData = (NSData*)CFBridgingRelease(SecKeyCopyExternalRepresentation(publicKey, &error));
    if (!keyData)
      NSLog(@"Failed to export public key");
    else {
      NSLog(@"-----BEGIN RSA PUBLIC KEY-----\n%@\n-----END RSA PUBLIC KEY-----\n", [keyData base64EncodedStringWithOptions:0]];
      NSString *pem_x509 = [NSString stringWithFormat:@"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A%@", [keyData base64EncodedStringWithOptions:0]];
      NSLog(@"-----BEGIN PUBLIC KEY-----\n%@\n-----END PUBLIC KEY-----\n", messageAsString:pem_x509];
    }
  }
  CFRelease(privateKey);
}

字符串

相关问题