Botan 1.10.17
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
11namespace Botan {
12
13namespace BER {
14
15/*
16* Decode a BER encoded KeyUsage
17*/
18void 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}
BER_Object get_next_object()
Definition ber_dec.cpp:196
SecureVector< byte > value
Definition asn1_int.h:83
ASN1_Tag class_tag
Definition asn1_int.h:82
ASN1_Tag type_tag
Definition asn1_int.h:82
size_t size() const
Definition secmem.h:29
void decode(BER_Decoder &source, Key_Constraints &key_usage)
@ BIT_STRING
Definition asn1_int.h:30
@ UNIVERSAL
Definition asn1_int.h:20
unsigned short u16bit
Definition types.h:27
Key_Constraints
BER_Bad_Tag(const std::string &msg, ASN1_Tag tag)
Definition asn1_int.cpp:22
BER_Decoding_Error(const std::string &)
Definition asn1_int.cpp:19