apps/pkcs12.c:352: error: BUFFER_OVERRUN_L3
  Offset: [1, +oo] Size: [0, +oo] by call to `sk_value`.
Showing all 7 steps of the trace


apps/pkcs12.c:351:8: Assignment
349.   		}
350.   		/* Exclude verified certificate */
351.   		for (i = 1; i < sk_num (chain2) ; i++) 
              ^
352.   				 sk_push(certs, sk_value (chain2, i));
353.   		sk_free(chain2);

apps/pkcs12.c:352:21: Call
350.   		/* Exclude verified certificate */
351.   		for (i = 1; i < sk_num (chain2) ; i++) 
352.   				 sk_push(certs, sk_value (chain2, i));
                           ^
353.   		sk_free(chain2);
354.   			

crypto/stack/stack.c:293:1: <Offset trace>
291.   }
292.   
293. > char *sk_value(STACK *st, int i)
294.   {
295.   	if(st == NULL) return NULL;

crypto/stack/stack.c:293:1: Parameter `i`
  291.   }
  292.   
  293. > char *sk_value(STACK *st, int i)
  294.   {
  295.   	if(st == NULL) return NULL;

crypto/stack/stack.c:293:1: <Length trace>
291.   }
292.   
293. > char *sk_value(STACK *st, int i)
294.   {
295.   	if(st == NULL) return NULL;

crypto/stack/stack.c:293:1: Parameter `*st->data`
  291.   }
  292.   
  293. > char *sk_value(STACK *st, int i)
  294.   {
  295.   	if(st == NULL) return NULL;

crypto/stack/stack.c:296:9: Array access: Offset: [1, +oo] Size: [0, +oo] by call to `sk_value` 
  294.   {
  295.   	if(st == NULL) return NULL;
  296.   	return st->data[i];
                 ^
  297.   }
  298.   

char *sk_value(STACK *st, int i)
{
	if(st == NULL) return NULL;
	return st->data[i];
}
['int MAIN(int argc, char **argv)\n{\n    char *infile=NULL, *outfile=NULL, *keyname = NULL;\n    char *certfile=NULL;\n    BIO *in=NULL, *out = NULL, *inkey = NULL, *certsin = NULL;\n    char **args;\n    char *name = NULL;\n    PKCS12 *p12 = NULL;\n    char pass[50], macpass[50];\n    int export_cert = 0;\n    int options = 0;\n    int chain = 0;\n    int badarg = 0;\n    int iter = PKCS12_DEFAULT_ITER;\n    int maciter = 1;\n    int twopass = 0;\n    int keytype = 0;\n    int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;\n    int ret = 1;\n    int macver = 1;\n    int noprompt = 0;\n    STACK *canames = NULL;\n    char *cpass = NULL, *mpass = NULL;\n    apps_startup();\n    enc = EVP_des_ede3_cbc();\n    if (bio_err == NULL ) bio_err = BIO_new_fp (stderr, BIO_NOCLOSE);\n    args = argv + 1;\n    while (*args) {\n\tif (*args[0] == \'-\') {\n\t\tif (!strcmp (*args, "-nokeys")) options |= NOKEYS;\n\t\telse if (!strcmp (*args, "-keyex")) keytype = KEY_EX;\n\t\telse if (!strcmp (*args, "-keysig")) keytype = KEY_SIG;\n\t\telse if (!strcmp (*args, "-nocerts")) options |= NOCERTS;\n\t\telse if (!strcmp (*args, "-clcerts")) options |= CLCERTS;\n\t\telse if (!strcmp (*args, "-cacerts")) options |= CACERTS;\n\t\telse if (!strcmp (*args, "-noout")) options |= (NOKEYS|NOCERTS);\n\t\telse if (!strcmp (*args, "-info")) options |= INFO;\n\t\telse if (!strcmp (*args, "-chain")) chain = 1;\n\t\telse if (!strcmp (*args, "-twopass")) twopass = 1;\n\t\telse if (!strcmp (*args, "-nomacver")) macver = 0;\n\t\telse if (!strcmp (*args, "-descert"))\n    \t\t\tcert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;\n\t\telse if (!strcmp (*args, "-export")) export_cert = 1;\n\t\telse if (!strcmp (*args, "-des")) enc=EVP_des_cbc();\n#ifndef NO_IDEA\n\t\telse if (!strcmp (*args, "-idea")) enc=EVP_idea_cbc();\n#endif\n\t\telse if (!strcmp (*args, "-des3")) enc = EVP_des_ede3_cbc();\n\t\telse if (!strcmp (*args, "-noiter")) iter = 1;\n\t\telse if (!strcmp (*args, "-maciter"))\n\t\t\t\t\t maciter = PKCS12_DEFAULT_ITER;\n\t\telse if (!strcmp (*args, "-nodes")) enc=NULL;\n\t\telse if (!strcmp (*args, "-inkey")) {\n\t\t    if (args[1]) {\n\t\t\targs++;\n\t\t\tkeyname = *args;\n\t\t    } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-certfile")) {\n\t\t    if (args[1]) {\n\t\t\targs++;\n\t\t\tcertfile = *args;\n\t\t    } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-name")) {\n\t\t    if (args[1]) {\n\t\t\targs++;\n\t\t\tname = *args;\n\t\t    } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-caname")) {\n\t\t    if (args[1]) {\n\t\t\targs++;\n\t\t\tif (!canames) canames = sk_new(NULL);\n\t\t\tsk_push(canames, *args);\n\t\t    } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-in")) {\n\t\t    if (args[1]) {\n\t\t\targs++;\n\t\t\tinfile = *args;\n\t\t    } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-out")) {\n\t\t    if (args[1]) {\n\t\t\targs++;\n\t\t\toutfile = *args;\n\t\t    } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-envpass")) {\n\t\t    if (args[1]) {\n\t\t\targs++;\n\t\t\tif(!(cpass = getenv(*args))) {\n\t\t\t\tBIO_printf(bio_err,\n\t\t\t\t "Can\'t read environment variable %s\\n", *args);\n\t\t\t\tgoto end;\n\t\t\t}\n\t\t\tnoprompt = 1;\n\t\t    } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-password")) {\n\t\t    if (args[1]) {\n\t\t\targs++;\n\t\t\tcpass = *args;\n\t\t    \tnoprompt = 1;\n\t\t    } else badarg = 1;\n\t\t} else badarg = 1;\n\t} else badarg = 1;\n\targs++;\n    }\n    if (badarg) {\n\tBIO_printf (bio_err, "Usage: pkcs12 [options]\\n");\n\tBIO_printf (bio_err, "where options are\\n");\n\tBIO_printf (bio_err, "-export       output PKCS12 file\\n");\n\tBIO_printf (bio_err, "-chain        add certificate chain\\n");\n\tBIO_printf (bio_err, "-inkey file   private key if not infile\\n");\n\tBIO_printf (bio_err, "-certfile f   add all certs in f\\n");\n\tBIO_printf (bio_err, "-name \\"name\\"  use name as friendly name\\n");\n\tBIO_printf (bio_err, "-caname \\"nm\\"  use nm as CA friendly name (can be used more than once).\\n");\n\tBIO_printf (bio_err, "-in  infile   input filename\\n");\n\tBIO_printf (bio_err, "-out outfile  output filename\\n");\n\tBIO_printf (bio_err, "-noout        don\'t output anything, just verify.\\n");\n\tBIO_printf (bio_err, "-nomacver     don\'t verify MAC.\\n");\n\tBIO_printf (bio_err, "-nocerts      don\'t output certificates.\\n");\n\tBIO_printf (bio_err, "-clcerts      only output client certificates.\\n");\n\tBIO_printf (bio_err, "-cacerts      only output CA certificates.\\n");\n\tBIO_printf (bio_err, "-nokeys       don\'t output private keys.\\n");\n\tBIO_printf (bio_err, "-info         give info about PKCS#12 structure.\\n");\n\tBIO_printf (bio_err, "-des          encrypt private keys with DES\\n");\n\tBIO_printf (bio_err, "-des3         encrypt private keys with triple DES (default)\\n");\n#ifndef NO_IDEA\n\tBIO_printf (bio_err, "-idea         encrypt private keys with idea\\n");\n#endif\n\tBIO_printf (bio_err, "-nodes        don\'t encrypt private keys\\n");\n\tBIO_printf (bio_err, "-noiter       don\'t use encryption iteration\\n");\n\tBIO_printf (bio_err, "-maciter      use MAC iteration\\n");\n\tBIO_printf (bio_err, "-twopass      separate MAC, encryption passwords\\n");\n\tBIO_printf (bio_err, "-descert      encrypt PKCS#12 certificates with triple DES (default RC2-40)\\n");\n\tBIO_printf (bio_err, "-keyex        set MS key exchange type\\n");\n\tBIO_printf (bio_err, "-keysig       set MS key signature type\\n");\n\tBIO_printf (bio_err, "-password p   set import/export password (NOT RECOMMENDED)\\n");\n\tBIO_printf (bio_err, "-envpass p    set import/export password from environment\\n");\n    \tgoto end;\n    }\n    if(cpass) mpass = cpass;\n    else {\n\tcpass = pass;\n\tmpass = macpass;\n    }\n    ERR_load_crypto_strings();\n    in = BIO_new (BIO_s_file());\n    out = BIO_new (BIO_s_file());\n    if (!infile) BIO_set_fp (in, stdin, BIO_NOCLOSE);\n    else {\n        if (BIO_read_filename (in, infile) <= 0) {\n\t    perror (infile);\n\t    goto end;\n\t}\n    }\n   if (certfile) {\n    \tcertsin = BIO_new (BIO_s_file());\n        if (BIO_read_filename (certsin, certfile) <= 0) {\n\t    perror (certfile);\n\t    goto end;\n\t}\n    }\n    if (keyname) {\n    \tinkey = BIO_new (BIO_s_file());\n        if (BIO_read_filename (inkey, keyname) <= 0) {\n\t    perror (keyname);\n\t    goto end;\n\t}\n     }\n    if (!outfile) BIO_set_fp (out, stdout, BIO_NOCLOSE);\n    else {\n        if (BIO_write_filename (out, outfile) <= 0) {\n\t    perror (outfile);\n\t    goto end;\n\t}\n    }\n    if (twopass) {\n\tif(EVP_read_pw_string (macpass, 50, "Enter MAC Password:", export_cert)) {\n    \t    BIO_printf (bio_err, "Can\'t read Password\\n");\n    \t    goto end;\n       \t}\n    }\nif (export_cert) {\n\tEVP_PKEY *key;\n\tSTACK *bags, *safes;\n\tPKCS12_SAFEBAG *bag;\n\tPKCS8_PRIV_KEY_INFO *p8;\n\tPKCS7 *authsafe;\n\tX509 *cert, *ucert = NULL;\n\tSTACK *certs;\n\tchar *catmp;\n\tint i, pmatch = 0;\n\tunsigned char keyid[EVP_MAX_MD_SIZE];\n\tunsigned int keyidlen;\n\tkey = PEM_read_bio_PrivateKey(inkey ? inkey : in, NULL, NULL);\n\tif (!inkey) BIO_reset(in);\n\tif (!key) {\n\t\tBIO_printf (bio_err, "Error loading private key\\n");\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t}\n\tcerts = sk_new(NULL);\n\tif(!cert_load(in, certs)) {\n\t\tBIO_printf(bio_err, "Error loading certificates from input\\n");\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t}\n\tbags = sk_new (NULL);\n\tif (certsin) {\n\t\tif(!cert_load(certsin, certs)) {\n\t\t\tBIO_printf(bio_err, "Error loading certificates from certfile\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t}\n\t    \tBIO_free(certsin);\n \t}\n\tfor(i = 0; i < sk_num(certs); i++) {\n\t\t\tcert = (X509 *)sk_value(certs, i);\n\t\t\tif(X509_check_private_key(cert, key)) {\n\t\t\t\tucert = cert;\n\t\t\t\tbreak;\n\t\t\t}\n\t}\n\tif(!ucert) {\n\t\tBIO_printf(bio_err, "No certificate matches private key\\n");\n\t\tgoto end;\n\t}\n\tif (chain) {\n        \tint vret;\n\t\tSTACK *chain2;\n\t\tvret = get_cert_chain (ucert, &chain2);\n\t\tif (vret) {\n\t\t\tBIO_printf (bio_err, "Error %s getting chain.\\n",\n\t\t\t\t\tX509_verify_cert_error_string(vret));\n\t\t\tgoto end;\n\t\t}\n\t\tfor (i = 1; i < sk_num (chain2) ; i++)\n\t\t\t\t sk_push(certs, sk_value (chain2, i));\n\t\tsk_free(chain2);\n    \t}\n\tfor(i = 0; i < sk_num(certs); i++) {\n\t\tcert = (X509 *)sk_value(certs, i);\n\t\tbag = M_PKCS12_x5092certbag(cert);\n\t\tif(cert == ucert) {\n\t\t\tif(name) PKCS12_add_friendlyname(bag, name, -1);\n\t\t\tX509_digest(cert, EVP_sha1(), keyid, &keyidlen);\n\t\t\tPKCS12_add_localkeyid(bag, keyid, keyidlen);\n\t\t\tpmatch = 1;\n\t\t} else if((catmp = sk_shift(canames)))\n\t\t\t\tPKCS12_add_friendlyname(bag, catmp, -1);\n\t\tsk_push(bags, (char *)bag);\n\t}\n\tif (canames) sk_free(canames);\n\tif(!noprompt &&\n\t\tEVP_read_pw_string(pass, 50, "Enter Export Password:", 1)) {\n\t    BIO_printf (bio_err, "Can\'t read Password\\n");\n\t    goto end;\n        }\n\tif (!twopass) strcpy(macpass, pass);\n\tauthsafe = PKCS12_pack_p7encdata (cert_pbe, cpass, -1, NULL, 0,\n\t\t\t\t\t\t\t\t iter, bags);\n\tsk_pop_free(bags, PKCS12_SAFEBAG_free);\n\tif (!authsafe) {\n\t\tERR_print_errors (bio_err);\n\t\tgoto end;\n\t}\n\tsafes = sk_new (NULL);\n\tsk_push (safes, (char *)authsafe);\n\tp8 = EVP_PKEY2PKCS8 (key);\n\tEVP_PKEY_free(key);\n\tif(keytype) PKCS8_add_keyusage(p8, keytype);\n\tbag = PKCS12_MAKE_SHKEYBAG (NID_pbe_WithSHA1And3_Key_TripleDES_CBC,\n\t\t\tcpass, -1, NULL, 0, iter, p8);\n\tPKCS8_PRIV_KEY_INFO_free(p8);\n        if (name) PKCS12_add_friendlyname (bag, name, -1);\n\tif(pmatch) PKCS12_add_localkeyid (bag, keyid, keyidlen);\n\tbags = sk_new(NULL);\n\tsk_push (bags, (char *)bag);\n\tauthsafe = PKCS12_pack_p7data (bags);\n\tsk_pop_free(bags, PKCS12_SAFEBAG_free);\n\tsk_push (safes, (char *)authsafe);\n\tp12 = PKCS12_init (NID_pkcs7_data);\n\tM_PKCS12_pack_authsafes (p12, safes);\n\tsk_pop_free(safes, PKCS7_free);\n\tPKCS12_set_mac (p12, mpass, -1, NULL, 0, maciter, NULL);\n\ti2d_PKCS12_bio (out, p12);\n\tPKCS12_free(p12);\n\tret = 0;\n\tgoto end;\n    }\n    if (!(p12 = d2i_PKCS12_bio (in, NULL))) {\n\tERR_print_errors(bio_err);\n\tgoto end;\n    }\n    if(!noprompt && EVP_read_pw_string(pass, 50, "Enter Import Password:", 0)) {\n\tBIO_printf (bio_err, "Can\'t read Password\\n");\n\tgoto end;\n    }\n    if (!twopass) strcpy(macpass, pass);\n    if (options & INFO) BIO_printf (bio_err, "MAC Iteration %ld\\n", p12->mac->iter ? ASN1_INTEGER_get (p12->mac->iter) : 1);\n    if(macver) {\n\tif (!PKCS12_verify_mac (p12, mpass, -1)) {\n\t    BIO_printf (bio_err, "Mac verify errror: invalid password?\\n");\n\t    ERR_print_errors (bio_err);\n\t    goto end;\n\t} else BIO_printf (bio_err, "MAC verified OK\\n");\n    }\n    if (!dump_certs_keys_p12 (out, p12, cpass, -1, options)) {\n\tBIO_printf(bio_err, "Error outputting keys and certificates\\n");\n\tERR_print_errors (bio_err);\n\tgoto end;\n    }\n    PKCS12_free(p12);\n    ret = 0;\n    end:\n    EXIT(ret);\n}', 'char *sk_value(STACK *st, int i)\n{\n\tif(st == NULL) return NULL;\n\treturn st->data[i];\n}']