1 /**
2  * WinPR: Windows Portable Runtime
3  * ASN.1 Encoding & Decoding Engine
4  *
5  * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <winpr/asn1.h>
25 
26 #include <winpr/crt.h>
27 
28 #ifndef _WIN32
29 
ASN1_CreateModule(ASN1uint32_t nVersion,ASN1encodingrule_e eRule,ASN1uint32_t dwFlags,ASN1uint32_t cPDU,const ASN1GenericFun_t apfnEncoder[],const ASN1GenericFun_t apfnDecoder[],const ASN1FreeFun_t apfnFreeMemory[],const ASN1uint32_t acbStructSize[],ASN1magic_t nModuleName)30 ASN1module_t ASN1_CreateModule(ASN1uint32_t nVersion, ASN1encodingrule_e eRule,
31                                ASN1uint32_t dwFlags, ASN1uint32_t cPDU,
32                                const ASN1GenericFun_t apfnEncoder[],
33                                const ASN1GenericFun_t apfnDecoder[],
34                                const ASN1FreeFun_t apfnFreeMemory[],
35                                const ASN1uint32_t acbStructSize[], ASN1magic_t nModuleName)
36 {
37 	ASN1module_t module = NULL;
38 
39 	if (!((apfnEncoder) && (apfnDecoder) && (apfnFreeMemory) && (acbStructSize)))
40 		return NULL;
41 
42 	module = (ASN1module_t)calloc(1, sizeof(struct tagASN1module_t));
43 
44 	if (module)
45 	{
46 		module->nModuleName = nModuleName;
47 		module->dwFlags = dwFlags;
48 		module->eRule = eRule;
49 		module->cPDUs = cPDU;
50 		module->apfnFreeMemory = apfnFreeMemory;
51 		module->acbStructSize = acbStructSize;
52 
53 		if (eRule & ASN1_BER_RULE)
54 		{
55 			module->BER.apfnEncoder = (const ASN1BerEncFun_t*)apfnEncoder;
56 			module->BER.apfnDecoder = (const ASN1BerDecFun_t*)apfnDecoder;
57 		}
58 	}
59 
60 	return module;
61 }
62 
ASN1_CloseModule(ASN1module_t pModule)63 void ASN1_CloseModule(ASN1module_t pModule)
64 {
65 	free(pModule);
66 }
67 
ASN1_CreateEncoder(ASN1module_t pModule,ASN1encoding_t * ppEncoderInfo,ASN1octet_t * pbBuf,ASN1uint32_t cbBufSize,ASN1encoding_t pParent)68 ASN1error_e ASN1_CreateEncoder(ASN1module_t pModule, ASN1encoding_t* ppEncoderInfo,
69                                ASN1octet_t* pbBuf, ASN1uint32_t cbBufSize, ASN1encoding_t pParent)
70 {
71 	ASN1error_e status;
72 	ASN1encoding_t encoder;
73 	ASN1encodingrule_e rule;
74 
75 	if (pModule && ppEncoderInfo)
76 	{
77 		*ppEncoderInfo = 0;
78 		encoder = (ASN1encoding_t)calloc(1, sizeof(struct ASN1encoding_s));
79 
80 		if (encoder)
81 		{
82 			encoder->magic = 0x44434E45;
83 			encoder->err = ASN1_SUCCESS;
84 			encoder->dwFlags = pModule->dwFlags;
85 			encoder->module = pModule;
86 
87 			if (pbBuf && cbBufSize)
88 			{
89 				encoder->dwFlags |= ASN1ENCODE_SETBUFFER;
90 				encoder->buf = pbBuf;
91 				encoder->pos = pbBuf;
92 				encoder->size = cbBufSize;
93 			}
94 
95 			if (pParent)
96 			{
97 				encoder[1].magic = (ASN1magic_t)pParent;
98 				rule = pParent->eRule;
99 			}
100 			else
101 			{
102 				encoder[1].magic = (ASN1magic_t)encoder;
103 				rule = pModule->eRule;
104 			}
105 
106 			encoder->eRule = rule;
107 
108 			if (encoder->dwFlags & ASN1ENCODE_SETBUFFER)
109 				goto LABEL_SET_BUFFER;
110 
111 			if (!pParent)
112 			{
113 			LABEL_ENCODER_COMPLETE:
114 				*ppEncoderInfo = encoder;
115 				return ASN1_SUCCESS;
116 			}
117 
118 			if (rule & ASN1_BER_RULE)
119 			{
120 				// if (ASN1BEREncCheck(encoder, 1))
121 				{
122 					if (encoder->buf)
123 						*encoder->buf = 0;
124 				LABEL_SET_BUFFER:
125 					if (pParent)
126 						pParent[1].version = (ASN1uint32_t)encoder;
127 
128 					goto LABEL_ENCODER_COMPLETE;
129 				}
130 
131 				status = ASN1_ERR_MEMORY;
132 			}
133 			else
134 			{
135 				status = ASN1_ERR_RULE;
136 			}
137 
138 			free(encoder);
139 		}
140 		else
141 		{
142 			status = ASN1_ERR_MEMORY;
143 		}
144 	}
145 	else
146 	{
147 		status = ASN1_ERR_BADARGS;
148 	}
149 
150 	return status;
151 }
152 
ASN1_Encode(ASN1encoding_t pEncoderInfo,void * pDataStruct,ASN1uint32_t nPduNum,ASN1uint32_t dwFlags,ASN1octet_t * pbBuf,ASN1uint32_t cbBufSize)153 ASN1error_e ASN1_Encode(ASN1encoding_t pEncoderInfo, void* pDataStruct, ASN1uint32_t nPduNum,
154                         ASN1uint32_t dwFlags, ASN1octet_t* pbBuf, ASN1uint32_t cbBufSize)
155 {
156 	int flags;
157 	ASN1error_e status;
158 	ASN1module_t module;
159 	ASN1BerEncFun_t pfnEncoder;
160 
161 	if (!pEncoderInfo)
162 		return ASN1_ERR_BADARGS;
163 
164 	ASN1EncSetError(pEncoderInfo, ASN1_SUCCESS);
165 
166 	if (dwFlags & 8)
167 	{
168 		pEncoderInfo->dwFlags |= 8;
169 		pEncoderInfo->buf = pbBuf;
170 		pEncoderInfo->pos = pbBuf;
171 		pEncoderInfo->size = cbBufSize;
172 	}
173 	else
174 	{
175 		flags = dwFlags | pEncoderInfo->dwFlags;
176 
177 		if (flags & 0x10)
178 		{
179 			pEncoderInfo->dwFlags &= 0xFFFFFFF7;
180 			pEncoderInfo->buf = 0;
181 			pEncoderInfo->pos = 0;
182 			pEncoderInfo->size = 0;
183 		}
184 		else
185 		{
186 			if (!(dwFlags & ASN1ENCODE_REUSEBUFFER) && (flags & ASN1ENCODE_APPEND))
187 				goto LABEL_MODULE;
188 
189 			pEncoderInfo->pos = pEncoderInfo->buf;
190 		}
191 	}
192 
193 	pEncoderInfo->len = 0;
194 	pEncoderInfo->bit = 0;
195 
196 LABEL_MODULE:
197 	module = pEncoderInfo->module;
198 
199 	if (nPduNum >= module->cPDUs)
200 		goto LABEL_BAD_PDU;
201 
202 	if (!(pEncoderInfo->eRule & ASN1_BER_RULE))
203 	{
204 		status = ASN1_ERR_RULE;
205 		return ASN1EncSetError(pEncoderInfo, status);
206 	}
207 
208 	pfnEncoder = module->BER.apfnEncoder[nPduNum];
209 
210 	if (!pfnEncoder)
211 	{
212 	LABEL_BAD_PDU:
213 		status = ASN1_ERR_BADPDU;
214 		return ASN1EncSetError(pEncoderInfo, status);
215 	}
216 
217 	if (pfnEncoder(pEncoderInfo, 0, pDataStruct))
218 	{
219 		// ASN1BEREncFlush(pEncoderInfo);
220 	}
221 	else
222 	{
223 		if (pEncoderInfo[1].err >= 0)
224 			ASN1EncSetError(pEncoderInfo, ASN1_ERR_CORRUPT);
225 	}
226 
227 	if (pEncoderInfo[1].err < 0)
228 	{
229 		if (((dwFlags & 0xFF) | (pEncoderInfo->dwFlags & 0xFF)) & 0x10)
230 		{
231 			ASN1_FreeEncoded(pEncoderInfo, pEncoderInfo->buf);
232 			pEncoderInfo->buf = 0;
233 			pEncoderInfo->pos = 0;
234 			pEncoderInfo->bit = 0;
235 			pEncoderInfo->len = 0;
236 			pEncoderInfo->size = 0;
237 		}
238 	}
239 
240 	return pEncoderInfo[1].err;
241 }
242 
ASN1_CloseEncoder(ASN1encoding_t pEncoderInfo)243 void ASN1_CloseEncoder(ASN1encoding_t pEncoderInfo)
244 {
245 	ASN1magic_t magic;
246 
247 	if (pEncoderInfo)
248 	{
249 		magic = pEncoderInfo[1].magic;
250 
251 		if (pEncoderInfo != (ASN1encoding_t)magic)
252 			pEncoderInfo[1].version = 0;
253 
254 		free(pEncoderInfo);
255 	}
256 }
257 
ASN1EncSetError(ASN1encoding_t enc,ASN1error_e err)258 ASN1error_e ASN1EncSetError(ASN1encoding_t enc, ASN1error_e err)
259 {
260 	ASN1error_e status;
261 	ASN1encoding_t encoder;
262 	ASN1encoding_t nextEncoder;
263 
264 	status = err;
265 	encoder = enc;
266 
267 	while (encoder)
268 	{
269 		nextEncoder = (ASN1encoding_t)&encoder[1];
270 
271 		encoder->err = err;
272 
273 		if (encoder == nextEncoder)
274 			break;
275 
276 		encoder = nextEncoder;
277 	}
278 
279 	return status;
280 }
281 
ASN1DecSetError(ASN1decoding_t dec,ASN1error_e err)282 ASN1error_e ASN1DecSetError(ASN1decoding_t dec, ASN1error_e err)
283 {
284 	ASN1error_e status;
285 	ASN1decoding_t decoder;
286 	ASN1decoding_t nextDecoder;
287 
288 	status = err;
289 	decoder = dec;
290 
291 	while (decoder)
292 	{
293 		nextDecoder = (ASN1decoding_t)&decoder[1];
294 
295 		decoder->err = err;
296 
297 		if (decoder == nextDecoder)
298 			break;
299 
300 		decoder = nextDecoder;
301 	}
302 
303 	return status;
304 }
305 
ASN1_FreeEncoded(ASN1encoding_t pEncoderInfo,void * pBuf)306 void ASN1_FreeEncoded(ASN1encoding_t pEncoderInfo, void* pBuf)
307 {
308 	return;
309 }
310 
ASN1_FreeDecoded(ASN1decoding_t pDecoderInfo,void * pDataStruct,ASN1uint32_t nPduNum)311 void ASN1_FreeDecoded(ASN1decoding_t pDecoderInfo, void* pDataStruct, ASN1uint32_t nPduNum)
312 {
313 	return;
314 }
315 
ASN1_CreateDecoder(ASN1module_t pModule,ASN1decoding_t * ppDecoderInfo,ASN1octet_t * pbBuf,ASN1uint32_t cbBufSize,ASN1decoding_t pParent)316 ASN1error_e ASN1_CreateDecoder(ASN1module_t pModule, ASN1decoding_t* ppDecoderInfo,
317                                ASN1octet_t* pbBuf, ASN1uint32_t cbBufSize, ASN1decoding_t pParent)
318 {
319 	return ASN1_ERR_BADARGS;
320 }
321 
ASN1_Decode(ASN1decoding_t pDecoderInfo,void ** ppDataStruct,ASN1uint32_t nPduNum,ASN1uint32_t dwFlags,ASN1octet_t * pbBuf,ASN1uint32_t cbBufSize)322 ASN1error_e ASN1_Decode(ASN1decoding_t pDecoderInfo, void** ppDataStruct, ASN1uint32_t nPduNum,
323                         ASN1uint32_t dwFlags, ASN1octet_t* pbBuf, ASN1uint32_t cbBufSize)
324 {
325 	return ASN1_ERR_BADARGS;
326 }
327 
328 #endif
329