FreeRDP/winpr/libwinpr/asn1/asn1.c

329 lines
6.7 KiB
C
Raw Normal View History

2012-06-26 20:12:41 +04:00
/**
* WinPR: Windows Portable Runtime
* ASN.1 Encoding & Decoding Engine
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2012-06-26 20:12:41 +04:00
#include <winpr/asn1.h>
2012-07-06 00:36:47 +04:00
#include <winpr/crt.h>
2012-06-26 20:12:41 +04:00
#ifndef _WIN32
2012-07-06 00:36:47 +04:00
ASN1module_t ASN1_CreateModule(ASN1uint32_t nVersion, ASN1encodingrule_e eRule,
2019-11-06 17:24:51 +03:00
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)
2012-07-06 00:36:47 +04:00
{
ASN1module_t module = NULL;
2012-06-26 20:12:41 +04:00
2012-07-06 00:36:47 +04:00
if (!((apfnEncoder) && (apfnDecoder) && (apfnFreeMemory) && (acbStructSize)))
return NULL;
2019-11-06 17:24:51 +03:00
module = (ASN1module_t)calloc(1, sizeof(struct tagASN1module_t));
2012-07-06 00:36:47 +04:00
if (module)
{
module->nModuleName = nModuleName;
module->dwFlags = dwFlags;
module->eRule = eRule;
module->cPDUs = cPDU;
module->apfnFreeMemory = apfnFreeMemory;
module->acbStructSize = acbStructSize;
if (eRule & ASN1_BER_RULE)
{
2019-11-06 17:24:51 +03:00
module->BER.apfnEncoder = (const ASN1BerEncFun_t*)apfnEncoder;
module->BER.apfnDecoder = (const ASN1BerDecFun_t*)apfnDecoder;
2012-07-06 00:36:47 +04:00
}
}
return module;
}
void ASN1_CloseModule(ASN1module_t pModule)
{
2015-05-11 10:07:39 +03:00
free(pModule);
2012-07-06 00:36:47 +04:00
}
ASN1error_e ASN1_CreateEncoder(ASN1module_t pModule, ASN1encoding_t* ppEncoderInfo,
2019-11-06 17:24:51 +03:00
ASN1octet_t* pbBuf, ASN1uint32_t cbBufSize, ASN1encoding_t pParent)
2012-07-06 00:36:47 +04:00
{
ASN1error_e status;
ASN1encoding_t encoder;
ASN1encodingrule_e rule;
if (pModule && ppEncoderInfo)
{
*ppEncoderInfo = 0;
2019-11-06 17:24:51 +03:00
encoder = (ASN1encoding_t)calloc(1, sizeof(struct ASN1encoding_s));
2012-07-06 00:36:47 +04:00
if (encoder)
{
encoder->magic = 0x44434E45;
encoder->err = ASN1_SUCCESS;
2012-07-06 00:36:47 +04:00
encoder->dwFlags = pModule->dwFlags;
encoder->module = pModule;
if (pbBuf && cbBufSize)
{
encoder->dwFlags |= ASN1ENCODE_SETBUFFER;
encoder->buf = pbBuf;
encoder->pos = pbBuf;
encoder->size = cbBufSize;
}
if (pParent)
{
2019-11-06 17:24:51 +03:00
encoder[1].magic = (ASN1magic_t)pParent;
2012-07-06 00:36:47 +04:00
rule = pParent->eRule;
}
else
{
2019-11-06 17:24:51 +03:00
encoder[1].magic = (ASN1magic_t)encoder;
2012-07-06 00:36:47 +04:00
rule = pModule->eRule;
}
encoder->eRule = rule;
if (encoder->dwFlags & ASN1ENCODE_SETBUFFER)
goto LABEL_SET_BUFFER;
if (!pParent)
{
2019-11-06 17:24:51 +03:00
LABEL_ENCODER_COMPLETE:
2012-07-06 00:36:47 +04:00
*ppEncoderInfo = encoder;
return ASN1_SUCCESS;
2012-07-06 00:36:47 +04:00
}
if (rule & ASN1_BER_RULE)
{
2019-11-06 17:24:51 +03:00
// if (ASN1BEREncCheck(encoder, 1))
2012-07-06 00:36:47 +04:00
{
if (encoder->buf)
*encoder->buf = 0;
2019-11-06 17:24:51 +03:00
LABEL_SET_BUFFER:
2012-07-06 00:36:47 +04:00
if (pParent)
2019-11-06 17:24:51 +03:00
pParent[1].version = (ASN1uint32_t)encoder;
2012-07-06 00:36:47 +04:00
goto LABEL_ENCODER_COMPLETE;
}
status = ASN1_ERR_MEMORY;
}
else
{
status = ASN1_ERR_RULE;
}
free(encoder);
}
else
{
status = ASN1_ERR_MEMORY;
}
}
else
{
status = ASN1_ERR_BADARGS;
}
return status;
}
ASN1error_e ASN1_Encode(ASN1encoding_t pEncoderInfo, void* pDataStruct, ASN1uint32_t nPduNum,
2019-11-06 17:24:51 +03:00
ASN1uint32_t dwFlags, ASN1octet_t* pbBuf, ASN1uint32_t cbBufSize)
2012-07-06 00:36:47 +04:00
{
int flags;
ASN1error_e status;
ASN1module_t module;
ASN1BerEncFun_t pfnEncoder;
if (!pEncoderInfo)
return ASN1_ERR_BADARGS;
ASN1EncSetError(pEncoderInfo, ASN1_SUCCESS);
if (dwFlags & 8)
{
pEncoderInfo->dwFlags |= 8;
pEncoderInfo->buf = pbBuf;
pEncoderInfo->pos = pbBuf;
pEncoderInfo->size = cbBufSize;
}
else
{
flags = dwFlags | pEncoderInfo->dwFlags;
if (flags & 0x10)
{
pEncoderInfo->dwFlags &= 0xFFFFFFF7;
pEncoderInfo->buf = 0;
pEncoderInfo->pos = 0;
pEncoderInfo->size = 0;
}
else
{
if (!(dwFlags & ASN1ENCODE_REUSEBUFFER) && (flags & ASN1ENCODE_APPEND))
goto LABEL_MODULE;
pEncoderInfo->pos = pEncoderInfo->buf;
}
}
pEncoderInfo->len = 0;
pEncoderInfo->bit = 0;
LABEL_MODULE:
module = pEncoderInfo->module;
if (nPduNum >= module->cPDUs)
goto LABEL_BAD_PDU;
if (!(pEncoderInfo->eRule & ASN1_BER_RULE))
{
status = ASN1_ERR_RULE;
return ASN1EncSetError(pEncoderInfo, status);
}
pfnEncoder = module->BER.apfnEncoder[nPduNum];
if (!pfnEncoder)
{
2019-11-06 17:24:51 +03:00
LABEL_BAD_PDU:
2012-07-06 00:36:47 +04:00
status = ASN1_ERR_BADPDU;
return ASN1EncSetError(pEncoderInfo, status);
}
if (pfnEncoder(pEncoderInfo, 0, pDataStruct))
{
2019-11-06 17:24:51 +03:00
// ASN1BEREncFlush(pEncoderInfo);
2012-07-06 00:36:47 +04:00
}
else
{
if (pEncoderInfo[1].err >= 0)
ASN1EncSetError(pEncoderInfo, ASN1_ERR_CORRUPT);
}
if (pEncoderInfo[1].err < 0)
{
if (((dwFlags & 0xFF) | (pEncoderInfo->dwFlags & 0xFF)) & 0x10)
{
ASN1_FreeEncoded(pEncoderInfo, pEncoderInfo->buf);
pEncoderInfo->buf = 0;
pEncoderInfo->pos = 0;
pEncoderInfo->bit = 0;
pEncoderInfo->len = 0;
pEncoderInfo->size = 0;
}
}
return pEncoderInfo[1].err;
}
void ASN1_CloseEncoder(ASN1encoding_t pEncoderInfo)
{
ASN1magic_t magic;
if (pEncoderInfo)
{
magic = pEncoderInfo[1].magic;
2019-11-06 17:24:51 +03:00
if (pEncoderInfo != (ASN1encoding_t)magic)
2012-07-06 00:36:47 +04:00
pEncoderInfo[1].version = 0;
free(pEncoderInfo);
}
}
ASN1error_e ASN1EncSetError(ASN1encoding_t enc, ASN1error_e err)
{
ASN1error_e status;
ASN1encoding_t encoder;
ASN1encoding_t nextEncoder;
status = err;
encoder = enc;
while (encoder)
{
2019-11-06 17:24:51 +03:00
nextEncoder = (ASN1encoding_t)&encoder[1];
2012-07-06 00:36:47 +04:00
encoder->err = err;
if (encoder == nextEncoder)
break;
encoder = nextEncoder;
}
return status;
}
ASN1error_e ASN1DecSetError(ASN1decoding_t dec, ASN1error_e err)
{
ASN1error_e status;
ASN1decoding_t decoder;
ASN1decoding_t nextDecoder;
status = err;
decoder = dec;
while (decoder)
{
2019-11-06 17:24:51 +03:00
nextDecoder = (ASN1decoding_t)&decoder[1];
2012-07-06 00:36:47 +04:00
decoder->err = err;
if (decoder == nextDecoder)
break;
decoder = nextDecoder;
}
return status;
}
2012-06-26 20:12:41 +04:00
void ASN1_FreeEncoded(ASN1encoding_t pEncoderInfo, void* pBuf)
{
return;
}
void ASN1_FreeDecoded(ASN1decoding_t pDecoderInfo, void* pDataStruct, ASN1uint32_t nPduNum)
{
return;
}
ASN1error_e ASN1_CreateDecoder(ASN1module_t pModule, ASN1decoding_t* ppDecoderInfo,
2019-11-06 17:24:51 +03:00
ASN1octet_t* pbBuf, ASN1uint32_t cbBufSize, ASN1decoding_t pParent)
{
2019-11-06 17:24:51 +03:00
return ASN1_ERR_BADARGS;
}
2019-11-06 17:24:51 +03:00
ASN1error_e ASN1_Decode(ASN1decoding_t pDecoderInfo, void** ppDataStruct, ASN1uint32_t nPduNum,
ASN1uint32_t dwFlags, ASN1octet_t* pbBuf, ASN1uint32_t cbBufSize)
2015-02-23 04:11:11 +03:00
{
2019-11-06 17:24:51 +03:00
return ASN1_ERR_BADARGS;
2015-02-23 04:11:11 +03:00
}
2012-06-26 20:12:41 +04:00
#endif