Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 4b34970

Browse files
committed
Strict on which signing algorithm that can be used.
1 parent 90f71ce commit 4b34970

File tree

3 files changed

+165
-98
lines changed

3 files changed

+165
-98
lines changed

src/oidcmsg/oidc/__init__.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import time
1010

1111
from cryptojwt import as_unicode
12+
from cryptojwt.exception import UnsupportedAlgorithm
1213
from cryptojwt.jws.jws import factory as jws_factory
1314
from cryptojwt.jws.utils import left_hash
1415
from cryptojwt.jwt import JWT
@@ -241,7 +242,7 @@ def check_char_set(string, allowed):
241242
ID_TOKEN_VERIFY_ARGS = ['keyjar', 'verify', 'encalg', 'encenc', 'sigalg',
242243
'issuer', 'allow_missing_kid', 'no_kid_issuer',
243244
'trusting', 'skew', 'nonce_storage_time', 'client_id',
244-
'allow_sign_alg_none']
245+
'allow_sign_alg_none', 'allowed_sign_alg']
245246

246247
CLAIMS_WITH_VERIFIED = ['id_token', 'id_token_hint', 'request']
247248

@@ -282,11 +283,18 @@ def verify_id_token(msg, check_hash=False, claim='id_token', **kwargs):
282283
_allow_none = kwargs['allow_sign_alg_none']
283284
except KeyError:
284285
logger.info('Signing algorithm None not allowed')
285-
return False
286+
raise UnsupportedAlgorithm('Signing algorithm None not allowed')
286287
else:
287288
if not _allow_none:
288289
logger.info('Signing algorithm None not allowed')
289-
return False
290+
raise UnsupportedAlgorithm('Signing algorithm None not allowed')
291+
else:
292+
if "allowed_sign_alg" in kwargs:
293+
if _jws.jwt.headers['alg'] != kwargs["allowed_sign_alg"]:
294+
_msg = "Wrong token signing algorithm, {} != {}".format(
295+
_jws.jwt.headers['alg'], kwargs["allowed_sign_alg"])
296+
logger.error(_msg)
297+
raise UnsupportedAlgorithm(_msg)
290298

291299
_body = _jws.jwt.payload()
292300
if 'keyjar' in kwargs:

src/oidcmsg/oidc/session.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import logging
22

3+
from cryptojwt.exception import UnsupportedAlgorithm
4+
35
from oidcmsg.time_util import utc_time_sans_frac
4-
from ..exception import MessageException, NotForMe
6+
from ..exception import MessageException
7+
from ..exception import NotForMe
58
from ..message import Message
69
from ..message import REQUIRED_LIST_OF_STRINGS
710
from ..message import SINGLE_OPTIONAL_STRING
811
from ..message import SINGLE_REQUIRED_INT
912
from ..message import SINGLE_REQUIRED_JSON
1013
from ..message import SINGLE_REQUIRED_STRING
1114
from ..oauth2 import ResponseMessage
12-
from ..oidc import clear_verified_claims, verify_id_token
13-
from ..oidc import verified_claim_name
14-
from ..oidc import IdToken
1515
from ..oidc import ID_TOKEN_VERIFY_ARGS
16+
from ..oidc import IdToken
1617
from ..oidc import MessageWithIdToken
1718
from ..oidc import SINGLE_OPTIONAL_IDTOKEN
18-
19+
from ..oidc import clear_verified_claims
20+
from ..oidc import verified_claim_name
21+
from ..oidc import verify_id_token
1922

2023
logger = logging.getLogger(__name__)
2124

@@ -25,7 +28,7 @@ class RefreshSessionRequest(MessageWithIdToken):
2528
c_param.update({
2629
"redirect_url": SINGLE_REQUIRED_STRING,
2730
"state": SINGLE_REQUIRED_STRING
28-
})
31+
})
2932

3033

3134
class RefreshSessionResponse(MessageWithIdToken, ResponseMessage):
@@ -47,7 +50,7 @@ class EndSessionRequest(Message):
4750
"id_token_hint": SINGLE_OPTIONAL_IDTOKEN,
4851
"post_logout_redirect_uri": SINGLE_OPTIONAL_STRING,
4952
"state": SINGLE_OPTIONAL_STRING
50-
}
53+
}
5154

5255
def verify(self, **kwargs):
5356
super(EndSessionRequest, self).verify(**kwargs)
@@ -111,7 +114,7 @@ def verify(self, **kwargs):
111114
raise ValueError('Wrong member value in "events"')
112115

113116
# There must be either a 'sub' or a 'sid', and may contain both
114-
if not('sub' in self or 'sid' in self):
117+
if not ('sub' in self or 'sid' in self):
115118
raise ValueError('There MUST be either a "sub" or a "sid"')
116119

117120
try:
@@ -141,6 +144,12 @@ def verify(self, **kwargs):
141144
if self['iat'] > (_now + _skew):
142145
raise ValueError('Invalid issued_at time')
143146

147+
_allowed = kwargs.get("allowed_sign_alg")
148+
if _allowed and self.jws_header['alg'] != _allowed:
149+
_msg = "Wrong token signing algorithm, {} != {}".format(
150+
self.jws_header['alg'], kwargs["allowed_sign_alg"])
151+
raise UnsupportedAlgorithm(_msg)
152+
144153
return True
145154

146155

@@ -155,7 +164,7 @@ class BackChannelLogoutRequest(Message):
155164

156165
c_param = {
157166
"logout_token": SINGLE_REQUIRED_STRING
158-
}
167+
}
159168

160169
def verify(self, **kwargs):
161170
super(BackChannelLogoutRequest, self).verify(**kwargs)

0 commit comments

Comments
 (0)