blob: 7ab78dbc83ab0b8f234e9f1fbd09480a542d2387 (
plain)
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
|
//-----------------------------------------------------------------------
// <copyright file="StandardExpirationBindingElement.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Messaging.Bindings {
using System;
using DotNetOpenAuth.Configuration;
/// <summary>
/// A message expiration enforcing binding element that supports messages
/// implementing the <see cref="IExpiringProtocolMessage"/> interface.
/// </summary>
internal class StandardExpirationBindingElement : IChannelBindingElement {
/// <summary>
/// Initializes a new instance of the <see cref="StandardExpirationBindingElement"/> class.
/// </summary>
internal StandardExpirationBindingElement() {
}
#region IChannelBindingElement Properties
/// <summary>
/// Gets the protection offered by this binding element.
/// </summary>
/// <value><see cref="MessageProtections.Expiration"/></value>
MessageProtections IChannelBindingElement.Protection {
get { return MessageProtections.Expiration; }
}
/// <summary>
/// Gets or sets the channel that this binding element belongs to.
/// </summary>
public Channel Channel { get; set; }
#endregion
/// <summary>
/// Gets the maximum age a message implementing the
/// <see cref="IExpiringProtocolMessage"/> interface can be before
/// being discarded as too old.
/// </summary>
protected internal static TimeSpan MaximumMessageAge {
get { return Configuration.DotNetOpenAuthSection.Messaging.MaximumMessageLifetime; }
}
#region IChannelBindingElement Methods
/// <summary>
/// Sets the timestamp on an outgoing message.
/// </summary>
/// <param name="message">The outgoing message.</param>
/// <returns>
/// The protections (if any) that this binding element applied to the message.
/// Null if this binding element did not even apply to this binding element.
/// </returns>
public MessageProtections? ProcessOutgoingMessage(IProtocolMessage message) {
IExpiringProtocolMessage expiringMessage = message as IExpiringProtocolMessage;
if (expiringMessage != null) {
expiringMessage.UtcCreationDate = DateTime.UtcNow;
return MessageProtections.Expiration;
}
return null;
}
/// <summary>
/// Reads the timestamp on a message and throws an exception if the message is too old.
/// </summary>
/// <param name="message">The incoming message.</param>
/// <returns>
/// The protections (if any) that this binding element applied to the message.
/// Null if this binding element did not even apply to this binding element.
/// </returns>
/// <exception cref="ExpiredMessageException">Thrown if the given message has already expired.</exception>
/// <exception cref="ProtocolException">
/// Thrown when the binding element rules indicate that this message is invalid and should
/// NOT be processed.
/// </exception>
public MessageProtections? ProcessIncomingMessage(IProtocolMessage message) {
IExpiringProtocolMessage expiringMessage = message as IExpiringProtocolMessage;
if (expiringMessage != null) {
// Yes the UtcCreationDate is supposed to always be in UTC already,
// but just in case a given message failed to guarantee that, we do it here.
DateTime creationDate = expiringMessage.UtcCreationDate.ToUniversalTimeSafe();
DateTime expirationDate = creationDate + MaximumMessageAge;
if (expirationDate < DateTime.UtcNow) {
throw new ExpiredMessageException(expirationDate, expiringMessage);
}
// Mitigate HMAC attacks (just guessing the signature until they get it) by
// disallowing post-dated messages.
ErrorUtilities.VerifyProtocol(
creationDate <= DateTime.UtcNow + DotNetOpenAuthSection.Messaging.MaximumClockSkew,
MessagingStrings.MessageTimestampInFuture,
creationDate);
return MessageProtections.Expiration;
}
return null;
}
#endregion
}
}
|