|
| 1 | +use ngx::allocator::{AllocError, Allocator, TryCloneIn}; |
| 2 | +use ngx::collections::Vec; |
| 3 | +use ngx::core::SlabPool; |
| 4 | +use ngx::sync::RwLock; |
| 5 | + |
| 6 | +use crate::time::{jitter, Time, TimeRange}; |
| 7 | + |
| 8 | +pub type SharedCertificateContext = RwLock<CertificateContextInner<SlabPool>>; |
| 9 | + |
| 10 | +#[derive(Debug, Default)] |
| 11 | +pub enum CertificateContext { |
| 12 | + #[default] |
| 13 | + Empty, |
| 14 | + // Ready to use certificate in shared memory. |
| 15 | + Shared(&'static SharedCertificateContext), |
| 16 | +} |
| 17 | + |
| 18 | +impl CertificateContext { |
| 19 | + pub fn as_ref(&self) -> Option<&'static SharedCertificateContext> { |
| 20 | + if let CertificateContext::Shared(data) = self { |
| 21 | + Some(data) |
| 22 | + } else { |
| 23 | + None |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug, Default, PartialEq, Eq)] |
| 29 | +pub enum CertificateState { |
| 30 | + #[default] |
| 31 | + Pending, |
| 32 | + Ready, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug)] |
| 36 | +pub struct CertificateContextInner<A> |
| 37 | +where |
| 38 | + A: Allocator + Clone, |
| 39 | +{ |
| 40 | + pub state: CertificateState, |
| 41 | + pub chain: Vec<u8, A>, |
| 42 | + pub pkey: Vec<u8, A>, |
| 43 | + pub valid: TimeRange, |
| 44 | + pub next: Time, |
| 45 | +} |
| 46 | + |
| 47 | +impl<OA> TryCloneIn for CertificateContextInner<OA> |
| 48 | +where |
| 49 | + OA: Allocator + Clone, |
| 50 | +{ |
| 51 | + type Target<A: Allocator + Clone> = CertificateContextInner<A>; |
| 52 | + |
| 53 | + fn try_clone_in<A: Allocator + Clone>(&self, alloc: A) -> Result<Self::Target<A>, AllocError> { |
| 54 | + let mut chain = Vec::new_in(alloc.clone()); |
| 55 | + chain |
| 56 | + .try_reserve_exact(self.chain.len()) |
| 57 | + .map_err(|_| AllocError)?; |
| 58 | + chain.extend(self.chain.iter()); |
| 59 | + |
| 60 | + let mut pkey = Vec::new_in(alloc); |
| 61 | + pkey.try_reserve_exact(self.pkey.len()) |
| 62 | + .map_err(|_| AllocError)?; |
| 63 | + pkey.extend(self.pkey.iter()); |
| 64 | + |
| 65 | + Ok(Self::Target { |
| 66 | + state: CertificateState::Ready, |
| 67 | + chain, |
| 68 | + pkey, |
| 69 | + valid: self.valid.clone(), |
| 70 | + next: self.next, |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl<A> CertificateContextInner<A> |
| 76 | +where |
| 77 | + A: Allocator + Clone, |
| 78 | +{ |
| 79 | + pub fn new_in(alloc: A) -> Self { |
| 80 | + Self { |
| 81 | + state: CertificateState::Pending, |
| 82 | + chain: Vec::new_in(alloc.clone()), |
| 83 | + pkey: Vec::new_in(alloc.clone()), |
| 84 | + valid: Default::default(), |
| 85 | + next: Default::default(), |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + pub fn set(&mut self, chain: &[u8], pkey: &[u8], valid: TimeRange) -> Result<Time, AllocError> { |
| 90 | + const PREFIX: &[u8] = b"data:"; |
| 91 | + |
| 92 | + // reallocate the storage only if the current capacity is insufficient |
| 93 | + |
| 94 | + fn needs_realloc<A: Allocator>(buf: &Vec<u8, A>, new_size: usize) -> bool { |
| 95 | + buf.capacity() < PREFIX.len() + new_size |
| 96 | + } |
| 97 | + |
| 98 | + if needs_realloc(&self.chain, chain.len()) || needs_realloc(&self.pkey, pkey.len()) { |
| 99 | + let alloc = self.chain.allocator(); |
| 100 | + |
| 101 | + let mut new_chain: Vec<u8, A> = Vec::new_in(alloc.clone()); |
| 102 | + new_chain |
| 103 | + .try_reserve_exact(PREFIX.len() + chain.len()) |
| 104 | + .map_err(|_| AllocError)?; |
| 105 | + |
| 106 | + let mut new_pkey: Vec<u8, A> = Vec::new_in(alloc.clone()); |
| 107 | + new_pkey |
| 108 | + .try_reserve_exact(PREFIX.len() + pkey.len()) |
| 109 | + .map_err(|_| AllocError)?; |
| 110 | + |
| 111 | + self.chain = new_chain; |
| 112 | + self.pkey = new_pkey; |
| 113 | + } |
| 114 | + |
| 115 | + // update the stored data in-place |
| 116 | + |
| 117 | + self.chain.clear(); |
| 118 | + self.chain.extend(PREFIX); |
| 119 | + self.chain.extend(chain); |
| 120 | + |
| 121 | + self.pkey.clear(); |
| 122 | + self.pkey.extend(PREFIX); |
| 123 | + self.pkey.extend(pkey); |
| 124 | + |
| 125 | + // Schedule the next update at around 2/3 of the cert lifetime, |
| 126 | + // as recommended in Let's Encrypt integration guide |
| 127 | + self.next = valid.start + jitter(valid.duration() * 2 / 3, 2); |
| 128 | + self.valid = valid; |
| 129 | + |
| 130 | + self.state = CertificateState::Ready; |
| 131 | + |
| 132 | + Ok(self.next) |
| 133 | + } |
| 134 | + |
| 135 | + pub fn chain(&self) -> Option<&[u8]> { |
| 136 | + if matches!(self.state, CertificateState::Ready) { |
| 137 | + return Some(&self.chain); |
| 138 | + } |
| 139 | + |
| 140 | + None |
| 141 | + } |
| 142 | + |
| 143 | + pub fn pkey(&self) -> Option<&[u8]> { |
| 144 | + if matches!(self.state, CertificateState::Ready) { |
| 145 | + return Some(&self.pkey); |
| 146 | + } |
| 147 | + |
| 148 | + None |
| 149 | + } |
| 150 | +} |
0 commit comments