-
Notifications
You must be signed in to change notification settings - Fork 1
feat(pq-algorithm-id/ts): phase 2 - implement conversion and x509 mapping apis (ENG-1915) #27
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { OID } from 'pq-oid'; | ||
| import { UnknownAlgorithmError, UnknownIdentifierError, UnsupportedMappingError } from './errors'; | ||
| import { getIdentifierRecord, listIdentifierRecords } from './registry'; | ||
| import type { AlgorithmName, CoseIdentifier, JoseIdentifier } from './types'; | ||
|
|
||
| const JOSE_TO_NAME = new Map<JoseIdentifier, AlgorithmName>(); | ||
| const COSE_TO_NAME = new Map<CoseIdentifier, AlgorithmName>(); | ||
|
|
||
| for (const record of listIdentifierRecords()) { | ||
| if (record.jose !== undefined) { | ||
| JOSE_TO_NAME.set(record.jose, record.name); | ||
| } | ||
| if (record.cose !== undefined) { | ||
| COSE_TO_NAME.set(record.cose, record.name); | ||
| } | ||
| } | ||
|
|
||
| function isCanonicalOid(oid: string): boolean { | ||
| if (oid.length === 0 || oid.trim() !== oid) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!/^\d+(?:\.\d+)+$/.test(oid)) { | ||
| return false; | ||
| } | ||
|
|
||
| const arcs = oid.split('.'); | ||
| if (arcs.some((arc) => arc.length > 1 && arc.startsWith('0'))) { | ||
| return false; | ||
| } | ||
|
|
||
| const firstArc = Number(arcs[0]); | ||
| if (!Number.isInteger(firstArc) || firstArc < 0 || firstArc > 2) { | ||
| return false; | ||
| } | ||
|
|
||
| const secondArc = Number(arcs[1]); | ||
| if (!Number.isInteger(secondArc)) { | ||
| return false; | ||
| } | ||
|
|
||
| if ((firstArc === 0 || firstArc === 1) && (secondArc < 0 || secondArc > 39)) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| export function toOid(name: AlgorithmName): string { | ||
| try { | ||
| return OID.fromName(getIdentifierRecord(name).name); | ||
| } catch { | ||
| throw new UnknownAlgorithmError(name); | ||
| } | ||
| } | ||
|
|
||
| export function fromOid(oid: string): AlgorithmName { | ||
| if (!isCanonicalOid(oid)) { | ||
| throw new UnknownIdentifierError('OID', oid); | ||
| } | ||
|
|
||
| try { | ||
| const name = OID.toName(oid); | ||
| getIdentifierRecord(name); | ||
| return name; | ||
| } catch { | ||
| throw new UnknownIdentifierError('OID', oid); | ||
| } | ||
| } | ||
|
|
||
| export function toJose(name: AlgorithmName): JoseIdentifier { | ||
| const record = getIdentifierRecord(name); | ||
| if (record.jose === undefined) { | ||
| throw new UnsupportedMappingError('JOSE', name); | ||
| } | ||
| return record.jose; | ||
| } | ||
|
|
||
| export function fromJose(jose: string): AlgorithmName { | ||
| const name = JOSE_TO_NAME.get(jose as JoseIdentifier); | ||
| if (name === undefined) { | ||
| throw new UnknownIdentifierError('JOSE', jose); | ||
| } | ||
| return name; | ||
| } | ||
|
|
||
| export function toCose(name: AlgorithmName): CoseIdentifier { | ||
| const record = getIdentifierRecord(name); | ||
| if (record.cose === undefined) { | ||
| throw new UnsupportedMappingError('COSE', name); | ||
| } | ||
| return record.cose; | ||
| } | ||
|
|
||
| export function fromCose(cose: number): AlgorithmName { | ||
| if (!Number.isFinite(cose) || !Number.isInteger(cose)) { | ||
| throw new UnknownIdentifierError('COSE', cose); | ||
| } | ||
|
|
||
| const name = COSE_TO_NAME.get(cose as CoseIdentifier); | ||
| if (name === undefined) { | ||
| throw new UnknownIdentifierError('COSE', cose); | ||
| } | ||
| return name; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { | ||
| AlgorithmIdentifierError, | ||
| UnknownIdentifierError, | ||
| UnsupportedMappingError, | ||
| } from './errors'; | ||
| import { fromOid, toOid } from './lookup'; | ||
| import { getIdentifierRecord } from './registry'; | ||
| import type { AlgorithmName, X509ParametersEncoding } from './types'; | ||
|
|
||
| export type X509NormalizedParameters = { kind: 'absent' } | { kind: 'null' }; | ||
|
|
||
| export interface X509AlgorithmIdentifier { | ||
| oid: string; | ||
| parameters: X509NormalizedParameters; | ||
| } | ||
|
|
||
| export interface X509AlgorithmIdentifierInput { | ||
| oid: string; | ||
| parameters?: unknown; | ||
| } | ||
|
|
||
| export interface X509AlgorithmIdentifierOptions { | ||
| parametersEncoding?: X509ParametersEncoding; | ||
| } | ||
eacet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const ABSENT_PARAMETERS: X509NormalizedParameters = Object.freeze({ kind: 'absent' }); | ||
| const NULL_PARAMETERS: X509NormalizedParameters = Object.freeze({ kind: 'null' }); | ||
|
|
||
| function validateParametersForAlgorithm( | ||
| name: AlgorithmName, | ||
| encoding: X509ParametersEncoding, | ||
| ): void { | ||
| const policy = getIdentifierRecord(name).x509; | ||
| if (encoding === 'absent' && !policy.acceptAbsent) { | ||
| throw new UnsupportedMappingError('X509', name); | ||
| } | ||
| if (encoding === 'null' && !policy.acceptNull) { | ||
| throw new UnsupportedMappingError('X509', name); | ||
| } | ||
| } | ||
eacet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| function normalizeParameters(input: unknown): X509NormalizedParameters { | ||
| if (input === undefined) { | ||
| return ABSENT_PARAMETERS; | ||
| } | ||
|
|
||
| if (input === null) { | ||
| return NULL_PARAMETERS; | ||
| } | ||
|
|
||
| if (typeof input === 'object' && input !== null) { | ||
eacet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const kind = (input as { kind?: unknown }).kind; | ||
| if (kind === 'absent') { | ||
| return ABSENT_PARAMETERS; | ||
| } | ||
| if (kind === 'null') { | ||
| return NULL_PARAMETERS; | ||
| } | ||
| } | ||
|
|
||
| throw new AlgorithmIdentifierError( | ||
| 'UNKNOWN_IDENTIFIER', | ||
| "Unknown X509 parameters. Expected undefined, null, { kind: 'absent' }, or { kind: 'null' }.", | ||
| ); | ||
| } | ||
|
|
||
| export function toX509AlgorithmIdentifier( | ||
| name: AlgorithmName, | ||
| options?: X509AlgorithmIdentifierOptions, | ||
| ): X509AlgorithmIdentifier { | ||
| const policy = getIdentifierRecord(name).x509; | ||
| const parametersEncoding = options?.parametersEncoding ?? policy.defaultParametersEncoding; | ||
| validateParametersForAlgorithm(name, parametersEncoding); | ||
|
|
||
| return { | ||
| oid: toOid(name), | ||
| parameters: parametersEncoding === 'null' ? NULL_PARAMETERS : ABSENT_PARAMETERS, | ||
| }; | ||
| } | ||
|
|
||
| export function fromX509AlgorithmIdentifier( | ||
| input: X509AlgorithmIdentifierInput, | ||
| ): X509AlgorithmIdentifier { | ||
| if (typeof input !== 'object' || input === null) { | ||
| throw new AlgorithmIdentifierError('UNKNOWN_IDENTIFIER', 'X509 input must be an object.'); | ||
| } | ||
|
|
||
| if (typeof input.oid !== 'string') { | ||
| throw new UnknownIdentifierError('OID', String(input.oid)); | ||
| } | ||
|
|
||
| const name = fromOid(input.oid); | ||
| const normalizedParameters = normalizeParameters(input.parameters); | ||
| validateParametersForAlgorithm(name, normalizedParameters.kind); | ||
|
|
||
| return { | ||
| oid: toOid(name), | ||
| parameters: normalizedParameters, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { describe, expect, test } from 'bun:test'; | ||
| import { OID } from 'pq-oid'; | ||
| import { UnknownIdentifierError, UnsupportedMappingError } from '../src/errors'; | ||
| import { fromCose, fromJose, fromOid, toCose, toJose, toOid } from '../src/lookup'; | ||
| import { listIdentifierRecords, listRegistryAlgorithmNames } from '../src/registry'; | ||
|
|
||
| function expectError<T extends Error>( | ||
| fn: () => unknown, | ||
| errorType: new (...args: never[]) => T, | ||
| ): T { | ||
| try { | ||
| fn(); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(errorType); | ||
| return error as T; | ||
| } | ||
| throw new Error('Expected function to throw.'); | ||
| } | ||
eacet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| describe('lookup - OID mapping', () => { | ||
| test('round-trip name <-> oid for all supported algorithms with fromOid(toOid(...))', () => { | ||
| for (const name of listRegistryAlgorithmNames()) { | ||
| expect(toOid(name)).toBe(OID.fromName(name)); | ||
| expect(fromOid(toOid(name))).toBe(name); | ||
| } | ||
| }); | ||
|
|
||
| test('fromOid enforces canonical dotted format and strict arc rules', () => { | ||
| const invalidOids = [ | ||
| '2.16.840.1.101.3.4.3.18 ', | ||
| ' 2.16.840.1.101.3.4.3.18', | ||
| '2..16.840.1.101.3.4.3.18', | ||
| '2.16.840.1.101.3.4.3.', | ||
| '2.16.840.1.101.3.4.3.1a', | ||
| '+2.16.840.1.101.3.4.3.18', | ||
| '2.-16.840.1.101.3.4.3.18', | ||
| '2.16.840.1.101.3.4.3.018', | ||
| '03.16.840.1.101.3.4.3.18', | ||
| '3.16.840.1.101.3.4.3.18', | ||
| '1.40.840.1.101.3.4.3.18', | ||
| ]; | ||
|
|
||
| for (const oid of invalidOids) { | ||
| const error = expectError(() => fromOid(oid), UnknownIdentifierError); | ||
| expect(error.code).toBe('UNKNOWN_IDENTIFIER'); | ||
| expect(error.identifierType).toBe('OID'); | ||
| expect(error.identifierValue).toBe(oid); | ||
| } | ||
| }); | ||
|
|
||
| test('fromOid rejects unknown canonical OIDs', () => { | ||
| const unknownOid = '2.16.840.1.101.3.4.3.255'; | ||
| const error = expectError(() => fromOid(unknownOid), UnknownIdentifierError); | ||
| expect(error.code).toBe('UNKNOWN_IDENTIFIER'); | ||
| expect(error.identifierType).toBe('OID'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('lookup - JOSE mapping', () => { | ||
| test('ML-DSA JOSE values stay parity-compatible with pq-oid', () => { | ||
| const names = ['ML-DSA-44', 'ML-DSA-65', 'ML-DSA-87'] as const; | ||
| for (const name of names) { | ||
| expect(toJose(name)).toBe(OID.toJOSE(name)); | ||
| expect(fromJose(OID.toJOSE(name))).toBe(name); | ||
| } | ||
| }); | ||
|
|
||
| test('unsupported JOSE mapping throws actionable typed error', () => { | ||
| const error = expectError(() => toJose('ML-KEM-512'), UnsupportedMappingError); | ||
| expect(error.code).toBe('UNSUPPORTED_MAPPING'); | ||
| expect(error.mapping).toBe('JOSE'); | ||
| expect(error.algorithm).toBe('ML-KEM-512'); | ||
| }); | ||
|
|
||
| test('fromJose is strict exact-match: rejects whitespace and case variants', () => { | ||
| const invalidJose = ['ml-dsa-44', 'ML-DSA-44 ', ' ML-DSA-44', 'RS256']; | ||
| for (const jose of invalidJose) { | ||
| const error = expectError(() => fromJose(jose), UnknownIdentifierError); | ||
| expect(error.code).toBe('UNKNOWN_IDENTIFIER'); | ||
| expect(error.identifierType).toBe('JOSE'); | ||
| expect(error.identifierValue).toBe(jose); | ||
| } | ||
| }); | ||
|
|
||
| test('JOSE mapping is unique and invertible via fromJose(toJose(...))', () => { | ||
| const joseSupported = listIdentifierRecords().filter((record) => record.jose !== undefined); | ||
| const joseValues = joseSupported.map((record) => toJose(record.name)); | ||
| expect(new Set(joseValues).size).toBe(joseValues.length); | ||
|
|
||
| for (const record of joseSupported) { | ||
| expect(fromJose(toJose(record.name))).toBe(record.name); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe('lookup - COSE mapping', () => { | ||
| test('ML-DSA COSE values stay parity-compatible with pq-oid', () => { | ||
| const names = ['ML-DSA-44', 'ML-DSA-65', 'ML-DSA-87'] as const; | ||
| for (const name of names) { | ||
| expect(toCose(name)).toBe(OID.toCOSE(name)); | ||
| expect(fromCose(OID.toCOSE(name))).toBe(name); | ||
| } | ||
| }); | ||
|
|
||
| test('unsupported COSE mapping throws actionable typed error', () => { | ||
| const error = expectError(() => toCose('SLH-DSA-SHA2-128s'), UnsupportedMappingError); | ||
| expect(error.code).toBe('UNSUPPORTED_MAPPING'); | ||
| expect(error.mapping).toBe('COSE'); | ||
| expect(error.algorithm).toBe('SLH-DSA-SHA2-128s'); | ||
| }); | ||
|
|
||
| test('fromCose rejects non-integer, float, NaN, Infinity, and unknown values', () => { | ||
| const invalidCose = [-48.1, Number.NaN, Number.POSITIVE_INFINITY, -999, ' -48'] as const; | ||
| for (const cose of invalidCose) { | ||
| const error = expectError(() => fromCose(cose as unknown as number), UnknownIdentifierError); | ||
| expect(error.code).toBe('UNKNOWN_IDENTIFIER'); | ||
| expect(error.identifierType).toBe('COSE'); | ||
| expect(error.identifierValue).toBe(cose); | ||
| } | ||
| }); | ||
|
|
||
| test('COSE mapping is unique and invertible via fromCose(toCose(...))', () => { | ||
| const coseSupported = listIdentifierRecords().filter((record) => record.cose !== undefined); | ||
| const coseValues = coseSupported.map((record) => toCose(record.name)); | ||
| expect(new Set(coseValues).size).toBe(coseValues.length); | ||
|
|
||
| for (const record of coseSupported) { | ||
| expect(fromCose(toCose(record.name))).toBe(record.name); | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.