Skip to content
This repository was archived by the owner on Jul 5, 2019. It is now read-only.

Commit 3fea6b4

Browse files
committed
Multiple initializations appear to work
1 parent 9d3ee26 commit 3fea6b4

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

src/libnss/ffi.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::libc::{c_char, c_int, c_void, c_ulong, c_uint};
22

3-
#[link_args = "-lnss3"]
3+
#[link_args = "-lnss3 -lnspr4"]
44
#[nolink]
55
extern "C" { }
66

@@ -15,6 +15,11 @@ pub static SECWouldBlock: c_int = -2;
1515
pub static PRTrue: PRBool = 1;
1616
pub static PRFalse: PRBool = 0;
1717

18+
pub static PR_AF_INET: c_int = 2;
19+
20+
pub static NSS_INIT_READONLY: c_uint = 0x1;
21+
pub static NSS_INIT_PK11RELOAD: c_uint = 0x80;
22+
1823
pub struct SECMODModule {
1924
arena: *c_void, //TODO
2025
internal: PRBool, /* true of internally linked modules, false for the loaded modules */
@@ -56,4 +61,8 @@ pub static TLS_RSA_WITH_AES_128_CBC_SHA: c_int = 0x002f;
5661

5762
externfn!(fn NSS_Init(configdir: *c_char) -> SECStatus)
5863
externfn!(fn NSS_NoDB_Init(configdir: *c_char) -> SECStatus)
64+
externfn!(fn NSS_InitContext(configdir: *c_char, certPrefix: *c_char, keyPrefix: *c_char, secmodName: *c_char, initStrings: *c_void, flags: c_uint) -> *c_void)
65+
externfn!(fn NSS_ShutdownContext(ctx: *c_void))
66+
externfn!(fn SECMOD_DestroyModule(module: *SECMODModule))
5967
externfn!(fn SECMOD_LoadUserModule(moduleSpec: *c_char, parent: *SECMODModule, recurse: PRBool) -> *SECMODModule)
68+
externfn!(fn PR_OpenTCPSocket(af: c_int) -> *c_void)

src/libnss/lib.rs

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,66 @@
1+
#[feature(struct_variant)];
12
#[link(name = "nss", vers = "0.0")]
23

34
use std::os;
45
use std::ptr;
5-
use ffi::{NSS_Init, SECStatus, SECMOD_LoadUserModule, PRTrue, PRFalse, SECFailure, SECSuccess};
6+
use std::rt::io::net::ip::{SocketAddr};
7+
use ffi::{NSS_InitContext, SECStatus, SECMOD_LoadUserModule, PRTrue, PRFalse,
8+
SECFailure, SECSuccess, PR_OpenTCPSocket, PR_AF_INET, SECMOD_DestroyModule,
9+
NSS_ShutdownContext, SECMODModule, NSS_INIT_READONLY, NSS_INIT_PK11RELOAD};
10+
use std::libc::{c_void};
11+
use std::default::Default;
612

713
#[cfg(test)]
814
mod tests;
915

1016
mod ffi;
1117

12-
pub fn init() -> SECStatus {
18+
pub struct NSS {
19+
20+
nss_ctx: Option<*c_void>,
21+
nss_cert_mod: Option<SECMODModule>,
22+
23+
}
24+
25+
impl NSS {
26+
27+
pub fn new() -> NSS {
28+
NSS { nss_ctx: None, nss_cert_mod: None }
29+
}
30+
31+
pub fn init(&mut self) -> SECStatus {
32+
33+
info!("NSS Init - Context: {}", self.nss_ctx.is_none());
34+
if(self.nss_ctx.is_none()) { return SECSuccess; }
1335
let dir = format!("sql:{}/.pki/nssdb", os::getenv("HOME").unwrap_or(~""));
14-
dir.with_c_str(|nssdb| unsafe { NSS_Init(nssdb) });
15-
16-
unsafe {
17-
let module = *SECMOD_LoadUserModule("library=libnssckbi.so name=\"Root Certs\"".to_c_str().unwrap(), ptr::null(), PRFalse);
18-
if(module.loaded != PRTrue)
19-
{
20-
return SECFailure;
21-
}
22-
23-
SECSuccess
36+
dir.with_c_str(|nssdb| self.nss_ctx = Some(unsafe { NSS_InitContext(nssdb, ptr::null(), ptr::null(), ptr::null(), ptr::null(), NSS_INIT_READONLY | NSS_INIT_PK11RELOAD) }));
37+
38+
self.nss_cert_mod = Some(unsafe { *SECMOD_LoadUserModule("library=libnssckbi.so name=\"Root Certs\"".to_c_str().unwrap(), ptr::null(), PRFalse)});
39+
if(self.nss_cert_mod.unwrap().loaded != PRTrue)
40+
{
41+
return SECFailure;
2442
}
43+
44+
SECSuccess
45+
}
46+
47+
pub fn uninit(&mut self) -> SECStatus {
48+
if(self.nss_ctx.is_none()) { return SECSuccess; }
49+
unsafe {
50+
SECMOD_DestroyModule(&self.nss_cert_mod.unwrap());
51+
NSS_ShutdownContext(self.nss_ctx.unwrap());
52+
}
53+
self.nss_ctx = None;
54+
SECSuccess
55+
}
56+
57+
}
58+
59+
60+
61+
62+
pub fn ssl_connect(addr: SocketAddr)
63+
{
64+
let socket = unsafe { PR_OpenTCPSocket(PR_AF_INET) };
2565
}
2666

src/libnss/tests.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
use ffi::SECSuccess;
2-
use super::init;
2+
use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
3+
use super::{ssl_connect, NSS};
34

45
#[test]
56
fn test_init() {
6-
assert_eq!(init(), SECSuccess);
7+
let mut nss = NSS::new();
8+
assert_eq!(nss.init(), SECSuccess);
9+
nss.uninit();
10+
}
11+
12+
#[test]
13+
fn test_ssl_connect(){
14+
let mut nss = NSS::new();
15+
nss.init();
16+
// ssl_connect(SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 443 });
717
}

0 commit comments

Comments
 (0)