pub struct CertD { /* private fields */ }
Expand description
A certificate store.
This is a handle to an on-disk certificate store that can be used to lookup and insert certificates.
A certificate store contains certificates. Its main role is to
hold certificates indexed by their fingerprint. (Note:
certificates are not indexed by their subkey fingerprints.) But,
it can also store certificates under special names. Currently,
the specification defines one special name, trust-root
, which
holds the user’s local trust root. Non-standard special names are
possible. These MUST start with an underscore, which SHOULD be
immediately followed by the vendor’s name, e.g.,
_sequoia_some_special.pgp
.
Implementations§
Source§impl CertD
impl CertD
Sourcepub fn new() -> Result<CertD>
pub fn new() -> Result<CertD>
Opens the default certificate store.
If not explicitly requested otherwise, an application SHOULD
use the default store. To use a store with a different
location, use CertD::with_base_dir
.
Sourcepub fn user_configured_store_path() -> Result<PathBuf>
pub fn user_configured_store_path() -> Result<PathBuf>
Returns the location of the user-configured store.
If set, this is the value of the environment variable
PGP_CERT_D
. Otherwise, it is the default store’s path as
returned by CertD::default_store_path
.
Sourcepub fn default_store_path() -> Result<PathBuf>
pub fn default_store_path() -> Result<PathBuf>
Returns the location of the default store.
The location of the default store is platform specific. This returns an error on unsupported platforms.
Sourcepub fn with_base_dir<P: AsRef<Path>>(base: P) -> Result<CertD>
pub fn with_base_dir<P: AsRef<Path>>(base: P) -> Result<CertD>
Opens a store with an explicit location.
Note: Most applications should use the default store, which
is shared. The default store can be opened using
CertD::new
.
Sourcepub fn tag(&self) -> Tag
pub fn tag(&self) -> Tag
Computes the certificate directory’s tag.
Modulo collisions in the hashing algorithm, this tag will change whenever a certificate indexed by its fingerprint is added, updated, or removed. The tag is designed to not change when a certificate indexed by a special name, or some other unrelated data in the certd directory is added, updated, or removed.
The tag should be stable in the sense that you can serialize it to disk, read it in again later, and compare it to the current value. However, how the tag is computed may change. In this case, you may observe a spurious update.
Sourcepub fn get_path_by_fingerprint(&self, fingerprint: &str) -> Result<PathBuf>
pub fn get_path_by_fingerprint(&self, fingerprint: &str) -> Result<PathBuf>
Turns a fingerprint into a path in the store.
The transformation from a fingerprint to a path is defined by the standard.
Sourcepub fn get_path_by_special(&self, special: &str) -> Result<PathBuf>
pub fn get_path_by_special(&self, special: &str) -> Result<PathBuf>
Turns a special name into a path in the store.
The specification currently defines one special name.
Non-standard special names are allowed, but they must MUST
start with an underscore, which SHOULD be immediately followed
by the vendor’s name, e.g., _sequoia_some_special.pgp
.
Other names cause this function to return Error::BadName
.
Sourcepub fn is_special(special: &str) -> Result<()>
pub fn is_special(special: &str) -> Result<()>
Returns whether the special name is valid.
The specification currently defines one special name.
Non-standard special names are allowed, but they must MUST
start with an underscore, which SHOULD be immediately followed
by the vendor’s name, e.g., _sequoia_some_special.pgp
.
Other names cause this function to return Error::BadName
.
Sourcepub fn get(&self, name: &str) -> Result<Option<(Tag, Vec<u8>)>>
pub fn get(&self, name: &str) -> Result<Option<(Tag, Vec<u8>)>>
Looks up a certificate in the store by name, i.e., by fingerprint or by special name.
The specification currently defines one special name.
Non-standard special names are allowed, but they must MUST
start with an underscore, which SHOULD be immediately followed
by the vendor’s name, e.g., _sequoia_some_special.pgp
.
Other names cause this function to return Error::BadName
.
If the certificate exists, this function computes the tag,
reads in the certificate data, and returns Ok(Some((tag, cert_data)))
. See Tag
for how the tag can be used to
cache lookups.
If the certificate does not exist, this function returns Ok(None)
.
If an I/O error occurs, or the name was invalid, this function returns
an Error
.
CertD::get_file
is often more efficient if you don’t
necessarily need the tag or the file’s contents.
Sourcepub fn get_file(&self, name: &str) -> Result<Option<File>>
pub fn get_file(&self, name: &str) -> Result<Option<File>>
Looks up a certificate in the store by name, i.e., by fingerprint or by special name.
The specification currently defines one special name.
Non-standard special names are allowed, but they must MUST
start with an underscore, which SHOULD be immediately followed
by the vendor’s name, e.g., _sequoia_some_special.pgp
.
Other names cause this function to return Error::BadName
.
If the certificate exists, this function returns
Ok(Some([std::fs::File]))
. If the certificate does not
exist, it returns Ok(None)
. If an I/O error occurs, or the
name was invalid, this function returns an Error
.
You can get the file’s Tag
by doing:
Tag::try_from(&file)
. See Tag
for how the tag can be
used to cache lookups.
Sourcepub fn get_if_changed(
&self,
since: Tag,
name: &str,
) -> Result<Option<(Tag, Vec<u8>)>>
pub fn get_if_changed( &self, since: Tag, name: &str, ) -> Result<Option<(Tag, Vec<u8>)>>
Conditionally looks up a certificate in the store by an name, i.e. a fingerprint or a special name.
The specification currently defines one special name.
Non-standard special names are allowed, but they must MUST
start with an underscore, which SHOULD be immediately followed
by the vendor’s name, e.g., _sequoia_some_special.pgp
.
Other names cause this function to return Error::BadName
.
If the certificate has changed, i.e., the provided tag does
not match the current tag, this function returns
Ok(Some((cert, tag)))
. The tag can be used in subsequent
calls to this function.
If the certificate has not changed, i.e., the provided tag
matches the current tag, or the certificate does not exist,
this function returns Ok(None)
.
If an I/O error occurs, or the name was invalid, this function returns
an Error
.
Sourcepub fn get_path(&self, name: &str) -> Result<PathBuf>
pub fn get_path(&self, name: &str) -> Result<PathBuf>
Get the path to a certificate by fingerprint or special name.
The specification currently defines one special name.
Non-standard special names are allowed, but they must MUST
start with an underscore, which SHOULD be immediately followed
by the vendor’s name, e.g., _sequoia_some_special.pgp
.
Other names cause this function to return Error::BadName
.
Sourcepub fn insert<'a, D, M>(
&self,
fingerprint: &str,
data: D,
return_inserted: bool,
merge: M,
) -> Result<(Tag, Option<Vec<u8>>)>
pub fn insert<'a, D, M>( &self, fingerprint: &str, data: D, return_inserted: bool, merge: M, ) -> Result<(Tag, Option<Vec<u8>>)>
Inserts or updates a cert.
Requires the fingerprint and a callback function. The
callback is passed data
, and an Option<Vec<u8>>
, which
contains the existing cert data, if any. The callback is
expected to merge the two copies of the certificate together.
The returned data is written to the store. Note: The callback
may decide to omit (parts of) the existing data, but this
should be done with great care as not to lose any vital
information.
The new Tag
is returned, and if return_inserted
is true,
the data written to the store is also returned.
This function locks store, which may block the current thread.
Use CertD::try_insert
to avoid blocking.
Sourcepub fn insert_extended<'a, D, PRE, M, POST>(
&self,
fingerprint: &str,
data: D,
return_inserted: bool,
pre: PRE,
merge: M,
post: POST,
) -> Result<(Tag, Option<Vec<u8>>)>
pub fn insert_extended<'a, D, PRE, M, POST>( &self, fingerprint: &str, data: D, return_inserted: bool, pre: PRE, merge: M, post: POST, ) -> Result<(Tag, Option<Vec<u8>>)>
Inserts or updates a cert, extended version.
This function is like CertD::insert
, but after obtaining
the lock on the cert-d, and before doing anything else, it
calls pre
. Similarly, just before dropping the lock, it
calls post
.
If the pre
callback returns an error, the operation is
aborted, and the error is propagated to the caller. The
post
callback can also transform the result.
The caller can use this functionality to get the cert-d’s tag prior to and after an insert operation, for instance.
Sourcepub fn try_insert<'a, D, M>(
&self,
fingerprint: &str,
data: D,
return_inserted: bool,
merge: M,
) -> Result<(Tag, Option<Vec<u8>>)>
pub fn try_insert<'a, D, M>( &self, fingerprint: &str, data: D, return_inserted: bool, merge: M, ) -> Result<(Tag, Option<Vec<u8>>)>
Inserts or updates a cert, non-blocking variant.
Requires the fingerprint and a callback function. The
callback is passed data
, and an Option<Vec<u8>>
, which
contains the existing cert data, if any. The callback is
expected to merge the two copies of the certificate together.
The returned data is written to the store. Note: The callback
may decide to omit (parts of) the existing data, but this
should be done with great care as not to lose any vital
information.
The new Tag
is returned, and if return_inserted
is true,
the data written to the store is also returned.
This function attempts to lock the store. If the store is
already locked, then it instead returns Error::IoError
with an std::io::ErrorKind::WouldBlock
.
Sourcepub fn try_insert_extended<'a, D, PRE, M, POST>(
&self,
fingerprint: &str,
data: D,
return_inserted: bool,
pre: PRE,
merge: M,
post: POST,
) -> Result<(Tag, Option<Vec<u8>>)>
pub fn try_insert_extended<'a, D, PRE, M, POST>( &self, fingerprint: &str, data: D, return_inserted: bool, pre: PRE, merge: M, post: POST, ) -> Result<(Tag, Option<Vec<u8>>)>
Inserts or updates a cert, extended version.
This function is like CertD::try_insert
, but after
obtaining the lock on the cert-d, and before doing anything
else, it calls pre
. Similarly, just before dropping the
lock, it calls post
.
If the pre
callback returns an error, the operation is
aborted, and the error is propagated to the caller. The
post
callback can also transform the result.
The caller can use this functionality to get the cert-d’s tag prior to and after an insert operation, for instance.
Sourcepub fn insert_data<'a, M>(
&self,
data: &'a [u8],
return_inserted: bool,
merge: M,
) -> Result<(Tag, Option<Vec<u8>>)>
pub fn insert_data<'a, M>( &self, data: &'a [u8], return_inserted: bool, merge: M, ) -> Result<(Tag, Option<Vec<u8>>)>
Inserts or updates a cert.
Requires the new certificate data, and a callback function.
The fingerprint is extracted from data
. The callback is
passed data
, and an Option<Vec<u8>>
, which contains the
existing cert data, if any. The callback is expected to merge
the two copies of the certificate together. The returned data
is written to the store. Note: The callback may decide to
omit (parts of) the existing data, but this should be done
with great care as not to lose any vital information.
The new Tag
is returned, and if return_inserted
is true,
the data written to the store is also returned.
This function locks store, which may block the current thread.
Use CertD::try_insert_data
to avoid blocking.
Sourcepub fn insert_data_extended<'a, PRE, M, POST>(
&self,
data: &'a [u8],
return_inserted: bool,
pre: PRE,
merge: M,
post: POST,
) -> Result<(Tag, Option<Vec<u8>>)>
pub fn insert_data_extended<'a, PRE, M, POST>( &self, data: &'a [u8], return_inserted: bool, pre: PRE, merge: M, post: POST, ) -> Result<(Tag, Option<Vec<u8>>)>
Inserts or updates a cert, extended version.
This function is like CertD::insert_data
, but after
obtaining the lock on the cert-d, and before doing anything
else, it calls pre
. Similarly, just before dropping the
lock, it calls post
.
If the pre
callback returns an error, the operation is
aborted, and the error is propagated to the caller. The
post
callback can also transform the result.
Sourcepub fn try_insert_data<'a, M>(
&self,
data: &'a [u8],
return_inserted: bool,
merge: M,
) -> Result<(Tag, Option<Vec<u8>>)>
pub fn try_insert_data<'a, M>( &self, data: &'a [u8], return_inserted: bool, merge: M, ) -> Result<(Tag, Option<Vec<u8>>)>
Inserts or updates a cert, non-blocking variant.
Requires the new certificate data, and a callback function.
The fingerprint is extracted from data
. The callback is
passed data
, and an Option<Vec<u8>>
, which contains the
existing cert data, if any. The callback is expected to merge
the two copies of the certificate together. The returned data
is written to the store. Note: The callback may decide to
omit (parts of) the existing data, but this should be done
with great care as not to lose any vital information.
The new Tag
is returned, and if return_inserted
is true,
the data written to the store is also returned.
This function attempts to lock the store. If the store is
already locked, then it instead returns Error::IoError
with an std::io::ErrorKind::WouldBlock
.
Sourcepub fn try_insert_data_extended<'a, PRE, M, POST>(
&self,
data: &'a [u8],
return_inserted: bool,
pre: PRE,
merge: M,
post: POST,
) -> Result<(Tag, Option<Vec<u8>>)>
pub fn try_insert_data_extended<'a, PRE, M, POST>( &self, data: &'a [u8], return_inserted: bool, pre: PRE, merge: M, post: POST, ) -> Result<(Tag, Option<Vec<u8>>)>
Inserts or updates a cert, non-blocking variant, extended version.
This function is like CertD::try_insert_data
, but after
obtaining the lock on the cert-d, and before doing anything
else, it calls pre
. Similarly, just before dropping the
lock, it calls post
.
If the pre
callback returns an error, the operation is
aborted, and the error is propagated to the caller. The
post
callback can also transform the result.
Sourcepub fn insert_special<'a, D, M>(
&self,
special_name: &str,
data: D,
return_inserted: bool,
merge: M,
) -> Result<(Tag, Option<Vec<u8>>)>
pub fn insert_special<'a, D, M>( &self, special_name: &str, data: D, return_inserted: bool, merge: M, ) -> Result<(Tag, Option<Vec<u8>>)>
Inserts or updates the cert or key stored under a special name.
Requires the special name, the cert or key in binary format
and a callback function. The callback is invoked with an
Option<Vec<u8>>
of the existing data (if any), and is
expected to merge the two copies together. The returned
Vec<u8>
is written to the store under the special name.
(Note: The function may decide to omit (parts of) the existing
data, but this should be done with great care as not to lose
any vital information.) The new Tag
is returned, and if
return_inserted
is true, the data written to the store is
also returned. Otherwise, None
is returned.
This function locks store, which may block the current thread.
Use CertD::try_insert_special
to avoid blocking.
The specification currently defines one special name.
Non-standard special names are allowed, but they must MUST
start with an underscore, which SHOULD be immediately followed
by the vendor’s name, e.g., _sequoia_some_special.pgp
.
Other names cause this function to return Error::BadName
.
Sourcepub fn insert_special_extended<'a, D, PRE, M, POST>(
&self,
special_name: &str,
data: D,
return_inserted: bool,
pre: PRE,
merge: M,
post: POST,
) -> Result<(Tag, Option<Vec<u8>>)>
pub fn insert_special_extended<'a, D, PRE, M, POST>( &self, special_name: &str, data: D, return_inserted: bool, pre: PRE, merge: M, post: POST, ) -> Result<(Tag, Option<Vec<u8>>)>
Inserts or updates the cert or key stored under a special name, extended version.
This function is like CertD::insert_special
, but after
obtaining the lock on the cert-d, and before doing anything
else, it calls pre
. Similarly, just before dropping the
lock, it calls post
.
If the pre
callback returns an error, the operation is
aborted, and the error is propagated to the caller. The
post
callback can also transform the result.
The caller can use this functionality to get the cert-d’s tag prior to and after an insert operation, for instance.
Sourcepub fn try_insert_special<'a, D, M>(
&self,
special_name: &str,
data: D,
return_inserted: bool,
merge: M,
) -> Result<(Tag, Option<Vec<u8>>)>
pub fn try_insert_special<'a, D, M>( &self, special_name: &str, data: D, return_inserted: bool, merge: M, ) -> Result<(Tag, Option<Vec<u8>>)>
Inserts or updates the cert or key stored under a special name, non-blocking variant.
Requires the special name, the cert or key in binary format
and a callback function. The callback is invoked with an
Option<Vec<u8>>
of the existing data (if any), and is
expected to merge the two copies together. The returned
Vec<u8>
is written to the store under the special name.
(Note: The function may decide to omit (parts of) the existing
data, but this should be done with great care as not to lose
any vital information.) The new Tag
is returned, and if
return_inserted
is true, the data written to the store is
also returned. Otherwise, None
is returned.
This function attempts to lock the store. If the store is
already locked, then it instead returns Error::IoError
with an std::io::ErrorKind::WouldBlock
.
The specification currently defines one special name.
Non-standard special names are allowed, but they must MUST
start with an underscore, which SHOULD be immediately followed
by the vendor’s name, e.g., _sequoia_some_special.pgp
.
Other names cause this function to return Error::BadName
.
Sourcepub fn try_insert_special_extended<'a, D, PRE, M, POST>(
&self,
special_name: &str,
data: D,
return_inserted: bool,
pre: PRE,
merge: M,
post: POST,
) -> Result<(Tag, Option<Vec<u8>>)>
pub fn try_insert_special_extended<'a, D, PRE, M, POST>( &self, special_name: &str, data: D, return_inserted: bool, pre: PRE, merge: M, post: POST, ) -> Result<(Tag, Option<Vec<u8>>)>
Inserts or updates the cert or key stored under a special name, non-blocking variant, extended version.
This function is like CertD::try_insert_special
, but after
obtaining the lock on the cert-d, and before doing anything
else, it calls pre
. Similarly, just before dropping the
lock, it calls post
.
If the pre
callback returns an error, the operation is
aborted, and the error is propagated to the caller. The
post
callback can also transform the result.
The caller can use this functionality to get the cert-d’s tag prior to and after an insert operation, for instance.
Sourcepub fn fingerprints(&self) -> impl Iterator<Item = Result<String>> + '_
pub fn fingerprints(&self) -> impl Iterator<Item = Result<String>> + '_
Iterates over the certs in the store returning their fingerprints.
Note: this only considers certificates that are stored under their fingerprint; it does not include certificates stored under a special name.
Sourcepub fn iter_files(&self) -> impl Iterator<Item = Result<(String, File)>> + '_
pub fn iter_files(&self) -> impl Iterator<Item = Result<(String, File)>> + '_
Iterates over the certs in the store.
Iterates over the certs in the store returning the fingerprint, and a file handle for each cert.
Note: this only considers certificates that are stored under their fingerprint; it does not include certificates stored under a special name.
Sourcepub fn iter(&self) -> impl Iterator<Item = Result<(String, Tag, Vec<u8>)>> + '_
pub fn iter(&self) -> impl Iterator<Item = Result<(String, Tag, Vec<u8>)>> + '_
Iterates over the certs in the store.
Iterates over the certs in the store returning the fingerprint, the tag, and the data for each cert.
Note: this only considers certificates that are stored under their fingerprint; it does not include certificates stored under a special name.