I'm trying to generate a JWK from an EC public key that I want to send to my server. The issue I'm running into is that the base64 URL Encoded string representation of the x and y coordinates from the external representation of the EC public key seems to be invalid on the server and I get an error saying that the points are not on the curve.
Here's what I'm doing to generate the base64 URL encoded string from the x and y coords:
The byte order on iOS seems to be little-endian (byteOrder above is 1 == CFByteOrderLittleEndian) and the JWK RFC 7517 Appendix A says it expects the big-endian values for x and y. So, I tried swapping the bytes to create a big-endian representation of the data. But, neither works.
I'd appreciate any insight or help with this.
Here's what I'm doing to generate the base64 URL encoded string from the x and y coords:
Code Block objc + (NSArray<NSString *> *)getBase64EncodedCoordinatesFromECPublicKey:(SecKeyRef)publicKey error:(out NSError **)error{ CFErrorRef copyPublicKeyError = NULL; NSData* keyData = (NSData*)CFBridgingRelease( SecKeyCopyExternalRepresentation(publicKey, ©PublicKeyError) ); if (!keyData) { NSError *err = CFBridgingRelease(copyPublicKeyError); NSLog(@"%@", err); return nil; } NSString *xbytes; NSString *ybytes; CFByteOrder byteOrder = CFByteOrderGetCurrent(); NSLog(@"%ld",(long)byteOrder); NSData *xData = [keyData subdataWithRange:NSMakeRange(1, 32)]; NSData *xDataRev = [self reverseData:xData]; NSString *xEncoded = [OIDTokenUtilities encodeBase64urlNoPadding:xDataRev]; NSData *yData = [keyData subdataWithRange:NSMakeRange(33, 32)]; NSData *yDataRev = [self reverseData:yData]; NSString *yEncoded = [OIDTokenUtilities encodeBase64urlNoPadding:yDataRev]; xbytes = [OIDTokenUtilities encodeBase64urlNoPadding:xData]; ybytes = [OIDTokenUtilities encodeBase64urlNoPadding:yData]; NSArray *coordinates = @[xbytes, ybytes]; return coordinates; }
The byte order on iOS seems to be little-endian (byteOrder above is 1 == CFByteOrderLittleEndian) and the JWK RFC 7517 Appendix A says it expects the big-endian values for x and y. So, I tried swapping the bytes to create a big-endian representation of the data. But, neither works.
I'd appreciate any insight or help with this.