~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/asn1_alt.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*************************************************
2
 
* AlternativeName Source File                    *
3
 
* (C) 1999-2007 The Botan Project                *
4
 
*************************************************/
5
 
 
6
 
#include <botan/asn1_obj.h>
7
 
#include <botan/der_enc.h>
8
 
#include <botan/ber_dec.h>
9
 
#include <botan/oids.h>
10
 
#include <botan/stl_util.h>
11
 
#include <botan/charset.h>
12
 
 
13
 
namespace Botan {
14
 
 
15
 
/*************************************************
16
 
* Create an AlternativeName                      *
17
 
*************************************************/
18
 
AlternativeName::AlternativeName(const std::string& email_addr,
19
 
                                 const std::string& uri,
20
 
                                 const std::string& dns)
21
 
   {
22
 
   add_attribute("RFC822", email_addr);
23
 
   add_attribute("DNS", dns);
24
 
   add_attribute("URI", uri);
25
 
   }
26
 
 
27
 
/*************************************************
28
 
* Add an attribute to an alternative name        *
29
 
*************************************************/
30
 
void AlternativeName::add_attribute(const std::string& type,
31
 
                                    const std::string& str)
32
 
   {
33
 
   if(type == "" || str == "")
34
 
      return;
35
 
 
36
 
   typedef std::multimap<std::string, std::string>::iterator iter;
37
 
   std::pair<iter, iter> range = alt_info.equal_range(type);
38
 
   for(iter j = range.first; j != range.second; ++j)
39
 
      if(j->second == str)
40
 
         return;
41
 
 
42
 
   multimap_insert(alt_info, type, str);
43
 
   }
44
 
 
45
 
/*************************************************
46
 
* Add an OtherName field                         *
47
 
*************************************************/
48
 
void AlternativeName::add_othername(const OID& oid, const std::string& value,
49
 
                                    ASN1_Tag type)
50
 
   {
51
 
   if(value == "")
52
 
      return;
53
 
   multimap_insert(othernames, oid, ASN1_String(value, type));
54
 
   }
55
 
 
56
 
/*************************************************
57
 
* Get the attributes of this alternative name    *
58
 
*************************************************/
59
 
std::multimap<std::string, std::string> AlternativeName::get_attributes() const
60
 
   {
61
 
   return alt_info;
62
 
   }
63
 
 
64
 
/*************************************************
65
 
* Get the otherNames                             *
66
 
*************************************************/
67
 
std::multimap<OID, ASN1_String> AlternativeName::get_othernames() const
68
 
   {
69
 
   return othernames;
70
 
   }
71
 
 
72
 
/*************************************************
73
 
* Return all of the alternative names            *
74
 
*************************************************/
75
 
std::multimap<std::string, std::string> AlternativeName::contents() const
76
 
   {
77
 
   std::multimap<std::string, std::string> names;
78
 
 
79
 
   typedef std::multimap<std::string, std::string>::const_iterator rdn_iter;
80
 
   for(rdn_iter j = alt_info.begin(); j != alt_info.end(); ++j)
81
 
      multimap_insert(names, j->first, j->second);
82
 
 
83
 
   typedef std::multimap<OID, ASN1_String>::const_iterator on_iter;
84
 
   for(on_iter j = othernames.begin(); j != othernames.end(); ++j)
85
 
      multimap_insert(names, OIDS::lookup(j->first), j->second.value());
86
 
 
87
 
   return names;
88
 
   }
89
 
 
90
 
/*************************************************
91
 
* Return if this object has anything useful      *
92
 
*************************************************/
93
 
bool AlternativeName::has_items() const
94
 
   {
95
 
   return (alt_info.size() > 0 || othernames.size() > 0);
96
 
   }
97
 
 
98
 
namespace {
99
 
 
100
 
/*************************************************
101
 
* DER encode an AlternativeName entry            *
102
 
*************************************************/
103
 
void encode_entries(DER_Encoder& encoder,
104
 
                    const std::multimap<std::string, std::string>& attr,
105
 
                    const std::string& type, ASN1_Tag tagging)
106
 
   {
107
 
   typedef std::multimap<std::string, std::string>::const_iterator iter;
108
 
 
109
 
   std::pair<iter, iter> range = attr.equal_range(type);
110
 
   for(iter j = range.first; j != range.second; ++j)
111
 
      {
112
 
      ASN1_String asn1_string(j->second, IA5_STRING);
113
 
      encoder.add_object(tagging, CONTEXT_SPECIFIC, asn1_string.iso_8859());
114
 
      }
115
 
   }
116
 
 
117
 
}
118
 
 
119
 
/*************************************************
120
 
* DER encode an AlternativeName extension        *
121
 
*************************************************/
122
 
void AlternativeName::encode_into(DER_Encoder& der) const
123
 
   {
124
 
   der.start_cons(SEQUENCE);
125
 
 
126
 
   encode_entries(der, alt_info, "RFC822", ASN1_Tag(1));
127
 
   encode_entries(der, alt_info, "DNS", ASN1_Tag(2));
128
 
   encode_entries(der, alt_info, "URI", ASN1_Tag(6));
129
 
 
130
 
   std::multimap<OID, ASN1_String>::const_iterator i;
131
 
   for(i = othernames.begin(); i != othernames.end(); ++i)
132
 
      {
133
 
      der.start_explicit(0)
134
 
         .encode(i->first)
135
 
         .start_explicit(0)
136
 
            .encode(i->second)
137
 
         .end_explicit()
138
 
      .end_explicit();
139
 
      }
140
 
 
141
 
   der.end_cons();
142
 
   }
143
 
 
144
 
/*************************************************
145
 
* Decode a BER encoded AlternativeName           *
146
 
*************************************************/
147
 
void AlternativeName::decode_from(BER_Decoder& source)
148
 
   {
149
 
   BER_Decoder names = source.start_cons(SEQUENCE);
150
 
 
151
 
   while(names.more_items())
152
 
      {
153
 
      BER_Object obj = names.get_next_object();
154
 
      if((obj.class_tag != CONTEXT_SPECIFIC) &&
155
 
         (obj.class_tag != (CONTEXT_SPECIFIC | CONSTRUCTED)))
156
 
         continue;
157
 
 
158
 
      ASN1_Tag tag = obj.type_tag;
159
 
 
160
 
      if(tag == 0)
161
 
         {
162
 
         BER_Decoder othername(obj.value);
163
 
 
164
 
         OID oid;
165
 
         othername.decode(oid);
166
 
         if(othername.more_items())
167
 
            {
168
 
            BER_Object othername_value_outer = othername.get_next_object();
169
 
            othername.verify_end();
170
 
 
171
 
            if(othername_value_outer.type_tag != ASN1_Tag(0) ||
172
 
               othername_value_outer.class_tag !=
173
 
                   (CONTEXT_SPECIFIC | CONSTRUCTED)
174
 
               )
175
 
               throw Decoding_Error("Invalid tags on otherName value");
176
 
 
177
 
            BER_Decoder othername_value_inner(othername_value_outer.value);
178
 
 
179
 
            BER_Object value = othername_value_inner.get_next_object();
180
 
            othername_value_inner.verify_end();
181
 
 
182
 
            ASN1_Tag value_type = value.type_tag;
183
 
 
184
 
            if(is_string_type(value_type) && value.class_tag == UNIVERSAL)
185
 
               add_othername(oid, ASN1::to_string(value), value_type);
186
 
            }
187
 
         }
188
 
      else if(tag == 1 || tag == 2 || tag == 6)
189
 
         {
190
 
         const std::string value = Charset::transcode(ASN1::to_string(obj),
191
 
                                                      LATIN1_CHARSET,
192
 
                                                      LOCAL_CHARSET);
193
 
 
194
 
         if(tag == 1) add_attribute("RFC822", value);
195
 
         if(tag == 2) add_attribute("DNS", value);
196
 
         if(tag == 6) add_attribute("URI", value);
197
 
         }
198
 
 
199
 
      }
200
 
   }
201
 
 
202
 
}