-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(website): add few source examples
- Loading branch information
Showing
21 changed files
with
375 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
website/docs/examples/certificates-and-revocation/create-and-validate-certificate.example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as pkijs from 'pkijs'; | ||
import * as asn1js from 'asn1js'; | ||
|
||
const crypto = pkijs.getCrypto(true); | ||
|
||
// Create certificate | ||
const certificate = new pkijs.Certificate(); | ||
certificate.version = 2; | ||
certificate.serialNumber = new asn1js.Integer({ value: 1 }); | ||
certificate.issuer.typesAndValues.push(new pkijs.AttributeTypeAndValue({ | ||
type: "2.5.4.3", // Common name | ||
value: new asn1js.BmpString({ value: "Test" }) | ||
})); | ||
certificate.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({ | ||
type: "2.5.4.3", // Common name | ||
value: new asn1js.BmpString({ value: "Test" }) | ||
})); | ||
|
||
certificate.notBefore.value = new Date(); | ||
const notAfter = new Date(); | ||
notAfter.setUTCFullYear(notAfter.getUTCFullYear() + 1); | ||
certificate.notAfter.value = notAfter; | ||
|
||
certificate.extensions = []; // Extensions are not a part of certificate by default, it's an optional array | ||
|
||
// "BasicConstraints" extension | ||
const basicConstr = new pkijs.BasicConstraints({ | ||
cA: true, | ||
pathLenConstraint: 3 | ||
}); | ||
certificate.extensions.push(new pkijs.Extension({ | ||
extnID: "2.5.29.19", | ||
critical: false, | ||
extnValue: basicConstr.toSchema().toBER(false), | ||
parsedValue: basicConstr // Parsed value for well-known extensions | ||
})); | ||
|
||
// "KeyUsage" extension | ||
const bitArray = new ArrayBuffer(1); | ||
const bitView = new Uint8Array(bitArray); | ||
bitView[0] |= 0x02; // Key usage "cRLSign" flag | ||
bitView[0] |= 0x04; // Key usage "keyCertSign" flag | ||
const keyUsage = new asn1js.BitString({ valueHex: bitArray }); | ||
certificate.extensions.push(new pkijs.Extension({ | ||
extnID: "2.5.29.15", | ||
critical: false, | ||
extnValue: keyUsage.toBER(false), | ||
parsedValue: keyUsage // Parsed value for well-known extensions | ||
})); | ||
|
||
const algorithm = pkijs.getAlgorithmParameters("RSASSA-PKCS1-v1_5", "generateKey"); | ||
if ("hash" in algorithm.algorithm) { | ||
algorithm.algorithm.hash.name = "SHA-256"; | ||
} | ||
|
||
const keys = await crypto.generateKey(algorithm.algorithm, true, algorithm.usages); | ||
|
||
// Exporting public key into "subjectPublicKeyInfo" value of certificate | ||
await certificate.subjectPublicKeyInfo.importKey(keys.publicKey); | ||
|
||
// Signing final certificate | ||
await certificate.sign(keys.privateKey, "SHA-256"); | ||
|
||
const raw = certificate.toSchema().toBER(); |
3 changes: 0 additions & 3 deletions
3
...te/docs/examples/certificates-and-revocation/create-and-validate-certificate.md
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
...e/docs/examples/certificates-and-revocation/create-and-validate-certificate.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import CodeBlock from '@theme/CodeBlock'; | ||
import source from '!!raw-loader!./create-and-validate-certificate.example'; | ||
|
||
# Create and validate certificate | ||
|
||
In this example, you will see how to create a self-signed X.509 certificate, parse an X.509 certificate, and show how to do certificate chain validation engine. | ||
|
||
<CodeBlock language="ts"> | ||
{source} | ||
</CodeBlock> |
26 changes: 26 additions & 0 deletions
26
website/docs/examples/certificates-and-revocation/working-with-OCSP-requests.example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as pkijs from 'pkijs'; | ||
import * as asn1js from 'asn1js'; | ||
|
||
// Create OCSP request | ||
const ocspReq = new pkijs.OCSPRequest(); | ||
|
||
ocspReq.tbsRequest.requestorName = new pkijs.GeneralName({ | ||
type: 4, | ||
value: cert.subject, | ||
}); | ||
|
||
await ocspReq.createForCertificate(cert, { | ||
hashAlgorithm: "SHA-256", | ||
issuerCertificate: issuerCert, | ||
}); | ||
|
||
const nonce = pkijs.getRandomValues(new Uint8Array(10)); | ||
ocspReq.tbsRequest.requestExtensions = [ | ||
new pkijs.Extension({ | ||
extnID: "1.3.6.1.5.5.7.48.1.2", // nonce | ||
extnValue: new asn1js.OctetString({ valueHex: nonce.buffer }).toBER(), | ||
}) | ||
]; | ||
|
||
// Encode OCSP request | ||
const ocspReqRaw = ocspReq.toSchema(true).toBER(); |
3 changes: 0 additions & 3 deletions
3
website/docs/examples/certificates-and-revocation/working-with-OCSP-requests.md
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
website/docs/examples/certificates-and-revocation/working-with-OCSP-requests.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import CodeBlock from '@theme/CodeBlock'; | ||
import source from '!!raw-loader!./working-with-OCSP-requests.example'; | ||
|
||
# Working with OCSP requests | ||
|
||
In this example, you will see how to create and parse OCSP requests. | ||
|
||
|
||
<CodeBlock language="ts"> | ||
{source} | ||
</CodeBlock> |
45 changes: 45 additions & 0 deletions
45
website/docs/examples/certificates-and-revocation/working-with-OCSP-responses.example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as pkijs from 'pkijs'; | ||
import * as asn1js from 'asn1js'; | ||
|
||
const ocspBasicResp = new pkijs.BasicOCSPResponse(); | ||
|
||
// Create specific TST info structure to sign | ||
ocspBasicResp.tbsResponseData.responderID = issuerCert.subject; | ||
ocspBasicResp.tbsResponseData.producedAt = new Date(); | ||
|
||
const certID = new pkijs.CertID(); | ||
await certID.createForCertificate(cert, { | ||
hashAlgorithm: "SHA-256", | ||
issuerCertificate: issuerCert, | ||
}); | ||
const response = new pkijs.SingleResponse({ | ||
certID, | ||
}); | ||
response.certStatus = new asn1js.Primitive({ | ||
idBlock: { | ||
tagClass: 3, // CONTEXT-SPECIFIC | ||
tagNumber: 0 // [0] | ||
}, | ||
lenBlockLength: 1 // The length contains one byte 0x00 | ||
}); // status - success | ||
response.thisUpdate = new Date(); | ||
|
||
ocspBasicResp.tbsResponseData.responses.push(response); | ||
|
||
// Add certificates for chain OCSP response validation | ||
ocspBasicResp.certs = [issuerCert]; | ||
|
||
await ocspBasicResp.sign(keys.privateKey, "SHA-256"); | ||
|
||
// Finally create completed OCSP response structure | ||
const ocspBasicRespRaw = ocspBasicResp.toSchema().toBER(false); | ||
|
||
const ocspResp = new pkijs.OCSPResponse({ | ||
responseStatus: new asn1js.Enumerated({ value: 0 }), // success | ||
responseBytes: new pkijs.ResponseBytes({ | ||
responseType: pkijs.id_PKIX_OCSP_Basic, | ||
response: new asn1js.OctetString({ valueHex: ocspBasicRespRaw }), | ||
}), | ||
}); | ||
|
||
const ocspRespRaw = ocspResp.toSchema().toBER(); |
3 changes: 0 additions & 3 deletions
3
website/docs/examples/certificates-and-revocation/working-with-OCSP-responses.md
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
website/docs/examples/certificates-and-revocation/working-with-OCSP-responses.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import CodeBlock from '@theme/CodeBlock'; | ||
import source from '!!raw-loader!./working-with-OCSP-responses.example'; | ||
|
||
# Working with OCSP responses | ||
|
||
In this example, you will see how to create and parse OCSP responses. | ||
|
||
<CodeBlock language="ts"> | ||
{source} | ||
</CodeBlock> |
61 changes: 61 additions & 0 deletions
61
...te/docs/examples/certificates-and-revocation/working-with-certificate-requests.example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as pkijs from 'pkijs'; | ||
import * as asn1js from 'asn1js'; | ||
|
||
// Get a "crypto" extension | ||
const crypto = pkijs.getCrypto(true); | ||
|
||
const pkcs10 = new pkijs.CertificationRequest(); | ||
|
||
pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({ | ||
type: "2.5.4.3", | ||
value: new asn1js.Utf8String({ value: "Test" }) | ||
})); | ||
|
||
await pkcs10.subjectPublicKeyInfo.importKey(keys.publicKey); | ||
|
||
pkcs10.attributes = []; | ||
|
||
// Subject Alternative Name | ||
const altNames = new pkijs.GeneralNames({ | ||
names: [ | ||
new pkijs.GeneralName({ // email | ||
type: 1, | ||
value: "[email protected]" | ||
}), | ||
new pkijs.GeneralName({ // domain | ||
type: 2, | ||
value: "www.domain.com" | ||
}), | ||
] | ||
}); | ||
|
||
// SubjectKeyIdentifier | ||
const subjectKeyIdentifier = await crypto.digest({ name: "SHA-1" }, pkcs10.subjectPublicKeyInfo.subjectPublicKey.valueBlock.valueHex); | ||
|
||
pkcs10.attributes.push(new pkijs.Attribute({ | ||
type: "1.2.840.113549.1.9.14", // pkcs-9-at-extensionRequest | ||
values: [(new pkijs.Extensions({ | ||
extensions: [ | ||
new pkijs.Extension({ | ||
extnID: "2.5.29.14", // id-ce-subjectKeyIdentifier | ||
critical: false, | ||
extnValue: (new asn1js.OctetString({ valueHex: subjectKeyIdentifier })).toBER(false) | ||
}), | ||
new pkijs.Extension({ | ||
extnID: "2.5.29.17", // id-ce-subjectAltName | ||
critical: false, | ||
extnValue: altNames.toSchema().toBER(false) | ||
}), | ||
new pkijs.Extension({ | ||
extnID: "1.2.840.113549.1.9.7", // pkcs-9-at-challengePassword | ||
critical: false, | ||
extnValue: (new asn1js.PrintableString({ value: "passwordChallenge" })).toBER(false) | ||
}) | ||
] | ||
})).toSchema()] | ||
})); | ||
|
||
// Signing final PKCS#10 request | ||
await pkcs10.sign(keys.privateKey, "SHA-256"); | ||
|
||
const pkcs10Raw = pkcs10.toSchema(true).toBER(); |
3 changes: 0 additions & 3 deletions
3
.../docs/examples/certificates-and-revocation/working-with-certificate-requests.md
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
...docs/examples/certificates-and-revocation/working-with-certificate-requests.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import CodeBlock from '@theme/CodeBlock'; | ||
import source from '!!raw-loader!./working-with-certificate-requests.example'; | ||
|
||
# Working with certificate requests | ||
|
||
In this example, you will see how to create a CSR, parse it and verify its signature. | ||
|
||
<CodeBlock language="ts"> | ||
{source} | ||
</CodeBlock> |
14 changes: 14 additions & 0 deletions
14
website/docs/examples/timestamping/creating-a-timestamp-request.example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as pkijs from 'pkijs'; | ||
import * as asn1js from 'asn1js'; | ||
|
||
const nonce = pkijs.getRandomValues(new Uint8Array(10)).buffer; | ||
|
||
const tspReq = new pkijs.TimeStampReq({ | ||
version: 1, | ||
messageImprint: await pkijs.MessageImprint.create("SHA-256", message), | ||
reqPolicy: "1.2.3.4.5.6", | ||
certReq: true, | ||
nonce: new asn1js.Integer({ valueHex: nonce }), | ||
}); | ||
|
||
const tspReqRaw = tspReq.toSchema().toBER(); |
3 changes: 0 additions & 3 deletions
3
website/docs/examples/timestamping/creating-a-timestamp-request.md
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
website/docs/examples/timestamping/creating-a-timestamp-request.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import CodeBlock from '@theme/CodeBlock'; | ||
import source from '!!raw-loader!./creating-a-timestamp-request.example'; | ||
|
||
# Creating a Timestamp request | ||
|
||
In this example, you will see how to create and parse a timestamp request. | ||
|
||
<CodeBlock language="ts"> | ||
{source} | ||
</CodeBlock> |
56 changes: 56 additions & 0 deletions
56
website/docs/examples/timestamping/creating-a-timestamp-response.example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as pkijs from 'pkijs'; | ||
import * as asn1js from 'asn1js'; | ||
|
||
// Generate random serial number | ||
const serialNumber = pkijs.getRandomValues(new Uint8Array(10)).buffer; | ||
|
||
// Create specific TST info structure to sign | ||
const tstInfo = new pkijs.TSTInfo({ | ||
version: 1, | ||
policy: tspReq.reqPolicy, | ||
messageImprint: tspReq.messageImprint, | ||
serialNumber: new asn1js.Integer({ valueHex: serialNumber }), | ||
genTime: new Date(), | ||
ordering: true, | ||
accuracy: new pkijs.Accuracy({ | ||
seconds: 1, | ||
millis: 1, | ||
micros: 10 | ||
}), | ||
nonce: tspReq.nonce, | ||
}); | ||
|
||
// Create and sign CMS Signed Data with TSTInfo | ||
const cmsSigned = new pkijs.SignedData({ | ||
version: 3, | ||
encapContentInfo: new pkijs.EncapsulatedContentInfo({ | ||
eContentType: "1.2.840.113549.1.9.16.1.4", // "tSTInfo" content type | ||
eContent: new asn1js.OctetString({ valueHex: tstInfo.toSchema().toBER() }), | ||
}), | ||
signerInfos: [ | ||
new pkijs.SignerInfo({ | ||
version: 1, | ||
sid: new pkijs.IssuerAndSerialNumber({ | ||
issuer: cert.issuer, | ||
serialNumber: cert.serialNumber | ||
}) | ||
}) | ||
], | ||
certificates: [cert] | ||
}); | ||
|
||
await cmsSigned.sign(keys.privateKey, 0, "SHA-256"); | ||
|
||
// Create CMS Content Info | ||
const cmsContent = new pkijs.ContentInfo({ | ||
contentType: pkijs.ContentInfo.SIGNED_DATA, | ||
content: cmsSigned.toSchema(true) | ||
}); | ||
|
||
// Finally create completed TSP response structure | ||
const tspResp = new pkijs.TimeStampResp({ | ||
status: new pkijs.PKIStatusInfo({ status: pkijs.PKIStatus.granted }), | ||
timeStampToken: new pkijs.ContentInfo({ schema: cmsContent.toSchema() }) | ||
}); | ||
|
||
const tspRespRaw = tspResp.toSchema().toBER(); |
3 changes: 0 additions & 3 deletions
3
website/docs/examples/timestamping/creating-a-timestamp-response.md
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
website/docs/examples/timestamping/creating-a-timestamp-response.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import CodeBlock from '@theme/CodeBlock'; | ||
import source from '!!raw-loader!./creating-a-timestamp-response.example'; | ||
|
||
# Creating a Timestamp response | ||
|
||
In this example, you will see how to create, parse and verify timestamp responses. | ||
|
||
<CodeBlock language="ts"> | ||
{source} | ||
</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.