@signature-kit/crypto 0.2.0 → 0.2.2

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 (42) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/README.md +28 -11
  3. package/dist/base64.d.ts +1 -1
  4. package/dist/base64.d.ts.map +1 -1
  5. package/dist/base64.js +15 -14
  6. package/dist/config.d.ts +31 -7
  7. package/dist/config.d.ts.map +1 -1
  8. package/dist/config.js +42 -27
  9. package/dist/pem.d.ts +1 -1
  10. package/dist/pem.d.ts.map +1 -1
  11. package/dist/pem.js +14 -8
  12. package/dist/pkcs12.d.ts +2 -10
  13. package/dist/pkcs12.d.ts.map +1 -1
  14. package/dist/pkcs12.js +380 -125
  15. package/dist/primitives/aes.d.ts +1 -14
  16. package/dist/primitives/aes.d.ts.map +1 -1
  17. package/dist/primitives/aes.js +8 -47
  18. package/dist/primitives/des.d.ts +1 -20
  19. package/dist/primitives/des.d.ts.map +1 -1
  20. package/dist/primitives/des.js +6 -67
  21. package/dist/primitives/hmac.d.ts +0 -30
  22. package/dist/primitives/hmac.d.ts.map +1 -1
  23. package/dist/primitives/hmac.js +4 -46
  24. package/dist/primitives/padding.d.ts +1 -8
  25. package/dist/primitives/padding.d.ts.map +1 -1
  26. package/dist/primitives/padding.js +2 -11
  27. package/dist/primitives/pbkdf2.d.ts +1 -17
  28. package/dist/primitives/pbkdf2.d.ts.map +1 -1
  29. package/dist/primitives/pbkdf2.js +8 -30
  30. package/dist/primitives/rc2.d.ts +1 -20
  31. package/dist/primitives/rc2.d.ts.map +1 -1
  32. package/dist/primitives/rc2.js +11 -69
  33. package/dist/primitives/sha1.d.ts +0 -13
  34. package/dist/primitives/sha1.d.ts.map +1 -1
  35. package/dist/primitives/sha1.js +1 -28
  36. package/dist/primitives/sha256.d.ts +0 -26
  37. package/dist/primitives/sha256.d.ts.map +1 -1
  38. package/dist/primitives/sha256.js +0 -34
  39. package/dist/primitives/sha512.d.ts +0 -19
  40. package/dist/primitives/sha512.d.ts.map +1 -1
  41. package/dist/primitives/sha512.js +1 -32
  42. package/package.json +8 -6
package/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
+ ## @signature-kit/crypto@0.2.2
2
+
3
+ ### Simplify Effect programs
4
+
5
+ Use native void effects and value-discarding combinators while preserving public success, error, and dependency contracts. The workspace now enforces Effect correctness and selected simplification rules through the official Effect TypeScript-Go integration with Oxlint.
6
+
7
+ ### Runtime updates and signature inspection
8
+
9
+ Update to stable Effect 4 and current compatible dependencies. Error classes use the native branded Schema.TaggedError constructor. Use TypeScript 7 for builds and checks with the official TypeScript 6 API compatibility alias. Upgrade direct ASN.1 dependencies to v4, interchange SDK values through DER, and encode timestamp requests with the native RFC 3161 schema. Packages do not require a consumer ASN.1 override.
10
+
11
+ Add per-signature PDF verification, certificate-validity inspection through the Effect clock, and cancellable polling for final request states in all five remote signers. Applications control polling deadlines and retry policy; a terminal request is not necessarily completed.
12
+
13
+ Update the documentation runtime, use the loading task for PDF.js cleanup, and fix dynamic Node routes by forwarding Request.duplex in the Hono adapter. Keep the app and Fumapress on a single Waku runtime.
14
+
15
+ Reject non-finite domain numbers, validate XML signature methods before WebCrypto verification, and preserve unexpected SDK failures as defects. Strengthen native Effect and accessibility lint checks.
16
+
17
+ Add branded PDF stamps with proportional PNG logos and handwriting, theme/title controls, and a PDF-rendered preview. Run browser PDF preparation and preview rendering through native Effect worker RPC with scoped termination and typed validation failures. Preserve caller buffers and signing dates across the worker boundary.
18
+
19
+ Fix structured batch stamp option conflicts, reject badges without usable drawing space, and prevent premeasured text from wrapping into adjacent rows. Restore signer interaction after React StrictMode mount replay and register localized Fumapress demo routes through the framework's native locale option. Add real worker, StrictMode, PDF text-stream, cropped/rotated page, cancellation, and invalid-image regressions.
20
+
1
21
  # Changelog
2
22
 
23
+ ## 0.2.1
24
+
25
+ - Export English and pt-BR `cryptoErrorMessages` catalogs for code-keyed display
26
+ copy.
27
+
28
+ - Security: bound PKCS#12 MAC, PBKDF2, and legacy PBE iteration counts at 10M so
29
+ attacker-controlled `.pfx` files cannot pin the event loop before the password
30
+ is checked.
31
+ - Extend hardening coverage across PBES2/PBKDF2 encrypted-data, legacy PBE, and
32
+ the exact 10M boundary; runtime behavior is unchanged.
33
+
3
34
  ## 0.2.0
4
35
 
5
36
  - Fix SHA-512/SHA-384 message-length encoding for inputs ≥ 512 MiB (also corrects
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @signature-kit/crypto
2
2
 
3
- Shared cryptographic primitives for PKCS#12, PEM, base64, hashing, and cipher operations.
3
+ Shared cryptographic primitives for base64, PEM, PKCS#12, hashing, and cipher operations.
4
4
 
5
5
  ## Install
6
6
 
@@ -8,22 +8,39 @@ Shared cryptographic primitives for PKCS#12, PEM, base64, hashing, and cipher op
8
8
  bun add @signature-kit/crypto effect
9
9
  ```
10
10
 
11
- ## Exports
11
+ `effect` is a direct runtime dependency. This is a low-level support package; prefer higher-level SignatureKit packages unless you need the primitive directly.
12
12
 
13
- - `@signature-kit/crypto/base64`
14
- - `@signature-kit/crypto/config`
15
- - `@signature-kit/crypto/pem`
16
- - `@signature-kit/crypto/pkcs12`
13
+ ## Public surface
17
14
 
18
- ## Runtime model
15
+ - `@signature-kit/crypto/base64` — `bytesToBase64` and `base64ToBytes`.
16
+ - `@signature-kit/crypto/config` — `CryptoError`, `CryptoErrorCodeValue`, operation schemas, message catalogs, and `Pkcs12ResultSchema`.
17
+ - `@signature-kit/crypto/pem` — `pemToDer` and `derToPem`.
18
+ - `@signature-kit/crypto/pkcs12` — `parsePkcs12`.
19
19
 
20
- SignatureKit packages are Effect-native. Public APIs return typed `Effect.Effect` values; recoverable faults stay in the typed error channel; callers provide required services and layers explicitly at the application boundary.
20
+ ## Example
21
21
 
22
- This is a low-level support package: keep its surface narrow and prefer the higher-level SignatureKit packages unless you need the primitive directly.
22
+ ```ts
23
+ import { bytesToBase64 } from "@signature-kit/crypto/base64";
24
+ import { pemToDer } from "@signature-kit/crypto/pem";
25
+ import { Effect } from "effect";
23
26
 
24
- ## Version
27
+ declare const certificatePem: string;
25
28
 
26
- Current npm release line: `0.1.0`.
29
+ const program = Effect.gen(function* () {
30
+ const der = yield* pemToDer(certificatePem);
31
+
32
+ return {
33
+ der,
34
+ contentBase64: bytesToBase64(der),
35
+ };
36
+ });
37
+ ```
38
+
39
+ ## Errors and i18n
40
+
41
+ Crypto failures use the `CryptoError` code catalog and `cryptoErrorMessages`. Applications can render localized copy through `@signature-kit/i18n` by passing that catalog to `errorMessage`.
42
+
43
+ Docs: <https://signaturekit.dev/en-US/docs/concepts/document-formats>.
27
44
 
28
45
  ## License
29
46
 
package/dist/base64.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Effect } from "effect";
2
- import { CryptoError } from "./config";
2
+ import { CryptoError } from "./config.js";
3
3
  export declare const bytesToBase64: (bytes: Uint8Array) => string;
4
4
  export declare const base64ToBytes: (base64: string) => Effect.Effect<Uint8Array<ArrayBuffer>, CryptoError>;
5
5
  //# sourceMappingURL=base64.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAA8C,MAAM,UAAU,CAAC;AAcnF,eAAO,MAAM,aAAa,GAAI,OAAO,UAAU,KAAG,MAajD,CAAC;AAEF,eAAO,MAAM,aAAa,GACxB,QAAQ,MAAM,KACb,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,EAAE,WAAW,CA2DjD,CAAC"}
1
+ {"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAA8C,MAAM,aAAa,CAAC;AActF,eAAO,MAAM,aAAa,UAAW,UAAU,KAAG,MAajD,CAAC;AAEF,eAAO,MAAM,aAAa,WAChB,MAAM,KACb,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,EAAE,WAAW,CAwDjD,CAAC"}
package/dist/base64.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Effect } from "effect";
2
- import { CryptoError, CryptoErrorCodeValue, CryptoOperationValue } from "./config";
2
+ import { CryptoError, CryptoErrorCodeValue, CryptoOperationValue } from "./config.js";
3
3
  const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4
4
  const base64Value = (char) => {
5
5
  const code = char.charCodeAt(0);
@@ -32,26 +32,19 @@ export const bytesToBase64 = (bytes) => {
32
32
  export const base64ToBytes = (base64) => Effect.gen(function* () {
33
33
  const clean = base64.replace(/[\t\n\r ]/g, "");
34
34
  if (!/^[A-Za-z0-9+/]*={0,2}$/.test(clean)) {
35
- return yield* Effect.fail(new CryptoError({
35
+ return yield* new CryptoError({
36
36
  code: CryptoErrorCodeValue.invalidFormat,
37
37
  reason: "Base64 input contains invalid characters.",
38
38
  operation: CryptoOperationValue.base64Decode,
39
- }));
39
+ });
40
40
  }
41
- if (clean.length % 4 === 1) {
42
- return yield* Effect.fail(new CryptoError({
41
+ const hasPadding = clean.endsWith("=");
42
+ if (clean.length % 4 === 1 || (hasPadding && clean.length % 4 !== 0)) {
43
+ return yield* new CryptoError({
43
44
  code: CryptoErrorCodeValue.invalidFormat,
44
45
  reason: "Base64 input has an invalid length.",
45
46
  operation: CryptoOperationValue.base64Decode,
46
- }));
47
- }
48
- const firstPadding = clean.indexOf("=");
49
- if (firstPadding >= 0 && firstPadding < clean.length - (clean.endsWith("==") ? 2 : 1)) {
50
- return yield* Effect.fail(new CryptoError({
51
- code: CryptoErrorCodeValue.invalidFormat,
52
- reason: "Base64 padding must be at the end.",
53
- operation: CryptoOperationValue.base64Decode,
54
- }));
47
+ });
55
48
  }
56
49
  const normalized = clean.padEnd(Math.ceil(clean.length / 4) * 4, "=");
57
50
  const padding = normalized.endsWith("==") ? 2 : normalized.endsWith("=") ? 1 : 0;
@@ -62,6 +55,14 @@ export const base64ToBytes = (base64) => Effect.gen(function* () {
62
55
  const second = base64Value(normalized[offset + 1] ?? "");
63
56
  const third = base64Value(normalized[offset + 2] ?? "");
64
57
  const fourth = base64Value(normalized[offset + 3] ?? "");
58
+ if (offset + 4 === normalized.length &&
59
+ ((padding === 2 && (second & 0x0f) !== 0) || (padding === 1 && (third & 0x03) !== 0))) {
60
+ return yield* new CryptoError({
61
+ code: CryptoErrorCodeValue.invalidFormat,
62
+ reason: "Base64 input has non-zero padding bits.",
63
+ operation: CryptoOperationValue.base64Decode,
64
+ });
65
+ }
65
66
  const chunk = (first << 18) | (second << 12) | (third << 6) | fourth;
66
67
  if (outputOffset < output.length) {
67
68
  output[outputOffset] = (chunk >> 16) & 0xff;
package/dist/config.d.ts CHANGED
@@ -1,9 +1,3 @@
1
- /**
2
- * @signature-kit/crypto — typed error catalog and the PKCS#12 result contract.
3
- *
4
- * The cipher primitives and the PKCS#12 parser construct `CryptoError` at the
5
- * exact decision point (bad padding, unsupported algorithm, missing material).
6
- */
7
1
  import { Schema } from "effect";
8
2
  export declare const CryptoErrorCodeSchema: Schema.Literals<readonly ["crypto.DECODE_ERROR", "crypto.INVALID_FORMAT", "crypto.UNSUPPORTED_ALGORITHM", "crypto.WRONG_PASSWORD", "crypto.NO_CERTIFICATE", "crypto.NO_PRIVATE_KEY", "crypto.CORRUPTED_FILE", "crypto.CIPHER_ERROR", "crypto.UNKNOWN"]>;
9
3
  export type CryptoErrorCode = (typeof CryptoErrorCodeSchema)["Type"];
@@ -30,11 +24,41 @@ export declare const CryptoOperationValue: {
30
24
  cipherDes: "cipher.des";
31
25
  cipherRc2: "cipher.rc2";
32
26
  };
27
+ export declare const CryptoErrorMessageLocaleSchema: Schema.Literals<readonly ["en-US", "pt-BR"]>;
28
+ export type CryptoErrorMessageLocale = (typeof CryptoErrorMessageLocaleSchema)["Type"];
29
+ export declare const CryptoErrorMessagesSchema: Schema.$Record<Schema.Literals<readonly ["en-US", "pt-BR"]>, Schema.$Record<Schema.Literals<readonly ["crypto.DECODE_ERROR", "crypto.INVALID_FORMAT", "crypto.UNSUPPORTED_ALGORITHM", "crypto.WRONG_PASSWORD", "crypto.NO_CERTIFICATE", "crypto.NO_PRIVATE_KEY", "crypto.CORRUPTED_FILE", "crypto.CIPHER_ERROR", "crypto.UNKNOWN"]>, Schema.String>>;
30
+ export type CryptoErrorMessages = (typeof CryptoErrorMessagesSchema)["Type"];
31
+ export declare const cryptoErrorMessages: {
32
+ "en-US": {
33
+ "crypto.DECODE_ERROR": string;
34
+ "crypto.INVALID_FORMAT": string;
35
+ "crypto.UNSUPPORTED_ALGORITHM": string;
36
+ "crypto.WRONG_PASSWORD": string;
37
+ "crypto.NO_CERTIFICATE": string;
38
+ "crypto.NO_PRIVATE_KEY": string;
39
+ "crypto.CORRUPTED_FILE": string;
40
+ "crypto.CIPHER_ERROR": string;
41
+ "crypto.UNKNOWN": string;
42
+ };
43
+ "pt-BR": {
44
+ "crypto.DECODE_ERROR": string;
45
+ "crypto.INVALID_FORMAT": string;
46
+ "crypto.UNSUPPORTED_ALGORITHM": string;
47
+ "crypto.WRONG_PASSWORD": string;
48
+ "crypto.NO_CERTIFICATE": string;
49
+ "crypto.NO_PRIVATE_KEY": string;
50
+ "crypto.CORRUPTED_FILE": string;
51
+ "crypto.CIPHER_ERROR": string;
52
+ "crypto.UNKNOWN": string;
53
+ };
54
+ };
33
55
  declare const CryptoError_base: Schema.Class<CryptoError, Schema.TaggedStruct<"CryptoError", {
34
56
  readonly code: Schema.Literals<readonly ["crypto.DECODE_ERROR", "crypto.INVALID_FORMAT", "crypto.UNSUPPORTED_ALGORITHM", "crypto.WRONG_PASSWORD", "crypto.NO_CERTIFICATE", "crypto.NO_PRIVATE_KEY", "crypto.CORRUPTED_FILE", "crypto.CIPHER_ERROR", "crypto.UNKNOWN"]>;
35
57
  readonly reason: Schema.optional<Schema.String>;
36
58
  readonly operation: Schema.optional<Schema.Literals<readonly ["base64.decode", "pem.decode", "pkcs12.decode", "pkcs12.mac", "pkcs12.decrypt", "cipher.aes", "cipher.des", "cipher.rc2"]>>;
37
- }>, import("effect/Cause").YieldableError>;
59
+ }>, import("effect/Cause").YieldableError & {
60
+ readonly brand: unique symbol;
61
+ }>;
38
62
  export declare class CryptoError extends CryptoError_base {
39
63
  get message(): string;
40
64
  }
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,eAAO,MAAM,qBAAqB,yPAUhC,CAAC;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC,CAAC;AACrE,eAAO,MAAM,oBAAoB;;;;;;;;;;CAUU,CAAC;AAE5C,eAAO,MAAM,qBAAqB,sJAShC,CAAC;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC,CAAC;AACrE,eAAO,MAAM,oBAAoB;;;;;;;;;CASU,CAAC;;;;;;AAE5C,qBAAa,WAAY,SAAQ,gBAI/B;IACA,IAAI,OAAO,IAAI,MAAM,CAqBpB;CACF;AAED,eAAO,MAAM,kBAAkB;;;;EAI7B,CAAC;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC"}
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,eAAO,MAAM,qBAAqB,yPAUhC,CAAC;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC,CAAC;AACrE,eAAO,MAAM,oBAAoB;;;;;;;;;;CAUU,CAAC;AAE5C,eAAO,MAAM,qBAAqB,sJAShC,CAAC;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC,CAAC;AACrE,eAAO,MAAM,oBAAoB;;;;;;;;;CASU,CAAC;AAE5C,eAAO,MAAM,8BAA8B,8CAAsC,CAAC;AAClF,MAAM,MAAM,wBAAwB,GAAG,CAAC,OAAO,8BAA8B,CAAC,CAAC,MAAM,CAAC,CAAC;AACvF,eAAO,MAAM,yBAAyB,sVAGrC,CAAC;AACF,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,yBAAyB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE7E,eAAO,MAAM,mBAAmB;;QAE5B,qBAAqB;QACrB,uBAAuB;QACvB,8BAA8B;QAC9B,uBAAuB;QACvB,uBAAuB;QACvB,uBAAuB;QACvB,uBAAuB;QACvB,qBAAqB;QACrB,gBAAgB;;;QAGhB,qBAAqB;QACrB,uBAAuB;QACvB,8BAA8B;QAC9B,uBAAuB;QACvB,uBAAuB;QACvB,uBAAuB;QACvB,uBAAuB;QACvB,qBAAqB;QACrB,gBAAgB;;CAEyD,CAAC;;;;;;oBAgB1D,OAAO,MAAM;;AAFjC,qBAAa,WAAY,SAAQ,gBAO/B;IACA,IAAI,OAAO,IAAI,MAAM,CAKpB;CACF;AAED,eAAO,MAAM,kBAAkB;;;;EAI7B,CAAC;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC"}
package/dist/config.js CHANGED
@@ -1,9 +1,3 @@
1
- /**
2
- * @signature-kit/crypto — typed error catalog and the PKCS#12 result contract.
3
- *
4
- * The cipher primitives and the PKCS#12 parser construct `CryptoError` at the
5
- * exact decision point (bad padding, unsupported algorithm, missing material).
6
- */
7
1
  import { Schema } from "effect";
8
2
  export const CryptoErrorCodeSchema = Schema.Literals([
9
3
  "crypto.DECODE_ERROR",
@@ -47,32 +41,53 @@ export const CryptoOperationValue = {
47
41
  cipherDes: "cipher.des",
48
42
  cipherRc2: "cipher.rc2",
49
43
  };
50
- export class CryptoError extends Schema.TaggedErrorClass()("CryptoError", {
44
+ export const CryptoErrorMessageLocaleSchema = Schema.Literals(["en-US", "pt-BR"]);
45
+ export const CryptoErrorMessagesSchema = Schema.Record(CryptoErrorMessageLocaleSchema, Schema.Record(CryptoErrorCodeSchema, Schema.String));
46
+ export const cryptoErrorMessages = {
47
+ "en-US": {
48
+ "crypto.DECODE_ERROR": "Failed to decode PKCS#12 ASN.1.",
49
+ "crypto.INVALID_FORMAT": "Unsupported PKCS#12 format.",
50
+ "crypto.UNSUPPORTED_ALGORITHM": "Unsupported PKCS#12 encryption algorithm.",
51
+ "crypto.WRONG_PASSWORD": "Wrong PKCS#12 password.",
52
+ "crypto.NO_CERTIFICATE": "No certificate found in PKCS#12 file.",
53
+ "crypto.NO_PRIVATE_KEY": "No private key found in PKCS#12 file.",
54
+ "crypto.CORRUPTED_FILE": "Corrupted PKCS#12 file.",
55
+ "crypto.CIPHER_ERROR": "Cipher operation failed.",
56
+ "crypto.UNKNOWN": "Unknown crypto failure.",
57
+ },
58
+ "pt-BR": {
59
+ "crypto.DECODE_ERROR": "Não foi possível ler os dados ASN.1 do PKCS#12.",
60
+ "crypto.INVALID_FORMAT": "Formato PKCS#12 não suportado.",
61
+ "crypto.UNSUPPORTED_ALGORITHM": "O certificado usa um algoritmo de criptografia não suportado.",
62
+ "crypto.WRONG_PASSWORD": "Senha do certificado incorreta.",
63
+ "crypto.NO_CERTIFICATE": "O arquivo não contém um certificado.",
64
+ "crypto.NO_PRIVATE_KEY": "O arquivo não contém uma chave privada.",
65
+ "crypto.CORRUPTED_FILE": "Arquivo PKCS#12 corrompido.",
66
+ "crypto.CIPHER_ERROR": "Falha na operação de criptografia.",
67
+ "crypto.UNKNOWN": "Falha de criptografia desconhecida.",
68
+ },
69
+ };
70
+ const cryptoErrorReasonOverridableByCode = {
71
+ "crypto.DECODE_ERROR": true,
72
+ "crypto.INVALID_FORMAT": true,
73
+ "crypto.UNSUPPORTED_ALGORITHM": true,
74
+ "crypto.WRONG_PASSWORD": false,
75
+ "crypto.NO_CERTIFICATE": false,
76
+ "crypto.NO_PRIVATE_KEY": false,
77
+ "crypto.CORRUPTED_FILE": true,
78
+ "crypto.CIPHER_ERROR": true,
79
+ "crypto.UNKNOWN": true,
80
+ };
81
+ export class CryptoError extends Schema.TaggedError()("CryptoError", {
51
82
  code: CryptoErrorCodeSchema,
52
83
  reason: Schema.optional(Schema.String),
53
84
  operation: Schema.optional(CryptoOperationSchema),
54
85
  }) {
55
86
  get message() {
56
- switch (this.code) {
57
- case "crypto.DECODE_ERROR":
58
- return this.reason ?? "Failed to decode PKCS#12 ASN.1.";
59
- case "crypto.INVALID_FORMAT":
60
- return this.reason ?? "Unsupported PKCS#12 format.";
61
- case "crypto.UNSUPPORTED_ALGORITHM":
62
- return this.reason ?? "Unsupported PKCS#12 encryption algorithm.";
63
- case "crypto.WRONG_PASSWORD":
64
- return "Wrong PKCS#12 password.";
65
- case "crypto.NO_CERTIFICATE":
66
- return "No certificate found in PKCS#12 file.";
67
- case "crypto.NO_PRIVATE_KEY":
68
- return "No private key found in PKCS#12 file.";
69
- case "crypto.CORRUPTED_FILE":
70
- return this.reason ?? "Corrupted PKCS#12 file.";
71
- case "crypto.CIPHER_ERROR":
72
- return this.reason ?? "Cipher operation failed.";
73
- case "crypto.UNKNOWN":
74
- return this.reason ?? "Unknown crypto failure.";
75
- }
87
+ const defaultMessage = cryptoErrorMessages["en-US"][this.code];
88
+ return cryptoErrorReasonOverridableByCode[this.code]
89
+ ? (this.reason ?? defaultMessage)
90
+ : defaultMessage;
76
91
  }
77
92
  }
78
93
  export const Pkcs12ResultSchema = Schema.Struct({
package/dist/pem.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Effect } from "effect";
2
- import { CryptoError } from "./config";
2
+ import { CryptoError } from "./config.js";
3
3
  export declare const pemToDer: (pem: string) => Effect.Effect<Uint8Array, CryptoError>;
4
4
  export declare const derToPem: (der: Uint8Array, label: string) => string;
5
5
  //# sourceMappingURL=pem.d.ts.map
package/dist/pem.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"pem.d.ts","sourceRoot":"","sources":["../src/pem.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAA8C,MAAM,UAAU,CAAC;AAGnF,eAAO,MAAM,QAAQ,GAAI,KAAK,MAAM,KAAG,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,WAAW,CAe3E,CAAC;AAEF,eAAO,MAAM,QAAQ,GAAI,KAAK,UAAU,EAAE,OAAO,MAAM,KAAG,MASzD,CAAC"}
1
+ {"version":3,"file":"pem.d.ts","sourceRoot":"","sources":["../src/pem.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAA8C,MAAM,aAAa,CAAC;AAMtF,eAAO,MAAM,QAAQ,QAAS,MAAM,KAAG,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,WAAW,CAsB3E,CAAC;AAEF,eAAO,MAAM,QAAQ,QAAS,UAAU,SAAS,MAAM,KAAG,MASzD,CAAC"}
package/dist/pem.js CHANGED
@@ -1,19 +1,25 @@
1
1
  import { Effect } from "effect";
2
- import { CryptoError, CryptoErrorCodeValue, CryptoOperationValue } from "./config";
3
- import { base64ToBytes, bytesToBase64 } from "./base64";
2
+ import { CryptoError, CryptoErrorCodeValue, CryptoOperationValue } from "./config.js";
3
+ import { base64ToBytes, bytesToBase64 } from "./base64.js";
4
+ const PEM_ENVELOPE = /^[\t \r\n]*-----BEGIN ([^\r\n]+)-----\r?\n([\s\S]*?)\r?\n-----END \1-----[\t \r\n]*$/;
4
5
  export const pemToDer = (pem) => {
5
- const base64 = pem
6
- .split(/\r?\n/)
7
- .filter((line) => !line.startsWith("-----"))
8
- .join("");
9
- if (base64.length === 0) {
6
+ const base64 = PEM_ENVELOPE.exec(pem)?.[2];
7
+ if (base64 === undefined || base64.includes("-----")) {
8
+ return Effect.fail(new CryptoError({
9
+ code: CryptoErrorCodeValue.invalidFormat,
10
+ reason: "PEM input must contain exactly one matching BEGIN/END envelope.",
11
+ operation: CryptoOperationValue.pemDecode,
12
+ }));
13
+ }
14
+ const normalizedBase64 = base64.replace(/[\t\n\r ]/g, "");
15
+ if (normalizedBase64.length === 0) {
10
16
  return Effect.fail(new CryptoError({
11
17
  code: CryptoErrorCodeValue.invalidFormat,
12
18
  reason: "PEM input does not contain base64 DER content.",
13
19
  operation: CryptoOperationValue.pemDecode,
14
20
  }));
15
21
  }
16
- return base64ToBytes(base64);
22
+ return base64ToBytes(normalizedBase64);
17
23
  };
18
24
  export const derToPem = (der, label) => {
19
25
  const base64 = bytesToBase64(der);
package/dist/pkcs12.d.ts CHANGED
@@ -1,13 +1,5 @@
1
- /**
2
- * PKCS#12 (.pfx/.p12) parser — fully Effect-native.
3
- *
4
- * Navigates the ASN.1 structure with the typed `@signature-kit/asn1` accessors, so
5
- * every read narrows without a cast. The MAC check is the password gate; once it
6
- * passes, a later cipher failure is treated as a corrupt file. All failures are
7
- * `CryptoError` constructed at the decision point — nothing throws.
8
- */
9
1
  import { Effect, Redacted } from "effect";
10
- import { CryptoError, type Pkcs12Result } from "./config";
11
- /** Parse a `.pfx`/`.p12` container, returning DER certificate + private key. */
2
+ import { CryptoError } from "./config.js";
3
+ import type { Pkcs12Result } from "./config.js";
12
4
  export declare const parsePkcs12: (data: Uint8Array, password: Redacted.Redacted<string>) => Effect.Effect<Pkcs12Result, CryptoError>;
13
5
  //# sourceMappingURL=pkcs12.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"pkcs12.d.ts","sourceRoot":"","sources":["../src/pkcs12.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAYH,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,EACL,WAAW,EAGX,KAAK,YAAY,EAClB,MAAM,UAAU,CAAC;AA0hBlB,gFAAgF;AAChF,eAAO,MAAM,WAAW,GACtB,MAAM,UAAU,EAChB,UAAU,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,KAClC,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,WAAW,CAyHvC,CAAC"}
1
+ {"version":3,"file":"pkcs12.d.ts","sourceRoot":"","sources":["../src/pkcs12.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,EAAE,WAAW,EAA8C,MAAM,aAAa,CAAC;AACtF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AA2yBhD,eAAO,MAAM,WAAW,SAChB,UAAU,YACN,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,KAClC,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,WAAW,CAiLvC,CAAC"}