Botan  1.10.9
pubkey_enums.cpp
Go to the documentation of this file.
1 /*
2 * KeyUsage
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/pubkey_enums.h>
9 #include <botan/ber_dec.h>
10 
11 namespace Botan {
12 
13 namespace BER {
14 
15 /*
16 * Decode a BER encoded KeyUsage
17 */
18 void decode(BER_Decoder& source, Key_Constraints& key_usage)
19  {
20  BER_Object obj = source.get_next_object();
21 
22  if(obj.type_tag != BIT_STRING || obj.class_tag != UNIVERSAL)
23  throw BER_Bad_Tag("Bad tag for usage constraint",
24  obj.type_tag, obj.class_tag);
25  if(obj.value.size() != 2 && obj.value.size() != 3)
26  throw BER_Decoding_Error("Bad size for BITSTRING in usage constraint");
27  if(obj.value[0] >= 8)
28  throw BER_Decoding_Error("Invalid unused bits in usage constraint");
29 
30  const byte mask = (0xFF << obj.value[0]);
31  obj.value[obj.value.size()-1] &= mask;
32 
33  u16bit usage = 0;
34  for(size_t j = 1; j != obj.value.size(); ++j)
35  usage = (obj.value[j] << 8) | usage;
36 
37  key_usage = Key_Constraints(usage);
38  }
39 
40 }
41 
42 }
unsigned char byte
Definition: types.h:22
void decode(BER_Decoder &source, Key_Constraints &key_usage)
unsigned short u16bit
Definition: types.h:27
size_t size() const
Definition: secmem.h:29
SecureVector< byte > value
Definition: asn1_int.h:83
BER_Object get_next_object()
Definition: ber_dec.cpp:193
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82
Key_Constraints
Definition: pubkey_enums.h:18