BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
	{
	unsigned int i,m;
	unsigned int n;
	BN_ULONG l;
	if (ret == NULL) ret=BN_new();
	if (ret == NULL) return(NULL);
	l=0;
	n=len;
	if (n == 0)
		{
		ret->top=0;
		return(ret);
		}
	if (bn_expand(ret,(int)(n+2)*8) == NULL)
		return(NULL);
	i=((n-1)/BN_BYTES)+1;
	m=((n-1)%(BN_BYTES));
	ret->top=i;
	while (n-- > 0)
		{
		l=(l<<8L)| *(s++);
		if (m-- == 0)
			{
			ret->d[--i]=l;
			l=0;
			m=BN_BYTES-1;
			}
		}
	bn_fix_top(ret);
	return(ret);
	}
['static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,\n\t     X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile, int create,\n\t     int days, int clrext, LHASH *conf, char *section)\n\t{\n\tint ret=0;\n\tBIO *io=NULL;\n\tMS_STATIC char buf2[1024];\n\tchar *buf=NULL,*p;\n\tBIGNUM *serial=NULL;\n\tASN1_INTEGER *bs=NULL,bs2;\n\tX509_STORE_CTX xsc;\n\tEVP_PKEY *upkey;\n\tupkey = X509_get_pubkey(xca);\n\tEVP_PKEY_copy_parameters(upkey,pkey);\n\tEVP_PKEY_free(upkey);\n\tX509_STORE_CTX_init(&xsc,ctx,x,NULL);\n\tbuf=Malloc(EVP_PKEY_size(pkey)*2+\n\t\t((serialfile == NULL)\n\t\t\t?(strlen(CAfile)+strlen(POSTFIX)+1)\n\t\t\t:(strlen(serialfile)))+1);\n\tif (buf == NULL) { BIO_printf(bio_err,"out of mem\\n"); goto end; }\n\tif (serialfile == NULL)\n\t\t{\n\t\tstrcpy(buf,CAfile);\n\t\tfor (p=buf; *p; p++)\n\t\t\tif (*p == \'.\')\n\t\t\t\t{\n\t\t\t\t*p=\'\\0\';\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\tstrcat(buf,POSTFIX);\n\t\t}\n\telse\n\t\tstrcpy(buf,serialfile);\n\tserial=BN_new();\n\tbs=ASN1_INTEGER_new();\n\tif ((serial == NULL) || (bs == NULL))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tio=BIO_new(BIO_s_file());\n\tif (io == NULL)\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (BIO_read_filename(io,buf) <= 0)\n\t\t{\n\t\tif (!create)\n\t\t\t{\n\t\t\tperror(buf);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tASN1_INTEGER_set(bs,1);\n\t\t\tBN_one(serial);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tif (!a2i_ASN1_INTEGER(io,bs,buf2,1024))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to load serial number from %s\\n",buf);\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tserial=BN_bin2bn(bs->data,bs->length,serial);\n\t\t\tif (serial == NULL)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"error converting bin 2 bn");\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif (!BN_add_word(serial,1))\n\t\t{ BIO_printf(bio_err,"add_word failure\\n"); goto end; }\n\tbs2.data=(unsigned char *)buf2;\n\tbs2.length=BN_bn2bin(serial,bs2.data);\n\tif (BIO_write_filename(io,buf) <= 0)\n\t\t{\n\t\tBIO_printf(bio_err,"error attempting to write serial number file\\n");\n\t\tperror(buf);\n\t\tgoto end;\n\t\t}\n\ti2a_ASN1_INTEGER(io,&bs2);\n\tBIO_puts(io,"\\n");\n\tBIO_free(io);\n\tio=NULL;\n\tif (!X509_STORE_add_cert(ctx,x)) goto end;\n\tX509_STORE_CTX_set_cert(&xsc,x);\n\tif (!reqfile && !X509_verify_cert(&xsc))\n\t\tgoto end;\n\tif (!X509_check_private_key(xca,pkey))\n\t\t{\n\t\tBIO_printf(bio_err,"CA certificate and CA private key do not match\\n");\n\t\tgoto end;\n\t\t}\n\tif (!X509_set_issuer_name(x,X509_get_subject_name(xca))) goto end;\n\tif (!X509_set_serialNumber(x,bs)) goto end;\n\tif (X509_gmtime_adj(X509_get_notBefore(x),0L) == NULL)\n\t\tgoto end;\n\tif (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)\n\t\tgoto end;\n\tif(clrext) {\n\t\twhile(X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);\n\t}\n\tif(conf) {\n\t\tX509V3_CTX ctx2;\n\t\tX509_set_version(x,2);\n                X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);\n                X509V3_set_conf_lhash(&ctx2, conf);\n                if(!X509V3_EXT_add_conf(conf, &ctx2, section, x)) goto end;\n\t}\n\tif (!X509_sign(x,pkey,digest)) goto end;\n\tret=1;\nend:\n\tX509_STORE_CTX_cleanup(&xsc);\n\tif (!ret)\n\t\tERR_print_errors(bio_err);\n\tif (buf != NULL) Free(buf);\n\tif (bs != NULL) ASN1_INTEGER_free(bs);\n\tif (io != NULL)\tBIO_free(io);\n\tif (serial != NULL) BN_free(serial);\n\treturn(ret);\n\t}', 'ASN1_INTEGER *ASN1_INTEGER_new(void)\n{ return M_ASN1_INTEGER_new();}', 'ASN1_STRING *ASN1_STRING_type_new(int type)\n\t{\n\tASN1_STRING *ret;\n\tret=(ASN1_STRING *)Malloc(sizeof(ASN1_STRING));\n\tif (ret == NULL)\n\t\t{\n\t\tASN1err(ASN1_F_ASN1_STRING_TYPE_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->length=0;\n\tret->type=type;\n\tret->data=NULL;\n\tret->flags=0;\n\treturn(ret);\n\t}', "int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size)\n\t{\n\tint ret=0;\n\tint i,j,k,m,n,again,bufsize;\n\tunsigned char *s=NULL,*sp;\n\tunsigned char *bufp;\n\tint num=0,slen=0,first=1;\n\tbs->type=V_ASN1_INTEGER;\n\tbufsize=BIO_gets(bp,buf,size);\n\tfor (;;)\n\t\t{\n\t\tif (bufsize < 1) goto err_sl;\n\t\ti=bufsize;\n\t\tif (buf[i-1] == '\\n') buf[--i]='\\0';\n\t\tif (i == 0) goto err_sl;\n\t\tif (buf[i-1] == '\\r') buf[--i]='\\0';\n\t\tif (i == 0) goto err_sl;\n\t\tagain=(buf[i-1] == '\\\\');\n\t\tfor (j=0; j<i; j++)\n\t\t\t{\n#ifndef CHARSET_EBCDIC\n\t\t\tif (!(\t((buf[j] >= '0') && (buf[j] <= '9')) ||\n\t\t\t\t((buf[j] >= 'a') && (buf[j] <= 'f')) ||\n\t\t\t\t((buf[j] >= 'A') && (buf[j] <= 'F'))))\n#else\n\t\t\tif (!isxdigit(buf[j]))\n#endif\n\t\t\t\t{\n\t\t\t\ti=j;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tbuf[i]='\\0';\n\t\tif (i < 2) goto err_sl;\n\t\tbufp=(unsigned char *)buf;\n\t\tif (first)\n\t\t\t{\n\t\t\tfirst=0;\n\t\t\tif ((bufp[0] == '0') && (buf[1] == '0'))\n\t\t\t\t{\n\t\t\t\tbufp+=2;\n\t\t\t\ti-=2;\n\t\t\t\t}\n\t\t\t}\n\t\tk=0;\n\t\ti-=again;\n\t\tif (i%2 != 0)\n\t\t\t{\n\t\t\tASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_ODD_NUMBER_OF_CHARS);\n\t\t\tgoto err;\n\t\t\t}\n\t\ti/=2;\n\t\tif (num+i > slen)\n\t\t\t{\n\t\t\tif (s == NULL)\n\t\t\t\tsp=(unsigned char *)Malloc(\n\t\t\t\t\t(unsigned int)num+i*2);\n\t\t\telse\n\t\t\t\tsp=(unsigned char *)Realloc(s,\n\t\t\t\t\t(unsigned int)num+i*2);\n\t\t\tif (sp == NULL)\n\t\t\t\t{\n\t\t\t\tASN1err(ASN1_F_A2I_ASN1_INTEGER,ERR_R_MALLOC_FAILURE);\n\t\t\t\tif (s != NULL) Free(s);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\ts=sp;\n\t\t\tslen=num+i*2;\n\t\t\t}\n\t\tfor (j=0; j<i; j++,k+=2)\n\t\t\t{\n\t\t\tfor (n=0; n<2; n++)\n\t\t\t\t{\n\t\t\t\tm=bufp[k+n];\n\t\t\t\tif ((m >= '0') && (m <= '9'))\n\t\t\t\t\tm-='0';\n\t\t\t\telse if ((m >= 'a') && (m <= 'f'))\n\t\t\t\t\tm=m-'a'+10;\n\t\t\t\telse if ((m >= 'A') && (m <= 'F'))\n\t\t\t\t\tm=m-'A'+10;\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_NON_HEX_CHARACTERS);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\ts[num+j]<<=4;\n\t\t\t\ts[num+j]|=m;\n\t\t\t\t}\n\t\t\t}\n\t\tnum+=i;\n\t\tif (again)\n\t\t\tbufsize=BIO_gets(bp,buf,size);\n\t\telse\n\t\t\tbreak;\n\t\t}\n\tbs->length=num;\n\tbs->data=s;\n\tret=1;\nerr:\n\tif (0)\n\t\t{\nerr_sl:\n\t\tASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_SHORT_LINE);\n\t\t}\n\treturn(ret);\n\t}", 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n\t{\n\tunsigned int i,m;\n\tunsigned int n;\n\tBN_ULONG l;\n\tif (ret == NULL) ret=BN_new();\n\tif (ret == NULL) return(NULL);\n\tl=0;\n\tn=len;\n\tif (n == 0)\n\t\t{\n\t\tret->top=0;\n\t\treturn(ret);\n\t\t}\n\tif (bn_expand(ret,(int)(n+2)*8) == NULL)\n\t\treturn(NULL);\n\ti=((n-1)/BN_BYTES)+1;\n\tm=((n-1)%(BN_BYTES));\n\tret->top=i;\n\twhile (n-- > 0)\n\t\t{\n\t\tl=(l<<8L)| *(s++);\n\t\tif (m-- == 0)\n\t\t\t{\n\t\t\tret->d[--i]=l;\n\t\t\tl=0;\n\t\t\tm=BN_BYTES-1;\n\t\t\t}\n\t\t}\n\tbn_fix_top(ret);\n\treturn(ret);\n\t}']