Skip to content

Commit

Permalink
feat: load ca certs from memory
Browse files Browse the repository at this point in the history
* Fixed up tests to work with the ca certs.
* Polykey generated certs now work with CA.

* Related #9

[ci skip]
  • Loading branch information
tegefaulkes committed Apr 26, 2023
1 parent ab1a4bb commit e1cc9f3
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 178 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export function retry(scid: Uint8Array, dcid: Uint8Array, newScid: Uint8Array, t
export function versionIsSupported(version: number): boolean
export class Config {
constructor()
static withBoringSslCtx(certPem?: Uint8Array | undefined | null, keyPem?: Uint8Array | undefined | null, supportedKeyAlgos?: string | undefined | null): Config
static withBoringSslCtx(certPem?: Uint8Array | undefined | null, keyPem?: Uint8Array | undefined | null, supportedKeyAlgos?: string | undefined | null, caCertPem?: Uint8Array | undefined | null): Config
loadCertChainFromPemFile(file: string): void
loadPrivKeyFromPemFile(file: string): void
loadVerifyLocationsFromFile(file: string): void
Expand Down
2 changes: 2 additions & 0 deletions src/QUICConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ class QUICConnection extends EventTarget {
} = {}
) {
this.logger.info(`Destroy ${this.constructor.name}`);
console.log(this.conn.localError())
console.log(this.conn.peerError())
for (const stream of this.streamMap.values()) {
await stream.destroy();
}
Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type TlsConfig = {

type QUICConfig = {
tlsConfig: TlsConfig | undefined;
verifyPem: string | undefined;
verifyFromPemFile: string | undefined;
supportedPrivateKeyAlgos: string | undefined;
verifyPeer: boolean;
Expand All @@ -35,6 +36,7 @@ type QUICConfig = {

const clientDefault: QUICConfig = {
tlsConfig: undefined,
verifyPem: undefined,
verifyFromPemFile: undefined,
supportedPrivateKeyAlgos: supportedPrivateKeyAlgosDefault,
logKeys: undefined,
Expand All @@ -61,6 +63,7 @@ const clientDefault: QUICConfig = {

const serverDefault: QUICConfig = {
tlsConfig: undefined,
verifyPem: undefined,
verifyFromPemFile: undefined,
supportedPrivateKeyAlgos: supportedPrivateKeyAlgosDefault,
logKeys: undefined,
Expand Down Expand Up @@ -96,6 +99,7 @@ function buildQuicheConfig(config: QUICConfig): QuicheConfig {
certChainPem,
privKeyPem,
config.supportedPrivateKeyAlgos ?? null,
config.verifyPem != null ? Buffer.from(config.verifyPem) : null,
);
if (config.tlsConfig != null && 'certChainFromPemFile' in config.tlsConfig) {
if (config.tlsConfig?.certChainFromPemFile != null) {
Expand Down
24 changes: 24 additions & 0 deletions src/native/napi/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl Config {
cert_pem: Option<Uint8Array>,
key_pem: Option<Uint8Array>,
supported_key_algos: Option<String>,
ca_cert_pem: Option<Uint8Array>,
) -> Result<Self> {
let mut ssl_ctx_builder = boring::ssl::SslContextBuilder::new(
boring::ssl::SslMethod::tls(),
Expand Down Expand Up @@ -98,6 +99,29 @@ impl Config {
|err| Err(Error::from_reason(err.to_string()))
)?;
}
// Processing CA certificate
if let Some(ca_cert_pem) = ca_cert_pem {
let x509_certs = boring::x509::X509::stack_from_pem(
&ca_cert_pem.to_vec()
).or_else(
|err| Err(Error::from_reason(err.to_string()))
)?;
let mut x509_store_builder = boring::x509::store::X509StoreBuilder::new()
.or_else(
|err| Err(Error::from_reason(err.to_string()))
)?;
for x509 in x509_certs.into_iter() {
x509_store_builder.add_cert(x509)
.or_else(
|err| Err(Error::from_reason(err.to_string()))
)?;
}
let x509_store = x509_store_builder.build();
ssl_ctx_builder.set_verify_cert_store(x509_store)
.or_else(
|err| Err(Error::from_reason(err.to_string()))
)?;
}
let ssl_ctx= ssl_ctx_builder.build();
let config = quiche::Config::with_boring_ssl_ctx(
quiche::PROTOCOL_VERSION,
Expand Down
1 change: 1 addition & 0 deletions src/native/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ interface ConfigConstructor {
certPem: Uint8Array | null,
keyPem: Uint8Array | null,
supportedKeyAlgos: String | null,
ca_cert_pem: Uint8Array | null,
): Config;
};

Expand Down
Loading

0 comments on commit e1cc9f3

Please sign in to comment.