1 /** @file
2  *
3  *  A brief file description
4  *
5  *  @section license License
6  *
7  *  Licensed to the Apache Software Foundation (ASF) under one
8  *  or more contributor license agreements.  See the NOTICE file
9  *  distributed with this work for additional information
10  *  regarding copyright ownership.  The ASF licenses this file
11  *  to you under the Apache License, Version 2.0 (the
12  *  "License"); you may not use this file except in compliance
13  *  with the License.  You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  *  Unless required by applicable law or agreed to in writing, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  */
23 
24 #pragma once
25 
26 #include <map>
27 #include <tscore/ink_memory.h>
28 
29 #include <openssl/ssl.h>
30 #include "QUICTypes.h"
31 #include <cstddef>
32 
33 class QUICTransportParameterId
34 {
35 public:
36   enum {
37     ORIGINAL_DESTINATION_CONNECTION_ID,
38     MAX_IDLE_TIMEOUT,
39     STATELESS_RESET_TOKEN,
40     MAX_UDP_PAYLOAD_SIZE,
41     INITIAL_MAX_DATA,
42     INITIAL_MAX_STREAM_DATA_BIDI_LOCAL,
43     INITIAL_MAX_STREAM_DATA_BIDI_REMOTE,
44     INITIAL_MAX_STREAM_DATA_UNI,
45     INITIAL_MAX_STREAMS_BIDI,
46     INITIAL_MAX_STREAMS_UNI,
47     ACK_DELAY_EXPONENT,
48     MAX_ACK_DELAY,
49     DISABLE_ACTIVE_MIGRATION,
50     PREFERRED_ADDRESS,
51     ACTIVE_CONNECTION_ID_LIMIT,
52     INITIAL_SOURCE_CONNECTION_ID,
53     RETRY_SOURCE_CONNECTION_ID,
54   };
55 
56   explicit operator bool() const { return true; }
57   bool
58   operator==(const QUICTransportParameterId &x) const
59   {
60     return this->_id == x._id;
61   }
62 
63   bool
64   operator==(const uint16_t &x) const
65   {
66     return this->_id == x;
67   }
68 
uint16_t()69   operator uint16_t() const { return _id; };
QUICTransportParameterId()70   QUICTransportParameterId() : _id(0){};
QUICTransportParameterId(uint16_t id)71   QUICTransportParameterId(uint16_t id) : _id(id){};
72 
73 private:
74   uint16_t _id = 0;
75 };
76 
77 class QUICTransportParameters
78 {
79 public:
80   QUICTransportParameters(const uint8_t *buf, size_t len, QUICVersion version);
81   virtual ~QUICTransportParameters();
82 
83   bool is_valid() const;
84 
85   const uint8_t *getAsBytes(QUICTransportParameterId id, uint16_t &len) const;
86   uint64_t getAsUInt(QUICTransportParameterId id) const;
87   bool contains(QUICTransportParameterId id) const;
88 
89   void set(QUICTransportParameterId id, const uint8_t *value, uint16_t value_len);
90   void set(QUICTransportParameterId id, uint64_t value);
91 
92   void store(uint8_t *buf, uint16_t *len) const;
93 
94 protected:
95   class Value
96   {
97   public:
98     Value(const uint8_t *data, uint16_t len);
99     ~Value();
100     const uint8_t *data() const;
101     uint16_t len() const;
102 
103   private:
104     uint8_t *_data = nullptr;
105     uint16_t _len  = 0;
106   };
107 
QUICTransportParameters()108   QUICTransportParameters(){};
109   void _load(const uint8_t *buf, size_t len, QUICVersion version);
110   bool _valid = false;
111 
112   virtual std::ptrdiff_t _parameters_offset(const uint8_t *buf) const = 0;
113   virtual int _validate_parameters(QUICVersion version) const;
114   void _print() const;
115 
116   std::map<QUICTransportParameterId, Value *> _parameters;
117 };
118 
119 class QUICTransportParametersInClientHello : public QUICTransportParameters
120 {
121 public:
QUICTransportParametersInClientHello()122   QUICTransportParametersInClientHello() : QUICTransportParameters(){};
123   QUICTransportParametersInClientHello(const uint8_t *buf, size_t len, QUICVersion version);
124 
125 protected:
126   std::ptrdiff_t _parameters_offset(const uint8_t *buf) const override;
127   int _validate_parameters(QUICVersion version) const override;
128 
129 private:
130 };
131 
132 class QUICTransportParametersInEncryptedExtensions : public QUICTransportParameters
133 {
134 public:
QUICTransportParametersInEncryptedExtensions()135   QUICTransportParametersInEncryptedExtensions() : QUICTransportParameters(){};
136   QUICTransportParametersInEncryptedExtensions(const uint8_t *buf, size_t len, QUICVersion version);
137 
138 protected:
139   std::ptrdiff_t _parameters_offset(const uint8_t *buf) const override;
140   int _validate_parameters(QUICVersion version) const override;
141 };
142 
143 class QUICTransportParametersHandler
144 {
145 public:
146   static constexpr int TRANSPORT_PARAMETER_ID = 0xffa5;
147 
148   static int add(SSL *s, unsigned int ext_type, unsigned int context, const unsigned char **out, size_t *outlen, X509 *x,
149                  size_t chainidx, int *al, void *add_arg);
150   static void free(SSL *s, unsigned int ext_type, unsigned int context, const unsigned char *out, void *add_arg);
151   static int parse(SSL *s, unsigned int ext_type, unsigned int context, const unsigned char *in, size_t inlen, X509 *x,
152                    size_t chainidx, int *al, void *parse_arg);
153 };
154