Skip to content

Commit

Permalink
chore: musap-react-native ios eSim fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderPostma committed Dec 10, 2024
1 parent 3d16b19 commit eea46d8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 48 deletions.
1 change: 1 addition & 0 deletions packages/kms-musap-rn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@veramo/core": "4.2.0",
"@veramo/key-manager": "4.2.0",
"@veramo/kms-local": "4.2.0",
"uint8arrays": "^3.1.1",
"text-encoding": "^0.7.0"
},
"devDependencies": {
Expand Down
89 changes: 43 additions & 46 deletions packages/kms-musap-rn/src/MusapKeyManagerSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
isRawCompressedPublicKey,
toRawCompressedHexPublicKey,
} from '@sphereon/ssi-sdk-ext.key-utils'
import * as u8a from 'uint8arrays'

export const logger = Loggers.DEFAULT.get('sphereon:musap-rn-kms')

Expand Down Expand Up @@ -195,65 +196,61 @@ export class MusapKeyManagementSystem extends AbstractKeyManagementSystem {
throw new Error('importKey is not implemented for MusapKeyManagementSystem.')
}


private decodeMusapPublicKey = (args: { publicKey: { pem: string }, keyType: TKeyType }): string => {
const { publicKey, keyType } = args;

try {
// First try the normal PEM decoding path
const pemBinary = PEMToBinary(publicKey.pem)
const { publicKey, keyType } = args

// Check if we got a string that looks like base64 (might be double encoded)
const isDoubleEncoded = pemBinary.length > 0 &&
typeof Buffer.from(pemBinary).toString() === 'string' &&
Buffer.from(pemBinary).toString().startsWith('MF');
// First try the normal PEM decoding path
const pemBinary = PEMToBinary(publicKey.pem)

if (isDoubleEncoded) {
// Handle double-encoded case
const innerBase64 = Buffer.from(pemBinary).toString()
const actualDerBytes = Buffer.from(innerBase64, 'base64')
// Check if we got a string that looks like base64 (might be double encoded)
// Convert Uint8Array to string safely
const pemString = u8a.toString(pemBinary, 'utf8')
const isDoubleEncoded = pemBinary.length > 0 &&
typeof pemString === 'string' &&
pemString.startsWith('MF')

// For double-encoded case, we know the key data starts after the header
const keyDataStart = 24
const keyData = actualDerBytes.slice(keyDataStart)
if (isDoubleEncoded) {
// Handle double-encoded case
const actualDerBytes = u8a.fromString(pemString, 'base64')

// Convert to public key hex
let publicKeyHex = Buffer.from(keyData).toString('hex')
// For double-encoded case, we know the key data starts after the header
const keyDataStart = 24
const keyData = actualDerBytes.slice(keyDataStart)

// If it's not compressed yet and doesn't start with 0x04 (uncompressed point marker), add it
if (publicKeyHex.length <= 128 && !publicKeyHex.startsWith('04')) {
publicKeyHex = '04' + publicKeyHex
}

// Ensure we have full 65 bytes for uncompressed keys
while (publicKeyHex.startsWith('04') && publicKeyHex.length < 130) {
publicKeyHex = publicKeyHex + '0'
}
// Convert to public key hex
let publicKeyHex = u8a.toString(keyData, 'hex')

// Now convert to compressed format if needed
if (publicKeyHex.startsWith('04') && publicKeyHex.length === 130) {
const xCoord = Buffer.from(publicKeyHex.slice(2, 66), 'hex')
const yCoord = Buffer.from(publicKeyHex.slice(66, 130), 'hex')
const prefix = Buffer.from([yCoord[31] % 2 === 0 ? 0x02 : 0x03])
const compressedKey = Buffer.concat([prefix, xCoord])
return compressedKey.toString('hex')
}
// If it's not compressed yet and doesn't start with 0x04 (uncompressed point marker), add it
if (publicKeyHex.length <= 128 && !publicKeyHex.startsWith('04')) {
publicKeyHex = '04' + publicKeyHex
}

return publicKeyHex
// Ensure we have full 65 bytes for uncompressed keys
while (publicKeyHex.startsWith('04') && publicKeyHex.length < 130) {
publicKeyHex = publicKeyHex + '0'
}

// Not double encoded, proceed with normal path
const publicKeyBinary = isAsn1Der(pemBinary) ? asn1DerToRawPublicKey(pemBinary, keyType) : pemBinary
return isRawCompressedPublicKey(publicKeyBinary)
? hexStringFromUint8Array(publicKeyBinary)
: toRawCompressedHexPublicKey(publicKeyBinary, keyType)
// Now convert to compressed format if needed
if (publicKeyHex.startsWith('04') && publicKeyHex.length === 130) {
const xCoord = u8a.fromString(publicKeyHex.slice(2, 66), 'hex')
const yCoord = u8a.fromString(publicKeyHex.slice(66, 130), 'hex')
const prefix = new Uint8Array([yCoord[31] % 2 === 0 ? 0x02 : 0x03])
const compressedKey = new Uint8Array(33) // 1 byte prefix + 32 bytes x coordinate
compressedKey.set(prefix, 0)
compressedKey.set(xCoord, 1)
return u8a.toString(compressedKey, 'hex')
}

} catch (error) {
console.warn('Error decoding public key:', error)
// If all else fails, try direct conversion
return publicKey.pem
return publicKeyHex
}

// Not double encoded, proceed with normal path
const publicKeyBinary = isAsn1Der(pemBinary) ? asn1DerToRawPublicKey(pemBinary, keyType) : pemBinary
return isRawCompressedPublicKey(publicKeyBinary)
? hexStringFromUint8Array(publicKeyBinary)
: toRawCompressedHexPublicKey(publicKeyBinary, keyType)
}


private asMusapKeyInfo(args: MusapKey): ManagedKeyInfo {
const { keyId, publicKey, ...metadata }: KeyMetadata = { ...args }
Expand Down
7 changes: 5 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eea46d8

Please sign in to comment.