dkim pubkey from rpivate key
This commit is contained in:
parent
139a46dce0
commit
b67e77ed6a
11 changed files with 24 additions and 40 deletions
|
@ -2,7 +2,7 @@ from os.path import join, exists
|
|||
from re import sub
|
||||
from cryptography.hazmat.primitives import serialization as crypto_serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
from cryptography.hazmat.backends import default_backend as crypto_default_backend
|
||||
from cryptography.hazmat.primitives.serialization import load_pem_private_key
|
||||
|
||||
|
||||
defaults = {
|
||||
|
@ -23,46 +23,40 @@ defaults = {
|
|||
)
|
||||
def keys(metadata):
|
||||
keys = {}
|
||||
|
||||
|
||||
for domain in metadata.get('mailserver/domains'):
|
||||
if domain in metadata.get(f'opendkim/keys'):
|
||||
continue
|
||||
|
||||
pubkey_path = join(repo.path, 'data', 'dkim', f'{domain}.pubkey')
|
||||
|
||||
privkey_path = join(repo.path, 'data', 'dkim', f'{domain}.privkey.enc')
|
||||
|
||||
if not exists(pubkey_path) or not exists(privkey_path):
|
||||
key = rsa.generate_private_key(
|
||||
backend=crypto_default_backend(),
|
||||
public_exponent=65537,
|
||||
key_size=2048
|
||||
)
|
||||
with open(pubkey_path, 'w') as file:
|
||||
file.write(
|
||||
''.join(
|
||||
key.public_key().public_bytes(
|
||||
crypto_serialization.Encoding.PEM,
|
||||
crypto_serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode().split('\n')[1:-2]
|
||||
)
|
||||
)
|
||||
|
||||
if not exists(privkey_path):
|
||||
with open(privkey_path, 'w') as file:
|
||||
file.write(
|
||||
repo.vault.encrypt(
|
||||
key.private_bytes(
|
||||
rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=2048
|
||||
).private_bytes(
|
||||
crypto_serialization.Encoding.PEM,
|
||||
crypto_serialization.PrivateFormat.PKCS8,
|
||||
crypto_serialization.NoEncryption()
|
||||
).decode()
|
||||
)
|
||||
)
|
||||
|
||||
with open(pubkey_path, 'r') as pubkey:
|
||||
with open(privkey_path, 'r') as privkey:
|
||||
keys[domain] = {
|
||||
'public': pubkey.read(),
|
||||
'private': repo.vault.decrypt(privkey.read()),
|
||||
}
|
||||
|
||||
with open(privkey_path, 'r') as file:
|
||||
privkey = str(repo.vault.decrypt(file.read()))
|
||||
|
||||
keys[domain] = {
|
||||
'public': ''.join(
|
||||
load_pem_private_key(privkey.encode(), password=None).public_key().public_bytes(
|
||||
crypto_serialization.Encoding.PEM,
|
||||
crypto_serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode().split('\n')[1:-2]
|
||||
),
|
||||
'private': privkey,
|
||||
}
|
||||
|
||||
return {
|
||||
'opendkim': {
|
||||
|
@ -76,13 +70,13 @@ def keys(metadata):
|
|||
)
|
||||
def dns(metadata):
|
||||
dns = {}
|
||||
|
||||
|
||||
for domain, keys in metadata.get('opendkim/keys').items():
|
||||
raw_key = sub('^ssh-rsa ', '', keys['public'])
|
||||
dns[f'mail._domainkey.{domain}'] = {
|
||||
'TXT': [f'v=DKIM1; k=rsa; p={raw_key}'],
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
'dns': dns,
|
||||
}
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn4dQp8/c9DHjoEIHdYZAzbpYoADB+/sR0iZiWrVUdckoSXCV7+kg3wPwC3jVs+mOMOoME7+t6/YssuHp6kzCP56afJYC/LwMz9DIQLIOY4EgTrT+wBIT82Z7EPuCPti+wkoNiiQ5a7SdV3SHhldow0UOHLm0wLuLVQgGAVPk6W+wKFBWWobYfFYR4gz/fZ/fMxVP9N6SR/cyUTFpZAi276bv5F00qDEPqEz9pKW0ZKIDanzPxvmJviJbcuFUt4EVM7VFHifgLVPwNaw8cIEIhyiWd7DYwQ+oIgWqgrJV3K0IFSIeRqIuUdHXTxtK05RQAubwfEAftukm5BuwJ7tX8wIDAQAB
|
|
@ -1 +0,0 @@
|
|||
MIIBIjANBgkqhkiG9w0BAQEFAAOC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