📄 req.c
字号:
fprintf(stdout,"Modulus=");#ifndef OPENSSL_NO_RSA if (tpubkey->type == EVP_PKEY_RSA) BN_print(out,tpubkey->pkey.rsa->n); else#endif fprintf(stdout,"Wrong Algorithm type"); EVP_PKEY_free(tpubkey); fprintf(stdout,"\n"); } if (!noout && !x509) { 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:#ifndef MONOLITH if(to_free) OPENSSL_free(to_free);#endif if (ex) { ERR_print_errors(bio_err); } if ((req_conf != NULL) && (req_conf != config)) NCONF_free(req_conf); BIO_free(in); BIO_free_all(out); EVP_PKEY_free(pkey); X509_REQ_free(req); X509_free(x509ss); ASN1_INTEGER_free(serial); if(passargin && passin) OPENSSL_free(passin); if(passargout && passout) OPENSSL_free(passout); OBJ_cleanup();#ifndef OPENSSL_NO_DSA if (dsa_params != NULL) DSA_free(dsa_params);#endif#ifndef OPENSSL_NO_ECDSA if (ec_params != NULL) EC_KEY_free(ec_params);#endif apps_shutdown(); OPENSSL_EXIT(ex); }static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn, int attribs, unsigned long chtype) { int ret=0,i; char no_prompt = 0; STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL; char *tmp, *dn_sect,*attr_sect; tmp=NCONF_get_string(req_conf,SECTION,PROMPT); if (tmp == NULL) ERR_clear_error(); if((tmp != NULL) && !strcmp(tmp, "no")) no_prompt = 1; dn_sect=NCONF_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=NCONF_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=NCONF_get_string(req_conf,SECTION,ATTRIBUTES); if (attr_sect == NULL) { ERR_clear_error(); attr_sk=NULL; } else { attr_sk=NCONF_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, chtype); else { if (subj) i = build_subject(req, subj, chtype, multirdn); else i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs, chtype); } if(!i) goto err; if (!X509_REQ_set_pubkey(req,pkey)) goto err; ret=1;err: return(ret); }/* * subject is expected to be in the format /type0=value0/type1=value1/type2=... * where characters may be escaped by \ */static int build_subject(X509_REQ *req, char *subject, unsigned long chtype, int multirdn) { X509_NAME *n; if (!(n = parse_name(subject, chtype, multirdn))) return 0; if (!X509_REQ_set_subject_name(req, n)) { X509_NAME_free(n); return 0; } X509_NAME_free(n); return 1;}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, unsigned long chtype) { int i; char *p,*q; char buf[100]; int nid, mval; long n_min,n_max; char *type, *value; const char *def; CONF_VALUE *v; X509_NAME *subj; subj = X509_REQ_get_subject_name(req); if(!batch) { 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 (*type == '+') { mval = -1; type++; } else mval = 0; /* If OBJ not recognised ignore it */ if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start; if (BIO_snprintf(buf,sizeof buf,"%s_default",v->name) >= (int)sizeof(buf)) { BIO_printf(bio_err,"Name '%s' too long\n",v->name); return 0; } if ((def=NCONF_get_string(req_conf,dn_sect,buf)) == NULL) { ERR_clear_error(); def=""; } BIO_snprintf(buf,sizeof buf,"%s_value",v->name); if ((value=NCONF_get_string(req_conf,dn_sect,buf)) == NULL) { ERR_clear_error(); value=NULL; } BIO_snprintf(buf,sizeof buf,"%s_min",v->name); if (!NCONF_get_number(req_conf,dn_sect,buf, &n_min)) { ERR_clear_error(); n_min = -1; } BIO_snprintf(buf,sizeof buf,"%s_max",v->name); if (!NCONF_get_number(req_conf,dn_sect,buf, &n_max)) { ERR_clear_error(); n_max = -1; } if (!add_DN_object(subj,v->value,def,value,nid, n_min,n_max, chtype, mval)) 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) && (!batch)) { 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; if (BIO_snprintf(buf,sizeof buf,"%s_default",type) >= (int)sizeof(buf)) { BIO_printf(bio_err,"Name '%s' too long\n",v->name); return 0; } if ((def=NCONF_get_string(req_conf,attr_sect,buf)) == NULL) { ERR_clear_error(); def=""; } BIO_snprintf(buf,sizeof buf,"%s_value",type); if ((value=NCONF_get_string(req_conf,attr_sect,buf)) == NULL) { ERR_clear_error(); value=NULL; } BIO_snprintf(buf,sizeof buf,"%s_min",type); if (!NCONF_get_number(req_conf,attr_sect,buf, &n_min)) n_min = -1; BIO_snprintf(buf,sizeof buf,"%s_max",type); if (!NCONF_get_number(req_conf,attr_sect,buf, &n_max)) n_max = -1; if (!add_attribute_object(req, v->value,def,value,nid,n_min,n_max, chtype)) 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, unsigned long chtype) { 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++) { int mval; 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; }#ifndef CHARSET_EBCDIC if (*p == '+')#else if (*p == os_toascii['+'])#endif { p++; mval = -1; } else mval = 0; if (!X509_NAME_add_entry_by_txt(subj,type, chtype, (unsigned char *) v->value,-1,-1,mval)) 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, chtype, (unsigned char *)v->value, -1)) return 0; } } return 1; }static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value, int nid, int n_min, int n_max, unsigned long chtype, int mval) { int i,ret=0; MS_STATIC char buf[1024];start: if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def); (void)BIO_flush(bio_err); if(value != NULL) { BUF_strlcpy(buf,value,sizeof buf); BUF_strlcat(buf,"\n",sizeof buf); BIO_printf(bio_err,"%s\n",value); } else { buf[0]='\0'; if (!batch) { fgets(buf,sizeof buf,stdin); } else { buf[0] = '\n'; buf[1] = '\0'; } } if (buf[0] == '\0') return(0); else if (buf[0] == '\n') { if ((def == NULL) || (def[0] == '\0')) return(1); BUF_strlcpy(buf,def,sizeof buf); BUF_strlcat(buf,"\n",sizeof buf); } 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, n_min, n_max)) goto start; if (!X509_NAME_add_entry_by_NID(n,nid, chtype, (unsigned char *) buf, -1,-1,mval)) goto err; ret=1;err: return(ret); }static int add_attribute_object(X509_REQ *req, char *text, const char *def, char *value, int nid, int n_min, int n_max, unsigned long chtype) { int i; static char buf[1024];start: if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def); (void)BIO_flush(bio_err); if (value != NULL) { BUF_strlcpy(buf,value,sizeof buf); BUF_strlcat(buf,"\n",sizeof buf); BIO_printf(bio_err,"%s\n",value); } else { buf[0]='\0'; if (!batch) { fgets(buf,sizeof buf,stdin); } else { buf[0] = '\n'; buf[1] = '\0'; } } if (buf[0] == '\0') return(0); else if (buf[0] == '\n') { if ((def == NULL) || (def[0] == '\0')) return(1); BUF_strlcpy(buf,def,sizeof buf); BUF_strlcat(buf,"\n",sizeof buf); } 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, n_min, n_max)) goto start; if(!X509_REQ_add1_attr_by_NID(req, nid, chtype, (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 OPENSSL_NO_RSAstatic int MS_CALLBACK req_cb(int p, int n, BN_GENCB *cb) { char c='*'; if (p == 0) c='.'; if (p == 1) c='+'; if (p == 2) c='*'; if (p == 3) c='\n'; BIO_write(cb->arg,&c,1); (void)BIO_flush(cb->arg);#ifdef LINT p=n;#endif return 1; }#endifstatic int req_check_len(int len, int n_min, int n_max) { if ((n_min > 0) && (len < n_min)) { BIO_printf(bio_err,"string is too short, it needs to be at least %d bytes long\n",n_min); return(0); } if ((n_max >= 0) && (len > n_max)) { BIO_printf(bio_err,"string is too long, it needs to be less than %d bytes long\n",n_max); return(0); } return(1); }/* Check if the end of a string matches 'end' */static int check_end(const char *str, const char *end){ int elen, slen; const 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 + -