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 are directly provided to compute the OID ASN.1 encode. At lease two methods can be taken into account.

1. Create a temporary object by invoking function OBJ_create(), 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 not a good implementation. OBJ_cleanup() will free all dynamically created object, so this function must be used carefully. Especially when multiple threads are 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 invoking 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 __cplusplus
extern "C" {
#endif /**************************************************
* Name: Asn1EncodeOid
* Function: compute ASN.1 encode for a specific OID
* Parameters:
oid [in] OID string terminated with '\0'
encode [in] buffer used to store OID ASN.1 encode
encode_len [out] byte length of OID ASN.1 encode
* Return value:
succeed -- 0
fail -- -1
* Notes:
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' is not NULL,
the OID ASN.1 encode is written into the buffer pointed by
parameter 'encode', and encode length is assigned to
parameter 'encode_len'. In this case the buffer length is
NOT checked before the encode is written into the buffer.
MAKE SURE that 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 HEADER_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__, __LINE__);
#endif
return (-1);
}
if ( !(payload_encode=(unsigned char *)malloc(payload_len)) )
{
#ifdef _DEBUG
printf("invoke malloc() function failed at %s, line %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__);
#endif
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__);
#endif
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");
free(buffer);
#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的更多相关文章

  1. PostgreSQL中的Object Identifier(oid)数据类型

    PostgreSQL在内部使用对象标识符(OID)作为各种系统表的主键.OID不会添加到用户创建的表中,除非在创建表时指定了WITH OIDS,或者启用了default_with_oids配置变量.类 ...

  2. Get RSA public key ASN.1 encode from a certificate in DER format

    RSA public key ASN.1 encode is defined in PKCS#1 as follows: RSAPublicKey :: = SEQUENCE  {     modul ...

  3. oracle impdp ORA-02304 invalid object identifier literal

    reference: https://webgeest.blogspot.com/2015/07/ora-39083-ora-02304-on-impdp-datapump.html     解决方法 ...

  4. OID的编解码(即在报文中的体现)

    先上干货: 我们常见到OID的地方是SNMP和MIB,实际上理论上所有对象都可以有自己的ID.已存在的ID可以在http://www.oid-info.com/查到.这些ID在报文里并非字符串或直接的 ...

  5. SNMP History and OID/MIB Tour

    https://www.pei.com/snmp-history-oid-mib/ Description: This document describes a bit of history and ...

  6. ECC公钥格式详解

    本文首先介绍公钥格式相关的若干概念/技术,随后以示例的方式剖析DER格式的ECC公钥,最后介绍如何使用Java生成.解析和使用ECC公钥. ASN.1 Abstract Syntax Notation ...

  7. Java & PHP & Javascript 通用 RSA 加密 解密 (长字符串)

    系统与系统的数据交互中,有些敏感数据是不能直接明文传输的,所以在发送数据之前要进行加密,在接收到数据时进行解密处理:然而由于系统与系统之间的开发语言不同. 本次需求是生成二维码是通过java生成,由p ...

  8. c# .NET RSA结合AES加密服务端和客户端请求数据

    这几天空闲时间就想研究一下加密,环境是web程序,通过js请求后台返回数据,我想做的事js在发送请求前将数据加密,服务端收到后解密,待服务端处理完请求后,将处理结果加密返回给客户端,客户端在解密,于是 ...

  9. COM Error Code(HRESULT)部分摘录

    Return value/code Description 0x00030200 STG_S_CONVERTED The underlying file was converted to compou ...

随机推荐

  1. 【原创】java中的父进程子进程 —— 坑爹的java Runtime.getRuntime().exec

    最近有一个需求,需要用一个java进程启动多个子进程来完成并发任务.由于必须给用户完成任务的反馈,所以需要父进程记录子进程的生命周期. exec方法返回一个Process对象,在当前进程内调用该对象的 ...

  2. Ewebeditor最新漏洞和漏洞指数

    Ewebeditor最新漏洞和漏洞指数[收集] 来源:转载作者:佚名时间:2009-06-03 00:04:26 下面文章收集转载于网络:) 算是比較全面的ewebeditor编辑器的漏洞收集,如今的 ...

  3. Threejs 它可以在建立其内部房间效果可见

    Threejs 中建立可看到其内部的房间效果 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协 ...

  4. java实现大数相加问题

    闲来没事.写了个acm中常常遇到的大数加减问题的java 解决代码,我想说.用java的BigInteger 非常easy. 大爱java!! 比如: 实现多组输入的大数加减问题: import ja ...

  5. FusionCharts简明教程(一)---建立FusionCharts图形

    由于该项目需要的报告需要做的事情,选择FusionCharts作为一种工具. 由于该报告没有任何接触,网上有没有更具体fusionCharts课程,所以我们决定做一个彻底的研究FusionCharts ...

  6. jQuery整理笔记2----jQuery选择整理

    一个.基本的选择 1.ID选择器 JavaScript提供了原生方法实如今DOM中选择指定ID值得元素. 使用方法例如以下: var element=document.getElementById(& ...

  7. vuejs 相关资料

    官网 http://vuejs.org/ 中文网站 http://cn.vuejs.org/ Vue.js——60分钟快速入门 http://www.cnblogs.com/keepfool/p/56 ...

  8. html学习笔记二

    html图片标记 <html> <head> <title>图片演示</title> </head> ----------------图片演 ...

  9. Android - 数据存储 -在SQL数据库中保存数据

    对于重复的或结构化的数据,保存到数据库中是很好的选择,比如联系人信息.这里假设你对SQL数据库大体上了解然后帮助你学习Android上的SQLite数据库.在Android数据库上需要用到的API可以 ...

  10. 计算机管理系统——VB与Excel联系

    今天爆震室管理系统--学生查看机器状态的时候发现有一个"导出到excel"的button.我去.感情还得跟excel表链接. 于是我咬碎了一地小银牙.一个下午都在查询vb与exce ...