• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PeculiarVentures / x509 / 13786403694

11 Mar 2025 11:00AM CUT coverage: 88.355%. Remained the same
13786403694

Pull #100

github

web-flow
Merge d79161f0b into 7124f535e
Pull Request #100: chore(deps): bump http-proxy-middleware from 2.0.6 to 2.0.7 in /website

616 of 774 branches covered (79.59%)

Branch coverage included in aggregate %.

1736 of 1888 relevant lines covered (91.95%)

45.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/src/rsa_algorithm.ts
1
import * as asn1Rsa from "@peculiar/asn1-rsa";
2✔
2
import { AsnConvert } from "@peculiar/asn1-schema";
2✔
3
import { AlgorithmIdentifier } from "@peculiar/asn1-x509";
2✔
4
import { container, injectable } from "tsyringe";
2✔
5
import { AlgorithmProvider, diAlgorithm, diAlgorithmProvider, IAlgorithm } from "./algorithm";
2✔
6
import { HashedAlgorithm } from "./types";
7

8
/**
9
 * RSA algorithm provider
10
 */
11
@injectable()
12
export class RsaAlgorithm implements IAlgorithm {
2✔
13

14
  public static createPssParams(hash: unknown, saltLength: number): asn1Rsa.RsaSaPssParams | null {
15
    const hashAlgorithm = RsaAlgorithm.getHashAlgorithm(hash);
2✔
16
    if (!hashAlgorithm) {
2!
17
      return null;
×
18
    }
19

20
    return new asn1Rsa.RsaSaPssParams({
2✔
21
      hashAlgorithm,
22
      maskGenAlgorithm: new AlgorithmIdentifier({
23
        algorithm: asn1Rsa.id_mgf1,
24
        parameters: AsnConvert.serialize(hashAlgorithm),
25
      }),
26
      saltLength,
27
    });
28
  }
29

30
  public static getHashAlgorithm(alg: unknown): AlgorithmIdentifier | null {
31
    const algProv = container.resolve<AlgorithmProvider>(diAlgorithmProvider);
2✔
32
    if (typeof alg === "string") {
2!
33
      return algProv.toAsnAlgorithm({ name: alg });
×
34
    } if (typeof alg === "object" && alg && "name" in alg) {
2✔
35
      return algProv.toAsnAlgorithm(alg as Algorithm);
2✔
36
    }
37

38
    return null;
×
39
  }
40

41
  public toAsnAlgorithm(alg: Algorithm): AlgorithmIdentifier | null {
42
    switch (alg.name.toLowerCase()) {
54✔
43
      case "rsassa-pkcs1-v1_5":
44
        if ("hash" in alg) {
48✔
45
          let hash: string;
46
          if (typeof alg.hash === "string") {
46✔
47
            hash = alg.hash;
4✔
48
          } else if (alg.hash && typeof alg.hash === "object"
42✔
49
            && "name" in alg.hash && typeof alg.hash.name === "string") {
50
            hash = alg.hash.name.toUpperCase();
40✔
51
          } else {
52
            throw new Error("Cannot get hash algorithm name");
2✔
53
          }
54

55
          switch (hash.toLowerCase()) {
44!
56
            case "sha-1":
57
              return new AlgorithmIdentifier({ algorithm: asn1Rsa.id_sha1WithRSAEncryption, parameters: null });
4✔
58
            case "sha-256":
59
              return new AlgorithmIdentifier({ algorithm: asn1Rsa.id_sha256WithRSAEncryption, parameters: null });
4✔
60
            case "sha-384":
61
              return new AlgorithmIdentifier({ algorithm: asn1Rsa.id_sha384WithRSAEncryption, parameters: null });
34✔
62
            case "sha-512":
63
              return new AlgorithmIdentifier({ algorithm: asn1Rsa.id_sha512WithRSAEncryption, parameters: null });
×
64
          }
65
        } else {
66
          return new AlgorithmIdentifier({ algorithm: asn1Rsa.id_rsaEncryption, parameters: null });
2✔
67
        }
68
        break;
2✔
69
      case "rsa-pss":
70
        if ("hash" in alg) {
2!
71
          if (!("saltLength" in alg && typeof alg.saltLength === "number")) {
2!
72
            throw new Error("Cannot get 'saltLength' from 'alg' argument");
×
73
          }
74
          const pssParams = RsaAlgorithm.createPssParams(alg.hash, alg.saltLength);
2✔
75
          if (!pssParams) {
2!
76
            throw new Error("Cannot create PSS parameters");
×
77
          }
78

79
          return new AlgorithmIdentifier({ algorithm: asn1Rsa.id_RSASSA_PSS, parameters: AsnConvert.serialize(pssParams) });
2✔
80
        } else {
81
          return new AlgorithmIdentifier({ algorithm: asn1Rsa.id_RSASSA_PSS, parameters: null });
×
82
        }
83
        break;
×
84
    }
85

86
    return null;
6✔
87
  }
88

89
  public toWebAlgorithm(alg: AlgorithmIdentifier): Algorithm | HashedAlgorithm | null {
90
    switch (alg.algorithm) {
278!
91
      case asn1Rsa.id_rsaEncryption:
92
        return { name: "RSASSA-PKCS1-v1_5" };
156✔
93
      case asn1Rsa.id_sha1WithRSAEncryption:
94
        return { name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-1" } };
2✔
95
      case asn1Rsa.id_sha256WithRSAEncryption:
96
        return { name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-256" } };
66✔
97
      case asn1Rsa.id_sha384WithRSAEncryption:
98
        return { name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-384" } };
34✔
99
      case asn1Rsa.id_sha512WithRSAEncryption:
100
        return { name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-512" } };
×
101
      case asn1Rsa.id_RSASSA_PSS:
102
        if (alg.parameters) {
4!
103
          const pssParams = AsnConvert.parse(alg.parameters, asn1Rsa.RsaSaPssParams);
4✔
104
          const algProv = container.resolve<AlgorithmProvider>(diAlgorithmProvider);
4✔
105
          const hashAlg = algProv.toWebAlgorithm(pssParams.hashAlgorithm);
4✔
106

107
          return {
4✔
108
            name: "RSA-PSS",
109
            hash: hashAlg,
110
            saltLength: pssParams.saltLength,
111
          } as Algorithm;
112
        } else {
113
          return { name: "RSA-PSS" };
×
114
        }
115
    }
116

117
    return null;
16✔
118
  }
119

120
}
121

122
// register RSA algorithm provider as a singleton object
123
container.registerSingleton(diAlgorithm, RsaAlgorithm);
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc