ASN.1 Encode an Object Identifier (OID) with OpenSSL

Source: Internet
Author: User

OID (object Identifier) denotes an object.
Examples:
------------------------------------------------------------------
OID Object
------------------------------------------------------------------
1.3.14.3.2.26 SHA-1
2.16.840.1.101.3.4.2.1 SHA-256
1.2.840.113549.1.7.2 PKCS-7 Signeddata
------------------------------------------------------------------

In OpenSSL no functions is directly provided to compute the OID ASN.1 encode. At lease, methods can be taken to account.

1. Create a temporary object by invoking function Obj_create () and then encode it by invoking function I2d_asn1_object ().
Implementation (not recommended)

/*************************************************** Author:han wei* Author ' s blog:http://blog.csdn.net/henter/* Date:oct 11th, 2014* description:implement the OID ASN.1 encode function********************************************** /#include <stdio.h> #include <openssl/objects.h> #include <openssl/asn1.h>int asn1encodeoid (  Char *oid, unsigned char *encode, int *encode_len) {int New_nid, Byte_len;  Asn1_object *obj;  unsigned char *tmp_pointer;  New_nid = Obj_create (OID, "oid example", "Object Identifier example");  obj = Obj_nid2obj (New_nid);  if (!encode) {Byte_len = I2d_asn1_object (obj, NULL), if (Byte_len <= 0) {#ifdef _DEBUG printf ("Get ASN.1 encode  Byte length failed at%s, line%d!\n ", __file__, __line__); #endif obj_cleanup (); Return (-1);}  else{*encode_len = Byte_len;  Obj_cleanup ();  return 0;}    } else {tmp_pointer = encode;    Byte_len = I2d_asn1_object (obj, &tmp_pointer); if (byte_len <= 0)    {#ifdef _DEBUG printf ("ASN.1 encode OID failed at%s, line%d!\n", __file__, __line__); #endif obj_cleanup ();    Return (-1);      } else {*encode_len = Byte_len;      Obj_cleanup ();    return 0; }  }}


This is a good implementation. Obj_cleanup () would free any dynamically created object, so this function must be used carefully. Especially when multiple threads is running, the fact that one thread invokes Obj_cleanup () may run the risk of cleaning Object created by other threads. The consequence is unpredictable.

2. Compute OID Payload Part ASN.1 encode by invoking function a2d_asn1_object () Firstly, Compute the OID encode by Invokin g function I2d_asn1_object () next.

A complete implementation (recommended)

Header file:

/*************************************************** File name:oid_encode.h* Author:han Wei* Author ' s blog:http:// blog.csdn.net/henter/* date:oct 11th, 2014* description:declare the OID ASN.1 encode function************************** /#ifndef header_oid_asn1_encode_h #define HEADER_OID_ASN1_ENCODE_H#IFDEF __cplusplusextern "C" {#endif/*************************************************** name:asn1encodeoid* function:compute ASN.1 encode For a specific oid* parameters:oid (in) OID string terminated with ' encode ', [in] buffer used to s Tore OID ASN.1 Encodeencode_len [out] byte length of OID ASN.1 encode* Return value:succeed--0 fail---1* Note S:1. If The NULL pointer is assigned to parameter ' encode ', this function does not perform ASN.1 encode. The OID ASN.1 encode length is assigned to parameter ' Encode_len ' and the function returns.2. If the value assigned to parameter ' encode ' are not NULL, the OID ASN.1 encodeWas written into the buffer pointed by parameter ' encode ', and encode length was assigned to parameter ' Encode_len '.   In this case the buffer length was not checked before the encode was written into the buffer. Make sure, the buffer length is big enough to accomodate the ASN.1 encode!****************************************** /int asn1encodeoid (char *oid, unsigned char *encode, int *encode_len); #ifdef __cplusplus} #endif #endif/* End of H Eader_oid_asn1_encode_h * *


Function implementation file:

/*************************************************** File name:oid_encode.c* Author:han Wei* Author ' s blog:http:// blog.csdn.net/henter/* date:oct 11th, 2014* description:implement the OID ASN.1 encode function************************ /#include <stdio.h> #include <openssl/objects.h> #include <openssl/asn1.h  >int asn1encodeoid (char *oid, unsigned char *encode, int *encode_len) {int Payload_len, Total_len;  Asn1_object obj;  unsigned char *tmp_pointer, *payload_encode;//get payload ASN.1 encode Payload_len = A2d_asn1_object (NULL, 0, OID,-1); if (Payload_len <= 0) {#ifdef _DEBUG printf ("Get ASN.1 encode byte length failed at%s, line%d!\n", __file__, __l  INE__); #endif return (-1); } if (! ( payload_encode= (unsigned char *) malloc (Payload_len)) {#ifdef _DEBUG printf ("Invoke malloc () function failed at%s, l  Ine%d!\n ", __file__, __line__); #endif return (-1); } Payload_len = A2d_asn1_object (payload_encode, Payload_len, OID,-1); if (Payload_len <= 0) {#ifdef _DEBUG printf ("ASN.1 encode payload failed at%s, line%d!\n", __file__, __line__); #e    NDIF free (payload_encode);  Return (-1);  }//get the whole OID ASN.1 encode obj.data = Payload_encode;  Obj.length = Payload_len;    if (!encode) {Total_len = I2d_asn1_object (&obj, NULL); if (Total_len <= 0) {#ifdef _DEBUG printf ("Get ASN.1 encode byte length failed at%s, line%d!\n", __file__, __      line__); #endif free (Payload_encode);    Return (-1);      } else {*encode_len = Total_len;      Free (Payload_encode);    return 0;    }} else {tmp_pointer = encode;    Total_len = I2d_asn1_object (&obj, &tmp_pointer); if (Total_len <= 0) {#ifdef _DEBUG printf ("ASN.1 encode OID failed at%s, line%d!\n", __file__, __line__); #end      If free (payload_encode);    Return (-1);      } else {*encode_len = Total_len;      Free (Payload_encode);    return 0; }  }}


A Demo Program file:

/*************************************************** File name:test.c* Author:han wei* Author ' s blog:http:// blog.csdn.net/henter/* date:oct 11th, 2014* Description:this program demonstrates how to invoke the OID ASN.1 encode function**************************************************/#include "oid_encode.h" #include <stdio.h>#  Include <stdlib.h>int main (void) {char oid[128]= "2.16.840.1.101.3.4.2.1";  /* SHA-256 oid*/unsigned char *buffer;  int Buffer_len, I;    if (Asn1encodeoid (OID, NULL, &buffer_len)) {printf ("error detected!\n"); #if defined (_WIN32) | | defined (_WIN64)  System ("pause"); #endif return (-1);  } printf ("OID ASN.1 encode length is%d bytes.\n", Buffer_len); if (! (  Buffer = (unsigned char *) malloc (Buffer_len)) {printf ("Invoke malloc () function failed!\n"); #if defined (_WIN32) | |  Defined (_win64) system ("Pause"), #endif return (-1);    } if (Asn1encodeoid (OID, buffer, &buffer_len)) {printf ("error detected!\n"); Freebuffer); #if defined (_WIN32) | |  Defined (_win64) system ("Pause"), #endif return (-1);  } printf ("OID ASN.1 encode:\n");  for (i=0; i<buffer_len; i++) printf ("0x%x", Buffer[i]);  printf ("\ n"); Free (buffer); #if defined (_WIN32) | |  Defined (_win64) system ("pause"); #endif return 0; }

ASN.1 encode of SHA-256 is obtained from the demo:0x6 0x9 0x60 0x86 0x48 0x1 0x65 0x3 0x4 0x2 0x1
This is a better implementation.

ASN.1 Encode an Object Identifier (OID) with OpenSSL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.