Thread: [Redbutton-devel] SF.net SVN: redbutton: [392] redbutton-author/trunk/der_encode.c
Brought to you by:
skilvington
|
From: <ski...@us...> - 2007-09-18 12:18:15
|
Revision: 392
http://redbutton.svn.sourceforge.net/redbutton/?rev=392&view=rev
Author: skilvington
Date: 2007-09-18 05:18:13 -0700 (Tue, 18 Sep 2007)
Log Message:
-----------
STRING is only allowed to contain chars 0x20 to 0x7e
Modified Paths:
--------------
redbutton-author/trunk/der_encode.c
Modified: redbutton-author/trunk/der_encode.c
===================================================================
--- redbutton-author/trunk/der_encode.c 2007-09-18 12:12:11 UTC (rev 391)
+++ redbutton-author/trunk/der_encode.c 2007-09-18 12:18:13 UTC (rev 392)
@@ -50,12 +50,14 @@
/*
* string is enclosed in "
+ * contains chars 0x20 to 0x7e
* " and \ within the string are encoded as \" and \\
*/
void
convert_STRING(unsigned char **out, unsigned int *len, const char *str)
{
+ unsigned char *whole_str = str;
unsigned char *p;
/* max size it could be */
@@ -66,8 +68,12 @@
p = *out;
while(*str != '"')
{
- if(*str != '\\')
+ if(*str < 0x20 || *str > 0x7e)
{
+ fatal("Invalid character (0x%02x) in STRING: %s", *str, whole_str);
+ }
+ else if(*str != '\\')
+ {
*p = *str;
p ++;
str ++;
@@ -86,13 +92,13 @@
}
else
{
- fatal("Invalid escape sequence in STRING: %s", str - 1);
+ fatal("Invalid escape sequence in STRING: %s", whole_str);
}
}
/* check we got to the closing quote */
if(*(str + 1) != '\0')
- fatal("Unquoted \" in STRING: %s", str - 1);
+ fatal("Unquoted \" in STRING: %s", whole_str);
/* return the length (note: no \0 terminator) */
*len = (p - *out);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ski...@us...> - 2007-09-18 12:55:38
|
Revision: 394
http://redbutton.svn.sourceforge.net/redbutton/?rev=394&view=rev
Author: skilvington
Date: 2007-09-18 05:55:32 -0700 (Tue, 18 Sep 2007)
Log Message:
-----------
DER encode +ve INTEGERs
Modified Paths:
--------------
redbutton-author/trunk/der_encode.c
Modified: redbutton-author/trunk/der_encode.c
===================================================================
--- redbutton-author/trunk/der_encode.c 2007-09-18 12:36:39 UTC (rev 393)
+++ redbutton-author/trunk/der_encode.c 2007-09-18 12:55:32 UTC (rev 394)
@@ -22,10 +22,37 @@
void
der_encode_INTEGER(unsigned char **out, unsigned int *len, int val)
{
+ unsigned int shifted;
+ unsigned int i;
+
/* assert */
if(*out != NULL || *len != 0)
fatal("der_encode_INTEGER: length already %u", *len);
+ /*
+ * work out how many bytes we need to store 'val'
+ * ints in DER are signed, so first byte we store must be <= 127
+ * we add a leading 0 if first byte is > 127
+ */
+ if(val >= 0)
+ {
+ shifted = val;
+ (*len) = 1;
+ while(shifted > 127)
+ {
+ (*len) ++;
+ shifted >>= 8;
+ }
+ *out = safe_malloc(*len);
+ /* big endian */
+ for(i=1; i<=(*len); i++)
+ (*out)[i - 1] = (val >> (((*len) - i) * 8)) & 0xff;
+ }
+ else
+ {
+fatal("TODO: negative INTEGER: %d", val);
+ }
+
return;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ski...@us...> - 2007-09-18 16:29:53
|
Revision: 397
http://redbutton.svn.sourceforge.net/redbutton/?rev=397&view=rev
Author: skilvington
Date: 2007-09-18 09:29:48 -0700 (Tue, 18 Sep 2007)
Log Message:
-----------
DER encode BOOLEAN
Modified Paths:
--------------
redbutton-author/trunk/der_encode.c
Modified: redbutton-author/trunk/der_encode.c
===================================================================
--- redbutton-author/trunk/der_encode.c 2007-09-18 16:23:06 UTC (rev 396)
+++ redbutton-author/trunk/der_encode.c 2007-09-18 16:29:48 UTC (rev 397)
@@ -16,6 +16,10 @@
if(*out != NULL || *len != 0)
fatal("der_encode_BOOLEAN: length already %u", *len);
+ *len = 1;
+ *out = safe_malloc(1);
+ (*out)[0] = val ? 1 : 0;
+
return;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ski...@us...> - 2007-09-18 16:42:53
|
Revision: 398
http://redbutton.svn.sourceforge.net/redbutton/?rev=398&view=rev
Author: skilvington
Date: 2007-09-18 09:42:05 -0700 (Tue, 18 Sep 2007)
Log Message:
-----------
DER encode -ve INTEGERs the lazy way
Modified Paths:
--------------
redbutton-author/trunk/der_encode.c
Modified: redbutton-author/trunk/der_encode.c
===================================================================
--- redbutton-author/trunk/der_encode.c 2007-09-18 16:29:48 UTC (rev 397)
+++ redbutton-author/trunk/der_encode.c 2007-09-18 16:42:05 UTC (rev 398)
@@ -28,20 +28,22 @@
{
unsigned int shifted;
unsigned int i;
+ unsigned int uval;
/* assert */
if(*out != NULL || *len != 0)
fatal("der_encode_INTEGER: length already %u", *len);
- /*
- * work out how many bytes we need to store 'val'
- * ints in DER are signed, so first byte we store must be <= 127
- * we add a leading 0 if first byte is > 127
- */
+ /* is it +ve or -ve */
if(val >= 0)
{
+ /*
+ * work out how many bytes we need to store 'val'
+ * ints in DER are signed, so first byte we store must be <= 127
+ * we add a leading 0 if first byte is > 127
+ */
shifted = val;
- (*len) = 1;
+ *len = 1;
while(shifted > 127)
{
(*len) ++;
@@ -54,7 +56,17 @@
}
else
{
-fatal("TODO: negative INTEGER: %d", val);
+ /*
+ * TODO
+ * should really use as few bytes as possible
+ * ie chop off leading 0xff's while the next byte has its top bit set
+ */
+ *len = sizeof(unsigned int);
+ *out = safe_malloc(*len);
+ /* big endian */
+ uval = (unsigned int) val;
+ for(i=1; i<=(*len); i++)
+ (*out)[i - 1] = (uval >> (((*len) - i) * 8)) & 0xff;
}
return;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ski...@us...> - 2007-09-21 16:17:24
|
Revision: 408
http://redbutton.svn.sourceforge.net/redbutton/?rev=408&view=rev
Author: skilvington
Date: 2007-09-21 09:17:21 -0700 (Fri, 21 Sep 2007)
Log Message:
-----------
use as few bytes as possible to store -ve ints
Modified Paths:
--------------
redbutton-author/trunk/der_encode.c
Modified: redbutton-author/trunk/der_encode.c
===================================================================
--- redbutton-author/trunk/der_encode.c 2007-09-21 15:52:33 UTC (rev 407)
+++ redbutton-author/trunk/der_encode.c 2007-09-21 16:17:21 UTC (rev 408)
@@ -56,15 +56,21 @@
}
else
{
+ /* work with an unsigned value */
+ uval = (unsigned int) val;
/*
- * TODO
- * should really use as few bytes as possible
+ * use as few bytes as possible
* ie chop off leading 0xff's while the next byte has its top bit set
*/
*len = sizeof(unsigned int);
+ while((*len) > 1
+ && ((uval >> (((*len) - 1) * 8)) & 0xff) == 0xff
+ && ((uval >> (((*len) - 2) * 8)) & 0x80) == 0x80)
+ {
+ (*len) --;
+ }
*out = safe_malloc(*len);
/* big endian */
- uval = (unsigned int) val;
for(i=1; i<=(*len); i++)
(*out)[i - 1] = (uval >> (((*len) - i) * 8)) & 0xff;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ski...@us...> - 2007-09-22 18:17:04
|
Revision: 413
http://redbutton.svn.sourceforge.net/redbutton/?rev=413&view=rev
Author: skilvington
Date: 2007-09-22 11:16:46 -0700 (Sat, 22 Sep 2007)
Log Message:
-----------
do everything apart from actually encode the DER tag and length
Modified Paths:
--------------
redbutton-author/trunk/der_encode.c
Modified: redbutton-author/trunk/der_encode.c
===================================================================
--- redbutton-author/trunk/der_encode.c 2007-09-22 16:57:02 UTC (rev 412)
+++ redbutton-author/trunk/der_encode.c 2007-09-22 18:16:46 UTC (rev 413)
@@ -221,9 +221,34 @@
unsigned int
gen_der_header(struct node *n)
{
-/* TODO */
-printf("TODO: gen_der_header\n");
+ unsigned int val_length;
+ struct node *kid;
- return n->hdr_length + n->length;
+ /* if it is a constructed type, sum the length of its children */
+ if(n->children)
+ {
+ /* assert */
+ if(n->length != 0)
+ fatal("Constructed type [%s %u] has a value", asn1class_name(n->asn1class), n->asn1tag);
+ /* recursively generate the DER headers for our child nodes */
+ val_length = 0;
+ for(kid=n->children; kid; kid=kid->siblings)
+ val_length += gen_der_header(kid);
+ }
+ else
+ {
+ /* assert */
+ if(n->length == 0 && !(n->asn1tag == ASN1TAG_NULL && n->asn1class == ASN1CLASS_UNIVERSAL))
+ fatal("Type [%s %u] has 0 length", asn1class_name(n->asn1class), n->asn1tag);
+ val_length = n->length;
+ }
+
+ /* we don't need a header if we are a synthetic object */
+ if(!is_synthetic(n->asn1tag))
+ {
+printf("TODO: gen_der_header: tag=%u class=%s len=%u\n", n->asn1tag, asn1class_name(n->asn1class), val_length);
+ }
+
+ return n->hdr_length + val_length;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ski...@us...> - 2007-09-22 18:35:43
|
Revision: 414
http://redbutton.svn.sourceforge.net/redbutton/?rev=414&view=rev
Author: skilvington
Date: 2007-09-22 11:35:40 -0700 (Sat, 22 Sep 2007)
Log Message:
-----------
OCTET STRINGs could be 0 length
Modified Paths:
--------------
redbutton-author/trunk/der_encode.c
Modified: redbutton-author/trunk/der_encode.c
===================================================================
--- redbutton-author/trunk/der_encode.c 2007-09-22 18:16:46 UTC (rev 413)
+++ redbutton-author/trunk/der_encode.c 2007-09-22 18:35:40 UTC (rev 414)
@@ -237,9 +237,6 @@
}
else
{
- /* assert */
- if(n->length == 0 && !(n->asn1tag == ASN1TAG_NULL && n->asn1class == ASN1CLASS_UNIVERSAL))
- fatal("Type [%s %u] has 0 length", asn1class_name(n->asn1class), n->asn1tag);
val_length = n->length;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ski...@us...> - 2007-09-26 16:33:30
|
Revision: 425
http://redbutton.svn.sourceforge.net/redbutton/?rev=425&view=rev
Author: skilvington
Date: 2007-09-26 09:33:24 -0700 (Wed, 26 Sep 2007)
Log Message:
-----------
encode base64 octet strings
Modified Paths:
--------------
redbutton-author/trunk/der_encode.c
Modified: redbutton-author/trunk/der_encode.c
===================================================================
--- redbutton-author/trunk/der_encode.c 2007-09-25 11:59:39 UTC (rev 424)
+++ redbutton-author/trunk/der_encode.c 2007-09-26 16:33:24 UTC (rev 425)
@@ -256,14 +256,112 @@
return;
}
+/*
+ * enclosed in `
+ * base 64 encoded as described in RFC 1521
+ * line breaks in the input can be ignored
+ */
+
+unsigned char next_base64_char(unsigned char **);
+
void
convert_BASE64(unsigned char **out, unsigned int *len, const unsigned char *str)
{
-/* TODO */
-printf("TODO: convert_BASE64\n");
+ const unsigned char *whole_str = str;
+ unsigned char *p;
+ bool err;
+
+ /* max size it could be */
+ *out = safe_malloc(strlen(str));
+
+ /* skip the initial ` */
+ str ++;
+ p = *out;
+ err = false;
+ while(!err && *str != '`')
+ {
+ /* need 4 chars */
+ unsigned char v1 = next_base64_char((unsigned char **) &str);
+ unsigned char v2 = next_base64_char((unsigned char **) &str);
+ unsigned char v3 = next_base64_char((unsigned char **) &str);
+ unsigned char v4 = next_base64_char((unsigned char **) &str);
+ /* how many chars are valid */
+ if(v1 < 64 && v2 < 64 && v3 < 64 && v4 < 64)
+ {
+ /* 4 chars => 24 bits */
+ p[0] = (v1 << 2) | (v2 >> 4);
+ p[1] = (v2 << 4) | (v3 >> 2);
+ p[2] = (v3 << 6) | v4;
+ p += 3;
+ }
+ else if(v1 < 64 && v2 < 64 && v3 < 64 && v4 == 64)
+ {
+ /* 3 chars => 16 bits */
+ p[0] = (v1 << 2) | (v2 >> 4);
+ p[1] = (v2 << 4) | (v3 >> 2);
+ p += 2;
+ }
+ else if(v1 < 64 && v2 < 64 && v3 == 64 && v4 == 64)
+ {
+ /* 2 chars => 8 bits */
+ p[0] = (v1 << 2) | (v2 >> 4);
+ p += 1;
+ }
+ else
+ {
+ parse_error("Invalid BASE64 string: %s", whole_str);
+ err = true;
+ }
+ }
+
+ /* check we got to the closing quote */
+ if(!err && *(str + 1) != '\0')
+ {
+ parse_error("Unexpected ` in BASE64: %s", whole_str);
+ err = true;
+ }
+
+ if(!err)
+ {
+ /* return the length (note: no \0 terminator) */
+ *len = (p - *out);
+ }
+ else
+ {
+ /* clean up */
+ safe_free(*out);
+ *out = NULL;
+ *len = 0;
+ }
+
+ return;
}
/*
+ * updates *in
+ */
+
+unsigned char
+next_base64_char(unsigned char **in)
+{
+ /* would be faster with a 256 byte lookup table */
+ unsigned char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+ unsigned char *pos;
+ unsigned char out;
+
+ while(**in != '`')
+ {
+ out = **in;
+ (*in) ++;
+ if((pos = strchr(alphabet, out)) != NULL)
+ return (pos - alphabet);
+ }
+
+ /* EOF, return (strchr(alphabet, '=') - alphabet) */
+ return 64;
+}
+
+/*
* recursively generate the DER tag/length header for the tree of nodes
*/
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ski...@us...> - 2008-01-25 15:32:14
|
Revision: 479
http://redbutton.svn.sourceforge.net/redbutton/?rev=479&view=rev
Author: skilvington
Date: 2008-01-25 07:32:11 -0800 (Fri, 25 Jan 2008)
Log Message:
-----------
encode true booleans as 255 rather than 1 (same as BBC) - should probably be a cmd line option
Modified Paths:
--------------
redbutton-author/trunk/der_encode.c
Modified: redbutton-author/trunk/der_encode.c
===================================================================
--- redbutton-author/trunk/der_encode.c 2008-01-25 15:29:40 UTC (rev 478)
+++ redbutton-author/trunk/der_encode.c 2008-01-25 15:32:11 UTC (rev 479)
@@ -36,7 +36,7 @@
*len = 1;
*out = safe_malloc(1);
- (*out)[0] = val ? 1 : 0;
+ (*out)[0] = val ? 0xff : 0;
return;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|