📄 req.c
字号:
if (outformat == FORMAT_ASN1) i=i2d_X509_REQ_bio(out,req); else if (outformat == FORMAT_PEM) { if(newhdr) i=PEM_write_bio_X509_REQ_NEW(out,req); else i=PEM_write_bio_X509_REQ(out,req); } else { BIO_printf(bio_err,"bad output format specified for outfile\n"); goto end; } if (!i) { BIO_printf(bio_err,"unable to write X509 request\n"); goto end; } } if (!noout && x509 && (x509ss != NULL)) { if (outformat == FORMAT_ASN1) i=i2d_X509_bio(out,x509ss); else if (outformat == FORMAT_PEM) i=PEM_write_bio_X509(out,x509ss); else { BIO_printf(bio_err,"bad output format specified for outfile\n"); goto end; } if (!i) { BIO_printf(bio_err,"unable to write X509 certificate\n"); goto end; } } ex=0;end: if (ex) { ERR_print_errors(bio_err); } if ((req_conf != NULL) && (req_conf != config)) CONF_free(req_conf); BIO_free(in); BIO_free_all(out); EVP_PKEY_free(pkey); X509_REQ_free(req); X509_free(x509ss); if(passargin && passin) OPENSSL_free(passin); if(passargout && passout) OPENSSL_free(passout); OBJ_cleanup();#ifndef NO_DSA if (dsa_params != NULL) DSA_free(dsa_params);#endif OPENSSL_EXIT(ex); }static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, int attribs) { int ret=0,i; char no_prompt = 0; STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL; char *tmp, *dn_sect,*attr_sect; tmp=CONF_get_string(req_conf,SECTION,PROMPT); if((tmp != NULL) && !strcmp(tmp, "no")) no_prompt = 1; dn_sect=CONF_get_string(req_conf,SECTION,DISTINGUISHED_NAME); if (dn_sect == NULL) { BIO_printf(bio_err,"unable to find '%s' in config\n", DISTINGUISHED_NAME); goto err; } dn_sk=CONF_get_section(req_conf,dn_sect); if (dn_sk == NULL) { BIO_printf(bio_err,"unable to get '%s' section\n",dn_sect); goto err; } attr_sect=CONF_get_string(req_conf,SECTION,ATTRIBUTES); if (attr_sect == NULL) attr_sk=NULL; else { attr_sk=CONF_get_section(req_conf,attr_sect); if (attr_sk == NULL) { BIO_printf(bio_err,"unable to get '%s' section\n",attr_sect); goto err; } } /* setup version number */ if (!X509_REQ_set_version(req,0L)) goto err; /* version 1 */ if(no_prompt) i = auto_info(req, dn_sk, attr_sk, attribs); else i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs); if(!i) goto err; if (!X509_REQ_set_pubkey(req,pkey)) goto err; ret=1;err: return(ret); }static int prompt_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect, STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs) { int i; char *p,*q; char buf[100]; int nid,min,max; char *type,*def,*value; CONF_VALUE *v; X509_NAME *subj; subj = X509_REQ_get_subject_name(req); BIO_printf(bio_err,"You are about to be asked to enter information that will be incorporated\n"); BIO_printf(bio_err,"into your certificate request.\n"); BIO_printf(bio_err,"What you are about to enter is what is called a Distinguished Name or a DN.\n"); BIO_printf(bio_err,"There are quite a few fields but you can leave some blank\n"); BIO_printf(bio_err,"For some fields there will be a default value,\n"); BIO_printf(bio_err,"If you enter '.', the field will be left blank.\n"); BIO_printf(bio_err,"-----\n"); if (sk_CONF_VALUE_num(dn_sk)) { i= -1;start: for (;;) { i++; if (sk_CONF_VALUE_num(dn_sk) <= i) break; v=sk_CONF_VALUE_value(dn_sk,i); p=q=NULL; type=v->name; if(!check_end(type,"_min") || !check_end(type,"_max") || !check_end(type,"_default") || !check_end(type,"_value")) continue; /* Skip past any leading X. X: X, etc to allow for * multiple instances */ for(p = v->name; *p ; p++) if ((*p == ':') || (*p == ',') || (*p == '.')) { p++; if(*p) type = p; break; } /* If OBJ not recognised ignore it */ if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start; sprintf(buf,"%s_default",v->name); if ((def=CONF_get_string(req_conf,dn_sect,buf)) == NULL) def=""; sprintf(buf,"%s_value",v->name); if ((value=CONF_get_string(req_conf,dn_sect,buf)) == NULL) value=NULL; sprintf(buf,"%s_min",v->name); min=(int)CONF_get_number(req_conf,dn_sect,buf); sprintf(buf,"%s_max",v->name); max=(int)CONF_get_number(req_conf,dn_sect,buf); if (!add_DN_object(subj,v->value,def,value,nid, min,max)) return 0; } if (X509_NAME_entry_count(subj) == 0) { BIO_printf(bio_err,"error, no objects specified in config file\n"); return 0; } if (attribs) { if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0)) { BIO_printf(bio_err,"\nPlease enter the following 'extra' attributes\n"); BIO_printf(bio_err,"to be sent with your certificate request\n"); } i= -1;start2: for (;;) { i++; if ((attr_sk == NULL) || (sk_CONF_VALUE_num(attr_sk) <= i)) break; v=sk_CONF_VALUE_value(attr_sk,i); type=v->name; if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start2; sprintf(buf,"%s_default",type); if ((def=CONF_get_string(req_conf,attr_sect,buf)) == NULL) def=""; sprintf(buf,"%s_value",type); if ((value=CONF_get_string(req_conf,attr_sect,buf)) == NULL) value=NULL; sprintf(buf,"%s_min",type); min=(int)CONF_get_number(req_conf,attr_sect,buf); sprintf(buf,"%s_max",type); max=(int)CONF_get_number(req_conf,attr_sect,buf); if (!add_attribute_object(req, v->value,def,value,nid,min,max)) return 0; } } } else { BIO_printf(bio_err,"No template, please set one up.\n"); return 0; } return 1; }static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk, STACK_OF(CONF_VALUE) *attr_sk, int attribs) { int i; char *p,*q; char *type; CONF_VALUE *v; X509_NAME *subj; subj = X509_REQ_get_subject_name(req); for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) { v=sk_CONF_VALUE_value(dn_sk,i); p=q=NULL; type=v->name; /* Skip past any leading X. X: X, etc to allow for * multiple instances */ for(p = v->name; *p ; p++) #ifndef CHARSET_EBCDIC if ((*p == ':') || (*p == ',') || (*p == '.')) {#else if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.'])) {#endif p++; if(*p) type = p; break; } if (!X509_NAME_add_entry_by_txt(subj,type, MBSTRING_ASC, (unsigned char *) v->value,-1,-1,0)) return 0; } if (!X509_NAME_entry_count(subj)) { BIO_printf(bio_err,"error, no objects specified in config file\n"); return 0; } if (attribs) { for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) { v=sk_CONF_VALUE_value(attr_sk,i); if(!X509_REQ_add1_attr_by_txt(req, v->name, MBSTRING_ASC, (unsigned char *)v->value, -1)) return 0; } } return 1; }static int add_DN_object(X509_NAME *n, char *text, char *def, char *value, int nid, int min, int max) { int i,ret=0; MS_STATIC char buf[1024];start: BIO_printf(bio_err,"%s [%s]:",text,def); (void)BIO_flush(bio_err); if (value != NULL) { strcpy(buf,value); strcat(buf,"\n"); BIO_printf(bio_err,"%s\n",value); } else { buf[0]='\0'; fgets(buf,1024,stdin); } if (buf[0] == '\0') return(0); else if (buf[0] == '\n') { if ((def == NULL) || (def[0] == '\0')) return(1); strcpy(buf,def); strcat(buf,"\n"); } else if ((buf[0] == '.') && (buf[1] == '\n')) return(1); i=strlen(buf); if (buf[i-1] != '\n') { BIO_printf(bio_err,"weird input :-(\n"); return(0); } buf[--i]='\0';#ifdef CHARSET_EBCDIC ebcdic2ascii(buf, buf, i);#endif if(!req_check_len(i, min, max)) goto start; if (!X509_NAME_add_entry_by_NID(n,nid, MBSTRING_ASC, (unsigned char *) buf, -1,-1,0)) goto err; ret=1;err: return(ret); }static int add_attribute_object(X509_REQ *req, char *text, char *def, char *value, int nid, int min, int max) { int i; static char buf[1024];start: BIO_printf(bio_err,"%s [%s]:",text,def); (void)BIO_flush(bio_err); if (value != NULL) { strcpy(buf,value); strcat(buf,"\n"); BIO_printf(bio_err,"%s\n",value); } else { buf[0]='\0'; fgets(buf,1024,stdin); } if (buf[0] == '\0') return(0); else if (buf[0] == '\n') { if ((def == NULL) || (def[0] == '\0')) return(1); strcpy(buf,def); strcat(buf,"\n"); } else if ((buf[0] == '.') && (buf[1] == '\n')) return(1); i=strlen(buf); if (buf[i-1] != '\n') { BIO_printf(bio_err,"weird input :-(\n"); return(0); } buf[--i]='\0';#ifdef CHARSET_EBCDIC ebcdic2ascii(buf, buf, i);#endif if(!req_check_len(i, min, max)) goto start; if(!X509_REQ_add1_attr_by_NID(req, nid, MBSTRING_ASC, (unsigned char *)buf, -1)) { BIO_printf(bio_err, "Error adding attribute\n"); ERR_print_errors(bio_err); goto err; } return(1);err: return(0); }#ifndef NO_RSAstatic void MS_CALLBACK req_cb(int p, int n, void *arg) { char c='*'; if (p == 0) c='.'; if (p == 1) c='+'; if (p == 2) c='*'; if (p == 3) c='\n'; BIO_write((BIO *)arg,&c,1); (void)BIO_flush((BIO *)arg);#ifdef LINT p=n;#endif }#endifstatic int req_check_len(int len, int min, int max) { if (len < min) { BIO_printf(bio_err,"string is too short, it needs to be at least %d bytes long\n",min); return(0); } if ((max != 0) && (len > max)) { BIO_printf(bio_err,"string is too long, it needs to be less than %d bytes long\n",max); return(0); } return(1); }/* Check if the end of a string matches 'end' */static int check_end(char *str, char *end){ int elen, slen; char *tmp; elen = strlen(end); slen = strlen(str); if(elen > slen) return 1; tmp = str + slen - elen; return strcmp(tmp, end);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -