How to use the pkijs.PrivateKeyInfo function in pkijs

To help you get started, we’ve selected a few pkijs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PeculiarVentures / node-webcrypto-p11 / src / mechs / rsa / crypto.ts View on Github external
public static async exportKey(session: graphene.Session, format: KeyFormat, key: RsaCryptoKey): Promise {
    switch (format.toLowerCase()) {
      case "jwk":
        if (key.type === "private") {
          return this.exportJwkPrivateKey(key);
        } else {
          return this.exportJwkPublicKey(key);
        }
      case "pkcs8": {
        const jwk = await this.exportJwkPrivateKey(key);
        const privateKey = new PrivateKeyInfo();
        privateKey.fromJSON(jwk);
        return privateKey.toSchema(true).toBER(false);
      }
      case "spki": {
        const jwk = await this.exportJwkPublicKey(key);
        const publicKey = new PublicKeyInfo();
        publicKey.fromJSON(jwk);
        return publicKey.toSchema(true).toBER(false);
      }
      case "raw": {
        // export subjectPublicKey BIT_STRING value
        const jwk = await this.exportJwkPublicKey(key);
        const publicKey = new PublicKeyInfo();
        publicKey.fromJSON(jwk);
        return publicKey.toSchema(true).valueBlock.value[1].valueBlock.valueHex;
      }
github PeculiarVentures / node-webcrypto-p11 / src / mechs / ec / crypto.ts View on Github external
protected static pkcs2jwk(raw: ArrayBuffer): JsonWebKey {
    const asn1Pkcs8 = Asn1Js.fromBER(raw);
    const pkcs8 = new pkijs.PrivateKeyInfo({ schema: asn1Pkcs8.result });

    if (pkcs8.privateKeyAlgorithm.algorithmId !== "1.2.840.10045.2.1") {
      throw new Error("PKCS8 is not EC private key");
    }

    const algId = pkcs8.privateKeyAlgorithm.algorithmParams.valueBlock.toString();
    let crvName = algId;

    switch (crvName) {
      case "1.3.132.0.10": // K-256
        crvName = "K-256";
        break;
      case "1.2.840.10045.3.1.7": // P-256
        crvName = "P-256";
        break;
      case "1.3.132.0.34": // P-384
github PeculiarVentures / node-webcrypto-p11 / lib / crypto / rsa.ts View on Github external
public static async exportKey(format: string, key: CryptoKey, session?: Session): Promise {
        await super.exportKey.call(this, format, key, session);
        switch (format.toLowerCase()) {
            case "jwk":
                if (key.type === "private") {
                    return this.exportJwkPrivateKey(key);
                } else {
                    return this.exportJwkPublicKey(key);
                }
            case "pkcs8": {
                const jwk = await this.exportJwkPrivateKey(key);
                const privateKey = new PrivateKeyInfo();
                privateKey.fromJSON(jwk);
                return privateKey.toSchema(true).toBER(false);
            }
            case "spki": {
                const jwk = await this.exportJwkPublicKey(key);
                const publicKey = new PublicKeyInfo();
                publicKey.fromJSON(jwk);
                return publicKey.toSchema(true).toBER(false);
            }
            case "raw": {
                // export subjectPublicKey BIT_STRING value
                const jwk = await this.exportJwkPublicKey(key);
                const publicKey = new PublicKeyInfo();
                publicKey.fromJSON(jwk);
                return publicKey.toSchema(true).valueBlock.value[1].valueBlock.valueHex;
            }
github PeculiarVentures / node-webcrypto-p11 / lib / crypto / ec.ts View on Github external
function pkcs2jwk(raw: ArrayBuffer): JsonWebKey {
    const asn1Pkcs8 = Asn1Js.fromBER(raw);
    const pkcs8 = new PrivateKeyInfo({ schema: asn1Pkcs8.result });

    if (pkcs8.privateKeyAlgorithm.algorithmId !== "1.2.840.10045.2.1") {
        throw new Error("PKCS8 is not EC private key");
    }

    const algId = pkcs8.privateKeyAlgorithm.algorithmParams.valueBlock.toString();
    let crvName = algId;

    switch (crvName) {
        case "1.3.132.0.10": // K-256
            crvName = "K-256";
            break;
        case "1.2.840.10045.3.1.7": // P-256
            crvName = "P-256";
            break;
        case "1.3.132.0.34": // P-384
github PeculiarVentures / node-webcrypto-p11 / lib / crypto / ec.ts View on Github external
break;
            default:
        }
    } else {
        throw new Error("Absent mandatory parameter \"crv\"");
    }

    ["d"].forEach((name) => {
        if (name in jwk) {
            parsedKey.privateKey = new Asn1Js.OctetString({ valueHex: getCoordinate((jwk as any)[name], coordinateLength) });
        } else {
            throw new Error(`Absent mandatory parameter '${name}'`);
        }
    });

    const pkcs8 = new PrivateKeyInfo();
    pkcs8.privateKeyAlgorithm = new AlgorithmIdentifier({
        algorithmId: "1.2.840.10045.2.1",
        algorithmParams: new Asn1Js.ObjectIdentifier({ value: parsedKey.namedCurve }),
    });
    pkcs8.privateKey = new Asn1Js.OctetString({ valueHex: parsedKey.toSchema().toBER(false) });

    return pkcs8.toSchema().toBER(false);
}
github PeculiarVentures / node-webcrypto-p11 / src / mechs / rsa / crypto.ts View on Github external
return this.importJwkPrivateKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
        } else {
          return this.importJwkPublicKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
        }
      case "spki": {
        const arBuf = new Uint8Array(keyData as Uint8Array).buffer as ArrayBuffer;
        const asn1 = asn1js.fromBER(arBuf);

        const jwk = new PublicKeyInfo({ schema: asn1.result }).toJSON();
        return this.importJwkPublicKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
      }
      case "pkcs8": {
        const arBuf = new Uint8Array(keyData as Uint8Array).buffer as ArrayBuffer;
        const asn1 = asn1js.fromBER(arBuf);

        const jwk = new PrivateKeyInfo({ schema: asn1.result }).toJSON();
        return this.importJwkPrivateKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
      }
      default:
        throw new core.OperationError("format: Must be 'jwk', 'pkcs8' or 'spki'");
    }
  }
github PeculiarVentures / node-webcrypto-p11 / src / mechs / ec / crypto.ts View on Github external
break;
        default:
      }
    } else {
      throw new Error("Absent mandatory parameter \"crv\"");
    }

    ["d"].forEach((name) => {
      if (name in jwk) {
        parsedKey.privateKey = new Asn1Js.OctetString({ valueHex: this.getCoordinate((jwk as any)[name], coordinateLength) });
      } else {
        throw new Error(`Absent mandatory parameter '${name}'`);
      }
    });

    const pkcs8 = new pkijs.PrivateKeyInfo();
    pkcs8.privateKeyAlgorithm = new pkijs.AlgorithmIdentifier({
      algorithmId: "1.2.840.10045.2.1",
      algorithmParams: new Asn1Js.ObjectIdentifier({ value: parsedKey.namedCurve }),
    });
    pkcs8.privateKey = new Asn1Js.OctetString({ valueHex: parsedKey.toSchema().toBER(false) });

    return pkcs8.toSchema().toBER(false);
  }
github PeculiarVentures / node-webcrypto-p11 / lib / crypto / rsa.ts View on Github external
return this.importJwkPrivateKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
                        } else {
                            return this.importJwkPublicKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
                        }
                    case "spki": {
                        const arBuf = new Uint8Array(keyData as Uint8Array).buffer as ArrayBuffer;
                        const asn1 = Asn1Js.fromBER(arBuf);

                        const jwk = new PublicKeyInfo({ schema: asn1.result }).toJSON();
                        return this.importJwkPublicKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
                    }
                    case "pkcs8": {
                        const arBuf = new Uint8Array(keyData as Uint8Array).buffer as ArrayBuffer;
                        const asn1 = Asn1Js.fromBER(arBuf);

                        const jwk = new PrivateKeyInfo({ schema: asn1.result }).toJSON();
                        return this.importJwkPrivateKey(session!, jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
                    }
                    default:
                        throw new Error(`Not supported format '${format}'`);
                }
            });
    }

pkijs

Public Key Infrastructure (PKI) is the basis of how identity and key management is performed on the web today. PKIjs is a pure JavaScript library implementing the formats that are used in PKI applications. It is built on WebCrypto and aspires to make it p

BSD-3-Clause
Latest version published 5 days ago

Package Health Score

81 / 100
Full package analysis