Skip to content

Commit

Permalink
chore: check vct scheme to choose hardware/software key
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
Berend Sliedrecht committed Aug 13, 2024
1 parent 3b7c7d9 commit 49c888b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 50 deletions.
11 changes: 11 additions & 0 deletions apps/funke/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ const animoFunkeRelyingPartyCertificate =
'MIIBAzCBq6ADAgECAhArxq0w60RTDK4WY9HzgcvBMAoGCCqGSM49BAMCMAAwIBcNNzAwMTAxMDAwMDAwWhgPMjI4NjExMjAxNzQ2NDBaMAAwOTATBgcqhkjOPQIBBggqhkjOPQMBBwMiAALcD1XzKepFxWMAOqV+ln1fybBt7DRO5CV0f9A6mRp2xaMlMCMwIQYDVR0RBBowGIYWaHR0cHM6Ly9mdW5rZS5hbmltby5pZDAKBggqhkjOPQQDAgNHADBEAiAfvGG6sqrvzIMWYpJB5VLloo9f51loYXSkKxJIOztlNwIgLLSvEl0Dmp5vtj2buZ2nXQ2RBKxiLbc5eYGeMeoUnjk='

export const trustedX509Certificates = [bdrPidIssuerCertificate, animoFunkeRelyingPartyCertificate]

// https://gitlab.opencode.de/bmi/eudi-wallet/eidas-2.0-architekturkonzept/-/blob/main/architecture-proposal.md#pid-contents
const sdJwtVcVcts = ['https://example.bmi.bund.de/credential/pid/1.0', 'urn:eu.europa.ec.eudi:pid:1']

// TODO
const msoMdocNamespaces = ['org.iso.18013.5.1.mDL']

export const pidSchemes = {
sdJwtVcVcts,
msoMdocNamespaces,
}
2 changes: 1 addition & 1 deletion apps/funke/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"prebuild": "APP_VARIANT=development expo prebuild --no-install"
},
"dependencies": {
"@animo-id/expo-ausweis-sdk": "0.0.1-alpha.5",
"@animo-id/expo-ausweis-sdk": "0.0.1-alpha.6",
"@animo-id/expo-secure-environment": "0.1.0-alpha.1",
"@credo-ts/core": "*",
"@expo-google-fonts/open-sans": "^0.2.3",
Expand Down
17 changes: 14 additions & 3 deletions apps/funke/use-cases/ReceivePidUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AppAgent } from '@/agent'
import { pidSchemes } from '@/constants'
import { AusweisAuthFlow } from '@animo-id/expo-ausweis-sdk'
import {
type OpenId4VciRequestTokenResponse,
Expand Down Expand Up @@ -114,6 +115,7 @@ export class ReceivePidUseCase {
resolvedCredentialOffer: this.resolvedCredentialOffer,
credentialConfigurationIdToRequest,
clientId: ReceivePidUseCase.CLIENT_ID,
pidSchemes,
})

// TODO: add error handling everywhere to set state to error
Expand All @@ -129,7 +131,10 @@ export class ReceivePidUseCase {
}

private async acquireAccessToken(refreshUrl: string) {
this.assertState({ expectedState: 'id-card-auth', newState: 'acquire-access-token' })
this.assertState({
expectedState: 'id-card-auth',
newState: 'acquire-access-token',
})

try {
const authorizationCodeResponse = await fetch(refreshUrl)
Expand All @@ -154,7 +159,10 @@ export class ReceivePidUseCase {
agent: this.agent,
})

this.assertState({ expectedState: 'acquire-access-token', newState: 'retrieve-credential' })
this.assertState({
expectedState: 'acquire-access-token',
newState: 'retrieve-credential',
})
} catch (error) {
this.handleError()
}
Expand All @@ -163,7 +171,10 @@ export class ReceivePidUseCase {
private assertState({
expectedState,
newState,
}: { expectedState: ReceivePidUseCase['currentState']; newState?: ReceivePidUseCase['currentState'] }) {
}: {
expectedState: ReceivePidUseCase['currentState']
newState?: ReceivePidUseCase['currentState']
}) {
if (this.currentState !== expectedState) {
throw new Error(`Expected state to be ${expectedState}. Found ${this.currentState}`)
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"@credo-ts/openid4vc": "0.5.10-alpha-20240805102402",
"@credo-ts/question-answer": "0.5.10-alpha-20240805102402",
"@credo-ts/react-hooks": "0.6.1",
"@credo-ts/react-native": "0.5.10-alpha-20240805102402"
"@credo-ts/react-native": "0.5.10-alpha-20240805102402",

"@animo-id/expo-secure-environment": "0.1.0-alpha.1"
},
"patchedDependencies": {
"@credo-ts/[email protected]": "patches/@[email protected]",
Expand Down
43 changes: 23 additions & 20 deletions packages/agent/src/invitation/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@ export const receiveCredentialFromOpenId4VciOffer = async ({
credentialConfigurationIdToRequest,
accessToken,
clientId,
pidSchemes,
}: {
agent: EitherAgent
resolvedCredentialOffer: OpenId4VciResolvedCredentialOffer
credentialConfigurationIdToRequest?: string
clientId?: string
pidSchemes?: { sdJwtVcVcts: Array<string>; msoMdocNamespaces: Array<string> }

// TODO: cNonce should maybe be provided separately (multiple calls can have different c_nonce values)
accessToken: OpenId4VciRequestTokenResponse
Expand Down Expand Up @@ -182,6 +184,7 @@ export const receiveCredentialFromOpenId4VciOffer = async ({
supportsAllDidMethods,
supportsJwk,
credentialFormat,
supportedCredentialId,
}) => {
// First, we try to pick a did method
// Prefer did:jwk, otherwise use did:key, otherwise use undefined
Expand All @@ -199,26 +202,26 @@ export const receiveCredentialFromOpenId4VciOffer = async ({
didMethod = 'key'
}

let key: Key | undefined = undefined

// For P-256 we first try secure enclave
if (keyType === KeyType.P256) {
key = await agent.wallet
.createKey({
keyType,
keyBackend: KeyBackend.SecureElement,
})
.catch((e) => {
agent.config.logger.warn('Could not create a key in the secure element', e as Record<string, unknown>)
return agent.wallet.createKey({
keyType,
})
})
} else {
key = await agent.wallet.createKey({
keyType,
})
}
const offeredCredentialConfiguration = supportedCredentialId
? resolvedCredentialOffer.offeredCredentialConfigurations[supportedCredentialId]
: undefined

const shouldKeyBeHardwareBackedForMsoMdoc = false
// offeredCredentialConfiguration?.format === "mso_mdoc" &&
// pidSchemes?.msoMdocNamespaces.includes(
// offeredCredentialConfiguration.namespace
// );
const shouldKeyBeHardwareBackedForSdJwtVc =
offeredCredentialConfiguration?.format === 'vc+sd-jwt' &&
pidSchemes?.sdJwtVcVcts.includes(offeredCredentialConfiguration.vct)

// TODO: add mso-mdoc config from above
const shouldKeyBeHardwareBacked = shouldKeyBeHardwareBackedForSdJwtVc ?? shouldKeyBeHardwareBackedForMsoMdoc

const key = await agent.wallet.createKey({
keyType,
keyBackend: shouldKeyBeHardwareBacked ? KeyBackend.SecureElement : KeyBackend.Software,
})

if (didMethod) {
const didResult = await agent.dids.create<JwkDidCreateOptions | KeyDidCreateOptions>({
Expand Down
35 changes: 10 additions & 25 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 49c888b

Please sign in to comment.