在 iOS 设备上生成的公钥在 Java 服务器上无效



我在iOS设备上生成的SecKeyRef出现问题 - 尝试在Java服务器上使用它时,会抛出异常:

无效密钥异常:EC 域参数必须在算法标识符中编码

以下是服务器代码中的代码片段:

String key = ...
byte[] byteKey =  Base64.decode(key.getBytes(StandardCharsets.UTF_8));
X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
KeyFactory kf = KeyFactory.getInstance("EC");
return kf.generatePublic(X509publicKey);

异常由kf.generatePublic(X509publicKey);引发

密钥是在 iOS 上使用SecKeyGeneratePair创建的

[keyPairAttr setObject:(__bridge id)kSecAttrKeyTypeEC forKey:(__bridge id)kSecAttrKeyType];
[keyPairAttr setObject:[NSNumber numberWithUnsignedInteger:256] forKey:(__bridge id)kSecAttrKeySizeInBits];
// Set the private key dictionary
[privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];
[privateKeyAttr setObject:self.privateTag forKey:(__bridge id)kSecAttrApplicationTag];
// Set the public key dictionary
[publicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];
[publicKeyAttr setObject:self.publicTag forKey:(__bridge id)kSecAttrApplicationTag];
// Set attributes to top level dictionary
[keyPairAttr setObject:privateKeyAttr forKey:(__bridge id)kSecPrivateKeyAttrs];
[keyPairAttr setObject:publicKeyAttr forKey:(__bridge id)kSecPublicKeyAttrs];
// Generate key pair
OSStatus sanityCheck = SecKeyGeneratePair((__bridge CFDictionaryRef)keyPairAttr, &publicKeyRef, &privateKeyRef);

密钥对创建成功。然后,我使用以下代码提取密钥的位数据

CFDataRef publicKeyBitsRef = NULL;
NSMutableDictionary *queryPublicKey = [NSMutableDictionary dictionary];
// Set the public key query dictionary.
[queryPublicKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
[queryPublicKey setObject:self.publicTag forKey:(__bridge id)kSecAttrApplicationTag];
[queryPublicKey setObject:(__bridge id)kSecAttrKeyTypeEC forKey:(__bridge id)kSecAttrKeyType];
[queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnData];
// Get the key bits.
OSStatus sanityCheck = SecItemCopyMatching((__bridge CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyBitsRef);

然后,我使用 CryptoExportImportManager 导出密钥

NSData *publicKeyIDERData = [manager exportPublicKeyToDER:keyBits keyType:(__bridge NSString*)kSecAttrKeyTypeEC keySize:256];
NSString *derKeyString = [publicKeyIDERData base64EncodedStringWithOptions:0];

根据此答案,DER 标头包含有关密钥类型和参数的信息,对于secp256r1密钥,它等效于以下数据

[
0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 
0x42, 0x00
]

这确实是在导出时添加到键头中的。

然后将derKeyString发送到后端并使用上述 Java 代码进行处理。但是,将引发异常。

相同的后端还使用以下代码处理在 Android 设备上创建的密钥

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_SIGN)
.setDigests(KeyProperties.DIGEST_SHA256)
.setAlgorithmParameterSpec(
new ECGenParameterSpec("secp256r1"))
.setUserAuthenticationRequired(true).build());
keyPairGenerator.generateKeyPair();

安卓键工作得很好。

我做错了什么?我在使用SecKeyGeneratePair创建密钥或导出公钥时是否忘记了某些内容?

我解决了这个问题,尽管我不确定为什么。

我所做的是放弃CryptoExportImportManager库,我正在手动创建密钥数据,如下所示:

unsigned char _encodedECOID[] = {
0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
0x42, 0x00
};
NSMutableData *data = [NSMutableData new];
[data appendBytes:_encodedECOID length:sizeof(_encodedECOID)];
[data appendData:keyBits]; // keyBits is od NSData type

现在 Java 服务器正确地从我的字符串创建公钥(从data编码的 base64 )。

但是,在查看了 CryptoExportImportManager 的源代码后,它从我的密钥位创建编码字符串的方式如下所示(在 Swift 中):

let curveOIDHeader: [UInt8] = [0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 
0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A,
0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 
0x42, 0x00]
let curveOIDHeaderLen: Int = 26
var data = Data(bytes: curveOIDHeader, count: curveOIDHeaderLen)
data.append(rawPublicKeyBytes)

它基本上做完全相同的事情。那么区别在哪里呢?

现在唯一想到的是标头存储方式的差异 - 在我的情况下它是一个unsigned char数组,在图书馆的情况下它是一个UInt8数组。

根据这个答案,C类型unsigned charuint8_t并不等价,它们只能保证具有相同的长度,但在字节排序方面可以不同。

虽然这个问题与 Swift 的UInt8无关(但被标记为C,其中 Objective-C 是超集),但 Swift 的 UInt8 类型的文档没有说明它与unsigned char类型的关系,这将是我能看到的唯一合理的解释。

最新更新