| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::ptr::NonNull; |
| 8 |
|
| 9 |
use neqo_common::qerror; |
| 10 |
|
| 11 |
use crate::{ |
| 12 |
experimental_api, null_safe_slice, |
| 13 |
p11::{ItemArray, ItemArrayIterator, SECItem, SECItemArray}, |
| 14 |
ssl::{PRFileDesc, SSL_PeerSignedCertTimestamps, SSL_PeerStapledOCSPResponses}, |
| 15 |
}; |
| 16 |
|
| 17 |
experimental_api!(SSL_PeerCertificateChainDER( |
| 18 |
fd: *mut PRFileDesc, |
| 19 |
out: *mut *mut SECItemArray, |
| 20 |
)); |
| 21 |
|
| 22 |
pub struct CertificateInfo { |
| 23 |
certs: ItemArray, |
| 24 |
/// `stapled_ocsp_responses` and `signed_cert_timestamp` are properties |
| 25 |
/// associated with each of the certificates. Right now, NSS only |
| 26 |
/// reports the value for the end-entity certificate (the first). |
| 27 |
stapled_ocsp_responses: Option<Vec<Vec<u8>>>, |
| 28 |
signed_cert_timestamp: Option<Vec<u8>>, |
| 29 |
} |
| 30 |
|
| 31 |
fn peer_certificate_chain(fd: *mut PRFileDesc) -> Option<ItemArray> { |
| 32 |
let mut chain_ptr: *mut SECItemArray = std::ptr::null_mut(); |
| 33 |
let rv = unsafe { SSL_PeerCertificateChainDER(fd, &mut chain_ptr) }; |
| 34 |
if rv.is_ok() { |
| 35 |
ItemArray::from_ptr(chain_ptr).ok() |
| 36 |
} else { |
| 37 |
None |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// As explained in rfc6961, an OCSPResponseList can have at most |
| 42 |
// 2^24 items. Casting its length is therefore safe even on 32 bits targets. |
| 43 |
fn stapled_ocsp_responses(fd: *mut PRFileDesc) -> Option<Vec<Vec<u8>>> { |
| 44 |
let ocsp_nss = unsafe { SSL_PeerStapledOCSPResponses(fd) }; |
| 45 |
match NonNull::new(ocsp_nss as *mut SECItemArray) { |
| 46 |
Some(ocsp_ptr) => { |
| 47 |
let mut ocsp_helper: Vec<Vec<u8>> = Vec::new(); |
| 48 |
let Ok(len) = isize::try_from(unsafe { ocsp_ptr.as_ref().len }) else { |
| 49 |
qerror!("[{fd:p}] Received illegal OSCP length"); |
| 50 |
return None; |
| 51 |
}; |
| 52 |
for idx in 0..len { |
| 53 |
let itemp: *const SECItem = unsafe { ocsp_ptr.as_ref().items.offset(idx).cast() }; |
| 54 |
let item = unsafe { null_safe_slice((*itemp).data, (*itemp).len) }; |
| 55 |
ocsp_helper.push(item.to_owned()); |
| 56 |
} |
| 57 |
Some(ocsp_helper) |
| 58 |
} |
| 59 |
None => None, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
fn signed_cert_timestamp(fd: *mut PRFileDesc) -> Option<Vec<u8>> { |
| 64 |
let sct_nss = unsafe { SSL_PeerSignedCertTimestamps(fd) }; |
| 65 |
NonNull::new(sct_nss as *mut SECItem).map(|sct_ptr| { |
| 66 |
if unsafe { sct_ptr.as_ref().len == 0 || sct_ptr.as_ref().data.is_null() } { |
| 67 |
Vec::new() |
| 68 |
} else { |
| 69 |
let sct_slice = unsafe { null_safe_slice(sct_ptr.as_ref().data, sct_ptr.as_ref().len) }; |
| 70 |
sct_slice.to_owned() |
| 71 |
} |
| 72 |
}) |
| 73 |
} |
| 74 |
|
| 75 |
impl<'a> IntoIterator for &'a CertificateInfo { |
| 76 |
type IntoIter = ItemArrayIterator<'a>; |
| 77 |
type Item = &'a [u8]; |
| 78 |
fn into_iter(self) -> Self::IntoIter { |
| 79 |
self.iter() |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
impl CertificateInfo { |
| 84 |
pub(crate) fn new(fd: *mut PRFileDesc) -> Option<Self> { |
| 85 |
peer_certificate_chain(fd).map(|certs| Self { |
| 86 |
certs, |
| 87 |
stapled_ocsp_responses: stapled_ocsp_responses(fd), |
| 88 |
signed_cert_timestamp: signed_cert_timestamp(fd), |
| 89 |
}) |
| 90 |
} |
| 91 |
|
| 92 |
#[must_use] |
| 93 |
pub fn iter(&self) -> ItemArrayIterator<'_> { |
| 94 |
self.certs.into_iter() |
| 95 |
} |
| 96 |
|
| 97 |
#[must_use] |
| 98 |
pub const fn stapled_ocsp_responses(&self) -> &Option<Vec<Vec<u8>>> { |
| 99 |
&self.stapled_ocsp_responses |
| 100 |
} |
| 101 |
|
| 102 |
#[must_use] |
| 103 |
pub const fn signed_cert_timestamp(&self) -> &Option<Vec<u8>> { |
| 104 |
&self.signed_cert_timestamp |
| 105 |
} |
| 106 |
} |