ASN.1 encoding of negative integers

2001-10-17 Thread Vadim Fedukovich

hi,

what are the rules or known-good examples of negative number encoding?
I'm trying to encode -2 as part of currency-amount-power sequence
and Peter Gutmann's dumpasn1 tool shows it as -254.

Any problem regarding negative number handling at the code attached?
Hope it's good enough to drive debugger

thank you,
Vadim

   0 30   11: SEQUENCE {
   2 022:   INTEGER 840
   6 022:   INTEGER 735
  10 021:   INTEGER -254

: Warning: Integer has a negative value
:   }


/* ASN.1 handling code (c) Vadim Fedukovich 2001
 * work-in-progress; please use for bug hunting only 
 */

#include stdio.h
#include openssl/asn1t.h

typedef enum {CURRENCY_USD=840} CurrencyCode;

struct set_CurrencyAmount_st {
  ASN1_INTEGER *code;
  ASN1_INTEGER *amount;
  ASN1_INTEGER *power;
};
typedef struct set_CurrencyAmount_st SET_CurrencyAmount;

ASN1_SEQUENCE(SET_CurrencyAmount) = {
  ASN1_SIMPLE(SET_CurrencyAmount, code, ASN1_INTEGER),
  ASN1_SIMPLE(SET_CurrencyAmount, amount, ASN1_INTEGER),
  ASN1_SIMPLE(SET_CurrencyAmount, power, ASN1_INTEGER)
} ASN1_SEQUENCE_END(SET_CurrencyAmount)

IMPLEMENT_ASN1_FUNCTIONS(SET_CurrencyAmount)

#define SZ 2048
int main() {
  SET_CurrencyAmount *amt;
  int sz, amount = 735;  // 7 dollars 35 cents, power -2
  unsigned char buff[SZ], *pp;

  amt = SET_CurrencyAmount_new();

  ASN1_INTEGER_set(amt-code, (long)CURRENCY_USD);
  ASN1_INTEGER_set(amt-amount, (long)amount);
  ASN1_INTEGER_set(amt-power, -2L);
  
  pp = buff;
  sz = i2d_SET_CurrencyAmount(amt, pp);
  fwrite(buff, sz, 1, stderr);

  return 0;
}



amt
Description: Binary data


Re: ASN.1 encoding of negative integers

2001-10-17 Thread Dr S N Henson

Vadim Fedukovich wrote:
 
 hi,
 
 what are the rules or known-good examples of negative number encoding?
 I'm trying to encode -2 as part of currency-amount-power sequence
 and Peter Gutmann's dumpasn1 tool shows it as -254.
 
 Any problem regarding negative number handling at the code attached?
 Hope it's good enough to drive debugger
 
 thank you,
 Vadim
 
0 30   11: SEQUENCE {
2 022:   INTEGER 840
6 022:   INTEGER 735
   10 021:   INTEGER -254
 
 : Warning: Integer has a negative value
 :   }
 

Try getting a newer version of dumpasn1. My version lists it as -2.

Steve.
-- 
Dr Stephen N. Henson.   http://www.drh-consultancy.demon.co.uk/
Personal Email: [EMAIL PROTECTED] 
Senior crypto engineer, Gemplus: http://www.gemplus.com/
Core developer of the   OpenSSL project: http://www.openssl.org/
Business Email: [EMAIL PROTECTED] PGP key: via homepage.


__
OpenSSL Project http://www.openssl.org
User Support Mailing List[EMAIL PROTECTED]
Automated List Manager   [EMAIL PROTECTED]



Re: ASN.1 encoding of negative integers

2001-10-17 Thread Mark H. Wood

On Wed, 17 Oct 2001, Vadim Fedukovich wrote:
 what are the rules or known-good examples of negative number encoding?
 I'm trying to encode -2 as part of currency-amount-power sequence
 and Peter Gutmann's dumpasn1 tool shows it as -254.

It surely looks like a simple error in the dump tool, to me.  An eight-bit
signed value of -2 would be 254 if interpreted as unsigned.

I can recommend Olivier Dubuisson's book on ASN.1, but my copy is at home
now so I can't refer to it.

-- 
Mark H. Wood, Lead System Programmer   [EMAIL PROTECTED]
Make a good day.

__
OpenSSL Project http://www.openssl.org
User Support Mailing List[EMAIL PROTECTED]
Automated List Manager   [EMAIL PROTECTED]