Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post quantum certificates #91

Merged
merged 9 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/asym-key/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Peculiar Ventures, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
77 changes: 77 additions & 0 deletions packages/asym-key/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# `@peculiar/asn1-asym-key`

[![License](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://raw.githubusercontent.com/PeculiarVentures/asn1-schema/master/packages/rfc5958/LICENSE.md)
[![npm version](https://badge.fury.io/js/%40peculiar%2Fasn1-rfc5958.svg)](https://badge.fury.io/js/%40peculiar%2Fasn1-rfc5958)

[![NPM](https://nodei.co/npm/@peculiar/asn1-asym-key.png)](https://nodei.co/npm/@peculiar/asn1-asym-key/)

This package provides ASN.1 schema definitions for the asymmetric key packages format specified in [RFC5958](https://datatracker.ietf.org/doc/html/rfc5958). This format is used to encapsulate private and public keys, along with additional parameters and metadata, for a variety of asymmetric key algorithms.

The schema definitions are based on `@peculiar/asn1-schema` library and can be used to encode and decode key packages in ASN.1 DER format.

## Installation

You can install the package using npm:

```sh
npm install @peculiar/asn1-asym-key
```

## Usage

The `@peculiar/asn1-asym-key` package exports several classes and constants that can be used to encode and decode key packages. Here is an example of decoding a key package for an RSA private key:

```ts
import * as assert from "node:assert";
import { OneAsymmetricKey } from "@peculiar/asn1-asym-key";
import { id_rsaEncryption, RSAPrivateKey } from "@peculiar/asn1-rsa";
import { AsnConvert } from "@peculiar/asn1-schema";

const pem = [
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC+G9YX1BboIIty",
"ZdjEnPeoJXh5J3UQak+lkO+KirllYeNQML7ent3l9KOp0tM/IfxSDmVWtyYAUoxM",
"3TGnDoJgCCn5jUQIdQkPXTxa7xeL11W4d0fqyaAOyMOXA2QZShE61dBEz9ly+bnI",
"N1MrcwLe0q/TPuKS1EBp8LS/lQzW/PrHTnDHwoGinBZq58B/ErgmPh27QlkhamdX",
"G4KxDdN2ayilmvdG06dMwrGKs2Tag5HKbUSMPA3BlKvvqrJhMb3w86xPf2MnvWzV",
"Wmi0tpE2zcCgXkX+4VtiikBW1QoFuFjiBWNJKWMXe2UKXYbdYFBPzaJiGCdiYXXJ",
"u1N16pSxAgMBAAECggEAMoUjN+Hc1IMRi60VFerAxaZvhYK+0UXvNIh3n9GNwyto",
"RP8vOent/hYtCAxGpxhY/VVmCM1TA/Lr5eFCOKraarrArNilo7m2VqboSyIOl9L5",
"rLPGINbD+inQEGmOSEoumfwIBMFXf/5tKI/LQlimAqDqqLCldW+CTqT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",
"dll8U0BCsgO6JQPk9riOsdU=",
].join("");

const key = AsnConvert.parse(Buffer.from(pem, "base64"), OneAsymmetricKey);
assert.strictEqual(key.privateKeyAlgorithm.algorithm, id_rsaEncryption);

const rsaKey = AsnConvert.parse(key.privateKey, RSAPrivateKey);
console.log(rsaKey);
/*
RSAPrivateKey {
version: 0,
modulus: Uint8Array(257) [ 0, 190, 27, 214, ...],
publicExponent: Uint8Array(3) [ 1, 0, 1 ],
privateExponent: Uint8Array(256) [ 50, 133, 35, 55, ...],
prime1: Uint8Array(129) [ 0, 238, 234, 84, ...],
prime2: Uint8Array(129) [ 0, 203, 180, 9, ...],
exponent1: Uint8Array(128) [ 26, 227, 164, 179, ...],
exponent2: Uint8Array(128) [ 98, 192, 77, 183, ...],
coefficient: Uint8Array(129) [ 0, 185, 67, 198, ...]
}
*/
```
48 changes: 48 additions & 0 deletions packages/asym-key/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@peculiar/asn1-asym-key",
"version": "2.3.4",
"description": "Provides ASN.1 schema definitions for the asymmetric key packages format specified in RFC5958. This format is used to encapsulate private and public keys, along with additional parameters and metadata, for a variety of asymmetric key algorithms.",
"keywords": [
"asn",
"rfc5958"
],
"author": "PeculiarVentures, Inc",
"license": "MIT",
"main": "build/cjs/index.js",
"files": [
"build",
"LICENSE",
"README.md"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/PeculiarVentures/asn1-schema.git"
},
"scripts": {
"test": "mocha",
"clear": "rimraf build",
"build": "npm run build:module && npm run build:types",
"build:module": "npm run build:cjs && npm run build:es2015",
"build:cjs": "tsc -p tsconfig.compile.json --removeComments --module commonjs --outDir build/cjs",
"build:es2015": "tsc -p tsconfig.compile.json --removeComments --module ES2015 --outDir build/es2015",
"prebuild:types": "rimraf build/types",
"build:types": "tsc -p tsconfig.compile.json --outDir build/types --declaration --emitDeclarationOnly",
"rebuild": "npm run clear && npm run build"
},
"dependencies": {
"@peculiar/asn1-pkcs8": "^2.3.4",
"@peculiar/asn1-schema": "^2.3.3",
"@peculiar/asn1-x509": "^2.3.4",
"asn1js": "^3.0.5",
"tslib": "^2.4.0"
},
"bugs": {
"url": "https://github.com/PeculiarVentures/asn1-schema/issues"
},
"homepage": "https://github.com/PeculiarVentures/asn1-schema#readme",
"module": "build/es2015/index.js",
"types": "build/types/index.d.ts"
}
159 changes: 159 additions & 0 deletions packages/asym-key/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { Attributes } from "@peculiar/asn1-pkcs8";
import { AsnArray, AsnProp, AsnPropTypes, AsnType, AsnTypeTypes, BitString, OctetString } from "@peculiar/asn1-schema";
import { AlgorithmIdentifier } from "@peculiar/asn1-x509";

/**
* ```asn
* id-ct-KP-aKeyPackage OBJECT IDENTIFIER ::=
* { joint-iso-itu-t(2) country(16) us(840) organization(1)
* gov(101) dod(2) infosec(1) formats(2)
* key-package-content-types(78) 5
* }
* ```
*/
export const id_ct_KP_aKeyPackage = "2.16.840.1.101.2.1.78.5";

/**
* ```asn
* Version ::= INTEGER { v1(0), v2(1) } (v1, ..., v2)
* ```
*/
export enum Version {
v1,
v2,
}

/**
* ```asn
* PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
* { PUBLIC-KEY,
* { PrivateKeyAlgorithms } }
* ```
*/
@AsnType({ type: AsnTypeTypes.Sequence })
export class PrivateKeyAlgorithmIdentifier extends AlgorithmIdentifier { }

/**
* ```asn
* PrivateKey ::= OCTET STRING
* -- Content varies based on type of key. The
* -- algorithm identifier dictates the format of
* -- the key.
* ```
*/
export class PrivateKey extends OctetString { }

/**
* ```asn
* PublicKey ::= BIT STRING
* -- Content varies based on type of key. The
* -- algorithm identifier dictates the format of
* -- the key.
* ```
*/
export class PublicKey extends BitString { }

/**
* ```asn
* OneAsymmetricKey ::= SEQUENCE {
* version Version,
* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
* privateKey PrivateKey,
* attributes [0] Attributes OPTIONAL,
* ...,
* [[2: publicKey [1] PublicKey OPTIONAL ]],
* ...
* }
* ```
*/
@AsnType({ type: AsnTypeTypes.Sequence })
export class OneAsymmetricKey {

/**
* Identifies the version of the OneAsymmetricKey.
*
* @remarks
* If the publicKey is present, then the version is set to v2; otherwise, the version is set to v1.
*/
@AsnProp({ type: AsnPropTypes.Integer })
public version = Version.v1;

/**
* identifies the private-key algorithm and
* optionally contains parameters associated with the asymmetric key
* pair. The algorithm is identified by an object identifier (OID)
* and the format of the parameters depends on the OID, but the
* PrivateKeyAlgorithms information object set restricts the
* permissible OIDs. The value placed in
* privateKeyAlgorithmIdentifier is the value an originator would
* apply to indicate which algorithm is to be used with the private
* key.
*/
@AsnProp({ type: AlgorithmIdentifier })
public privateKeyAlgorithm = new AlgorithmIdentifier();

/**
* OCTET STRING that contains the value of the
* private key. The interpretation of the content is defined in the
* registration of the private-key algorithm. For example, a DSA key
* is an INTEGER, an RSA key is represented as RSAPrivateKey as
* defined in [RFC3447], and an Elliptic Curve Cryptography (ECC) key
* is represented as ECPrivateKey as defined in [RFC5915].
*/
@AsnProp({ type: AsnPropTypes.OctetString })
public privateKey = new ArrayBuffer(0);

/**
* Contains information corresponding to
* the public key (e.g., certificates). The attributes field uses the
* class ATTRIBUTE which is restricted by the
* OneAsymmetricKeyAttributes information object set.
* OneAsymmetricKeyAttributes is an open ended set in this document.
* Others documents can constrain these values. Attributes from
* [RFC2985] MAY be supported.
*/
@AsnProp({ type: Attributes, context: 0, implicit: true, optional: true })
public attributes?: Attributes;

// "..." - omitted properties

/**
* When present, it contains the public key
* encoded in a BIT STRING. The structure within the BIT STRING, if
* any, depends on the privateKeyAlgorithm. For example, a DSA key is
* an INTEGER. Note that RSA public keys are included in
* RSAPrivateKey (i.e., n and e are present), as per [RFC3447], and
* ECC public keys are included in ECPrivateKey (i.e., in the
* publicKey field), as per [RFC5915].
*/
@AsnProp({ type: AsnPropTypes.BitString, context: 1, implicit: true, optional: true })
public publicKey?: ArrayBuffer;

}

/**
* ```asn
* PrivateKeyInfo ::= OneAsymmetricKey
* ```
*/
@AsnType({ type: AsnTypeTypes.Sequence })
export class PrivateKeyInfo extends OneAsymmetricKey { }

/**
* ```asn
* AsymmetricKeyPackage ::= SEQUENCE SIZE (1..MAX) OF OneAsymmetricKey
* ```
*/
@AsnType({ type: AsnTypeTypes.Sequence, itemType: OneAsymmetricKey })
export class AsymmetricKeyPackage extends AsnArray<OneAsymmetricKey>{

constructor(items?: OneAsymmetricKey[]) {
super(items);

// Set the prototype explicitly.
Object.setPrototypeOf(this, AsymmetricKeyPackage.prototype);
}

}

export { EncryptedPrivateKeyInfo, EncryptedData } from "@peculiar/asn1-pkcs8";
42 changes: 42 additions & 0 deletions packages/asym-key/test/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as assert from "node:assert";
import { OneAsymmetricKey } from "@peculiar/asn1-asym-key";
import { AsnConvert } from "@peculiar/asn1-schema";
import { id_rsaEncryption } from "@peculiar/asn1-rsa";

context("RFC5958", () => {

it("RSA private key", () => {
const pem = [
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC+G9YX1BboIIty",
"ZdjEnPeoJXh5J3UQak+lkO+KirllYeNQML7ent3l9KOp0tM/IfxSDmVWtyYAUoxM",
"3TGnDoJgCCn5jUQIdQkPXTxa7xeL11W4d0fqyaAOyMOXA2QZShE61dBEz9ly+bnI",
"N1MrcwLe0q/TPuKS1EBp8LS/lQzW/PrHTnDHwoGinBZq58B/ErgmPh27QlkhamdX",
"G4KxDdN2ayilmvdG06dMwrGKs2Tag5HKbUSMPA3BlKvvqrJhMb3w86xPf2MnvWzV",
"Wmi0tpE2zcCgXkX+4VtiikBW1QoFuFjiBWNJKWMXe2UKXYbdYFBPzaJiGCdiYXXJ",
"u1N16pSxAgMBAAECggEAMoUjN+Hc1IMRi60VFerAxaZvhYK+0UXvNIh3n9GNwyto",
"RP8vOent/hYtCAxGpxhY/VVmCM1T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xcKR9Yv8tT//s9qzrqqwLk",
"pCuOtlM+ru5rJRVV2/Nm2izNSn9IePGPmLhCUZUVZzQqUR0d3caNKtR8urR04Y15",
"dll8U0BCsgO6JQPk9riOsdU=",
].join("");

const key = AsnConvert.parse(Buffer.from(pem, "base64"), OneAsymmetricKey);
assert.strictEqual(key.privateKeyAlgorithm.algorithm, id_rsaEncryption);
});

});
6 changes: 6 additions & 0 deletions packages/asym-key/tsconfig.compile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.compile.json",
"include": [
"src"
]
}
21 changes: 21 additions & 0 deletions packages/x509-post-quantum/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Peculiar Ventures, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.