1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/*************************************************
* DER Coding Source File *
* (C) 1999-2005 The Botan Project *
*************************************************/
#include <botan/der_enc.h>
namespace Botan {
namespace DER {
/*************************************************
* Put some arbitrary bytes into a SEQUENCE *
*************************************************/
SecureVector<byte> put_in_sequence(const MemoryRegion<byte>& contents)
{
DER_Encoder encoder;
encoder.start_sequence();
encoder.add_raw_octets(contents);
encoder.end_sequence();
return encoder.get_contents();
}
/*************************************************
* DER encode NULL *
*************************************************/
void encode_null(DER_Encoder& encoder)
{
encoder.add_object(NULL_TAG, UNIVERSAL, 0, 0);
}
/*************************************************
* DER encode a BOOLEAN *
*************************************************/
void encode(DER_Encoder& encoder, bool is_true)
{
encode(encoder, is_true, BOOLEAN, UNIVERSAL);
}
/*************************************************
* DER encode a small INTEGER *
*************************************************/
void encode(DER_Encoder& encoder, u32bit n)
{
encode(encoder, BigInt(n), INTEGER, UNIVERSAL);
}
/*************************************************
* DER encode a small INTEGER *
*************************************************/
void encode(DER_Encoder& encoder, int n)
{
if(n < 0)
throw Invalid_Argument("DER::encode(int): n must be >= 0");
encode(encoder, BigInt(n), INTEGER, UNIVERSAL);
}
/*************************************************
* DER encode an INTEGER *
*************************************************/
void encode(DER_Encoder& encoder, const BigInt& n)
{
encode(encoder, n, INTEGER, UNIVERSAL);
}
/*************************************************
* DER encode an OCTET STRING or BIT STRING *
*************************************************/
void encode(DER_Encoder& encoder, const MemoryRegion<byte>& octets,
ASN1_Tag real_type)
{
encode(encoder, octets.begin(), octets.size(),
real_type, real_type, UNIVERSAL);
}
/*************************************************
* DER encode an OCTET STRING or BIT STRING *
*************************************************/
void encode(DER_Encoder& encoder, const byte octets[], u32bit length,
ASN1_Tag real_type)
{
encode(encoder, octets, length, real_type, real_type, UNIVERSAL);
}
/*************************************************
* DER encode a BOOLEAN *
*************************************************/
void encode(DER_Encoder& encoder, bool is_true,
ASN1_Tag type_tag, ASN1_Tag class_tag)
{
if(is_true)
encoder.add_object(type_tag, class_tag, 0xFF);
else
encoder.add_object(type_tag, class_tag, 0x00);
}
/*************************************************
* DER encode a small INTEGER *
*************************************************/
void encode(DER_Encoder& encoder, u32bit n,
ASN1_Tag type_tag, ASN1_Tag class_tag)
{
encode(encoder, BigInt(n), type_tag, class_tag);
}
/*************************************************
* DER encode a small INTEGER *
*************************************************/
void encode(DER_Encoder& encoder, int n,
ASN1_Tag type_tag, ASN1_Tag class_tag)
{
if(n < 0)
throw Invalid_Argument("DER::encode(int): n must be >= 0");
encode(encoder, BigInt(n), type_tag, class_tag);
}
/*************************************************
* DER encode an INTEGER *
*************************************************/
void encode(DER_Encoder& encoder, const BigInt& n,
ASN1_Tag type_tag, ASN1_Tag class_tag)
{
if(n == 0)
encoder.add_object(type_tag, class_tag, 0);
else
{
bool extra_zero = (n.bits() % 8 == 0);
SecureVector<byte> contents(extra_zero + n.bytes());
BigInt::encode(contents.begin() + extra_zero, n);
if(n < 0)
{
for(u32bit j = 0; j != contents.size(); j++)
contents[j] = ~contents[j];
for(u32bit j = contents.size(); j > 0; j--)
if(++contents[j-1])
break;
}
encoder.add_object(type_tag, class_tag, contents);
}
}
/*************************************************
* DER encode an OCTET STRING or BIT STRING *
*************************************************/
void encode(DER_Encoder& encoder, const MemoryRegion<byte>& octets,
ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag)
{
encode(encoder, octets.begin(), octets.size(),
real_type, type_tag, class_tag);
}
/*************************************************
* DER encode an OCTET STRING or BIT STRING *
*************************************************/
void encode(DER_Encoder& encoder, const byte octets[], u32bit length,
ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag)
{
if(real_type != OCTET_STRING && real_type != BIT_STRING)
throw Invalid_Argument("DER_Encoder: Invalid tag for byte/bit string");
if(real_type == OCTET_STRING)
encoder.add_object(type_tag, class_tag, octets, length);
else
{
SecureVector<byte> encoded;
encoded.append(0);
encoded.append(octets, length);
encoder.add_object(type_tag, class_tag, encoded);
}
}
}
}
|