hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! TLS material for the AI bridge listener. |
| 2 | //! |
| 3 | //! QUIC has no plaintext mode, so the bridge always needs a certificate. Two |
| 4 | //! sources: a PEM pair from config (`AI_TLS_CERT` / `AI_TLS_KEY`) for a real |
| 5 | //! deployment, or a self-signed one generated at boot for local development. |
| 6 | //! |
| 7 | //! The certificate only proves *the backend* to a dialling service. It is not |
| 8 | //! the authentication: that is the shared token in the `Hello` frame, checked |
| 9 | //! in constant time by [`super::server`]. A self-signed certificate therefore |
| 10 | //! costs nothing in a private network — the service pins the fingerprint the |
| 11 | //! bridge logs at startup. |
| 12 | |
| 13 | use fs; |
| 14 | use Arc; |
| 15 | use Duration; |
| 16 | |
| 17 | use QuicServerConfig; |
| 18 | use ; |
| 19 | |
| 20 | use crateAiError; |
| 21 | use crate; |
| 22 | |
| 23 | /// The certificate chain the listener presents, plus the leaf in DER so a |
| 24 | /// caller (the boot log, an in-process test service) can pin it. |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | /// Install the process-wide rustls crypto provider, once. Both the bridge and |
| 32 | /// any in-process test client need it, and rustls refuses a second install — |
| 33 | /// an already-installed provider is success, not an error. |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | /// Build the listener's TLS config from a PEM pair, or self-signed when *both* |
| 39 | /// paths are absent. |
| 40 | /// |
| 41 | /// Half-configured is a setup error, never a fallback: with one path set the |
| 42 | /// operator plainly meant to present a real certificate, and self-signing |
| 43 | /// instead would hand every dialling service a throwaway `localhost` leaf, |
| 44 | /// regenerated on every boot, with nothing in the log to say so — every service |
| 45 | /// pinning the real fingerprint then fails the handshake and every chatbot send |
| 46 | /// degrades to a `503` with no cause visible anywhere. Note that config's |
| 47 | /// `parse_optional` folds a blank variable to `None`, so `AI_TLS_KEY=""` |
| 48 | /// arrives here as an absent path like any other. |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | /// Read a PEM certificate chain and private key off disk. |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | /// Generate a throwaway certificate for `localhost` / `127.0.0.1`. |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | /// SHA-256 of the leaf certificate, hex, for the startup log. A service pins |
| 149 | /// this instead of installing a CA. |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 |