Synchronise VMS build system with the Unixly one
[openssl.git] / apps / req.c
index de1b182fc772ce8169cb25a4535b7f2ca5142916..e728d5bf9511d6c501506393c2453fb42de1f210 100644 (file)
@@ -144,9 +144,9 @@ static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value,
 static int genpkey_cb(EVP_PKEY_CTX *ctx);
 static int req_check_len(int len,int n_min,int n_max);
 static int check_end(const char *str, const char *end);
-static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr,
-                                       long *pkeylen, const char **palgnam,
-                                       ENGINE *e);
+static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr, int *pkey_type,
+                                       long *pkeylen, char **palgnam,
+                                       ENGINE *keygen_engine);
 #ifndef MONOLITH
 static char *default_config_file=NULL;
 #endif
@@ -157,22 +157,17 @@ int MAIN(int, char **);
 
 int MAIN(int argc, char **argv)
        {
-       ENGINE *e = NULL;
-#ifndef OPENSSL_NO_DSA
-       DSA *dsa_params=NULL;
-#endif
-#ifndef OPENSSL_NO_ECDSA
-       EC_KEY *ec_params = NULL;
-#endif
+       ENGINE *e = NULL, *gen_eng = NULL;
        unsigned long nmflag = 0, reqflag = 0;
        int ex=1,x509=0,days=30;
        X509 *x509ss=NULL;
        X509_REQ *req=NULL;
        EVP_PKEY_CTX *genctx = NULL;
-       const char *keyalg = NULL, *keyalgstr;
-       STACK *pkeyopts = NULL;
+       const char *keyalg = NULL;
+       char *keyalgstr = NULL;
+       STACK_OF(STRING) *pkeyopts = NULL;
        EVP_PKEY *pkey=NULL;
-       int i=0,badops=0,newreq=0,verbose=0,pkey_type=EVP_PKEY_RSA;
+       int i=0,badops=0,newreq=0,verbose=0,pkey_type=-1;
        long newkey = -1;
        BIO *in=NULL,*out=NULL;
        int informat,outformat,verify=0,noout=0,text=0,keyform=FORMAT_PEM;
@@ -235,6 +230,16 @@ int MAIN(int argc, char **argv)
                        if (--argc < 1) goto bad;
                        engine= *(++argv);
                        }
+               else if (strcmp(*argv,"-keygen_engine") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       gen_eng = ENGINE_by_id(*(++argv));
+                       if (gen_eng == NULL)
+                               {
+                               BIO_printf(bio_err, "Can't find keygen engine %s\n", *argv);
+                               goto end;
+                               }
+                       }
 #endif
                else if (strcmp(*argv,"-key") == 0)
                        {
@@ -301,8 +306,8 @@ int MAIN(int argc, char **argv)
                        if (--argc < 1)
                                goto bad;
                        if (!pkeyopts)
-                               pkeyopts = sk_new_null();
-                       if (!pkeyopts || !sk_push(pkeyopts, *(++argv)))
+                               pkeyopts = sk_STRING_new_null();
+                       if (!pkeyopts || !sk_STRING_push(pkeyopts, *(++argv)))
                                goto bad;
                        }
                else if (strcmp(*argv,"-batch") == 0)
@@ -612,8 +617,7 @@ bad:
                           message */
                        goto end;
                        }
-               if (EVP_PKEY_type(pkey->type) == EVP_PKEY_DSA || 
-                       EVP_PKEY_type(pkey->type) == EVP_PKEY_EC)
+               else
                        {
                        char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE");
                        if (randfile == NULL)
@@ -633,8 +637,8 @@ bad:
 
                if (keyalg)
                        {
-                       genctx = set_keygen_ctx(bio_err, keyalg, &newkey,
-                                                       &keyalgstr, e);
+                       genctx = set_keygen_ctx(bio_err, keyalg, &pkey_type, &newkey,
+                                                       &keyalgstr, gen_eng);
                        if (!genctx)
                                goto end;
                        }
@@ -654,8 +658,8 @@ bad:
 
                if (!genctx)
                        {
-                       genctx = set_keygen_ctx(bio_err, NULL, &newkey,
-                                                       &keyalgstr, e);
+                       genctx = set_keygen_ctx(bio_err, NULL, &pkey_type, &newkey,
+                                                       &keyalgstr, gen_eng);
                        if (!genctx)
                                goto end;
                        }
@@ -663,9 +667,9 @@ bad:
                if (pkeyopts)
                        {
                        char *genopt;
-                       for (i = 0; i < sk_num(pkeyopts); i++)
+                       for (i = 0; i < sk_STRING_num(pkeyopts); i++)
                                {
-                               genopt = sk_value(pkeyopts, i);
+                               genopt = sk_STRING_value(pkeyopts, i);
                                if (pkey_ctrl_string(genctx, genopt) <= 0)
                                        {
                                        BIO_printf(bio_err,
@@ -834,7 +838,7 @@ loop:
 
                        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_time_adj_ex(X509_get_notAfter(x509ss), days, 0, NULL)) 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;
@@ -1079,19 +1083,19 @@ end:
        if (genctx)
                EVP_PKEY_CTX_free(genctx);
        if (pkeyopts)
-               sk_free(pkeyopts);
+               sk_STRING_free(pkeyopts);
+#ifndef OPENSSL_NO_ENGINE
+       if (gen_eng)
+               ENGINE_free(gen_eng);
+#endif
+       if (keyalgstr)
+               OPENSSL_free(keyalgstr);
        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);
        }
@@ -1565,25 +1569,24 @@ static int check_end(const char *str, const char *end)
        return strcmp(tmp, end);
 }
 
-static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr,
-                                       long *pkeylen, const char **palgnam,
-                                       ENGINE *e)
+static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr, int *pkey_type,
+                                       long *pkeylen, char **palgnam,
+                                       ENGINE *keygen_engine)
        {
        EVP_PKEY_CTX *gctx = NULL;
        EVP_PKEY *param = NULL;
        long keylen = -1;
-       int pkey_type = -1;
        BIO *pbio = NULL;
        const char *paramfile = NULL;
 
        if (gstr == NULL)
                {
-               pkey_type = EVP_PKEY_RSA;
+               *pkey_type = EVP_PKEY_RSA;
                keylen = *pkeylen;
                }
        else if (gstr[0] >= '0' && gstr[0] <= '9')
                {
-               pkey_type = EVP_PKEY_RSA;
+               *pkey_type = EVP_PKEY_RSA;
                keylen = atol(gstr);
                *pkeylen = keylen;
                }
@@ -1593,14 +1596,18 @@ static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr,
                {
                const char *p = strchr(gstr, ':');
                int len;
+               ENGINE *tmpeng;
                const EVP_PKEY_ASN1_METHOD *ameth;
 
                if (p)
                        len = p - gstr;
                else
                        len = strlen(gstr);
+               /* The lookup of a the string will cover all engines so
+                * keep a note of the implementation.
+                */
 
-               ameth = EVP_PKEY_asn1_find_str(gstr, len);
+               ameth = EVP_PKEY_asn1_find_str(&tmpeng, gstr, len);
 
                if (!ameth)
                        {
@@ -1608,9 +1615,13 @@ static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr,
                        return NULL;
                        }
 
-               EVP_PKEY_asn1_get0_info(NULL, &pkey_type, NULL, NULL, NULL,
-                                               ameth);
-               if (pkey_type == EVP_PKEY_RSA)
+               EVP_PKEY_asn1_get0_info(NULL, pkey_type, NULL, NULL, NULL,
+                                                                       ameth);
+#ifndef OPENSSL_NO_ENGINE
+               if (tmpeng)
+                       ENGINE_finish(tmpeng);
+#endif
+               if (*pkey_type == EVP_PKEY_RSA)
                        {
                        if (p)
                                {
@@ -1636,7 +1647,7 @@ static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr,
                if (!param)
                        {
                        X509 *x;
-                       BIO_reset(pbio);
+                       (void)BIO_reset(pbio);
                        x = PEM_read_bio_X509(pbio, NULL, NULL, NULL);
                        if (x)
                                {
@@ -1653,9 +1664,9 @@ static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr,
                                        paramfile);
                        return NULL;
                        }
-               if (pkey_type == -1)
-                       pkey_type = EVP_PKEY_id(param);
-               else if (pkey_type != EVP_PKEY_base_id(param))
+               if (*pkey_type == -1)
+                       *pkey_type = EVP_PKEY_id(param);
+               else if (*pkey_type != EVP_PKEY_base_id(param))
                        {
                        BIO_printf(err, "Key Type does not match parameters\n");
                        EVP_PKEY_free(param);
@@ -1666,24 +1677,30 @@ static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr,
        if (palgnam)
                {
                const EVP_PKEY_ASN1_METHOD *ameth;
-               ameth = EVP_PKEY_asn1_find(pkey_type);
+               ENGINE *tmpeng;
+               const char *anam;
+               ameth = EVP_PKEY_asn1_find(&tmpeng, *pkey_type);
                if (!ameth)
                        {
                        BIO_puts(err, "Internal error: can't find key algorithm\n");
                        return NULL;
                        }
-               EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, palgnam,
-                                               ameth);
+               EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, &anam, ameth);
+               *palgnam = BUF_strdup(anam);
+#ifndef OPENSSL_NO_ENGINE
+               if (tmpeng)
+                       ENGINE_finish(tmpeng);
+#endif
                }
 
        if (param)
                {
-               gctx = EVP_PKEY_CTX_new(param, e);
+               gctx = EVP_PKEY_CTX_new(param, keygen_engine);
                *pkeylen = EVP_PKEY_bits(param);
                EVP_PKEY_free(param);
                }
        else
-               gctx = EVP_PKEY_CTX_new_id(pkey_type, e);
+               gctx = EVP_PKEY_CTX_new_id(*pkey_type, keygen_engine);
 
        if (!gctx)
                {
@@ -1699,7 +1716,7 @@ static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr,
                return NULL;
                }
 
-       if ((pkey_type == EVP_PKEY_RSA) && (keylen != -1))
+       if ((*pkey_type == EVP_PKEY_RSA) && (keylen != -1))
                {
                if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx, keylen) <= 0)
                        {