Skip to content

Commit

Permalink
test: x509 chain validation (#4)
Browse files Browse the repository at this point in the history
* tmp

Signed-off-by: Berend Sliedrecht <sliedrecht@berend.io>

* test: more x509 tests for chain validation

Signed-off-by: Berend Sliedrecht <sliedrecht@berend.io>

---------

Signed-off-by: Berend Sliedrecht <sliedrecht@berend.io>
  • Loading branch information
berendsliedrecht authored Jun 24, 2024
1 parent 8a31122 commit 14548bf
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
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}`)
}
}
}
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)
})
})

0 comments on commit 14548bf

Please sign in to comment.