Aktualizr
C++ SOTA Client
asn1-cer.h
1 #ifndef ASN1_CER_H
2 #define ASN1_CER_H
3 
4 #include <stdexcept>
5 #include <string>
6 
7 // Limitations:
8 // - Maximal supported integer width of 32 bits
9 // - Maximal supported string length of 2**31 -1
10 // - Only type tags up to 30 are supported
11 
12 #define CER_MAX_PRIMITIVESTRING 1000 // defined in the standard
13 
14 enum ASN1_Class {
15  kAsn1Universal = 0x00,
16  kAsn1Application = 0x40,
17  kAsn1Context = 0x80,
18  kAsn1Private = 0xC0,
19 };
20 
21 enum ASN1_UniversalTag {
22  kUnknown = 0xff,
23  kAsn1EndSequence = 0x00,
24  kAsn1Sequence = 0x10,
25  kAsn1Boolean = 0x01,
26  kAsn1Integer = 0x02,
27  kAsn1OctetString = 0x04,
28  kAsn1Enum = 0x0a,
29  kAsn1Utf8String = 0x0c,
30  kAsn1NumericString = 0x12,
31  kAsn1PrintableString = 0x13,
32  kAsn1TeletexString = 0x14,
33  kAsn1VideotexString = 0x15,
34  kAsn1UTCTime = 0x16,
35  kAsn1GeneralizedTime = 0x17,
36  kAsn1GraphicString = 0x18,
37  kAsn1VisibleString = 0x19,
38  kAsn1GeneralString = 0x1a,
39  kAsn1UniversalString = 0x1b,
40  kAsn1CharacterString = 0x1c,
41  kAsn1BMPString = 0x1d,
42 };
43 
44 class deserialization_error : public std::exception {
45  public:
46  const char* what() const noexcept override { return "ASN.1 deserialization error"; }
47 };
48 
49 // Decode token.
50 // * ber - serialization
51 // * endpos - next position in the string after what was deserialized
52 // * int_param - integer value associated with token. Depends on token type.
53 // * string_param - string associated with token. Depends on token type.
54 // Return value: token type, or kUnknown in case of an error.
55 
56 uint8_t cer_decode_token(const std::string& ber, int32_t* endpos, int32_t* int_param, std::string* string_param);
57 
58 std::string cer_encode_integer(int32_t number);
59 std::string cer_encode_string(const std::string& contents, ASN1_UniversalTag tag);
60 
61 #endif // ASN1_CER_H
deserialization_error
Definition: asn1-cer.h:44