📄 req.c
字号:
if (template != NULL) { long errline = -1; if( verbose ) BIO_printf(bio_err,"Using configuration from %s\n",template); req_conf=NCONF_new(NULL); i=NCONF_load(req_conf,template,&errline); if (i == 0) { BIO_printf(bio_err,"error on line %ld of %s\n",errline,template); goto end; } } else { req_conf=config; if (req_conf == NULL) { BIO_printf(bio_err,"Unable to load config info from %s\n", default_config_file); if (newreq) goto end; } else if( verbose ) BIO_printf(bio_err,"Using configuration from %s\n", default_config_file); } if (req_conf != NULL) { if (!load_config(bio_err, req_conf)) goto end; p=NCONF_get_string(req_conf,NULL,"oid_file"); if (p == NULL) ERR_clear_error(); if (p != NULL) { BIO *oid_bio; oid_bio=BIO_new_file(p,"r"); if (oid_bio == NULL) { /* BIO_printf(bio_err,"problems opening %s for extra oid's\n",p); ERR_print_errors(bio_err); */ } else { OBJ_create_objects(oid_bio); BIO_free(oid_bio); } } } if(!add_oid_section(bio_err, req_conf)) goto end; if (md_alg == NULL) { p=NCONF_get_string(req_conf,SECTION,"default_md"); if (p == NULL) ERR_clear_error(); if (p != NULL) { if ((md_alg=EVP_get_digestbyname(p)) != NULL) digest=md_alg; } } if (!extensions) { extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS); if (!extensions) ERR_clear_error(); } if (extensions) { /* Check syntax of file */ X509V3_CTX ctx; X509V3_set_ctx_test(&ctx); X509V3_set_nconf(&ctx, req_conf); if(!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) { BIO_printf(bio_err, "Error Loading extension section %s\n", extensions); goto end; } } if(!passin) { passin = NCONF_get_string(req_conf, SECTION, "input_password"); if (!passin) ERR_clear_error(); } if(!passout) { passout = NCONF_get_string(req_conf, SECTION, "output_password"); if (!passout) ERR_clear_error(); } p = NCONF_get_string(req_conf, SECTION, STRING_MASK); if (!p) ERR_clear_error(); if(p && !ASN1_STRING_set_default_mask_asc(p)) { BIO_printf(bio_err, "Invalid global string mask setting %s\n", p); goto end; } if (chtype != MBSTRING_UTF8) { p = NCONF_get_string(req_conf, SECTION, UTF8_IN); if (!p) ERR_clear_error(); else if (!strcmp(p, "yes")) chtype = MBSTRING_UTF8; } if(!req_exts) { req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS); if (!req_exts) ERR_clear_error(); } if(req_exts) { /* Check syntax of file */ X509V3_CTX ctx; X509V3_set_ctx_test(&ctx); X509V3_set_nconf(&ctx, req_conf); if(!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) { BIO_printf(bio_err, "Error Loading request extension section %s\n", req_exts); goto end; } } in=BIO_new(BIO_s_file()); out=BIO_new(BIO_s_file()); if ((in == NULL) || (out == NULL)) goto end;#ifndef OPENSSL_NO_ENGINE e = setup_engine(bio_err, engine, 0);#endif if (keyfile != NULL) { pkey = load_key(bio_err, keyfile, keyform, 0, passin, e, "Private Key"); if (!pkey) { /* load_key() has already printed an appropriate message */ goto end; } if (EVP_PKEY_type(pkey->type) == EVP_PKEY_DSA || EVP_PKEY_type(pkey->type) == EVP_PKEY_EC) { char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE"); if (randfile == NULL) ERR_clear_error(); app_RAND_load_file(randfile, bio_err, 0); } } if (newreq && (pkey == NULL)) {#ifndef OPENSSL_NO_RSA BN_GENCB cb;#endif char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE"); if (randfile == NULL) ERR_clear_error(); app_RAND_load_file(randfile, bio_err, 0); if (inrand) app_RAND_load_files(inrand); if (newkey <= 0) { if (!NCONF_get_number(req_conf,SECTION,BITS, &newkey)) newkey=DEFAULT_KEY_LENGTH; } if (newkey < MIN_KEY_LENGTH && (pkey_type == TYPE_RSA || pkey_type == TYPE_DSA)) { BIO_printf(bio_err,"private key length is too short,\n"); BIO_printf(bio_err,"it needs to be at least %d bits, not %ld\n",MIN_KEY_LENGTH,newkey); goto end; } BIO_printf(bio_err,"Generating a %ld bit %s private key\n", newkey,(pkey_type == TYPE_RSA)?"RSA": (pkey_type == TYPE_DSA)?"DSA":"EC"); if ((pkey=EVP_PKEY_new()) == NULL) goto end;#ifndef OPENSSL_NO_RSA BN_GENCB_set(&cb, req_cb, bio_err); if (pkey_type == TYPE_RSA) { RSA *rsa = RSA_new(); BIGNUM *bn = BN_new(); if(!bn || !rsa || !BN_set_word(bn, 0x10001) || !RSA_generate_key_ex(rsa, newkey, bn, &cb) || !EVP_PKEY_assign_RSA(pkey, rsa)) { if(bn) BN_free(bn); if(rsa) RSA_free(rsa); goto end; } BN_free(bn); } else#endif#ifndef OPENSSL_NO_DSA if (pkey_type == TYPE_DSA) { if (!DSA_generate_key(dsa_params)) goto end; if (!EVP_PKEY_assign_DSA(pkey,dsa_params)) goto end; dsa_params=NULL; }#endif#ifndef OPENSSL_NO_ECDSA if (pkey_type == TYPE_EC) { if (!EC_KEY_generate_key(ec_params)) goto end; if (!EVP_PKEY_assign_EC_KEY(pkey, ec_params)) goto end; ec_params = NULL; }#endif app_RAND_write_file(randfile, bio_err); if (pkey == NULL) goto end; if (keyout == NULL) { keyout=NCONF_get_string(req_conf,SECTION,KEYFILE); if (keyout == NULL) ERR_clear_error(); } if (keyout == NULL) { BIO_printf(bio_err,"writing new private key to stdout\n"); BIO_set_fp(out,stdout,BIO_NOCLOSE);#ifdef OPENSSL_SYS_VMS { BIO *tmpbio = BIO_new(BIO_f_linebuffer()); out = BIO_push(tmpbio, out); }#endif } else { BIO_printf(bio_err,"writing new private key to '%s'\n",keyout); if (BIO_write_filename(out,keyout) <= 0) { perror(keyout); goto end; } } p=NCONF_get_string(req_conf,SECTION,"encrypt_rsa_key"); if (p == NULL) { ERR_clear_error(); p=NCONF_get_string(req_conf,SECTION,"encrypt_key"); if (p == NULL) ERR_clear_error(); } if ((p != NULL) && (strcmp(p,"no") == 0)) cipher=NULL; if (nodes) cipher=NULL; i=0;loop: if (!PEM_write_bio_PrivateKey(out,pkey,cipher, NULL,0,NULL,passout)) { if ((ERR_GET_REASON(ERR_peek_error()) == PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) { ERR_clear_error(); i++; goto loop; } goto end; } BIO_printf(bio_err,"-----\n"); } if (!newreq) { /* Since we are using a pre-existing certificate * request, the kludge 'format' info should not be * changed. */ kludge= -1; if (infile == NULL) BIO_set_fp(in,stdin,BIO_NOCLOSE); else { if (BIO_read_filename(in,infile) <= 0) { perror(infile); goto end; } } if (informat == FORMAT_ASN1) req=d2i_X509_REQ_bio(in,NULL); else if (informat == FORMAT_PEM) req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL); else { BIO_printf(bio_err,"bad input format specified for X509 request\n"); goto end; } if (req == NULL) { BIO_printf(bio_err,"unable to load X509 request\n"); goto end; } } if (newreq || x509) { if (pkey == NULL) { BIO_printf(bio_err,"you need to specify a private key\n"); goto end; }#ifndef OPENSSL_NO_DSA if (pkey->type == EVP_PKEY_DSA) digest=EVP_dss1();#endif#ifndef OPENSSL_NO_ECDSA if (pkey->type == EVP_PKEY_EC) digest=EVP_ecdsa();#endif if (req == NULL) { req=X509_REQ_new(); if (req == NULL) { goto end; } i=make_REQ(req,pkey,subj,multirdn,!x509, chtype); subj=NULL; /* done processing '-subj' option */ if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes)) { sk_X509_ATTRIBUTE_free(req->req_info->attributes); req->req_info->attributes = NULL; } if (!i) { BIO_printf(bio_err,"problems making Certificate Request\n"); goto end; } } if (x509) { EVP_PKEY *tmppkey; X509V3_CTX ext_ctx; if ((x509ss=X509_new()) == NULL) goto end; /* Set version to V3 */ if(extensions && !X509_set_version(x509ss, 2)) goto end; if (serial) { if (!X509_set_serialNumber(x509ss, serial)) goto end; } else { if (!rand_serial(NULL, X509_get_serialNumber(x509ss))) goto end; } if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req))) goto end; if (!X509_gmtime_adj(X509_get_notBefore(x509ss),0)) goto end; if (!X509_gmtime_adj(X509_get_notAfter(x509ss), (long)60*60*24*days)) goto end; if (!X509_set_subject_name(x509ss, X509_REQ_get_subject_name(req))) goto end; tmppkey = X509_REQ_get_pubkey(req); if (!tmppkey || !X509_set_pubkey(x509ss,tmppkey)) goto end; EVP_PKEY_free(tmppkey); /* Set up V3 context struct */ X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0); X509V3_set_nconf(&ext_ctx, req_conf); /* Add extensions */ if(extensions && !X509V3_EXT_add_nconf(req_conf, &ext_ctx, extensions, x509ss)) { BIO_printf(bio_err, "Error Loading extension section %s\n", extensions); goto end; } if (!(i=X509_sign(x509ss,pkey,digest))) goto end; } else { X509V3_CTX ext_ctx; /* Set up V3 context struct */ X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0); X509V3_set_nconf(&ext_ctx, req_conf); /* Add extensions */ if(req_exts && !X509V3_EXT_REQ_add_nconf(req_conf, &ext_ctx, req_exts, req)) { BIO_printf(bio_err, "Error Loading extension section %s\n", req_exts); goto end; } if (!(i=X509_REQ_sign(req,pkey,digest))) goto end; } } if (subj && x509) { BIO_printf(bio_err, "Cannot modifiy certificate subject\n"); goto end; } if (subj && !x509) { if (verbose) { BIO_printf(bio_err, "Modifying Request's Subject\n"); print_name(bio_err, "old subject=", X509_REQ_get_subject_name(req), nmflag); } if (build_subject(req, subj, chtype, multirdn) == 0) { BIO_printf(bio_err, "ERROR: cannot modify subject\n"); ex=1; goto end; } req->req_info->enc.modified = 1; if (verbose) { print_name(bio_err, "new subject=", X509_REQ_get_subject_name(req), nmflag); } } if (verify && !x509) { int tmp=0; if (pkey == NULL) { pkey=X509_REQ_get_pubkey(req); tmp=1; if (pkey == NULL) goto end; } i=X509_REQ_verify(req,pkey); if (tmp) { EVP_PKEY_free(pkey); pkey=NULL; } if (i < 0) { goto end; } else if (i == 0) { BIO_printf(bio_err,"verify failure\n"); ERR_print_errors(bio_err); } else /* if (i > 0) */ BIO_printf(bio_err,"verify OK\n"); } if (noout && !text && !modulus && !subject && !pubkey) { ex=0; goto end; } if (outfile == NULL) { BIO_set_fp(out,stdout,BIO_NOCLOSE);#ifdef OPENSSL_SYS_VMS { BIO *tmpbio = BIO_new(BIO_f_linebuffer()); out = BIO_push(tmpbio, out); }#endif } else { if ((keyout != NULL) && (strcmp(outfile,keyout) == 0)) i=(int)BIO_append_filename(out,outfile); else i=(int)BIO_write_filename(out,outfile); if (!i) { perror(outfile); goto end; } } if (pubkey) { EVP_PKEY *tpubkey; tpubkey=X509_REQ_get_pubkey(req); if (tpubkey == NULL) { BIO_printf(bio_err,"Error getting public key\n"); ERR_print_errors(bio_err); goto end; } PEM_write_bio_PUBKEY(out, tpubkey); EVP_PKEY_free(tpubkey); } if (text) { if (x509) X509_print_ex(out, x509ss, nmflag, reqflag); else X509_REQ_print_ex(out, req, nmflag, reqflag); } if(subject) { if(x509) print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag); else print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag); } if (modulus) { EVP_PKEY *tpubkey; if (x509) tpubkey=X509_get_pubkey(x509ss); else tpubkey=X509_REQ_get_pubkey(req); if (tpubkey == NULL) { fprintf(stdout,"Modulus=unavailable\n"); goto end; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -