-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmessage.py
212 lines (181 loc) · 8.98 KB
/
message.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# -*- coding: utf-8 -*-
# pylint: disable=r0913
""" message class """
from __future__ import print_function
import json
from typing import Tuple, Dict
from acme_srv.helper import decode_message, load_config
from acme_srv.error import Error
from acme_srv.db_handler import DBstore
from acme_srv.nonce import Nonce
from acme_srv.signature import Signature
class Message(object):
""" Message handler """
def __init__(self, debug: bool = False, srv_name: str = None, logger: object = None):
self.debug = debug
self.logger = logger
self.nonce = Nonce(self.debug, self.logger)
self.dbstore = DBstore(self.debug, self.logger)
self.server_name = srv_name
self.path_dic = {'acct_path': '/acme/acct/', 'revocation_path': '/acme/revokecert'}
self.disable_dic = {'signature_check_disable': False, 'nonce_check_disable': False}
self._config_load()
def __enter__(self):
""" Makes ACMEHandler a Context Manager """
return self
def __exit__(self, *args):
""" cose the connection at the end of the context """
def _config_load(self):
"""" load config from file """
self.logger.debug('_config_load()')
config_dic = load_config()
if 'Nonce' in config_dic:
self.disable_dic['nonce_check_disable'] = config_dic.getboolean('Nonce', 'nonce_check_disable', fallback=False)
self.disable_dic['signature_check_disable'] = config_dic.getboolean('Nonce', 'signature_check_disable', fallback=False)
if 'Directory' in config_dic and 'url_prefix' in config_dic['Directory']:
self.path_dic = {k: config_dic['Directory']['url_prefix'] + v for k, v in self.path_dic.items()}
def _name_rev_get(self, content: Dict[str, str]) -> str:
""" this is needed for cases where we get a revocation message signed with account key but account name is missing """
self.logger.debug('Message._name_rev_get()')
try:
account_list = self.dbstore.account_lookup('jwk', json.dumps(content['jwk']))
except Exception as err_:
self.logger.critical('acme2certifier database error in Message._name_rev_get(): %s', err_)
account_list = []
if account_list:
if 'name' in account_list:
kid = account_list['name']
else:
kid = None
else:
kid = None
self.logger.debug('Message._name_rev_get() ended with kid: %s', kid)
return kid
def _name_get(self, content: Dict[str, str]) -> str:
""" get name for account """
self.logger.debug('Message._name_get()')
if 'kid' in content:
self.logger.debug('kid: %s', content['kid'])
kid = content['kid'].replace(f'{self.server_name}{self.path_dic["acct_path"]}', '')
if '/' in kid:
kid = None
elif 'jwk' in content and 'url' in content:
if content['url'] == f'{self.server_name}{self.path_dic["revocation_path"]}':
# this is needed for cases where we get a revocation message signed with account key but account name is missing
kid = self._name_rev_get(content)
else:
kid = None
else:
kid = None
self.logger.debug('Message._name_get() returns: %s', kid)
return kid
def _check(self, skip_nonce_check: bool, skip_signature_check: bool, content: str, protected: Dict[str, str], use_emb_key: bool) -> Tuple[int, str, str, str]:
""" decoding successful - check nonce for anti replay protection """
self.logger.debug('Message._check()')
account_name = None
if skip_nonce_check or self.disable_dic['nonce_check_disable']:
# nonce check can be skipped by configuration and in case of key-rollover
if self.disable_dic['nonce_check_disable']:
self.logger.error('**** NONCE CHECK DISABLED!!! Severe security issue ****')
else:
self.logger.info('skip nonce check of inner payload during keyrollover')
code = 200
message = None
detail = None
else:
(code, message, detail) = self.nonce.check(protected)
if code == 200 and not skip_signature_check:
# nonce check successful - check signature
account_name = self._name_get(protected)
signature = Signature(self.debug, self.server_name, self.logger)
# we need the decoded protected header to grab a key to verify signature
(sig_check, error, error_detail) = signature.check(account_name, content, use_emb_key, protected)
if sig_check:
code = 200
message = None
detail = None
else:
code = 403
message = error
detail = error_detail
self.logger.debug('Message._check() ended with: %s', code)
return (code, message, detail, account_name)
# pylint: disable=R0914
def check(self, content: str, use_emb_key: bool = False, skip_nonce_check: bool = False) -> Tuple[int, str, str, Dict[str, str], Dict[str, str], str]:
""" validate message """
self.logger.debug('Message.check()')
# disable signature check if paramter has been set
if self.disable_dic['signature_check_disable']:
self.logger.error('**** SIGNATURE_CHECK_DISABLE!!! Severe security issue ****')
skip_signature_check = True
else:
skip_signature_check = False
# decode message
(result, error_detail, protected, payload, _signature) = decode_message(self.logger, content)
account_name = None
if result:
(code, message, detail, account_name) = self._check(skip_nonce_check, skip_signature_check, content, protected, use_emb_key)
else:
# message could not get decoded
code = 400
message = 'urn:ietf:params:acme:error:malformed'
detail = error_detail
self.logger.debug('Message.check() ended with:%s', code)
return (code, message, detail, protected, payload, account_name)
def cli_check(self, content: str) -> Tuple[int, str, str, Dict[str, str], Dict[str, str], str, Dict[str, str]]:
""" validate message coming from CLI client """
self.logger.debug('Message.cli_check()')
# decode message
(result, error_detail, protected, payload, _signature) = decode_message(self.logger, content)
account_name = None
permissions = {}
if result:
# check signature
account_name = self._name_get(protected)
signature = Signature(self.debug, self.server_name, self.logger)
# we need the decoded protected header to grab a key to verify signature
(sig_check, error, error_detail) = signature.cli_check(account_name, content)
if sig_check:
code = 200
message = None
detail = None
permissions = self.dbstore.cli_permissions_get(account_name)
else:
code = 403
message = error
detail = error_detail
else:
# message could not get decoded
code = 400
message = 'urn:ietf:params:acme:error:malformed'
detail = error_detail
self.logger.debug('Message.check() ended with:%s', code)
return (code, message, detail, protected, payload, account_name, permissions)
def prepare_response(self, response_dic: Dict[str, str], status_dic: Dict[str, str], add_nonce: bool = True) -> Dict[str, str]:
""" prepare response_dic """
self.logger.debug('Message.prepare_response()')
if 'code' not in status_dic:
status_dic['code'] = 400
status_dic['type'] = 'urn:ietf:params:acme:error:serverInternal'
status_dic['detail'] = 'http status code missing'
if 'type' not in status_dic:
status_dic['type'] = 'urn:ietf:params:acme:error:serverInternal'
if 'detail' not in status_dic:
status_dic['detail'] = None
# create response
response_dic['code'] = status_dic['code']
# create header if not existing
if 'header' not in response_dic:
response_dic['header'] = {}
if status_dic['code'] >= 400:
if status_dic['detail']:
# some error occured get details
error_message = Error(self.debug, self.logger)
status_dic['detail'] = error_message.enrich_error(status_dic['type'], status_dic['detail'])
response_dic['data'] = {'status': status_dic['code'], 'type': status_dic['type'], 'detail': status_dic['detail']}
else:
response_dic['data'] = {'status': status_dic['code'], 'type': status_dic['type']}
# always add nonce to header
if add_nonce:
response_dic['header']['Replay-Nonce'] = self.nonce.generate_and_add()
return response_dic