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

📄 x509.c

📁 数字证书处理程序
💻 C
📖 第 1 页 / 共 3 页
字号:
						(j+1 == (int)n)						?'\n':':');					}				}			/* should be in the library */			else if ((sign_flag == i) && (x509req == 0))				{				BIO_printf(bio_err,"Getting Private key\n");				if (Upkey == NULL)					{					Upkey=load_key(bio_err,						keyfile,keyformat, passin);					if (Upkey == NULL) goto end;					}#ifndef NO_DSA		                if (Upkey->type == EVP_PKEY_DSA)		                        digest=EVP_dss1();#endif				assert(need_rand);				if (!sign(x,Upkey,days,clrext,digest,						 extconf, extsect)) goto end;				}			else if (CA_flag == i)				{				BIO_printf(bio_err,"Getting CA Private Key\n");				if (CAkeyfile != NULL)					{					CApkey=load_key(bio_err,						CAkeyfile,CAkeyformat, passin);					if (CApkey == NULL) goto end;					}#ifndef NO_DSA		                if (CApkey->type == EVP_PKEY_DSA)		                        digest=EVP_dss1();#endif								assert(need_rand);				if (!x509_certify(ctx,CAfile,digest,x,xca,					CApkey, CAserial,CA_createserial,days, clrext,					extconf, extsect))					goto end;				}			else if (x509req == i)				{				EVP_PKEY *pk;				BIO_printf(bio_err,"Getting request Private Key\n");				if (keyfile == NULL)					{					BIO_printf(bio_err,"no request key file specified\n");					goto end;					}				else					{					pk=load_key(bio_err,						keyfile,FORMAT_PEM, passin);					if (pk == NULL) goto end;					}				BIO_printf(bio_err,"Generating certificate request\n");#ifndef NO_DSA		                if (pk->type == EVP_PKEY_DSA)		                        digest=EVP_dss1();#endif				rq=X509_to_X509_REQ(x,pk,digest);				EVP_PKEY_free(pk);				if (rq == NULL)					{					ERR_print_errors(bio_err);					goto end;					}				if (!noout)					{					X509_REQ_print(out,rq);					PEM_write_bio_X509_REQ(out,rq);					}				noout=1;				}			}		}	if (checkend)		{		time_t tnow=time(NULL);		if (ASN1_UTCTIME_cmp_time_t(X509_get_notAfter(x), tnow+checkoffset) == -1)			{			BIO_printf(out,"Certificate will expire\n");			ret=1;			}		else			{			BIO_printf(out,"Certificate will not expire\n");			ret=0;			}		goto end;		}	if (noout)		{		ret=0;		goto end;		}	if 	(outformat == FORMAT_ASN1)		i=i2d_X509_bio(out,x);	else if (outformat == FORMAT_PEM)		{		if (trustout) i=PEM_write_bio_X509_AUX(out,x);		else i=PEM_write_bio_X509(out,x);		}	else if (outformat == FORMAT_NETSCAPE)		{		ASN1_HEADER ah;		ASN1_OCTET_STRING os;		os.data=(unsigned char *)NETSCAPE_CERT_HDR;		os.length=strlen(NETSCAPE_CERT_HDR);		ah.header= &os;		ah.data=(char *)x;		ah.meth=X509_asn1_meth();		/* no macro for this one yet */		i=ASN1_i2d_bio(i2d_ASN1_HEADER,out,(unsigned char *)&ah);		}	else	{		BIO_printf(bio_err,"bad output format specified for outfile\n");		goto end;		}	if (!i)		{		BIO_printf(bio_err,"unable to write certificate\n");		ERR_print_errors(bio_err);		goto end;		}	ret=0;end:	if (need_rand)		app_RAND_write_file(NULL, bio_err);	OBJ_cleanup();	CONF_free(extconf);	BIO_free_all(out);	BIO_free_all(STDout);	X509_STORE_free(ctx);	X509_REQ_free(req);	X509_free(x);	X509_free(xca);	EVP_PKEY_free(Upkey);	EVP_PKEY_free(CApkey);	X509_REQ_free(rq);	sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);	sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);	if (passin) OPENSSL_free(passin);	OPENSSL_EXIT(ret);	}static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,	     X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile, int create,	     int days, int clrext, LHASH *conf, char *section)	{	int ret=0;	BIO *io=NULL;	MS_STATIC char buf2[1024];	char *buf=NULL,*p;	BIGNUM *serial=NULL;	ASN1_INTEGER *bs=NULL,bs2;	X509_STORE_CTX xsc;	EVP_PKEY *upkey;	upkey = X509_get_pubkey(xca);	EVP_PKEY_copy_parameters(upkey,pkey);	EVP_PKEY_free(upkey);	X509_STORE_CTX_init(&xsc,ctx,x,NULL);	buf=OPENSSL_malloc(EVP_PKEY_size(pkey)*2+		((serialfile == NULL)			?(strlen(CAfile)+strlen(POSTFIX)+1)			:(strlen(serialfile)))+1);	if (buf == NULL) { BIO_printf(bio_err,"out of mem\n"); goto end; }	if (serialfile == NULL)		{		strcpy(buf,CAfile);		for (p=buf; *p; p++)			if (*p == '.')				{				*p='\0';				break;				}		strcat(buf,POSTFIX);		}	else		strcpy(buf,serialfile);	serial=BN_new();	bs=ASN1_INTEGER_new();	if ((serial == NULL) || (bs == NULL))		{		ERR_print_errors(bio_err);		goto end;		}	io=BIO_new(BIO_s_file());	if (io == NULL)		{		ERR_print_errors(bio_err);		goto end;		}		if (BIO_read_filename(io,buf) <= 0)		{		if (!create)			{			perror(buf);			goto end;			}		else			{			ASN1_INTEGER_set(bs,1);			BN_one(serial);			}		}	else 		{		if (!a2i_ASN1_INTEGER(io,bs,buf2,1024))			{			BIO_printf(bio_err,"unable to load serial number from %s\n",buf);			ERR_print_errors(bio_err);			goto end;			}		else			{			serial=BN_bin2bn(bs->data,bs->length,serial);			if (serial == NULL)				{				BIO_printf(bio_err,"error converting bin 2 bn");				goto end;				}			}		}	if (!BN_add_word(serial,1))		{ BIO_printf(bio_err,"add_word failure\n"); goto end; }	bs2.data=(unsigned char *)buf2;	bs2.length=BN_bn2bin(serial,bs2.data);	if (BIO_write_filename(io,buf) <= 0)		{		BIO_printf(bio_err,"error attempting to write serial number file\n");		perror(buf);		goto end;		}	i2a_ASN1_INTEGER(io,&bs2);	BIO_puts(io,"\n");	BIO_free(io);	io=NULL;		if (!X509_STORE_add_cert(ctx,x)) goto end;	/* NOTE: this certificate can/should be self signed, unless it was	 * a certificate request in which case it is not. */	X509_STORE_CTX_set_cert(&xsc,x);	if (!reqfile && !X509_verify_cert(&xsc))		goto end;	if (!X509_check_private_key(xca,pkey))		{		BIO_printf(bio_err,"CA certificate and CA private key do not match\n");		goto end;		}	if (!X509_set_issuer_name(x,X509_get_subject_name(xca))) goto end;	if (!X509_set_serialNumber(x,bs)) goto end;	if (X509_gmtime_adj(X509_get_notBefore(x),0L) == NULL)		goto end;	/* hardwired expired */	if (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)		goto end;	if (clrext)		{		while (X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);		}	if (conf)		{		X509V3_CTX ctx2;		X509_set_version(x,2); /* version 3 certificate */                X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);                X509V3_set_conf_lhash(&ctx2, conf);                if (!X509V3_EXT_add_conf(conf, &ctx2, section, x)) goto end;		}	if (!X509_sign(x,pkey,digest)) goto end;	ret=1;end:	X509_STORE_CTX_cleanup(&xsc);	if (!ret)		ERR_print_errors(bio_err);	if (buf != NULL) OPENSSL_free(buf);	if (bs != NULL) ASN1_INTEGER_free(bs);	if (io != NULL)	BIO_free(io);	if (serial != NULL) BN_free(serial);	return ret;	}static int MS_CALLBACK callb(int ok, X509_STORE_CTX *ctx)	{	int err;	X509 *err_cert;	/* it is ok to use a self signed certificate	 * This case will catch both the initial ok == 0 and the	 * final ok == 1 calls to this function */	err=X509_STORE_CTX_get_error(ctx);	if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)		return 1;	/* BAD we should have gotten an error.  Normally if everything	 * worked X509_STORE_CTX_get_error(ctx) will still be set to	 * DEPTH_ZERO_SELF_.... */	if (ok)		{		BIO_printf(bio_err,"error with certificate to be certified - should be self signed\n");		return 0;		}	else		{		err_cert=X509_STORE_CTX_get_current_cert(ctx);		print_name(bio_err, NULL, X509_get_subject_name(err_cert),0);		BIO_printf(bio_err,"error with certificate - error %d at depth %d\n%s\n",			err,X509_STORE_CTX_get_error_depth(ctx),			X509_verify_cert_error_string(err));		return 1;		}	}/* self sign */static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, const EVP_MD *digest, 						LHASH *conf, char *section)	{	EVP_PKEY *pktmp;	pktmp = X509_get_pubkey(x);	EVP_PKEY_copy_parameters(pktmp,pkey);	EVP_PKEY_save_parameters(pktmp,1);	EVP_PKEY_free(pktmp);	if (!X509_set_issuer_name(x,X509_get_subject_name(x))) goto err;	if (X509_gmtime_adj(X509_get_notBefore(x),0) == NULL) goto err;	/* Lets just make it 12:00am GMT, Jan 1 1970 */	/* memcpy(x->cert_info->validity->notBefore,"700101120000Z",13); */	/* 28 days to be certified */	if (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)		goto err;	if (!X509_set_pubkey(x,pkey)) goto err;	if (clrext)		{		while (X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);		}	if (conf)		{		X509V3_CTX ctx;		X509_set_version(x,2); /* version 3 certificate */                X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);                X509V3_set_conf_lhash(&ctx, conf);                if (!X509V3_EXT_add_conf(conf, &ctx, section, x)) goto err;		}	if (!X509_sign(x,pkey,digest)) goto err;	return 1;err:	ERR_print_errors(bio_err);	return 0;	}static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt){	int id, i, idret;	char *pname;	id = X509_PURPOSE_get_id(pt);	pname = X509_PURPOSE_get0_name(pt);	for (i = 0; i < 2; i++)		{		idret = X509_check_purpose(cert, id, i);		BIO_printf(bio, "%s%s : ", pname, i ? " CA" : ""); 		if (idret == 1) BIO_printf(bio, "Yes\n");		else if (idret == 0) BIO_printf(bio, "No\n");		else BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);		}	return 1;}

⌨️ 快捷键说明

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