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 02    2:   INTEGER 840
   6 02    2:   INTEGER 735
  10 02    1:   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;
}

Attachment: amt
Description: Binary data

Reply via email to