+#include <time.h>
+#include <assert.h>
+
+#include <inttypes.h>
+
+/*
+ * Given a protocol version sent by peer, determine if we accept that version
+ * and downgrade if appropriate (which can not be done for the client side).
+ */
+static
+OSStatus sslVerifyProtVersion(
+ SSLContext *ctx,
+ SSLProtocolVersion peerVersion, // sent by peer
+ SSLProtocolVersion *negVersion) // final negotiated version if return success
+{
+ if ((ctx->isDTLS)
+ ? peerVersion > ctx->minProtocolVersion
+ : peerVersion < ctx->minProtocolVersion) {
+ return errSSLNegotiation;
+ }
+ if ((ctx->isDTLS)
+ ? peerVersion < ctx->maxProtocolVersion
+ : peerVersion > ctx->maxProtocolVersion) {
+ if (ctx->protocolSide == kSSLClientSide) {
+ return errSSLNegotiation;
+ }
+ *negVersion = ctx->maxProtocolVersion;
+ } else {
+ *negVersion = peerVersion;
+ }
+
+ return errSecSuccess;
+}
+