libfreerdp-crypto: integrate DER code from Jiten Pathy

This commit is contained in:
Marc-André Moreau 2012-03-05 15:45:54 -05:00
parent dfb7ea3775
commit 386d815969
5 changed files with 733 additions and 0 deletions

View File

@ -0,0 +1,63 @@
/**
* FreeRDP: A Remote Desktop Protocol Client
* ASN.1 Basic Encoding Rules (DER)
*
* Copyright 2011 Samsung, Author Jiten Pathy
*
* 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.
*/
#ifndef FREERDP_CRYPTO_DER_H
#define FREERDP_CRYPTO_DER_H
#include <freerdp/crypto/er.h>
#include <freerdp/utils/memory.h>
#define der_read_length er_read_length
#define _der_skip_length _er_skip_length
#define der_get_content_length er_get_content_length
#define der_read_universal_tag er_read_universal_tag
#define der_write_universal_tag er_write_universal_tag
#define der_read_application_tag er_read_application_tag
#define der_read_enumerated er_read_enumerated
#define der_read_contextual_tag er_read_contextual_tag
#define der_skip_contextual_tag er_skip_contextual_tag
#define der_read_sequence_tag er_read_sequence_tag
#define der_skip_sequence er_skip_sequence
#define der_skip_sequence_tag er_skip_sequence_tag
#define der_read_bit_string er_read_bit_string
#define der_read_octet_string er_read_octet_string
#define der_skip_octet_string er_skip_octet_string
#define der_read_boolean er_read_boolean
#define der_write_boolean er_write_boolean
#define der_read_integer er_read_integer
#define der_write_integer er_write_integer
#define der_read_integer_length er_read_integer_length
#define der_skip_integer er_skip_integer
#define der_write_sequence_tag(_a, _b) er_write_sequence_tag(_a, _b, true)
#define der_write_octet_string_tag(_a, _b) er_write_octet_string_tag(_a, _b, true)
#define der_write_octet_string(_a, _b, _c) er_write_octet_string(_a, _b, _c, true)
#define der_write_bit_string_tag(_a, _b, _c) er_write_bit_string_tag(_a, _b, _c, true);
#define der_write_contextual_tag(_a, _b, _c, _d) er_write_contextual_tag(_a, _b, _c, _d, true);
#define der_write_enumerated(_a, _b, _c) er_write_enumerated(_a, _b, _c, true)
#define der_write_application_tag(_a, _b, _c) er_write_application_tag(_a, _b, _c, true)
FREERDP_API int der_write_length(STREAM* s, int length);
FREERDP_API boolean der_write_bit_string(STREAM* s, uint32 length, uint8 padding);
FREERDP_API boolean der_write_general_string(STREAM* s, char* str);
FREERDP_API char* der_read_general_string(STREAM* s, int *length);
FREERDP_API int der_write_principal_name(STREAM* s, uint8 ntype, char** name);
FREERDP_API int der_write_generalized_time(STREAM* s, char* tstr);
FREERDP_API boolean der_read_generalized_time(STREAM* s, char** tstr);
#endif /* FREERDP_CRYPTO_DER_H */

View File

@ -0,0 +1,88 @@
/**
* FreeRDP: A Remote Desktop Protocol Client
* ASN.1 Encoding Rules (BER/DER common functions)
*
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Modified by Jiten Pathy
*
* 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.
*/
#ifndef FREERDP_CRYPTO_ER_H
#define FREERDP_CRYPTO_ER_H
#include <freerdp/api.h>
#include <freerdp/types.h>
#include <freerdp/utils/stream.h>
/* ER type */
/* Class - bits 8 and 7 */
#define ER_CLASS_MASK 0xC0
#define ER_CLASS_UNIV 0x00 /* 0 0 */
#define ER_CLASS_APPL 0x40 /* 0 1 */
#define ER_CLASS_CTXT 0x80 /* 1 0 */
#define ER_CLASS_PRIV 0xC0 /* 1 1 */
/* P/C - bit 6 */
#define ER_PC_MASK 0x20
#define ER_PRIMITIVE 0x00 /* 0 */
#define ER_CONSTRUCT 0x20 /* 1 */
/* Tag - bits 5 to 1 */
#define ER_TAG_MASK 0x1F
#define ER_TAG_BOOLEAN 0x01
#define ER_TAG_INTEGER 0x02
#define ER_TAG_BIT_STRING 0x03
#define ER_TAG_OCTET_STRING 0x04
#define ER_TAG_OBJECT_IDENFIER 0x06
#define ER_TAG_ENUMERATED 0x0A
#define ER_TAG_SEQUENCE 0x10
#define ER_TAG_SEQUENCE_OF 0x10
#define ER_TAG_GENERAL_STRING 0x1B
#define ER_TAG_GENERALIZED_TIME 0x18
#define ER_PC(_pc) (_pc ? ER_CONSTRUCT : ER_PRIMITIVE)
FREERDP_API void er_read_length(STREAM* s, int* length);
FREERDP_API int er_write_length(STREAM* s, int length, boolean flag);
FREERDP_API int _er_skip_length(int length);
FREERDP_API int er_get_content_length(int length);
FREERDP_API boolean er_read_universal_tag(STREAM* s, uint8 tag, boolean pc);
FREERDP_API void er_write_universal_tag(STREAM* s, uint8 tag, boolean pc);
FREERDP_API boolean er_read_application_tag(STREAM* s, uint8 tag, int* length);
FREERDP_API void er_write_application_tag(STREAM* s, uint8 tag, int length, boolean flag);
FREERDP_API boolean er_read_application_tag(STREAM* s, uint8 tag, int* length);
FREERDP_API boolean er_read_enumerated(STREAM* s, uint8* enumerated, uint8 count);
FREERDP_API void er_write_enumerated(STREAM* s, uint8 enumerated, uint8 count, boolean flag);
FREERDP_API boolean er_read_contextual_tag(STREAM* s, uint8 tag, int* length, boolean pc);
FREERDP_API int er_write_contextual_tag(STREAM* s, uint8 tag, int length, boolean pc, boolean flag);
FREERDP_API int er_skip_contextual_tag(int length);
FREERDP_API boolean er_read_sequence_tag(STREAM* s, int* length);
FREERDP_API int er_write_sequence_tag(STREAM* s, int length, boolean flag);
FREERDP_API int er_skip_sequence(int length);
FREERDP_API int er_skip_sequence_tag(int length);
FREERDP_API boolean er_read_bit_string(STREAM* s, int* length, uint8* padding);
FREERDP_API boolean er_write_bit_string_tag(STREAM* s, uint32 length, uint8 padding, boolean flag);
FREERDP_API boolean er_read_octet_string(STREAM* s, int* length);
FREERDP_API void er_write_octet_string(STREAM* s, uint8* oct_str, int length, boolean flag);
FREERDP_API int er_write_octet_string_tag(STREAM* s, int length, boolean flag);
FREERDP_API int er_skip_octet_string(int length);
FREERDP_API boolean er_read_boolean(STREAM* s, boolean* value);
FREERDP_API void er_write_boolean(STREAM* s, boolean value);
FREERDP_API boolean er_read_integer(STREAM* s, uint32* value);
FREERDP_API int er_write_integer(STREAM* s, sint32 value);
FREERDP_API boolean er_read_integer_length(STREAM* s, int* length);
FREERDP_API int er_skip_integer(sint32 value);
#endif /* FREERDP_CRYPTO_ER_H */

View File

@ -18,6 +18,8 @@
# limitations under the License.
set(FREERDP_CRYPTO_SRCS
er.c
der.c
ber.c
per.c
certificate.c

150
libfreerdp-crypto/der.c Normal file
View File

@ -0,0 +1,150 @@
/**
* FreeRDP: A Remote Desktop Protocol Client
* ASN.1 Basic Encoding Rules (DER)
*
* Copyright 2011 Samsung, Author Jiten Pathy
*
* 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.
*/
#include <freerdp/crypto/der.h>
int der_write_length(STREAM* s, int length)
{
if (length > 0x7F && length <= 0xFF)
{
stream_write_uint8(s, 0x81);
stream_write_uint8(s, length);
return 2;
}
else if (length > 0xFF)
{
stream_write_uint8(s, 0x82);
stream_write_uint16_be(s, length);
return 3;
}
else
{
stream_write_uint8(s, length);
return 1;
}
}
boolean der_write_general_string(STREAM* s, char* str)
{
STREAM* tmp_s;
tmp_s = stream_new(0);
stream_attach(tmp_s, (uint8*)str, strlen(str));
der_write_universal_tag(s, ER_TAG_GENERAL_STRING, false);
der_write_length(s, strlen(str));
stream_copy(s, tmp_s, strlen(str));
stream_detach(tmp_s);
return true;
}
char* der_read_general_string(STREAM* s, int *length)
{
char* str;
int len;
if(der_read_universal_tag(s, ER_TAG_GENERAL_STRING, false))
{
der_read_length(s, &len);
str = (char*)xzalloc((len + 1) * sizeof(char));
memcpy(str, s->p, len);
stream_seek(s, len);
*length = len + 2;
return str;
}
stream_rewind(s, 1);
*length = 0;
return NULL;
}
int der_write_principal_name(STREAM* s, uint8 ntype, char** name)
{
uint8 len;
len = 0;
char** p;
p = name;
while(*p != NULL)
{
len += strlen(*p) + 2;
p++;
}
p = name;
der_write_sequence_tag(s, len+9);
der_write_contextual_tag(s, 0, 3, true);
der_write_integer(s, ntype);
der_write_contextual_tag(s, 1, len + 2, true);
der_write_sequence_tag(s, len);
while(*p != NULL)
{
der_write_general_string(s, *p);
p++;
}
return len + 11;
}
int der_write_generalized_time(STREAM* s, char* tstr)
{
uint8 len;
STREAM* tmp_s;
len = strlen(tstr);
tmp_s = stream_new(0);
stream_attach(tmp_s, (uint8*) tstr, strlen(tstr));
der_write_universal_tag(s, ER_TAG_GENERALIZED_TIME, false);
der_write_length(s, len);
stream_copy(s, tmp_s, len);
stream_detach(tmp_s);
return len + 2;
}
boolean der_read_generalized_time(STREAM* s, char** tstr)
{
int length;
uint8* bm;
stream_get_mark(s, bm);
if (!der_read_universal_tag(s, ER_TAG_GENERALIZED_TIME, false))
goto err;
der_read_length(s, &length);
if (length != 15)
goto err;
*tstr = xzalloc(length + 1);
stream_read(s, *tstr, length);
return true;
err:
stream_set_mark(s, bm);
return false;
}

430
libfreerdp-crypto/er.c Normal file
View File

@ -0,0 +1,430 @@
/**
* FreeRDP: A Remote Desktop Protocol Client
* ASN.1 Encoding Rules (BER/DER common functions)
*
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Modified by Jiten Pathy
*
* 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.
*/
#include <freerdp/crypto/er.h>
#include <freerdp/crypto/ber.h>
#include <freerdp/crypto/der.h>
void er_read_length(STREAM* s, int* length)
{
uint8 byte;
stream_read_uint8(s, byte);
if (byte & 0x80)
{
byte &= ~(0x80);
if (byte == 1)
stream_read_uint8(s, *length);
if (byte == 2)
stream_read_uint16_be(s, *length);
}
else
{
*length = byte;
}
}
/**
* Write er length.
* @param s stream
* @param length length
*/
int er_write_length(STREAM* s, int length, boolean flag)
{
if (flag)
return der_write_length(s, length);
else
return ber_write_length(s, length);
}
int _er_skip_length(int length)
{
if (length > 0x7F)
return 3;
else
return 1;
}
int er_get_content_length(int length)
{
if (length - 1 > 0x7F)
return length - 4;
else
return length - 2;
}
/**
* Read er Universal tag.
* @param s stream
* @param tag er universally-defined tag
* @return
*/
boolean er_read_universal_tag(STREAM* s, uint8 tag, boolean pc)
{
uint8 byte;
stream_read_uint8(s, byte);
if (byte != (ER_CLASS_UNIV | ER_PC(pc) | (ER_TAG_MASK & tag)))
return false;
return true;
}
/**
* Write er Universal tag.
* @param s stream
* @param tag er universally-defined tag
* @param pc primitive (false) or constructed (true)
*/
void er_write_universal_tag(STREAM* s, uint8 tag, boolean pc)
{
stream_write_uint8(s, (ER_CLASS_UNIV | ER_PC(pc)) | (ER_TAG_MASK & tag));
}
/**
* Read er Application tag.
* @param s stream
* @param tag er application-defined tag
* @param length length
*/
boolean er_read_application_tag(STREAM* s, uint8 tag, int* length)
{
uint8 byte;
if (tag > 30)
{
stream_read_uint8(s, byte);
if (byte != ((ER_CLASS_APPL | ER_CONSTRUCT) | ER_TAG_MASK))
return false;
stream_read_uint8(s, byte);
if (byte != tag)
return false;
er_read_length(s, length);
}
else
{
stream_read_uint8(s, byte);
if (byte != ((ER_CLASS_APPL | ER_CONSTRUCT) | (ER_TAG_MASK & tag)))
return false;
er_read_length(s, length);
}
return true;
}
/**
* Write er Application tag.
* @param s stream
* @param tag er application-defined tag
* @param length length
*/
void er_write_application_tag(STREAM* s, uint8 tag, int length, boolean flag)
{
if (tag > 30)
{
stream_write_uint8(s, (ER_CLASS_APPL | ER_CONSTRUCT) | ER_TAG_MASK);
stream_write_uint8(s, tag);
er_write_length(s, length, flag);
}
else
{
stream_write_uint8(s, (ER_CLASS_APPL | ER_CONSTRUCT) | (ER_TAG_MASK & tag));
er_write_length(s, length, flag);
}
}
boolean er_read_contextual_tag(STREAM* s, uint8 tag, int* length, boolean pc)
{
uint8 byte;
stream_read_uint8(s, byte);
if (byte != ((ER_CLASS_CTXT | ER_PC(pc)) | (ER_TAG_MASK & tag)))
{
stream_rewind(s, 1);
return false;
}
er_read_length(s, length);
return true;
}
int er_write_contextual_tag(STREAM* s, uint8 tag, int length, boolean pc, boolean flag)
{
stream_write_uint8(s, (ER_CLASS_CTXT | ER_PC(pc)) | (ER_TAG_MASK & tag));
return er_write_length(s, length, flag) + 1;
}
int er_skip_contextual_tag(int length)
{
return _er_skip_length(length) + 1;
}
boolean er_read_sequence_tag(STREAM* s, int* length)
{
uint8 byte;
stream_read_uint8(s, byte);
if (byte != ((ER_CLASS_UNIV | ER_CONSTRUCT) | (ER_TAG_SEQUENCE_OF)))
return false;
er_read_length(s, length);
return true;
}
/**
* Write er SEQUENCE tag.
* @param s stream
* @param length length
*/
int er_write_sequence_tag(STREAM* s, int length, boolean flag)
{
stream_write_uint8(s, (ER_CLASS_UNIV | ER_CONSTRUCT) | (ER_TAG_MASK & ER_TAG_SEQUENCE));
return er_write_length(s, length, flag) + 1;
}
int er_skip_sequence(int length)
{
return 1 + _er_skip_length(length) + length;
}
int er_skip_sequence_tag(int length)
{
return 1 + _er_skip_length(length);
}
boolean er_read_enumerated(STREAM* s, uint8* enumerated, uint8 count)
{
int length;
er_read_universal_tag(s, ER_TAG_ENUMERATED, false);
er_read_length(s, &length);
if (length == 1)
stream_read_uint8(s, *enumerated);
else
return false;
/* check that enumerated value falls within expected range */
if (*enumerated + 1 > count)
return false;
return true;
}
void er_write_enumerated(STREAM* s, uint8 enumerated, uint8 count, boolean flag)
{
er_write_universal_tag(s, ER_TAG_ENUMERATED, false);
er_write_length(s, 1, flag);
stream_write_uint8(s, enumerated);
}
boolean er_read_bit_string(STREAM* s, int* length, uint8* padding)
{
er_read_universal_tag(s, ER_TAG_BIT_STRING, false);
er_read_length(s, length);
stream_read_uint8(s, *padding);
return true;
}
boolean er_write_bit_string_tag(STREAM* s, uint32 length, uint8 padding, boolean flag)
{
er_write_universal_tag(s, ER_TAG_BIT_STRING, false);
er_write_length(s, length, flag);
stream_write_uint8(s, padding);
return true;
}
boolean er_read_octet_string(STREAM* s, int* length)
{
if(!er_read_universal_tag(s, ER_TAG_OCTET_STRING, false))
return false;
er_read_length(s, length);
return true;
}
/**
* Write a er OCTET_STRING
* @param s stream
* @param oct_str octet string
* @param length string length
*/
void er_write_octet_string(STREAM* s, uint8* oct_str, int length, boolean flag)
{
er_write_universal_tag(s, ER_TAG_OCTET_STRING, false);
er_write_length(s, length, flag);
stream_write(s, oct_str, length);
}
int er_write_octet_string_tag(STREAM* s, int length, boolean flag)
{
er_write_universal_tag(s, ER_TAG_OCTET_STRING, false);
er_write_length(s, length, flag);
return 1 + _er_skip_length(length);
}
int er_skip_octet_string(int length)
{
return 1 + _er_skip_length(length) + length;
}
/**
* Read a er BOOLEAN
* @param s
* @param value
*/
boolean er_read_boolean(STREAM* s, boolean* value)
{
int length;
uint8 v;
if (!er_read_universal_tag(s, ER_TAG_BOOLEAN, false))
return false;
er_read_length(s, &length);
if (length != 1)
return false;
stream_read_uint8(s, v);
*value = (v ? true : false);
return true;
}
/**
* Write a er BOOLEAN
* @param s
* @param value
*/
void er_write_boolean(STREAM* s, boolean value)
{
er_write_universal_tag(s, ER_TAG_BOOLEAN, false);
er_write_length(s, 1, false);
stream_write_uint8(s, (value == true) ? 0xFF : 0);
}
boolean er_read_integer(STREAM* s, uint32* value)
{
int length;
er_read_universal_tag(s, ER_TAG_INTEGER, false);
er_read_length(s, &length);
if (value == NULL)
{
stream_seek(s, length);
return true;
}
if (length == 1)
stream_read_uint8(s, *value);
else if (length == 2)
stream_read_uint16_be(s, *value);
else if (length == 3)
{
uint8 byte;
stream_read_uint8(s, byte);
stream_read_uint16_be(s, *value);
*value += (byte << 16);
}
else if (length == 4)
stream_read_uint32_be(s, *value);
else
return false;
return true;
}
/**
* Write a er INTEGER
* @param s
* @param value
*/
int er_write_integer(STREAM* s, sint32 value)
{
er_write_universal_tag(s, ER_TAG_INTEGER, false);
if (value <= 127 && value >= -128)
{
er_write_length(s, 1, false);
stream_write_uint8(s, value);
return 2;
}
else if (value <= 32767 && value >= -32768)
{
er_write_length(s, 2, false);
stream_write_uint16_be(s, value);
return 3;
}
else
{
er_write_length(s, 4, false);
stream_write_uint32_be(s, value);
return 5;
}
return 0;
}
int er_skip_integer(sint32 value)
{
if (value <= 127 && value >= -128)
{
return _er_skip_length(1) + 2;
}
else if (value <= 32767 && value >= -32768)
{
return _er_skip_length(2) + 3;
}
else
{
return _er_skip_length(4) + 5;
}
return 0;
}
boolean er_read_integer_length(STREAM* s, int* length)
{
er_read_universal_tag(s, ER_TAG_INTEGER, false);
er_read_length(s, length);
return true;
}