Thank you, I rewrite the code ,but 0x00 is still trimmed off. int main() { unsigned char *buf = NULL,*temp_buf = NULL; int buflen = 128; ASN1_BIT_STRING *bs = NULL,*bs2 = NULL; int i = 0,j = 0; int ret = 0; int derlen = 0; unsigned char *p = NULL; buf = OPENSSL_malloc(150); temp_buf = OPENSSL_malloc(150); p = temp_buf; for (i=0;i<buflen-2;i++) { buf[i]='A'; } buf[buflen-2]='B'; buf[buflen-1]='\0'; // (1)
printf("buflen=%d\n",buflen); for (i=0;i<buflen;i++) { printf("%02X%c",buf[i],(i+1)%20==0?'\n':' '); } printf("\n"); bs = ASN1_BIT_STRING_new(); ret = ASN1_BIT_STRING_set(bs, buf, buflen); printf("ASN1_BIT_STRING_set() = %d\n",ret); derlen = i2d_ASN1_BIT_STRING(bs, &temp_buf); printf("i2d_ASN1_BIT_STRING() = %d\n",derlen); for (i=0;i<derlen;i++) { printf("%02X%c",p[i],(i+1)%20==0?'\n':' '); } printf("\n"); bs2 = d2i_ASN1_BIT_STRING(NULL, &p, derlen); if (bs2 != NULL) { printf("d2i_ASN1_BIT_STRING()\n"); printf("bs2->length = %d\n",bs2->length); for (i=0;i<bs2->length;i++) { printf("%02X%c",bs2->data[i],(i+1)%20==0?'\n':' '); } } } The result is: buflen=128 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 42 00 ASN1_BIT_STRING_set() = 1 i2d_ASN1_BIT_STRING() = 131 03 81 80 01 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 42 d2i_ASN1_BIT_STRING() bs2->length = 127 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 42 On Tue, Oct 29, 2013 at 8:10 PM, Salz, Rich <rs...@akamai.com> wrote: > You are comparing the native form to the DER encoding. You need to d2i > back and see that the data is preserved.**** > > Not only is “the 0x00 trimmed” but those bytes at the front 03 81 80 01 > are also a clue. J**** > > /r$**** > > ** ** > > -- **** > > Principal Security Engineer**** > > Akamai Technology**** > > Cambridge, MA**** > > ** ** >