⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asn1parsepage.cpp

📁 MiniCA V2.0版本源码。《小型CA系统V2.1含源码》发表以来
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	}
	else /* not a UTF8 name */		///////////转UTF-8
	{
		len = ASN1_STRING_to_UTF8(&tmp, str);
	}
	int dstlen = 0;
	Utf8ToAnsi(tmp, len, outName, dstlen);
	OPENSSL_free(tmp);
	return TRUE;
}

int CAsn1ParsePage::asn1_parse2(stuTREE *& TREE, unsigned char **pp, long length, int offset,
				int depth, int indent, int dump,char * msg)
{
	const unsigned char *p,*ep,*tot,*op,*opp;
	long len;
	int tag,xclass,ret=0;
	int nl,hl,j,r;
	ASN1_OBJECT *o=NULL;
	ASN1_OCTET_STRING *os=NULL;
	/* ASN1_BMPSTRING *bmp=NULL;*/
	int dump_indent = 6;	/* Because we know BIO_dump_indent() */
	p= *pp;
	tot=p+length;
	op=p-1;
	char  strInfo[128]={0};
	BUF_MEM *bptr;
	int biolen;
	char * pbuf;
	int index=0,i=0;
	ASN1_STRING * AsnStr;
	char buf[255]={0};

	BIO *mem = BIO_new(BIO_s_mem());
	BIO_set_close(mem, BIO_CLOSE); /*  BIO_free() free BUF_MEM  */

	while ((p < tot) && (op < p))
	{
		i=0;
		op=p;
		j=ASN1_get_object(&p,&len,&tag,&xclass,length);//p指向变化
		if (j & 0x80)
		{
			strcpy(msg,"Error in encoding\n");
			ret=0;
			goto end;
		}
		hl=(p-op);
		length-=hl;
		/* if j == 0x21 it is a constructed indefinite length object */
		if (!asn1_print_info(tag,xclass,j,(indent)?depth:0,strInfo))//得到strInfo(SEQUENCE)
			goto end;
		//增加链表
		index=AddTree(TREE,(long)offset+(long)(op- *pp),depth,len,strInfo);
		if (j & V_ASN1_CONSTRUCTED)
		{
			ep=p+len;
			if (len > length)
			{
				sprintf(msg,"length is greater than %ld",length);
				ret=0;
				goto end;
			}
			if ((j == 0x21) && (len == 0))
			{
				for (;;)
				{
					r=asn1_parse2(TREE,(UCHAR **)&p,(long)(tot-p),
						offset+(p - *pp),depth+1,
						indent,dump,msg);
					if (r == 0) { ret=0; goto end; }
					if ((r == 2) || (p >= tot)) break;
				}
			}
			else
				while (p < ep)
				{
					r=asn1_parse2(TREE,(UCHAR **)&p,(long)len,
						offset+(p - *pp),depth+1,
						indent,dump,msg);
					if (r == 0) { ret=0; goto end; }
				}
		}
		else if (xclass != 0)
		{
			p+=len;
		}
		else
		{
			nl=0;
			if (	(tag == V_ASN1_PRINTABLESTRING) ||
				(tag == V_ASN1_T61STRING) ||
				(tag == V_ASN1_IA5STRING) ||
				(tag == V_ASN1_VISIBLESTRING) ||
				(tag == V_ASN1_UTCTIME) ||
				(tag == V_ASN1_GENERALIZEDTIME))
			{
				if ((len > 0) &&
					BIO_write(mem,(char *)p,(int)len)
					!= (int)len)
					goto end;
			}
			else if (tag == V_ASN1_OBJECT)
			{
				opp=op;
				if (d2i_ASN1_OBJECT(&o,&opp,len+hl) != NULL)
				{
					i2a_ASN1_OBJECT(mem,o);
				}
				else
				{
					if (BIO_write(mem,":BAD OBJECT",11) <= 0)
						goto end;
				}
			}
			else if (tag == V_ASN1_BOOLEAN)
			{
				int ii;		
				opp=op;
				ii=d2i_ASN1_BOOLEAN(NULL,&opp,len+hl);
				if (ii < 0)
				{
					if (BIO_write(mem,"Bad boolean\n",12))
						goto end;
				}
				BIO_printf(mem,":%d",ii);

			}
			else if (tag == V_ASN1_BMPSTRING||tag ==V_ASN1_UTF8STRING)
			{
				AsnStr=ASN1_STRING_type_new(tag);
				ASN1_STRING_set(AsnStr,p,len);
				GetAsn1Str(AsnStr,buf);
				ASN1_STRING_free(AsnStr);

				if (BIO_write(mem,buf,strlen(buf)) < 0)
					goto end;

			}
			else if (tag == V_ASN1_OCTET_STRING)
			{
				int i,printable=1;
				opp=op;
				os=d2i_ASN1_OCTET_STRING(NULL,&opp,len+hl);
				if (os != NULL)
				{
					opp=os->data;
					for (i=0; i<os->length; i++)
					{
						if ((	(opp[i] < ' ') &&
							(opp[i] != '\n') &&
							(opp[i] != '\r') &&
							(opp[i] != '\t')) ||
							(opp[i] > '~'))
						{
							printable=0;
							break;
						}
					}
					if (printable && (os->length > 0))
					{
						if (BIO_write(mem,(char *)opp,
							os->length) <= 0)
							goto end;
					}
					if (!printable && (os->length > 0)
						&& dump)
					{
						if (!nl) 
						{
						}
						if (BIO_dump_indent(mem,(char *)opp,
							((dump == -1 || dump > os->length)?os->length:dump),
							dump_indent) <= 0)
							goto end;
						nl=1;
					}
					M_ASN1_OCTET_STRING_free(os);
					os=NULL;
				}
			}
			else if (tag == V_ASN1_INTEGER)
			{
				ASN1_INTEGER *bs;
				int i;
				
				opp=op;
				bs=d2i_ASN1_INTEGER(NULL,&opp,len+hl);
				if (bs != NULL)
				{
					for (i=0; i<bs->length; i++)
					{
						if (BIO_printf(mem,"%02X",bs->data[i]) <= 0)
							goto end;
					}
					if (bs->length == 0)
					{
						if (BIO_write(mem,"00",2) <= 0)
							goto end;
					}
				}
				else
				{
					if (BIO_write(mem,"BAD INTEGER",11) <= 0)
						goto end;
				}
				M_ASN1_INTEGER_free(bs);
			}
			else if (tag == V_ASN1_ENUMERATED)
			{
				ASN1_ENUMERATED *bs;
				int i;
				
				opp=op;
				bs=d2i_ASN1_ENUMERATED(NULL,&opp,len+hl);
				if (bs != NULL)
				{
					if (bs->type == V_ASN1_NEG_ENUMERATED)
						if (BIO_write(mem,"-",1) <= 0)
						for (i=0; i<bs->length; i++)
						{
							if (BIO_printf(mem,"%02X",bs->data[i]) <= 0)
								goto end;

						}
						if (bs->length == 0)
						{
							if (BIO_write(mem,"00",2) <= 0)
								goto end;
						}
				}
				else
				{
					if (BIO_write(mem,"BAD ENUMERATED",11) <= 0)
						goto end;
				}
				M_ASN1_ENUMERATED_free(bs);
			}
			else if (len > 0 && dump)
			{
				if (!nl) 
				{
				}
				if (BIO_dump_indent(mem,(char *)p,
					((dump == -1 || dump > len)?len:dump),
					dump_indent) <= 0)
					goto end;
				nl=1;
			}
			
			p+=len;
			if ((tag == V_ASN1_EOC) && (xclass == 0))
			{
				ret=2; // End of sequence 
				goto end;
			}
		}
		length-=len;
		if((p < tot) && (op < p))
		{
			BIO_get_mem_ptr(mem, &bptr);
//			BIO_get_mem_ptr(mem, &bptr);
			biolen=bptr->length;
			pbuf=new char[biolen+1];
			memset(pbuf,0,biolen+1);
			memcpy(pbuf,bptr->data,biolen);
			for(stuTREE * temp=TREE;temp!=NULL;temp=temp->Link,i++)
			{
				if(i==index)
				{
					strncpy(temp->INFO,pbuf,biolen+1>sizeof(temp->INFO)?128:biolen+1);
					break;
				}
			}
			delete [] pbuf;
			BIO_reset(mem);//恢复bio
		}
	}
	ret=1;
end:
		BIO_get_mem_ptr(mem, &bptr);
//		BIO_get_mem_ptr(mem, &bptr);
		biolen=bptr->length;
		pbuf=new char[biolen+1];
		memset(pbuf,0,biolen+1);
		memcpy(pbuf,bptr->data,biolen);
		for(stuTREE * temp=TREE;temp!=NULL;temp=temp->Link,i++)
		{
			if(i==index)
			{
				strncpy(temp->INFO,pbuf,biolen+1>sizeof(temp->INFO)?128:biolen+1);
				break;
			}
		}

		delete [] pbuf;
		if (mem != NULL) BIO_free(mem);
		if (o != NULL) ASN1_OBJECT_free(o);
		if (os != NULL) M_ASN1_OCTET_STRING_free(os);
		*pp=(UCHAR *)p;
		return(ret);
}

int CAsn1ParsePage::ASN1_parsedump(stuTREE *& TREE, unsigned char *pp, long len,char * msg)
{
	int indent=0;//不用格式化根据输出的数据自动缩进(不使用-i参数)
	int dump= 0;//输出16进制数据(0不使用 -dump参数,-1使用)
	return(asn1_parse2(TREE,&pp,len,0,0,indent,dump,msg));
}

BOOL CAsn1ParsePage::Asn1Parse(const char * infile,stuTREE *& TREE,const int informat,char * msg)
{
	int i,badops=0,ret=0;//offset=0,,j
	unsigned int length=0;
	long num;
	BIO *in=NULL,*b64=NULL;
	int indent=0;
	char *str=NULL;
	BUF_MEM *buf=NULL;

	OpenSSL_add_all_algorithms();
	in=BIO_new(BIO_s_file());
	if (in == NULL)
	{
		sprintf(msg,"opne %s error",infile);
		goto end;
	}

	if (BIO_read_filename(in,infile) <= 0)
	{
		strcpy(msg,"read file error");
		goto end;
	}

	if ((buf=BUF_MEM_new()) == NULL) goto end;
	if (!BUF_MEM_grow(buf,BUFSIZ*8)) goto end; /* Pre-allocate :-) changes the size */

	if (informat == PEM)//PEM格式的先解码为der
	{
		BIO *tmp;
		if ((b64=BIO_new(BIO_f_base64())) == NULL)
			goto end;
		BIO_push(b64,in);//该函数把in附加到b64上,并返回b64,形成b64-in链
		tmp=in;
		in=b64;
		b64=tmp;
	}

	num=0;
	for (;;)
	{
		if (!BUF_MEM_grow(buf,(int)num+BUFSIZ)) goto end;
		try
		{
			i = BIO_read(in,&(buf->data[num]),BUFSIZ);
		}
		catch(...)
		{
			return FALSE;
		}
		if (i <= 0) break;
		num+=i;
	}
	str=buf->data;

	/* If any structs to parse go through in sequence */

	if (length == 0) length = (unsigned int)num;
	if (length == 0) 
		goto end;
	if (!ASN1_parsedump(TREE,(unsigned char *)&(str)[0],length,msg))//str[offset]
	{
		strcpy(msg,"parse file error");
		goto end;
	}
	ret=1;
end:
	if (in != NULL) BIO_free(in);
	if (b64 != NULL) BIO_free(b64);
	if (ret == 0)
		strncpy(msg, MiniCT_1608, 20); //解码错误
	if (buf != NULL) BUF_MEM_free(buf);
	OBJ_cleanup();
	EVP_cleanup();
	return ret;
}

void CAsn1ParsePage::TranslateCT()	//繙譯諸如樹型控件,列錶控件等內容
{
	SetDlgItemText(IDC_B_READ, MiniCT_10001); //
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -