@webpqc/jose 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +434 -2
  3. package/esm/_dnt.test_polyfills.d.ts.map +1 -0
  4. package/esm/jwk.d.ts +37 -0
  5. package/esm/jwk.d.ts.map +1 -0
  6. package/esm/jwk.js +34 -0
  7. package/esm/jws.d.ts +33 -0
  8. package/esm/jws.d.ts.map +1 -0
  9. package/esm/jws.js +31 -0
  10. package/esm/mod.d.ts +15 -0
  11. package/esm/mod.d.ts.map +1 -0
  12. package/esm/mod.js +14 -0
  13. package/esm/package.json +3 -0
  14. package/esm/src/alg.d.ts +9 -0
  15. package/esm/src/alg.d.ts.map +1 -0
  16. package/esm/src/alg.js +20 -0
  17. package/esm/src/base64url/decode.d.ts +13 -0
  18. package/esm/src/base64url/decode.d.ts.map +1 -0
  19. package/esm/src/base64url/decode.js +61 -0
  20. package/esm/src/base64url/encode.d.ts +9 -0
  21. package/esm/src/base64url/encode.d.ts.map +1 -0
  22. package/esm/src/base64url/encode.js +51 -0
  23. package/esm/src/base64url/length.d.ts +5 -0
  24. package/esm/src/base64url/length.d.ts.map +1 -0
  25. package/esm/src/base64url/length.js +6 -0
  26. package/esm/src/bytes.d.ts +13 -0
  27. package/esm/src/bytes.d.ts.map +1 -0
  28. package/esm/src/bytes.js +21 -0
  29. package/esm/src/errors.d.ts +8 -0
  30. package/esm/src/errors.d.ts.map +1 -0
  31. package/esm/src/errors.js +11 -0
  32. package/esm/src/jwk/decode.d.ts +72 -0
  33. package/esm/src/jwk/decode.d.ts.map +1 -0
  34. package/esm/src/jwk/decode.js +116 -0
  35. package/esm/src/jwk/encode.d.ts +57 -0
  36. package/esm/src/jwk/encode.d.ts.map +1 -0
  37. package/esm/src/jwk/encode.js +69 -0
  38. package/esm/src/jwk/key.d.ts +11 -0
  39. package/esm/src/jwk/key.d.ts.map +1 -0
  40. package/esm/src/jwk/key.js +18 -0
  41. package/esm/src/jwk/mlDsa.d.ts +20 -0
  42. package/esm/src/jwk/mlDsa.d.ts.map +1 -0
  43. package/esm/src/jwk/mlDsa.js +32 -0
  44. package/esm/src/jwk/mlKem.d.ts +29 -0
  45. package/esm/src/jwk/mlKem.d.ts.map +1 -0
  46. package/esm/src/jwk/mlKem.js +41 -0
  47. package/esm/src/jwk/slhDsa.d.ts +51 -0
  48. package/esm/src/jwk/slhDsa.d.ts.map +1 -0
  49. package/esm/src/jwk/slhDsa.js +79 -0
  50. package/esm/src/jwk/thumbprint.d.ts +43 -0
  51. package/esm/src/jwk/thumbprint.d.ts.map +1 -0
  52. package/esm/src/jwk/thumbprint.js +56 -0
  53. package/esm/src/jwk/types.d.ts +76 -0
  54. package/esm/src/jwk/types.d.ts.map +1 -0
  55. package/esm/src/jwk/types.js +1 -0
  56. package/esm/src/jws/b64.d.ts +14 -0
  57. package/esm/src/jws/b64.d.ts.map +1 -0
  58. package/esm/src/jws/b64.js +18 -0
  59. package/esm/src/jws/decode.d.ts +37 -0
  60. package/esm/src/jws/decode.d.ts.map +1 -0
  61. package/esm/src/jws/decode.js +39 -0
  62. package/esm/src/jws/parse.d.ts +19 -0
  63. package/esm/src/jws/parse.d.ts.map +1 -0
  64. package/esm/src/jws/parse.js +46 -0
  65. package/esm/src/jws/sign.d.ts +43 -0
  66. package/esm/src/jws/sign.d.ts.map +1 -0
  67. package/esm/src/jws/sign.js +106 -0
  68. package/esm/src/jws/types.d.ts +136 -0
  69. package/esm/src/jws/types.d.ts.map +1 -0
  70. package/esm/src/jws/types.js +1 -0
  71. package/esm/src/jws/verify.d.ts +45 -0
  72. package/esm/src/jws/verify.d.ts.map +1 -0
  73. package/esm/src/jws/verify.js +75 -0
  74. package/package.json +57 -4
@@ -0,0 +1,43 @@
1
+ import type { JwsSigner, JwsSignOptions } from "./types.js";
2
+ /**
3
+ * Signs a payload, and gives the JWS in the JWS Compact Serialization
4
+ * (RFC 7515).
5
+ *
6
+ * The JWS Protected Header has "alg" of the signer, followed by the header
7
+ * parameters of the option `header`, such as "kid". The signature is made by
8
+ * the signer, which has the private key and the name of its algorithm, e.g.
9
+ * the one which `createMlDsa65Signer` of `@webpqc/mldsa` creates.
10
+ *
11
+ * "alg" is always the one of the signer: one in the option `header` is
12
+ * replaced. The other parameters are not checked, except "b64", which must
13
+ * not tell that the payload is not encoded (RFC 7797), as it always is. Nor
14
+ * may the option have "toJSON", which `JSON.stringify` would call, to write
15
+ * what it gives in place of this header.
16
+ *
17
+ * @param signer What signs with the private key: see {@link JwsSigner}.
18
+ * @param payload The payload.
19
+ * @param opts Options: see {@link JwsSignOptions}.
20
+ * @returns A promise that resolves to the JWS.
21
+ * @throws {JoseError} if the signer has no "alg", if the payload is not a
22
+ * Uint8Array, if the header is not an object which JSON can represent or
23
+ * has "toJSON", or if the signer does not give a Uint8Array. What the
24
+ * signer throws is thrown as it is.
25
+ *
26
+ * @example
27
+ *
28
+ * ```ts
29
+ * import { ML_DSA_65, importPrivateJwk, signJws } from "@webpqc/jose";
30
+ * import { createMlDsa65Signer } from "@webpqc/mldsa";
31
+ *
32
+ * async function sign(jwk: unknown, kid: string, payload: Uint8Array) {
33
+ * const sk = importPrivateJwk(ML_DSA_65, jwk);
34
+ * const signer = await createMlDsa65Signer(sk);
35
+ * sk.fill(0);
36
+ * const jws = await signJws(signer, payload, { header: { kid } });
37
+ * signer.destroy();
38
+ * return jws;
39
+ * }
40
+ * ```
41
+ */
42
+ export declare function signJws(signer: JwsSigner, payload: Uint8Array, opts?: JwsSignOptions): Promise<string>;
43
+ //# sourceMappingURL=sign.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sign.d.ts","sourceRoot":"","sources":["../../../src/src/jws/sign.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAsB,OAAO,CAC3B,MAAM,EAAE,SAAS,EACjB,OAAO,EAAE,UAAU,EACnB,IAAI,CAAC,EAAE,cAAc,GACpB,OAAO,CAAC,MAAM,CAAC,CAgEjB"}
@@ -0,0 +1,106 @@
1
+ import { encodeBase64Url } from "../base64url/encode.js";
2
+ import { sizeOfBytes } from "../bytes.js";
3
+ import { JoseError } from "../errors.js";
4
+ import { checkB64 } from "./b64.js";
5
+ /**
6
+ * Signs a payload, and gives the JWS in the JWS Compact Serialization
7
+ * (RFC 7515).
8
+ *
9
+ * The JWS Protected Header has "alg" of the signer, followed by the header
10
+ * parameters of the option `header`, such as "kid". The signature is made by
11
+ * the signer, which has the private key and the name of its algorithm, e.g.
12
+ * the one which `createMlDsa65Signer` of `@webpqc/mldsa` creates.
13
+ *
14
+ * "alg" is always the one of the signer: one in the option `header` is
15
+ * replaced. The other parameters are not checked, except "b64", which must
16
+ * not tell that the payload is not encoded (RFC 7797), as it always is. Nor
17
+ * may the option have "toJSON", which `JSON.stringify` would call, to write
18
+ * what it gives in place of this header.
19
+ *
20
+ * @param signer What signs with the private key: see {@link JwsSigner}.
21
+ * @param payload The payload.
22
+ * @param opts Options: see {@link JwsSignOptions}.
23
+ * @returns A promise that resolves to the JWS.
24
+ * @throws {JoseError} if the signer has no "alg", if the payload is not a
25
+ * Uint8Array, if the header is not an object which JSON can represent or
26
+ * has "toJSON", or if the signer does not give a Uint8Array. What the
27
+ * signer throws is thrown as it is.
28
+ *
29
+ * @example
30
+ *
31
+ * ```ts
32
+ * import { ML_DSA_65, importPrivateJwk, signJws } from "@webpqc/jose";
33
+ * import { createMlDsa65Signer } from "@webpqc/mldsa";
34
+ *
35
+ * async function sign(jwk: unknown, kid: string, payload: Uint8Array) {
36
+ * const sk = importPrivateJwk(ML_DSA_65, jwk);
37
+ * const signer = await createMlDsa65Signer(sk);
38
+ * sk.fill(0);
39
+ * const jws = await signJws(signer, payload, { header: { kid } });
40
+ * signer.destroy();
41
+ * return jws;
42
+ * }
43
+ * ```
44
+ */
45
+ export async function signJws(signer, payload, opts) {
46
+ // The signer is checked as what it may be from a caller without the types:
47
+ // the JWS of one which has no name of its algorithm would not tell what it
48
+ // is signed with.
49
+ const alg = signer?.alg;
50
+ if (typeof alg !== "string") {
51
+ throw new JoseError('"alg" of signer must be a string');
52
+ }
53
+ const size = sizeOfBytes(payload);
54
+ if (size < 0) {
55
+ throw new JoseError("payload must be a Uint8Array");
56
+ }
57
+ // So is the option.
58
+ const params = opts?.header;
59
+ if (params !== undefined &&
60
+ (typeof params !== "object" || params === null || Array.isArray(params))) {
61
+ throw new JoseError("header must be an object");
62
+ }
63
+ // The header has no prototype. What is checked and what is written is so
64
+ // its own parameters alone, which are read once from the option, as they
65
+ // are copied: nothing of Object.prototype is read, where "b64" and "toJSON"
66
+ // might be accessors, which give something else each time and are given
67
+ // the header itself, to write to it.
68
+ //
69
+ // "alg" is the one of the signer whatever the option has for it, and the
70
+ // first parameter, but for the ones whose names are integers, which
71
+ // JSON.stringify writes before the others.
72
+ const header = {
73
+ __proto__: null,
74
+ alg,
75
+ ...params,
76
+ };
77
+ header.alg = alg;
78
+ checkB64(header);
79
+ let json;
80
+ try {
81
+ // Not a header which has "toJSON": JSON.stringify would write what it
82
+ // gives in place of the header which is checked above, with any "alg"
83
+ // and "b64".
84
+ if (header.toJSON === undefined) {
85
+ json = JSON.stringify(header);
86
+ }
87
+ }
88
+ catch {
89
+ // e.g. a bigint, or an object which refers to itself.
90
+ }
91
+ if (json === undefined) {
92
+ throw new JoseError("header must be what JSON can represent");
93
+ }
94
+ const utf8 = new TextEncoder().encode(json);
95
+ // The JWS Signing Input (RFC 7515, Section 5.1), which is ASCII. The
96
+ // payload is read here, before anything is awaited.
97
+ const input = `${encodeBase64Url(utf8, utf8.length)}.${encodeBase64Url(payload, size)}`;
98
+ // The signer gets the message alone: no context string, for example.
99
+ const sig = await signer.sign(new TextEncoder().encode(input));
100
+ // e.g. not the ArrayBuffer which the Web Cryptography API gives.
101
+ const sigSize = sizeOfBytes(sig);
102
+ if (sigSize < 0) {
103
+ throw new JoseError("the signature must be a Uint8Array");
104
+ }
105
+ return `${input}.${encodeBase64Url(sig, sigSize)}`;
106
+ }
@@ -0,0 +1,136 @@
1
+ /**
2
+ * What signs a JWS: the private key, and the algorithm which signs with it.
3
+ *
4
+ * The signers of `@webpqc/mldsa` and `@webpqc/slhdsa`, e.g. the one which
5
+ * `createMlDsa65Signer` creates, are of this type: `alg` is the name of
6
+ * their algorithm, which is the value of "alg" in JOSE. `sign` is called
7
+ * with the message alone, so they sign with the empty context string, as the
8
+ * algorithms for JWS require (RFC 9964, Section 5).
9
+ *
10
+ * Anything else which signs is a plain object, e.g. with a key of the Web
11
+ * Cryptography API:
12
+ *
13
+ * ```ts
14
+ * import { type JwsSigner, signJws } from "@webpqc/jose";
15
+ *
16
+ * async function sign(privateKey: CryptoKey, payload: Uint8Array) {
17
+ * const signer: JwsSigner = {
18
+ * alg: "ML-DSA-65",
19
+ * sign: async (msg) =>
20
+ * new Uint8Array(
21
+ * await crypto.subtle.sign(
22
+ * "ML-DSA-65",
23
+ * privateKey,
24
+ * msg as BufferSource,
25
+ * ),
26
+ * ),
27
+ * };
28
+ * return await signJws(signer, payload);
29
+ * }
30
+ * ```
31
+ */
32
+ export interface JwsSigner {
33
+ /**
34
+ * The value of the header parameter "alg": the algorithm which `sign`
35
+ * signs with.
36
+ */
37
+ readonly alg: string;
38
+ /** Signs a message: returns the signature, or a promise of it. */
39
+ sign(msg: Uint8Array): Uint8Array | Promise<Uint8Array>;
40
+ }
41
+ /**
42
+ * What verifies a JWS: the public key, and the algorithm which verifies with
43
+ * it.
44
+ *
45
+ * The verifiers of `@webpqc/mldsa` and `@webpqc/slhdsa`, e.g. the one which
46
+ * `createMlDsa65Verifier` creates, are of this type: `alg` is the name of
47
+ * their algorithm, which is the value of "alg" in JOSE. `verify` is called
48
+ * with the signature and the message alone, so they verify with the empty
49
+ * context string, as the algorithms for JWS require (RFC 9964, Section 5).
50
+ *
51
+ * Anything else which verifies is a plain object, e.g. with a key of the Web
52
+ * Cryptography API:
53
+ *
54
+ * ```ts
55
+ * import { type JwsVerifier, verifyJws } from "@webpqc/jose";
56
+ *
57
+ * async function verify(publicKey: CryptoKey, jws: string) {
58
+ * const verifier: JwsVerifier = {
59
+ * alg: "ML-DSA-65",
60
+ * verify: (sig, msg) =>
61
+ * crypto.subtle.verify(
62
+ * "ML-DSA-65",
63
+ * publicKey,
64
+ * sig as BufferSource,
65
+ * msg as BufferSource,
66
+ * ),
67
+ * };
68
+ * return await verifyJws(verifier, jws);
69
+ * }
70
+ * ```
71
+ */
72
+ export interface JwsVerifier {
73
+ /**
74
+ * The value of the header parameter "alg" which a JWS has to have: the
75
+ * algorithm which `verify` verifies with.
76
+ */
77
+ readonly alg: string;
78
+ /**
79
+ * Verifies a signature of a message: returns true, or a promise of it, if
80
+ * and only if the signature is valid.
81
+ */
82
+ verify(sig: Uint8Array, msg: Uint8Array): boolean | Promise<boolean>;
83
+ }
84
+ /**
85
+ * The header parameters which an application gives a JWS, other than "alg".
86
+ * It may have any other parameter as well: the functions of this package
87
+ * neither know nor check them.
88
+ */
89
+ export interface JwsHeaderParameters {
90
+ /** The key ID. */
91
+ kid?: string;
92
+ /** The media type of the JWS, e.g. "JWT". */
93
+ typ?: string;
94
+ /** The media type of the payload. */
95
+ cty?: string;
96
+ /** "alg" is the one of the signer, which the function sets. */
97
+ alg?: never;
98
+ /**
99
+ * Not a header parameter: `JSON.stringify` would write what it gives in
100
+ * place of the header, so an object which has it is rejected.
101
+ */
102
+ toJSON?: never;
103
+ /** Any other header parameter. */
104
+ [name: string]: unknown;
105
+ }
106
+ /**
107
+ * The JWS Protected Header of a JWS which is verified: "alg" is the one of
108
+ * the verifier. The other parameters are what the JSON of the header has, of
109
+ * whatever type it has them, so check the type of one before using it.
110
+ */
111
+ export interface JwsHeader {
112
+ /** The algorithm which the JWS is signed with. */
113
+ alg: string;
114
+ /** Any other header parameter. */
115
+ [name: string]: unknown;
116
+ }
117
+ /**
118
+ * Options for {@link signJws}.
119
+ */
120
+ export interface JwsSignOptions {
121
+ /**
122
+ * The header parameters to protect together with "alg", such as "kid" and
123
+ * "typ".
124
+ */
125
+ header?: JwsHeaderParameters;
126
+ }
127
+ /**
128
+ * What {@link verifyJws} resolves to for a JWS whose signature is valid.
129
+ */
130
+ export interface JwsVerifyResult {
131
+ /** The JWS Protected Header. */
132
+ header: JwsHeader;
133
+ /** The payload. */
134
+ payload: Uint8Array;
135
+ }
136
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/src/jws/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,WAAW,SAAS;IACxB;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,kEAAkE;IAClE,IAAI,CAAC,GAAG,EAAE,UAAU,GAAG,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CACzD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,MAAM,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACtE;AAED;;;;GAIG;AACH,MAAM,WAAW,mBAAmB;IAClC,kBAAkB;IAClB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,6CAA6C;IAC7C,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,qCAAqC;IACrC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,+DAA+D;IAC/D,GAAG,CAAC,EAAE,KAAK,CAAC;IACZ;;;OAGG;IACH,MAAM,CAAC,EAAE,KAAK,CAAC;IACf,kCAAkC;IAClC,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;CACzB;AAED;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACxB,kDAAkD;IAClD,GAAG,EAAE,MAAM,CAAC;IACZ,kCAAkC;IAClC,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,MAAM,CAAC,EAAE,mBAAmB,CAAC;CAC9B;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,gCAAgC;IAChC,MAAM,EAAE,SAAS,CAAC;IAClB,mBAAmB;IACnB,OAAO,EAAE,UAAU,CAAC;CACrB"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,45 @@
1
+ import type { JwsVerifier, JwsVerifyResult } from "./types.js";
2
+ /**
3
+ * Verifies a JWS in the JWS Compact Serialization (RFC 7515), and gives its
4
+ * header and its payload.
5
+ *
6
+ * The JWS has to be signed with the algorithm of the verifier: "alg" of the
7
+ * header is checked against the one of the verifier, and never chooses how
8
+ * the JWS is verified. The signature is verified by the verifier, which has
9
+ * the public key and the name of its algorithm, e.g. the one which
10
+ * `createMlDsa65Verifier` of `@webpqc/mldsa` creates.
11
+ *
12
+ * It checks that the JWS has three parts, that the header is the base64url
13
+ * encoding of a JSON object in UTF-8 whose "alg" is the one of the verifier,
14
+ * and that the payload and the signature are in base64url, each with no
15
+ * padding and no other character. A JWS whose header has "crit" is
16
+ * rejected, as the function understands no extension, and so is one whose
17
+ * "b64" is not true (RFC 7797). The other header parameters, such as "kid"
18
+ * and "typ", are not checked, and the size of the signature is the
19
+ * verifier's to check.
20
+ *
21
+ * @param verifier What verifies the signature with the public key: see
22
+ * {@link JwsVerifier}.
23
+ * @param jws The JWS.
24
+ * @returns A promise that resolves to the header and the payload of the JWS.
25
+ * @throws {JoseError} if the JWS is not one of the algorithm of the
26
+ * verifier, which one of a verifier without "alg" is not either, or if its
27
+ * signature is not valid. What the verifier throws is thrown as it is.
28
+ *
29
+ * @example
30
+ *
31
+ * ```ts
32
+ * import { ML_DSA_65, importPublicJwk, verifyJws } from "@webpqc/jose";
33
+ * import { createMlDsa65Verifier } from "@webpqc/mldsa";
34
+ *
35
+ * async function verify(jwk: unknown, jws: string) {
36
+ * const verifier = await createMlDsa65Verifier(
37
+ * importPublicJwk(ML_DSA_65, jwk),
38
+ * );
39
+ * const { header, payload } = await verifyJws(verifier, jws);
40
+ * return { kid: header.kid, text: new TextDecoder().decode(payload) };
41
+ * }
42
+ * ```
43
+ */
44
+ export declare function verifyJws(verifier: JwsVerifier, jws: string): Promise<JwsVerifyResult>;
45
+ //# sourceMappingURL=verify.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../../../src/src/jws/verify.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAa,WAAW,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAsB,SAAS,CAC7B,QAAQ,EAAE,WAAW,EACrB,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,eAAe,CAAC,CA2B1B"}
@@ -0,0 +1,75 @@
1
+ import { checkAlg } from "../alg.js";
2
+ import { decodeBase64Url } from "../base64url/decode.js";
3
+ import { JoseError } from "../errors.js";
4
+ import { checkB64 } from "./b64.js";
5
+ import { headerOf, partsOf } from "./parse.js";
6
+ /**
7
+ * Verifies a JWS in the JWS Compact Serialization (RFC 7515), and gives its
8
+ * header and its payload.
9
+ *
10
+ * The JWS has to be signed with the algorithm of the verifier: "alg" of the
11
+ * header is checked against the one of the verifier, and never chooses how
12
+ * the JWS is verified. The signature is verified by the verifier, which has
13
+ * the public key and the name of its algorithm, e.g. the one which
14
+ * `createMlDsa65Verifier` of `@webpqc/mldsa` creates.
15
+ *
16
+ * It checks that the JWS has three parts, that the header is the base64url
17
+ * encoding of a JSON object in UTF-8 whose "alg" is the one of the verifier,
18
+ * and that the payload and the signature are in base64url, each with no
19
+ * padding and no other character. A JWS whose header has "crit" is
20
+ * rejected, as the function understands no extension, and so is one whose
21
+ * "b64" is not true (RFC 7797). The other header parameters, such as "kid"
22
+ * and "typ", are not checked, and the size of the signature is the
23
+ * verifier's to check.
24
+ *
25
+ * @param verifier What verifies the signature with the public key: see
26
+ * {@link JwsVerifier}.
27
+ * @param jws The JWS.
28
+ * @returns A promise that resolves to the header and the payload of the JWS.
29
+ * @throws {JoseError} if the JWS is not one of the algorithm of the
30
+ * verifier, which one of a verifier without "alg" is not either, or if its
31
+ * signature is not valid. What the verifier throws is thrown as it is.
32
+ *
33
+ * @example
34
+ *
35
+ * ```ts
36
+ * import { ML_DSA_65, importPublicJwk, verifyJws } from "@webpqc/jose";
37
+ * import { createMlDsa65Verifier } from "@webpqc/mldsa";
38
+ *
39
+ * async function verify(jwk: unknown, jws: string) {
40
+ * const verifier = await createMlDsa65Verifier(
41
+ * importPublicJwk(ML_DSA_65, jwk),
42
+ * );
43
+ * const { header, payload } = await verifyJws(verifier, jws);
44
+ * return { kid: header.kid, text: new TextDecoder().decode(payload) };
45
+ * }
46
+ * ```
47
+ */
48
+ export async function verifyJws(verifier, jws) {
49
+ const parts = partsOf(jws);
50
+ const header = headerOf(parts[0]);
51
+ // The algorithm is the one of the verifier, whatever the JWS says. One
52
+ // which is not a string, from a caller without the types, needs no check
53
+ // of its own: "alg" of a header is a string, which it is not equal to, so
54
+ // such a verifier is asked to verify no JWS.
55
+ checkAlg(header, verifier.alg);
56
+ // The parameters which it lists have to be understood (RFC 7515, Section
57
+ // 4.1.11), and none is.
58
+ if (header.crit !== undefined) {
59
+ throw new JoseError('"crit" is not supported');
60
+ }
61
+ checkB64(header);
62
+ const payload = decodeBase64Url(parts[1]);
63
+ // Of any size: the verifier knows the one of a signature of its algorithm.
64
+ const sig = decodeBase64Url(parts[2]);
65
+ if (payload === undefined || sig === undefined) {
66
+ throw new JoseError("the payload and the signature must be in base64url");
67
+ }
68
+ // The JWS Signing Input (RFC 7515, Section 5.2), which is ASCII by now.
69
+ const input = new TextEncoder().encode(`${parts[0]}.${parts[1]}`);
70
+ // Nothing but true is a valid signature: not what is only truthy.
71
+ if (await verifier.verify(sig, input) !== true) {
72
+ throw new JoseError("the signature is not valid");
73
+ }
74
+ return { header: header, payload };
75
+ }
package/package.json CHANGED
@@ -1,6 +1,59 @@
1
1
  {
2
2
  "name": "@webpqc/jose",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "description": "JOSE for post-quantum cryptography: JWK (AKP, RFC 9964) for ML-DSA, ML-KEM and SLH-DSA, and JWS with ML-DSA and SLH-DSA, written in TypeScript for various JavaScript runtimes",
5
+ "keywords": [
6
+ "jose",
7
+ "jwk",
8
+ "jws",
9
+ "akp",
10
+ "rfc9964",
11
+ "ml-dsa",
12
+ "ml-kem",
13
+ "slh-dsa",
14
+ "security",
15
+ "pqc",
16
+ "post-quantum"
17
+ ],
18
+ "author": "Ajitomi Daisuke",
19
+ "homepage": "https://github.com/dajiaji/webpqc/tree/main/packages/jose#readme",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/dajiaji/webpqc.git"
23
+ },
24
+ "license": "MIT",
25
+ "bugs": {
26
+ "url": "https://github.com/dajiaji/webpqc/issues"
27
+ },
28
+ "type": "module",
29
+ "main": "./esm/mod.js",
30
+ "module": "./esm/mod.js",
31
+ "types": "./esm/mod.d.ts",
32
+ "exports": {
33
+ ".": {
34
+ "types": "./esm/mod.d.ts",
35
+ "default": "./esm/mod.js"
36
+ },
37
+ "./jwk": {
38
+ "types": "./esm/jwk.d.ts",
39
+ "default": "./esm/jwk.js"
40
+ },
41
+ "./jws": {
42
+ "types": "./esm/jws.d.ts",
43
+ "default": "./esm/jws.js"
44
+ },
45
+ "./package.json": "./package.json"
46
+ },
47
+ "funding": {
48
+ "type": "github",
49
+ "url": "https://github.com/sponsors/dajiaji"
50
+ },
51
+ "sideEffects": false,
52
+ "engines": {
53
+ "node": ">=20.0.0"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public"
57
+ },
58
+ "_generatedBy": "dnt@dev"
59
+ }