aboutsummaryrefslogtreecommitdiffstats
path: root/include/x509_ext.h
blob: 7e3b682882cadec5d21234325e1d607a9b3bf5de (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
/*************************************************
* X.509 Certificate Extensions Header File       *
* (C) 1999-2006 The Botan Project                *
*************************************************/

#ifndef BOTAN_X509_EXTENSIONS__
#define BOTAN_X509_EXTENSIONS__

#include <botan/asn1_int.h>
#include <botan/asn1_oid.h>
#include <botan/asn1_obj.h>

namespace Botan {

/*************************************************
* X.509 Certificate Extension                    *
*************************************************/
class Certificate_Extension : public ASN1_Object
   {
   public:
      void encode_into(class DER_Encoder&) const;
      void decode_from(class BER_Decoder&);
      void maybe_add(class DER_Encoder&) const;

      OID oid_of() const;
      void make_critical() { critical = true; }
      bool is_critical() const { return critical; }

      //virtual std::multimap<std::string, std::string> contents() const = 0;
      virtual std::string config_id() const = 0;
      virtual std::string oid_name() const = 0;

      Certificate_Extension() { critical = false; }
      virtual ~Certificate_Extension() {}
   protected:
      virtual bool should_encode() const { return true; }
      virtual MemoryVector<byte> encode_inner() const = 0;
      virtual void decode_inner(const MemoryRegion<byte>&) = 0;
   private:
      void encode_into(class DER_Encoder&, bool) const;
      bool critical;
   };

/*************************************************
* X.509 Certificate Extensions                   *
*************************************************/
class Extensions : public ASN1_Object
   {
   public:
      void encode_into(class DER_Encoder&) const;

      void add(Certificate_Extension* extn)
         { extensions.push_back(extn); }
         
      ~Extensions();
   private:
      std::vector<Certificate_Extension*> extensions;
   };

namespace Cert_Extension {

/*************************************************
* Basic Constraints Extension                    *
*************************************************/
class Basic_Constraints : public Certificate_Extension
   {
   public:
      Basic_Constraints(bool = false, u32bit = 0);
   private:
      std::string config_id() const { return "basic_constraints"; }
      std::string oid_name() const { return "X509v3.BasicConstraints"; }

      MemoryVector<byte> encode_inner() const;
      void decode_inner(const MemoryRegion<byte>&);

      bool is_ca;
      u32bit path_limit;
   };

/*************************************************
* Key Usage Constraints Extension                *
*************************************************/
class Key_Usage : public Certificate_Extension
   {
   public:
      Key_Usage(Key_Constraints);
   private:
      std::string config_id() const { return "key_usage"; }
      std::string oid_name() const { return "X509v3.KeyUsage"; }

      bool should_encode() const { return (constraints != NO_CONSTRAINTS); }
      MemoryVector<byte> encode_inner() const;
      void decode_inner(const MemoryRegion<byte>&);

      Key_Constraints constraints;
   };

/*************************************************
* Subject Key Identifier Extension               *
*************************************************/
class Subject_Key_ID : public Certificate_Extension
   {
   public:
      Subject_Key_ID(const MemoryRegion<byte>&);
   private:
      std::string config_id() const { return "subject_key_id"; }
      std::string oid_name() const { return "X509v3.SubjectKeyIdentifier"; }

      MemoryVector<byte> encode_inner() const;
      void decode_inner(const MemoryRegion<byte>&);

      MemoryVector<byte> key_id;
   };

/*************************************************
* Authority Key Identifier Extension             *
*************************************************/
class Authority_Key_ID : public Certificate_Extension
   {
   public:
      Authority_Key_ID(const MemoryRegion<byte>&);
   private:
      std::string config_id() const { return "authority_key_id"; }
      std::string oid_name() const { return "X509v3.AuthorityKeyIdentifier"; }

      bool should_encode() const { return (key_id.size() > 0); }
      MemoryVector<byte> encode_inner() const;
      void decode_inner(const MemoryRegion<byte>&);

      MemoryVector<byte> key_id;
   };

/*************************************************
* Alternative Name Extension                     *
*************************************************/
class Alternative_Name : public Certificate_Extension
   {
   public:
      Alternative_Name(const AlternativeName&,
                       const std::string&, const std::string&);
   private:
      std::string config_id() const { return config_name_str; }
      std::string oid_name() const { return oid_name_str; }

      bool should_encode() const { return alt_name.has_items(); }
      MemoryVector<byte> encode_inner() const;
      void decode_inner(const MemoryRegion<byte>&);

      std::string config_name_str, oid_name_str;
      AlternativeName alt_name;
   };

/*************************************************
* Extended Key Usage Extension                   *
*************************************************/
class Extended_Key_Usage : public Certificate_Extension
   {
   public:
      Extended_Key_Usage(const std::vector<OID>&);
   private:
      std::string config_id() const { return "extended_key_usage"; }
      std::string oid_name() const { return "X509v3.ExtendedKeyUsage"; }

      bool should_encode() const { return (oids.size() > 0); }
      MemoryVector<byte> encode_inner() const;
      void decode_inner(const MemoryRegion<byte>&);

      std::vector<OID> oids;
   };

/*************************************************
* CRL Number Extension                           *
*************************************************/
class CRL_Number : public Certificate_Extension
   {
   public:
      CRL_Number(u32bit = 0);
   private:
      std::string config_id() const { return "crl_number"; }
      std::string oid_name() const { return "X509v3.CRLNumber"; }

      bool should_encode() const { return (crl_number != 0); }

      MemoryVector<byte> encode_inner() const;
      void decode_inner(const MemoryRegion<byte>&);

      u32bit crl_number;
   };

}

}

#endif