Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: x509 chain validation #4

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @credo-ts/core should be a peer dependency, as we will depend on this library in core.

Even better would be to not depend on Credo in this library (as it seems lower level)

Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
"@peculiar/asn1-ecc": "^2.3.8",
"@peculiar/asn1-schema": "^2.3.8",
"@peculiar/asn1-x509": "^2.3.8",
"@peculiar/webcrypto": "^1.5.0",
"webcrypto-core": "^1.8.0"
},
"devDependencies": {
"@biomejs/biome": "1.8.1",
"@hyperledger/aries-askar-nodejs": "^0.2.1",
"@peculiar/webcrypto": "^1.5.0",
"@peculiar/x509": "^1.11.0",
"@types/node": "^20.14.2",
"release-it": "^17.3.0",
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

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

18 changes: 14 additions & 4 deletions src/EcdsaProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { ecdsaWithSHA256 } from '@peculiar/asn1-ecc'
import * as core from 'webcrypto-core'
import {
askarExportKeyToJwk,
askarKeyFromSecretBytes,
askarKeyFromPublicBytes,
askarKeyGenerate,
askarKeyGetPublicBytes,
askarKeySign,
askarKeyVerify,
askarKeyFromJwk,
askarKeyFromPublicBytes,
} from './askar'
import type {
CryptoKeyPair,
Expand Down Expand Up @@ -81,7 +80,7 @@ export class EcdsaProvider extends core.EcdsaProvider {
public async onImportKey(
format: KeyFormat,
keyData: JsonWebKey | ArrayBuffer,
_algorithm: EcKeyImportParams,
algorithm: EcKeyImportParams,
extractable: boolean,
keyUsages: KeyUsage[]
): Promise<core.CryptoKey> {
Expand All @@ -96,8 +95,19 @@ export class EcdsaProvider extends core.EcdsaProvider {
keyUsages,
keyData: keyData as JsonWebKey,
})
case 'spki': {
const keyInfo = AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), core.asn1.PublicKeyInfo)

return askarKeyFromPublicBytes({
format,
keyData: new Uint8Array(keyInfo.publicKey),
keyUsages,
extractable,
algorithm,
})
}
default:
throw new core.OperationError("Only format 'jwt' is supported for importing keys")
throw new core.OperationError(`Only format 'jwt' is supported for importing keys. Received: ${format}`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new core.OperationError(`Only format 'jwt' is supported for importing keys. Received: ${format}`)
throw new core.OperationError(`Only format 'jwk' and 'spki' is supported for importing keys. Received: ${format}`)

}
}
}
6 changes: 4 additions & 2 deletions src/askar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CryptoBox, Jwk, Key, KeyAlgs } from '@hyperledger/aries-askar-shared'
import type * as core from 'webcrypto-core'
import { getCryptoKey, setCryptoKey } from './storage'
import { Key, type KeyAlgs, CryptoBox, Jwk } from '@hyperledger/aries-askar-shared'
import type { EcKeyGenParams, EcKeyImportParams, JsonWebKey, KeyFormat, KeyUsage } from './types'

const CBOX_NONCE_LENGTH = 24
Expand Down Expand Up @@ -176,4 +176,6 @@ export const askarKeyFromJwk = ({

// TODO: this needs a proper conversion
const cryptoAlgorithmToAskarAlgorithm = (algorithm: EcKeyGenParams) =>
(algorithm.namedCurve ? algorithm.namedCurve : algorithm.name) as KeyAlgs
algorithm.name === 'ECDSA'
? KeyAlgs.EcSecp256r1
: ((algorithm.namedCurve ? algorithm.namedCurve : algorithm.name) as KeyAlgs)
71 changes: 57 additions & 14 deletions tests/x509.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ describe('x509', async () => {
{ type: 'dns', value: 'paradym.id' },
{ type: 'dns', value: 'wallet.paradym.id' },
]),
new x509.SubjectAlternativeNameExtension([{ type: 'dns', value: 'animo.id' }]),
new x509.SubjectAlternativeNameExtension([{ type: 'url', value: 'animo.id' }]),
],
})

Expand Down Expand Up @@ -66,21 +68,9 @@ describe('x509', async () => {

const jwkPublic = await crypto.subtle.exportKey('jwk', askarKeys.publicKey)

const nodejsPrivateKey = await webCrypto.subtle.importKey(
'jwk',
jwkPrivate,
{ name: 'ECDSA', namedCurve: 'P-256', hash: { name: 'SHA-256' } },
true,
['sign']
)
const nodejsPrivateKey = await webCrypto.subtle.importKey('jwk', jwkPrivate, alg, true, ['sign'])

const nodejsPublicKey = await webCrypto.subtle.importKey(
'jwk',
jwkPublic,
{ name: 'ECDSA', namedCurve: 'P-256', hash: { name: 'SHA-256' } },
true,
['verify']
)
const nodejsPublicKey = await webCrypto.subtle.importKey('jwk', jwkPublic, alg, true, ['verify'])

const now = new Date()

Expand Down Expand Up @@ -135,4 +125,57 @@ describe('x509', async () => {
assert(isValidNodejsCertificate)
assert(isValidAskarCertificate)
})

it('Validate a certificate chain', async () => {
const crypto = new Crypto()
x509.cryptoProvider.set(crypto)

const alg = {
name: 'ECDSA',
namedCurve: 'p-256',
hash: 'SHA-256',
}

const rootKeys = await crypto.subtle.generateKey(alg, true, ['sign', 'verify'])
const rootCert = await x509.X509CertificateGenerator.createSelfSigned({
serialNumber: '01',
name: 'CN=Root',
notBefore: new Date(),
notAfter: new Date(),
keys: rootKeys,
signingAlgorithm: alg,
})

const intermediateKeys = await crypto.subtle.generateKey(alg, true, ['sign', 'verify'])
const intermediateCert = await x509.X509CertificateGenerator.create({
serialNumber: '02',
subject: 'CN=Intermediate',
issuer: rootCert.subject,
notBefore: new Date(),
notAfter: new Date(),
signingKey: rootKeys.privateKey,
publicKey: intermediateKeys.publicKey,
signingAlgorithm: alg,
})

const leafKeys = await crypto.subtle.generateKey(alg, true, ['sign', 'verify'])
const leafCert = await x509.X509CertificateGenerator.create({
serialNumber: '03',
subject: 'CN=Leaf',
issuer: intermediateCert.subject,
notBefore: new Date(),
notAfter: new Date(),
signingKey: intermediateKeys.privateKey,
publicKey: leafKeys.publicKey,
signingAlgorithm: alg,
})

const chain = new x509.X509ChainBuilder({
certificates: [rootCert, intermediateCert],
})

const items = await chain.build(leafCert)

assert.strictEqual(items.length, 3)
})
})