bcrypt: Move the ASN1 buffer functions to gnutls.c.

Signed-off-by: Hans Leidekker <hans@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Hans Leidekker 2018-07-20 14:51:15 +02:00 committed by Alexandre Julliard
parent 8b8a32f7a8
commit b6cb27ea4c
3 changed files with 103 additions and 109 deletions

View File

@ -105,18 +105,6 @@ VOID WINAPI A_SHAInit(SHA_CTX *ctx);
VOID WINAPI A_SHAUpdate(SHA_CTX *ctx, const UCHAR *buffer, UINT size);
VOID WINAPI A_SHAFinal(SHA_CTX *ctx, PULONG result);
struct buffer
{
BYTE *buffer;
DWORD length;
DWORD pos;
BOOL error;
};
void buffer_init( struct buffer * ) DECLSPEC_HIDDEN;
void buffer_free( struct buffer * ) DECLSPEC_HIDDEN;
void buffer_append_asn1_r_s( struct buffer *, BYTE *, DWORD, BYTE *, DWORD ) DECLSPEC_HIDDEN;
#define MAGIC_ALG (('A' << 24) | ('L' << 16) | ('G' << 8) | '0')
#define MAGIC_HASH (('H' << 24) | ('A' << 16) | ('S' << 8) | 'H')
#define MAGIC_KEY (('K' << 24) | ('E' << 16) | ('Y' << 8) | '0')

View File

@ -718,103 +718,6 @@ NTSTATUS WINAPI BCryptHash( BCRYPT_ALG_HANDLE algorithm, UCHAR *secret, ULONG se
return BCryptDestroyHash( handle );
}
#if defined(HAVE_GNUTLS_CIPHER_INIT)
void buffer_init( struct buffer *buffer )
{
buffer->buffer = NULL;
buffer->length = 0;
buffer->pos = 0;
buffer->error = FALSE;
}
void buffer_free( struct buffer *buffer )
{
heap_free( buffer->buffer );
}
static void buffer_append( struct buffer *buffer, BYTE *data, DWORD len )
{
if (!len) return;
if (buffer->pos + len > buffer->length)
{
DWORD new_length = max( max( buffer->pos + len, buffer->length * 2 ), 64 );
BYTE *new_buffer;
if (!(new_buffer = heap_realloc( buffer->buffer, new_length )))
{
ERR( "out of memory\n" );
buffer->error = TRUE;
return;
}
buffer->buffer = new_buffer;
buffer->length = new_length;
}
memcpy( &buffer->buffer[buffer->pos], data, len );
buffer->pos += len;
}
static void buffer_append_byte( struct buffer *buffer, BYTE value )
{
buffer_append( buffer, &value, sizeof(value) );
}
static void buffer_append_asn1_length( struct buffer *buffer, DWORD length )
{
DWORD num_bytes;
if (length < 128)
{
buffer_append_byte( buffer, length );
return;
}
if (length <= 0xff) num_bytes = 1;
else if (length <= 0xffff) num_bytes = 2;
else if (length <= 0xffffff) num_bytes = 3;
else num_bytes = 4;
buffer_append_byte( buffer, 0x80 | num_bytes );
while (num_bytes--) buffer_append_byte( buffer, length >> (num_bytes * 8) );
}
static void buffer_append_asn1_integer( struct buffer *buffer, BYTE *data, DWORD len )
{
DWORD leading_zero = (*data & 0x80) != 0;
buffer_append_byte( buffer, 0x02 ); /* tag */
buffer_append_asn1_length( buffer, len + leading_zero );
if (leading_zero) buffer_append_byte( buffer, 0 );
buffer_append( buffer, data, len );
}
static void buffer_append_asn1_sequence( struct buffer *buffer, struct buffer *content )
{
if (content->error)
{
buffer->error = TRUE;
return;
}
buffer_append_byte( buffer, 0x30 ); /* tag */
buffer_append_asn1_length( buffer, content->pos );
buffer_append( buffer, content->buffer, content->pos );
}
void buffer_append_asn1_r_s( struct buffer *buffer, BYTE *r, DWORD r_len, BYTE *s, DWORD s_len )
{
struct buffer value;
buffer_init( &value );
buffer_append_asn1_integer( &value, r, r_len );
buffer_append_asn1_integer( &value, s, s_len );
buffer_append_asn1_sequence( buffer, &value );
buffer_free( &value );
}
#endif
#if defined(HAVE_GNUTLS_CIPHER_INIT) || defined(HAVE_COMMONCRYPTO_COMMONCRYPTOR_H) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
BOOL key_is_symmetric( struct key *key )
{

View File

@ -215,6 +215,109 @@ void gnutls_uninitialize(void)
libgnutls_handle = NULL;
}
struct buffer
{
BYTE *buffer;
DWORD length;
DWORD pos;
BOOL error;
};
static void buffer_init( struct buffer *buffer )
{
buffer->buffer = NULL;
buffer->length = 0;
buffer->pos = 0;
buffer->error = FALSE;
}
static void buffer_free( struct buffer *buffer )
{
heap_free( buffer->buffer );
}
static void buffer_append( struct buffer *buffer, BYTE *data, DWORD len )
{
if (!len) return;
if (buffer->pos + len > buffer->length)
{
DWORD new_length = max( max( buffer->pos + len, buffer->length * 2 ), 64 );
BYTE *new_buffer;
if (!(new_buffer = heap_realloc( buffer->buffer, new_length )))
{
ERR( "out of memory\n" );
buffer->error = TRUE;
return;
}
buffer->buffer = new_buffer;
buffer->length = new_length;
}
memcpy( &buffer->buffer[buffer->pos], data, len );
buffer->pos += len;
}
static void buffer_append_byte( struct buffer *buffer, BYTE value )
{
buffer_append( buffer, &value, sizeof(value) );
}
static void buffer_append_asn1_length( struct buffer *buffer, DWORD length )
{
DWORD num_bytes;
if (length < 128)
{
buffer_append_byte( buffer, length );
return;
}
if (length <= 0xff) num_bytes = 1;
else if (length <= 0xffff) num_bytes = 2;
else if (length <= 0xffffff) num_bytes = 3;
else num_bytes = 4;
buffer_append_byte( buffer, 0x80 | num_bytes );
while (num_bytes--) buffer_append_byte( buffer, length >> (num_bytes * 8) );
}
static void buffer_append_asn1_integer( struct buffer *buffer, BYTE *data, DWORD len )
{
DWORD leading_zero = (*data & 0x80) != 0;
buffer_append_byte( buffer, 0x02 ); /* tag */
buffer_append_asn1_length( buffer, len + leading_zero );
if (leading_zero) buffer_append_byte( buffer, 0 );
buffer_append( buffer, data, len );
}
static void buffer_append_asn1_sequence( struct buffer *buffer, struct buffer *content )
{
if (content->error)
{
buffer->error = TRUE;
return;
}
buffer_append_byte( buffer, 0x30 ); /* tag */
buffer_append_asn1_length( buffer, content->pos );
buffer_append( buffer, content->buffer, content->pos );
}
static void buffer_append_asn1_r_s( struct buffer *buffer, BYTE *r, DWORD r_len, BYTE *s, DWORD s_len )
{
struct buffer value;
buffer_init( &value );
buffer_append_asn1_integer( &value, r, r_len );
buffer_append_asn1_integer( &value, s, s_len );
buffer_append_asn1_sequence( buffer, &value );
buffer_free( &value );
}
NTSTATUS key_set_property( struct key *key, const WCHAR *prop, UCHAR *value, ULONG size, ULONG flags )
{
if (!strcmpW( prop, BCRYPT_CHAINING_MODE ))