monotone

monotone Mtn Source Tree

Root/botan/asn1_ku.cpp

1/*************************************************
2* KeyUsage 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
10namespace Botan {
11
12namespace BER {
13
14/*************************************************
15* Decode a BER encoded KeyUsage *
16*************************************************/
17void decode(BER_Decoder& source, Key_Constraints& key_usage)
18 {
19 BER_Object obj = source.get_next_object();
20
21 if(obj.type_tag != BIT_STRING || obj.class_tag != UNIVERSAL)
22 throw BER_Bad_Tag("Bad tag for usage constraint",
23 obj.type_tag, obj.class_tag);
24 if(obj.value.size() != 2 && obj.value.size() != 3)
25 throw BER_Decoding_Error("Bad size for BITSTRING in usage constraint");
26 if(obj.value[0] >= 8)
27 throw BER_Decoding_Error("Invalid unused bits in usage constraint");
28
29 const byte mask = (0xFF << obj.value[0]);
30 obj.value[obj.value.size()-1] &= mask;
31
32 u16bit usage = 0;
33 for(u32bit j = 1; j != obj.value.size(); ++j)
34 usage = (obj.value[j] << 8) | usage;
35
36 key_usage = Key_Constraints(usage);
37 }
38
39}
40
41}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status