2013-06-13 88 views
0

我有一個96字節長的ecdsa簽名,使用原始格式的智能卡使用sha384算法創建。它由兩個長度爲整數r和s的長整數字組成。 ecdsa簽名位於由sign_ptr指向的緩衝區中。我使用此功能轉換RAW格式 簽名改成buf_out在ASN1格式(C):將原始格式ecdsa sha384 csr簽名轉換爲ASN1格式

int convert_ecdsa_sha384_sign(char **buf_out, char *sign_ptr) 
{ 
    buf_out[0]=0x30;       /* Type = Sequence of */ 
    buf_out[2]=0x02;       /* Type = Integer */ 
    /* Verify if negative bit is set */ 
    if (!(sign_ptr[0] & 0x80)) 
    { 
     buf_out[3]=0x30;      /* Length */ 
     memcpy(&(buf_out[4]), sign_ptr, 48); /* Copy first integer */ 
    } 
    else 
    { 
     /* Negative bit is set. Add one padding byte */ 
     buf_out[3]=0x31;      /* Length */ 
     buf_out[4]=0x00;      /* Padding */ 
     memcpy(&(buf_out[5]), sign_ptr, 48); /* Copy first integer */ 
     sign_offset += 1; 
    } 

    buf_out[52+sign_offset]=0x02;          /* Type = Integer */ 
    /* Verify if negative bit is set */ 
    if (!(sign_ptr[48] & 0x80)) 
    { 
     buf_out[53+sign_offset]=0x30;         /* Length */ 
     memcpy(((&(buf_out[54]))+ sign_offset), sign_ptr + 48, 48); /* Copy second integer */ 
    } 
    else 
    { 
     /* Negative bit is set. Add one padding byte */ 
     buf_out[53+sign_offset]=0x31;         /* Length */ 
     buf_out[54+sign_offset]=0x00;         /* Padding */ 
     memcpy(((&(buf_out[55]))+ sign_offset), sign_ptr + 48, 48); /* Copy second integer */ 
     sign_offset += 1; 
    } 
    buf_out[1]= 100 + sign_offset;         /* Total signature length */ 
    return 1; 

}

我想知道如果有一個相當於OpenSSL的功能,可以幫我做這在一個更優雅的方式?我看過很多d2i函數(d2i_ASN1_xxxx,ASN1_item_d2i,ASN_d2i_func等),但不清楚哪一個適合。

回答

0

這是我從Stephen N. Henson博士那裏得到的答案。 OpenSSL的項目核心開發人員在OpenSSL的用戶論壇:

「的結構ECDSA_SIG是一個你需要的

在大綱:分配使用ECDSA_SIG_new結構,設置使用BN_bin2bn R和S值,編碼與結果i2d_ECDSA_SIG,並最終免費使用ECDSA_SIG_free「。

+0

信息:http://www.codeproject.com/Articles/25590/Cryptographic-Interoperability-Digital-Signatures – Andrew

相關問題