Debian Codegraph @

src/lib/crypto/DerUtil.cpp

raw
1 /*
2 * Copyright (c) 2018 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 DerUtil.h
29
30 DER encoding convenience functions
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "DerUtil.h"
35
36 // Convert a raw ByteString to a DER encoded octet string
37 ByteString DERUTIL::raw2Octet(const ByteString& byteString)
38 {
39 ByteString header;
40 size_t len = byteString.size();
41
42 // Definite, short
43 if (len < 0x80)
44 {
45 header.resize(2);
46 header[0] = (unsigned char)0x04;
47 header[1] = (unsigned char)(len & 0x7F);
48 }
49 // Definite, long
50 else
51 {
52 // Count significate bytes
53 size_t bytes = sizeof(size_t);
54 for(; bytes > 0; bytes--)
55 {
56 size_t value = len >> ((bytes - 1) * 8);
57 if (value & 0xFF) break;
58 }
59
60 // Set header data
61 header.resize(2 + bytes);
62 header[0] = (unsigned char)0x04;
63 header[1] = (unsigned char)(0x80 | bytes);
64 for (size_t i = 1; i <= bytes; i++)
65 {
66 header[2+bytes-i] = (unsigned char) (len & 0xFF);
67 len >>= 8;
68 }
69 }
70
71 return header + byteString;
72 }
73
74 // Convert a DER encoded octet string to a raw ByteString
75 ByteString DERUTIL::octet2Raw(const ByteString& byteString)
76 {
77 ByteString rv;
78 ByteString repr = byteString;
79 size_t len = repr.size();
80 size_t controlOctets = 2;
81
82 if (len < controlOctets)
83 {
84 ERROR_MSG("Undersized octet string");
85
86 return rv;
87 }
88
89 if (repr[0] != 0x04)
90 {
91 ERROR_MSG("ByteString is not an octet string");
92
93 return rv;
94 }
95
96 // Definite, short
97 if (repr[1] < 0x80)
98 {
99 if (repr[1] != (len - controlOctets))
100 {
101 if (repr[1] < (len - controlOctets))
102 {
103 ERROR_MSG("Underrun octet string");
104 }
105 else
106 {
107 ERROR_MSG("Overrun octet string");
108 }
109
110 return rv;
111 }
112 }
113 // Definite, long
114 else
115 {
116 size_t lengthOctets = repr[1] & 0x7f;
117 controlOctets += lengthOctets;
118
119 if (controlOctets >= repr.size())
120 {
121 ERROR_MSG("Undersized octet string");
122
123 return rv;
124 }
125
126 ByteString length(&repr[2], lengthOctets);
127
128 if (length.long_val() != (len - controlOctets))
129 {
130 if (length.long_val() < (len - controlOctets))
131 {
132 ERROR_MSG("Underrun octet string");
133 }
134 else
135 {
136 ERROR_MSG("Overrun octet string");
137 }
138
139 return rv;
140 }
141 }
142
143 return repr.substr(controlOctets, len - controlOctets);
144 }
145