aboutsummaryrefslogtreecommitdiff
path: root/openssl/agent/pkcs11/url.cxx
blob: 0c946d9d719740ba3274adf604daa58d04671731 (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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// file      : openssl/agent/pkcs11/url.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <openssl/agent/pkcs11/url.hxx>

#include <cerrno>
#include <cstdlib>  // strtoull()
#include <iterator> // back_inserter()

namespace openssl
{
  namespace agent
  {
    namespace pkcs11
    {
      using namespace std;

      // Convenience functions.
      //
      static uint64_t
      parse_uint64 (const string& s,
                    uint64_t min,
                    uint64_t max,
                    const char* what)
      {
        if (s[0] != '-' && s[0] != '+') // strtoull() allows these.
        {
          const char* b (s.c_str ());
          char* e (nullptr);
          errno = 0; // We must clear it according to POSIX.
          uint64_t v (strtoull (b, &e, 10)); // Can't throw.

          if (errno != ERANGE && e == b + s.size () && v >= min && v <= max)
            return v;
        }

        throw invalid_argument (string ("invalid ") + what + " '" + s + "'");
      }

      // url_traits
      //
      optional<url_traits::scheme_type> url_traits::
      translate_scheme (const string_type&         /* url */,
                        string_type&&              scheme,
                        optional<authority_type>&  authority,
                        optional<path_type>&       path,
                        optional<string_type>&     /* query */,
                        optional<string_type>&     fragment,
                        bool&                      rootless)
      {
        // If something is wrong with the URL leave the basic_url constructor
        // to throw.
        //
        if (scheme.empty ())
          return nullopt;

        if (icasecmp (scheme, "pkcs11") != 0)
          throw invalid_argument ("invalid scheme");

        if (authority)
          throw invalid_argument ("unexpected authority");

        if (path && (!rootless || path->find ('/') != string::npos))
          throw invalid_argument ("one-level path expected");

        if (fragment)
          throw invalid_argument ("unexpected fragment");

        return move (scheme);
      }

      url_traits::string_type url_traits::
      translate_scheme (string_type&                     /* url */,
                        const scheme_type&               scheme,
                        const optional<authority_type>&  /* authority */,
                        const optional<path_type>&       /* path */,
                        const optional<string_type>&     /* query */,
                        const optional<string_type>&     /* fragment */,
                        bool                             /* rootless */)
      {
        return scheme;
      }

      url_traits::path_type url_traits::
      translate_path (string_type&& path)
      {
        return move (path);
      }

      url_traits::string_type url_traits::
      translate_path (const path_type& path)
      {
        return path;
      }

      // library_version
      //
      library_version::
      library_version (const string& s)
      {
        auto num = [] (const string& s, const char* what)
        {
          return static_cast<unsigned char> (parse_uint64 (s, 0, 255, what));
        };

        size_t p (s.find ('.'));

        if (p != string::npos)
        {
          major = num (string (s, 0, p),  "library major version");
          minor = num (string (s, p + 1), "library minor version");
        }
        else
        {
          major = num (s, "library major version");
          minor = 0;
        }
      }

      // Parse the attribute name=value representation. The value can contain
      // binary data.
      //
      // It would probably be cleaner to return pair<string, vector<char>>,
      // but this would uglify a client code quite a bit and make it less
      // efficient.
      //
      static pair<string, string>
      attribute (const string& s, size_t b, size_t n)
      {
        size_t i (b);
        size_t e (b + n);

        for (; i != e && s[i] != '='; ++i) ;

        if (i == e)
          throw invalid_argument (
            "no value for attribute '" + string (s, b, n) + "'");

        string a;
        url::decode (s.begin () + b, s.begin () + i, back_inserter (a));

        string v;
        url::decode (s.begin () + i + 1, s.begin () + e, back_inserter (v));

        return make_pair (move (a), move (v));
      }

      // identity
      //
      identity::
      identity (const url& u)
      {
        const optional<string>& path (u.path);

        // If URL path component is absent then create the identity that
        // matches all PKCS#11 entities in the system.
        //
        if (!path)
          return;

        for (size_t b (0), e (0), n; (n = next_word (*path, b, e, ';')); )
        {
          pair<string, string> a (attribute (*path, b, n));

          const string& an (a.first);
          string& av (a.second);

          auto set = [&an] (auto& attr, auto&& val)
          {
            if (attr)
              throw invalid_argument ("duplicate attribute '" + an + "'");

            attr = move (val);
          };

          // Module.
          //
          if (an == "library-manufacturer")
            set (library_manufacturer, move (av));
          else if (an == "library-version")
            set (library_version, library_version_type (av));
          else if (an == "library-description")
            set (library_description, move (av));

          // Slot.
          //
          else if (an == "slot-id")
            set (slot_id,
                 static_cast<unsigned long> (
                   parse_uint64 (av, 0, ~0UL, "slot-id attribute value")));
          else if (an == "slot-manufacturer")
            set (slot_manufacturer, move (av));
          else if (an == "slot-description")
            set (slot_description, move (av));

          // Token.
          //
          else if (an == "serial")
            set (serial, move (av));
          else if (an == "token")
            set (token, move (av));
          else if (an == "model")
            set (model, move (av));
          else if (an == "manufacturer")
            set (manufacturer, move (av));

          // Storage object.
          //
          else if (an == "id")
            set (id, vector<unsigned char> (av.begin (), av.end ()));
          else if (an == "object")
            set (object, move (av));
          else if (an == "type")
            set (type, move (av));
          else
            throw invalid_argument ("unknown attribute '" + an + "'");
        }
      }

      // access
      //
      access::
      access (const url& u)
      {
        const optional<string>& query (u.query);

        // If URL query component is absent then create an object that
        // provides no access attributes.
        //
        if (!query)
          return;

        for (size_t b (0), e (0), n; (n = next_word (*query, b, e, ';')); )
        {
          pair<string, string> a (attribute (*query, b, n));

          const string& an (a.first);
          string& av (a.second);

          auto set = [&an] (auto& attr, auto&& val)
          {
            if (attr)
              throw invalid_argument ("duplicate attribute '" + an + "'");

            attr = move (val);
          };

          // Note that unrecognized attributes are ignored (see the traits
          // class notes for details).
          //
          if (an == "pin-source")
            set (pin_source, move (av));
          else if (an == "pin-value")
            set (pin_value, move (av));
          else if (an == "module-name")
          {
            try
            {
              path p (av);

              if (!p.empty () && p.simple ())
              {
                set (module_name, move (p));
                continue;
              }

              // Fall through.
            }
            catch (const invalid_path& e)
            {
              // Fall through.
            }

            throw invalid_argument (
              "invalid value '" + av + "' for module-name attribute");
          }
          else if (an == "module-path")
          {
            try
            {
              path p (move (av));

              if (p.relative ())
                throw invalid_argument ("relative path '" + p.string () +
                                        "' for module-path attribute");

              set (module_path, move (p));
            }
            catch (const invalid_path& e)
            {
              throw invalid_argument (
                "invalid path '" + e.path + "' for module-path attribute");
            }
          }
        }

        if (pin_source && pin_value)
          throw invalid_argument (
            "both pin-source and pin-value attributes specified");
      }
    }
  }
}