How to use the pkijs.PublicKeyInfo 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 / lib / crypto / rsa.ts View on Github external
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;
            }
            default:
                throw new Error(`Not supported format '${format}'`);
        }
    }
github PeculiarVentures / node-webcrypto-p11 / lib / crypto / ec.ts View on Github external
}
            case "pkcs8": {
                const jwk = await this.exportJwkPrivateKey(key);
                return jwk2pkcs(jwk);
            }
            case "spki": {
                const jwk = await this.exportJwkPublicKey(key);
                return jwk2spki(jwk);
            }
            case "raw": {
                // export subjectPublicKey BIT_STRING value
                const jwk = await this.exportJwkPublicKey(key);
                if ((key.algorithm as EcKeyGenParams).namedCurve === "X25519") {
                    return Base64Url.decode(jwk.x!).buffer as ArrayBuffer;
                } else {
                    const publicKey = new PublicKeyInfo();
                    publicKey.fromJSON(jwk);
                    return publicKey.toSchema(true).valueBlock.value[1].valueBlock.valueHex;
                }
            }
            default:
                throw new Error(`Not supported format '${format}'`);
        }
    }
github PeculiarVentures / node-webcrypto-p11 / src / mechs / ec / crypto.ts View on Github external
}
      case "pkcs8": {
        const jwk = await this.exportJwkPrivateKey(key);
        return this.jwk2pkcs(jwk);
      }
      case "spki": {
        const jwk = await this.exportJwkPublicKey(key);
        return this.jwk2spki(jwk);
      }
      case "raw": {
        // export subjectPublicKey BIT_STRING value
        const jwk = await this.exportJwkPublicKey(key);
        if ((key.algorithm as EcKeyGenParams).namedCurve === "X25519") {
          return Convert.FromBase64Url(jwk.x);
        } else {
          const publicKey = new pkijs.PublicKeyInfo();
          publicKey.fromJSON(jwk);
          return publicKey.toSchema(true).valueBlock.value[1].valueBlock.valueHex;
        }
      }
      default:
        throw new core.OperationError("format: Must be 'jwk', 'raw', pkcs8' or 'spki'");
    }
  }
github PeculiarVentures / node-webcrypto-p11 / src / mechs / rsa / crypto.ts View on Github external
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;
      }
      default:
        throw new core.OperationError("format: Must be 'jwk', 'pkcs8' or 'spki'");
    }
  }
github PeculiarVentures / node-webcrypto-p11 / lib / crypto / ec.ts View on Github external
function spki2jwk(raw: ArrayBuffer): JsonWebKey {
    const asn1Spki = Asn1Js.fromBER(raw);
    const spki = new PublicKeyInfo({ schema: asn1Spki.result });

    if (spki.algorithm.algorithmId !== "1.2.840.10045.2.1") {
        throw new Error("SPKI is not EC public key");
    }

    const algId = spki.algorithm.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 / src / mechs / rsa / crypto.ts View on Github external
public static async importKey(session: graphene.Session, format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: RsaHashedImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise {
    switch (format.toLowerCase()) {
      case "jwk":
        const jwk: any = keyData;
        if (jwk.d) {
          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 / lib / crypto / ec.ts View on Github external
break;
            default:
        }
    } else {
        throw new Error("Absent mandatory parameter \"crv\"");
    }

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

    const spki = new PublicKeyInfo();
    spki.algorithm = new AlgorithmIdentifier({
        algorithmId: "1.2.840.10045.2.1",
        algorithmParams: new Asn1Js.ObjectIdentifier({ value: parsedKey.namedCurve }),
    });
    spki.subjectPublicKey = new Asn1Js.BitString({ valueHex: parsedKey.toSchema().toBER(false) });

    return spki.toSchema().toBER(false);
}
github PeculiarVentures / node-webcrypto-p11 / src / mechs / ec / crypto.ts View on Github external
break;
        default:
      }
    } else {
      throw new Error("Absent mandatory parameter \"crv\"");
    }

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

    const spki = new pkijs.PublicKeyInfo();
    spki.algorithm = new pkijs.AlgorithmIdentifier({
      algorithmId: "1.2.840.10045.2.1",
      algorithmParams: new Asn1Js.ObjectIdentifier({ value: parsedKey.namedCurve }),
    });
    spki.subjectPublicKey = new Asn1Js.BitString({ valueHex: parsedKey.toSchema().toBER(false) });

    return spki.toSchema().toBER(false);
  }
github PeculiarVentures / node-webcrypto-p11 / lib / crypto / rsa.ts View on Github external
.then(() => {
                switch (format.toLowerCase()) {
                    case "jwk":
                        const jwk: any = keyData;
                        if (jwk.d) {
                            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