tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

cert_storage.rs (1403B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
      4 
      5 use cstr::cstr;
      6 use log;
      7 use nserror::{nsresult, NS_ERROR_SERVICE_NOT_AVAILABLE};
      8 use thin_vec::ThinVec;
      9 use xpcom::{interfaces::nsICertStorage, RefPtr};
     10 
     11 fn get_cert_storage() -> Result<RefPtr<nsICertStorage>, nsresult> {
     12    let cert_storage_uri = cstr!("@mozilla.org/security/certstorage;1");
     13    xpcom::get_service::<nsICertStorage>(cert_storage_uri).ok_or(NS_ERROR_SERVICE_NOT_AVAILABLE)
     14 }
     15 
     16 pub fn has_all_certs_by_hash(hashes: &ThinVec<ThinVec<u8>>) -> Result<bool, nsresult> {
     17    let cert_storage = get_cert_storage()?;
     18    let mut found: bool = false;
     19    unsafe {
     20        cert_storage
     21            .HasAllCertsByHash(hashes, &mut found)
     22            .to_result()?;
     23    }
     24    log::debug!("Looking for {} hashes, result: {}", hashes.len(), found);
     25    Ok(found)
     26 }
     27 
     28 pub fn get_cert_from_hash(hash: &ThinVec<u8>) -> Result<ThinVec<u8>, nsresult> {
     29    let cert_storage = get_cert_storage()?;
     30    let mut value = ThinVec::with_capacity(2000); // Avoid needing to reallocate
     31    unsafe {
     32        cert_storage.FindCertByHash(hash, &mut value).to_result()?;
     33    }
     34    log::debug!(
     35        "Looking for {:#02X?}, found result of size: {}",
     36        hash,
     37        value.len()
     38    );
     39    Ok(value)
     40 }