(
organization: str,
cn: str,
key_size: int,
)
| 230 | |
| 231 | |
| 232 | def create_ca( |
| 233 | organization: str, |
| 234 | cn: str, |
| 235 | key_size: int, |
| 236 | ) -> tuple[rsa.RSAPrivateKeyWithSerialization, x509.Certificate]: |
| 237 | now = datetime.datetime.now() |
| 238 | |
| 239 | private_key = rsa.generate_private_key( |
| 240 | public_exponent=65537, |
| 241 | key_size=key_size, |
| 242 | ) # type: ignore |
| 243 | name = x509.Name( |
| 244 | [ |
| 245 | x509.NameAttribute(NameOID.COMMON_NAME, cn), |
| 246 | x509.NameAttribute(NameOID.ORGANIZATION_NAME, organization), |
| 247 | ] |
| 248 | ) |
| 249 | builder = x509.CertificateBuilder() |
| 250 | builder = builder.serial_number(x509.random_serial_number()) |
| 251 | builder = builder.subject_name(name) |
| 252 | builder = builder.not_valid_before(now + CERT_VALIDITY_OFFSET) |
| 253 | builder = builder.not_valid_after(now + CERT_VALIDITY_OFFSET + CA_EXPIRY) |
| 254 | builder = builder.issuer_name(name) |
| 255 | builder = builder.public_key(private_key.public_key()) |
| 256 | builder = builder.add_extension( |
| 257 | x509.BasicConstraints(ca=True, path_length=None), critical=True |
| 258 | ) |
| 259 | builder = builder.add_extension( |
| 260 | x509.ExtendedKeyUsage([ExtendedKeyUsageOID.SERVER_AUTH]), critical=False |
| 261 | ) |
| 262 | builder = builder.add_extension( |
| 263 | x509.KeyUsage( |
| 264 | digital_signature=False, |
| 265 | content_commitment=False, |
| 266 | key_encipherment=False, |
| 267 | data_encipherment=False, |
| 268 | key_agreement=False, |
| 269 | key_cert_sign=True, |
| 270 | crl_sign=True, |
| 271 | encipher_only=False, |
| 272 | decipher_only=False, |
| 273 | ), |
| 274 | critical=True, |
| 275 | ) |
| 276 | builder = builder.add_extension( |
| 277 | x509.SubjectKeyIdentifier.from_public_key(private_key.public_key()), |
| 278 | critical=False, |
| 279 | ) |
| 280 | cert = builder.sign(private_key=private_key, algorithm=hashes.SHA256()) # type: ignore |
| 281 | return private_key, cert |
| 282 | |
| 283 | |
| 284 | def _fix_legacy_sans(sans: Iterable[x509.GeneralName] | list[str]) -> x509.GeneralNames: |
no test coverage detected
searching dependent graphs…