Skip to content

Commit e1cc9f3

Browse files
committed
feat: load ca certs from memory
* Fixed up tests to work with the ca certs. * Polykey generated certs now work with CA. * Related #9 [ci skip]
1 parent ab1a4bb commit e1cc9f3

File tree

7 files changed

+256
-178
lines changed

7 files changed

+256
-178
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export function retry(scid: Uint8Array, dcid: Uint8Array, newScid: Uint8Array, t
132132
export function versionIsSupported(version: number): boolean
133133
export class Config {
134134
constructor()
135-
static withBoringSslCtx(certPem?: Uint8Array | undefined | null, keyPem?: Uint8Array | undefined | null, supportedKeyAlgos?: string | undefined | null): Config
135+
static withBoringSslCtx(certPem?: Uint8Array | undefined | null, keyPem?: Uint8Array | undefined | null, supportedKeyAlgos?: string | undefined | null, caCertPem?: Uint8Array | undefined | null): Config
136136
loadCertChainFromPemFile(file: string): void
137137
loadPrivKeyFromPemFile(file: string): void
138138
loadVerifyLocationsFromFile(file: string): void

src/QUICConnection.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ class QUICConnection extends EventTarget {
265265
} = {}
266266
) {
267267
this.logger.info(`Destroy ${this.constructor.name}`);
268+
console.log(this.conn.localError())
269+
console.log(this.conn.peerError())
268270
for (const stream of this.streamMap.values()) {
269271
await stream.destroy();
270272
}

src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type TlsConfig = {
1515

1616
type QUICConfig = {
1717
tlsConfig: TlsConfig | undefined;
18+
verifyPem: string | undefined;
1819
verifyFromPemFile: string | undefined;
1920
supportedPrivateKeyAlgos: string | undefined;
2021
verifyPeer: boolean;
@@ -35,6 +36,7 @@ type QUICConfig = {
3536

3637
const clientDefault: QUICConfig = {
3738
tlsConfig: undefined,
39+
verifyPem: undefined,
3840
verifyFromPemFile: undefined,
3941
supportedPrivateKeyAlgos: supportedPrivateKeyAlgosDefault,
4042
logKeys: undefined,
@@ -61,6 +63,7 @@ const clientDefault: QUICConfig = {
6163

6264
const serverDefault: QUICConfig = {
6365
tlsConfig: undefined,
66+
verifyPem: undefined,
6467
verifyFromPemFile: undefined,
6568
supportedPrivateKeyAlgos: supportedPrivateKeyAlgosDefault,
6669
logKeys: undefined,
@@ -96,6 +99,7 @@ function buildQuicheConfig(config: QUICConfig): QuicheConfig {
9699
certChainPem,
97100
privKeyPem,
98101
config.supportedPrivateKeyAlgos ?? null,
102+
config.verifyPem != null ? Buffer.from(config.verifyPem) : null,
99103
);
100104
if (config.tlsConfig != null && 'certChainFromPemFile' in config.tlsConfig) {
101105
if (config.tlsConfig?.certChainFromPemFile != null) {

src/native/napi/config.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl Config {
5151
cert_pem: Option<Uint8Array>,
5252
key_pem: Option<Uint8Array>,
5353
supported_key_algos: Option<String>,
54+
ca_cert_pem: Option<Uint8Array>,
5455
) -> Result<Self> {
5556
let mut ssl_ctx_builder = boring::ssl::SslContextBuilder::new(
5657
boring::ssl::SslMethod::tls(),
@@ -98,6 +99,29 @@ impl Config {
9899
|err| Err(Error::from_reason(err.to_string()))
99100
)?;
100101
}
102+
// Processing CA certificate
103+
if let Some(ca_cert_pem) = ca_cert_pem {
104+
let x509_certs = boring::x509::X509::stack_from_pem(
105+
&ca_cert_pem.to_vec()
106+
).or_else(
107+
|err| Err(Error::from_reason(err.to_string()))
108+
)?;
109+
let mut x509_store_builder = boring::x509::store::X509StoreBuilder::new()
110+
.or_else(
111+
|err| Err(Error::from_reason(err.to_string()))
112+
)?;
113+
for x509 in x509_certs.into_iter() {
114+
x509_store_builder.add_cert(x509)
115+
.or_else(
116+
|err| Err(Error::from_reason(err.to_string()))
117+
)?;
118+
}
119+
let x509_store = x509_store_builder.build();
120+
ssl_ctx_builder.set_verify_cert_store(x509_store)
121+
.or_else(
122+
|err| Err(Error::from_reason(err.to_string()))
123+
)?;
124+
}
101125
let ssl_ctx= ssl_ctx_builder.build();
102126
let config = quiche::Config::with_boring_ssl_ctx(
103127
quiche::PROTOCOL_VERSION,

src/native/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ interface ConfigConstructor {
4343
certPem: Uint8Array | null,
4444
keyPem: Uint8Array | null,
4545
supportedKeyAlgos: String | null,
46+
ca_cert_pem: Uint8Array | null,
4647
): Config;
4748
};
4849

0 commit comments

Comments
 (0)