crypto/pem/pem_info.c:244: error: BUFFER_OVERRUN_L3
  Offset: [2, +oo] Size: [1, 2147483644] by call to `d2i_PrivateKey`.
Showing all 28 steps of the trace


crypto/pem/pem_info.c:118:13: Call
116.           raw = 0;
117.           ptype = 0;
118.           i = PEM_read_bio(bp, &name, &header, &data, &len);
                   ^
119.           if (i == 0) {
120.               error = ERR_GET_REASON(ERR_peek_last_error());

crypto/pem/pem_lib.c:677:1: Parameter `**data`
  675.   #endif
  676.   
  677. > int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
  678.                    long *len)
  679.   {

crypto/pem/pem_info.c:242:17: Assignment
240.                   if (!PEM_do_header(&cipher, data, &len, cb, u))
241.                       goto err;
242.                   p = data;
                       ^
243.                   if (ptype) {
244.                       if (!d2i_PrivateKey(ptype, pp, &p, len)) {

crypto/pem/pem_info.c:244:26: Call
242.                   p = data;
243.                   if (ptype) {
244.                       if (!d2i_PrivateKey(ptype, pp, &p, len)) {
                                ^
245.                           PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB);
246.                           goto err;

crypto/asn1/d2i_pr.c:72:1: Parameter `**pp`
  70.   #include "internal/evp_int.h"
  71.   
  72. > EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
  73.                            long length)
  74.   {

crypto/asn1/d2i_pr.c:76:5: Assignment
  74.   {
  75.       EVP_PKEY *ret;
  76.       const unsigned char *p = *pp;
            ^
  77.   
  78.       if ((a == NULL) || (*a == NULL)) {

crypto/asn1/d2i_pr.c:102:18: Call
  100.           if (ret->ameth->priv_decode) {
  101.               PKCS8_PRIV_KEY_INFO *p8 = NULL;
  102.               p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
                          ^
  103.               if (!p8)
  104.                   goto err;

crypto/asn1/p8_pkey.c:87:1: Parameter `**in`
    85.   } ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
    86.   
    87. > IMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO)
    88.   
    89.   int PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj,

crypto/asn1/p8_pkey.c:87:1: Call
    85.   } ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
    86.   
    87. > IMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO)
    88.   
    89.   int PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj,

crypto/asn1/tasn_dec.c:144:1: Parameter `**in`
      142.    */
      143.   
      144. > ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
      145.                             const unsigned char **in, long len,
      146.                             const ASN1_ITEM *it)

crypto/asn1/tasn_dec.c:153:9: Call
      151.           pval = &ptmpval;
      152.       asn1_tlc_clear_nc(&c);
      153.       if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
                     ^
      154.           return *pval;
      155.       return NULL;

crypto/asn1/tasn_dec.c:158:1: Parameter `**in`
        156.   }
        157.   
        158. > int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
        159.                        const ASN1_ITEM *it,
        160.                        int tag, int aclass, char opt, ASN1_TLC *ctx)

crypto/asn1/tasn_dec.c:163:10: Call
        161.   {
        162.       int rv;
        163.       rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx);
                        ^
        164.       if (rv <= 0)
        165.           ASN1_item_ex_free(pval, it);

crypto/asn1/tasn_dec.c:174:1: Parameter `**in`
          172.    */
          173.   
          174. > static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
          175.                                  long len, const ASN1_ITEM *it,
          176.                                  int tag, int aclass, char opt, ASN1_TLC *ctx)

crypto/asn1/tasn_dec.c:218:9: Assignment
          216.   
          217.       case ASN1_ITYPE_MSTRING:
          218.           p = *in;
                         ^
          219.           /* Just read in tag and class */
          220.           ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,

crypto/asn1/tasn_dec.c:220:15: Call
          218.           p = *in;
          219.           /* Just read in tag and class */
          220.           ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
                               ^
          221.                                 &p, len, -1, 0, 1, ctx);
          222.           if (!ret) {

crypto/asn1/tasn_dec.c:1098:1: Parameter `**in`
            1096.    */
            1097.   
            1098. > static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
            1099.                              char *inf, char *cst,
            1100.                              const unsigned char **in, long len,

crypto/asn1/tasn_dec.c:1107:5: Assignment
            1105.       long plen;
            1106.       const unsigned char *p, *q;
            1107.       p = *in;
                        ^
            1108.       q = p;
            1109.   

crypto/asn1/tasn_dec.c:1117:13: Call
            1115.           p += ctx->hdrlen;
            1116.       } else {
            1117.           i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
                                ^
            1118.           if (ctx) {
            1119.               ctx->ret = i;

crypto/asn1/asn1_lib.c:92:1: Parameter `**pp`
              90.   }
              91.   
              92. > int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
              93.                       int *pclass, long omax)
              94.   {

crypto/asn1/asn1_lib.c:97:5: Assignment
              95.       int i, ret;
              96.       long l;
              97.       const unsigned char *p = *pp;
                         ^
              98.       int tag, xclass, inf;
              99.       long max = omax;

crypto/asn1/asn1_lib.c:126:9: Assignment
              124.       } else {
              125.           tag = i;
              126.           p++;
                             ^
              127.           if (--max == 0)
              128.               goto err;

crypto/asn1/asn1_lib.c:132:10: Call
              130.       *ptag = tag;
              131.       *pclass = xclass;
              132.       if (!asn1_get_length(&p, &inf, plength, (int)max))
                              ^
              133.           goto err;
              134.   

crypto/asn1/asn1_lib.c:153:1: <Length trace>
151.   }
152.   
153. > static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
154.                              int max)
155.   {

crypto/asn1/asn1_lib.c:153:1: Parameter `**pp`
                151.   }
                152.   
                153. > static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
                154.                              int max)
                155.   {

crypto/asn1/asn1_lib.c:156:5: Assignment
                154.                              int max)
                155.   {
                156.       const unsigned char *p = *pp;
                           ^
                157.       unsigned long ret = 0;
                158.       unsigned int i;

crypto/asn1/asn1_lib.c:169:15: Assignment
                167.           *inf = 0;
                168.           i = *p & 0x7f;
                169.           if (*(p++) & 0x80) {
                                     ^
                170.               if (max < (int)i)
                171.                   return 0;

crypto/asn1/asn1_lib.c:173:25: Array access: Offset: [2, +oo] Size: [1, 2147483644] by call to `d2i_PrivateKey` 
                171.                   return 0;
                172.               /* Skip leading zeroes */
                173.               while (i && *p == 0) {
                                               ^
                174.                   p++;
                175.                   i--;

static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
                           int max)
{
    const unsigned char *p = *pp;
    unsigned long ret = 0;
    unsigned int i;
    if (max-- < 1)
        return (0);
    if (*p == 0x80) {
        *inf = 1;
        ret = 0;
        p++;
    } else {
        *inf = 0;
        i = *p & 0x7f;
        if (*(p++) & 0x80) {
            if (max < (int)i)
                return 0;
            while (i && *p == 0) {
                p++;
                i--;
            }
            if (i > sizeof(long))
                return 0;
            while (i-- > 0) {
                ret <<= 8L;
                ret |= *(p++);
            }
        } else
            ret = i;
    }
    if (ret > LONG_MAX)
        return 0;
    *pp = p;
    *rl = (long)ret;
    return (1);
}
['STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,\n                                            pem_password_cb *cb, void *u)\n{\n    X509_INFO *xi = NULL;\n    char *name = NULL, *header = NULL;\n    void *pp;\n    unsigned char *data = NULL;\n    const unsigned char *p;\n    long len, error = 0;\n    int ok = 0;\n    STACK_OF(X509_INFO) *ret = NULL;\n    unsigned int i, raw, ptype;\n    d2i_of_void *d2i = 0;\n    if (sk == NULL) {\n        if ((ret = sk_X509_INFO_new_null()) == NULL) {\n            PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n    } else\n        ret = sk;\n    if ((xi = X509_INFO_new()) == NULL)\n        goto err;\n    for (;;) {\n        raw = 0;\n        ptype = 0;\n        i = PEM_read_bio(bp, &name, &header, &data, &len);\n        if (i == 0) {\n            error = ERR_GET_REASON(ERR_peek_last_error());\n            if (error == PEM_R_NO_START_LINE) {\n                ERR_clear_error();\n                break;\n            }\n            goto err;\n        }\n start:\n        if ((strcmp(name, PEM_STRING_X509) == 0) ||\n            (strcmp(name, PEM_STRING_X509_OLD) == 0)) {\n            d2i = (D2I_OF(void)) d2i_X509;\n            if (xi->x509 != NULL) {\n                if (!sk_X509_INFO_push(ret, xi))\n                    goto err;\n                if ((xi = X509_INFO_new()) == NULL)\n                    goto err;\n                goto start;\n            }\n            pp = &(xi->x509);\n        } else if ((strcmp(name, PEM_STRING_X509_TRUSTED) == 0)) {\n            d2i = (D2I_OF(void)) d2i_X509_AUX;\n            if (xi->x509 != NULL) {\n                if (!sk_X509_INFO_push(ret, xi))\n                    goto err;\n                if ((xi = X509_INFO_new()) == NULL)\n                    goto err;\n                goto start;\n            }\n            pp = &(xi->x509);\n        } else if (strcmp(name, PEM_STRING_X509_CRL) == 0) {\n            d2i = (D2I_OF(void)) d2i_X509_CRL;\n            if (xi->crl != NULL) {\n                if (!sk_X509_INFO_push(ret, xi))\n                    goto err;\n                if ((xi = X509_INFO_new()) == NULL)\n                    goto err;\n                goto start;\n            }\n            pp = &(xi->crl);\n        } else\n#ifndef OPENSSL_NO_RSA\n        if (strcmp(name, PEM_STRING_RSA) == 0) {\n            d2i = (D2I_OF(void)) d2i_RSAPrivateKey;\n            if (xi->x_pkey != NULL) {\n                if (!sk_X509_INFO_push(ret, xi))\n                    goto err;\n                if ((xi = X509_INFO_new()) == NULL)\n                    goto err;\n                goto start;\n            }\n            xi->enc_data = NULL;\n            xi->enc_len = 0;\n            xi->x_pkey = X509_PKEY_new();\n            if (xi->x_pkey == NULL)\n                goto err;\n            ptype = EVP_PKEY_RSA;\n            pp = &xi->x_pkey->dec_pkey;\n            if ((int)strlen(header) > 10)\n                raw = 1;\n        } else\n#endif\n#ifndef OPENSSL_NO_DSA\n        if (strcmp(name, PEM_STRING_DSA) == 0) {\n            d2i = (D2I_OF(void)) d2i_DSAPrivateKey;\n            if (xi->x_pkey != NULL) {\n                if (!sk_X509_INFO_push(ret, xi))\n                    goto err;\n                if ((xi = X509_INFO_new()) == NULL)\n                    goto err;\n                goto start;\n            }\n            xi->enc_data = NULL;\n            xi->enc_len = 0;\n            xi->x_pkey = X509_PKEY_new();\n            if (xi->x_pkey == NULL)\n                goto err;\n            ptype = EVP_PKEY_DSA;\n            pp = &xi->x_pkey->dec_pkey;\n            if ((int)strlen(header) > 10)\n                raw = 1;\n        } else\n#endif\n#ifndef OPENSSL_NO_EC\n        if (strcmp(name, PEM_STRING_ECPRIVATEKEY) == 0) {\n            d2i = (D2I_OF(void)) d2i_ECPrivateKey;\n            if (xi->x_pkey != NULL) {\n                if (!sk_X509_INFO_push(ret, xi))\n                    goto err;\n                if ((xi = X509_INFO_new()) == NULL)\n                    goto err;\n                goto start;\n            }\n            xi->enc_data = NULL;\n            xi->enc_len = 0;\n            xi->x_pkey = X509_PKEY_new();\n            if (xi->x_pkey == NULL)\n                goto err;\n            ptype = EVP_PKEY_EC;\n            pp = &xi->x_pkey->dec_pkey;\n            if ((int)strlen(header) > 10)\n                raw = 1;\n        } else\n#endif\n        {\n            d2i = NULL;\n            pp = NULL;\n        }\n        if (d2i != NULL) {\n            if (!raw) {\n                EVP_CIPHER_INFO cipher;\n                if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))\n                    goto err;\n                if (!PEM_do_header(&cipher, data, &len, cb, u))\n                    goto err;\n                p = data;\n                if (ptype) {\n                    if (!d2i_PrivateKey(ptype, pp, &p, len)) {\n                        PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB);\n                        goto err;\n                    }\n                } else if (d2i(pp, &p, len) == NULL) {\n                    PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB);\n                    goto err;\n                }\n            } else {\n                if (!PEM_get_EVP_CIPHER_INFO(header, &xi->enc_cipher))\n                    goto err;\n                xi->enc_data = (char *)data;\n                xi->enc_len = (int)len;\n                data = NULL;\n            }\n        } else {\n        }\n        OPENSSL_free(name);\n        name = NULL;\n        OPENSSL_free(header);\n        header = NULL;\n        OPENSSL_free(data);\n        data = NULL;\n    }\n    if ((xi->x509 != NULL) || (xi->crl != NULL) ||\n        (xi->x_pkey != NULL) || (xi->enc_data != NULL)) {\n        if (!sk_X509_INFO_push(ret, xi))\n            goto err;\n        xi = NULL;\n    }\n    ok = 1;\n err:\n    X509_INFO_free(xi);\n    if (!ok) {\n        for (i = 0; ((int)i) < sk_X509_INFO_num(ret); i++) {\n            xi = sk_X509_INFO_value(ret, i);\n            X509_INFO_free(xi);\n        }\n        if (ret != sk)\n            sk_X509_INFO_free(ret);\n        ret = NULL;\n    }\n    OPENSSL_free(name);\n    OPENSSL_free(header);\n    OPENSSL_free(data);\n    return (ret);\n}', 'int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,\n                 long *len)\n{\n    EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();\n    int end = 0, i, k, bl = 0, hl = 0, nohead = 0;\n    char buf[256];\n    BUF_MEM *nameB;\n    BUF_MEM *headerB;\n    BUF_MEM *dataB, *tmpB;\n    if (ctx == NULL) {\n        PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);\n        return (0);\n    }\n    nameB = BUF_MEM_new();\n    headerB = BUF_MEM_new();\n    dataB = BUF_MEM_new();\n    if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) {\n        goto err;\n    }\n    buf[254] = \'\\0\';\n    for (;;) {\n        i = BIO_gets(bp, buf, 254);\n        if (i <= 0) {\n            PEMerr(PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE);\n            goto err;\n        }\n        while ((i >= 0) && (buf[i] <= \' \'))\n            i--;\n        buf[++i] = \'\\n\';\n        buf[++i] = \'\\0\';\n        if (strncmp(buf, "-----BEGIN ", 11) == 0) {\n            i = strlen(&(buf[11]));\n            if (strncmp(&(buf[11 + i - 6]), "-----\\n", 6) != 0)\n                continue;\n            if (!BUF_MEM_grow(nameB, i + 9)) {\n                PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);\n                goto err;\n            }\n            memcpy(nameB->data, &(buf[11]), i - 6);\n            nameB->data[i - 6] = \'\\0\';\n            break;\n        }\n    }\n    hl = 0;\n    if (!BUF_MEM_grow(headerB, 256)) {\n        PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    headerB->data[0] = \'\\0\';\n    for (;;) {\n        i = BIO_gets(bp, buf, 254);\n        if (i <= 0)\n            break;\n        while ((i >= 0) && (buf[i] <= \' \'))\n            i--;\n        buf[++i] = \'\\n\';\n        buf[++i] = \'\\0\';\n        if (buf[0] == \'\\n\')\n            break;\n        if (!BUF_MEM_grow(headerB, hl + i + 9)) {\n            PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        if (strncmp(buf, "-----END ", 9) == 0) {\n            nohead = 1;\n            break;\n        }\n        memcpy(&(headerB->data[hl]), buf, i);\n        headerB->data[hl + i] = \'\\0\';\n        hl += i;\n    }\n    bl = 0;\n    if (!BUF_MEM_grow(dataB, 1024)) {\n        PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    dataB->data[0] = \'\\0\';\n    if (!nohead) {\n        for (;;) {\n            i = BIO_gets(bp, buf, 254);\n            if (i <= 0)\n                break;\n            while ((i >= 0) && (buf[i] <= \' \'))\n                i--;\n            buf[++i] = \'\\n\';\n            buf[++i] = \'\\0\';\n            if (i != 65)\n                end = 1;\n            if (strncmp(buf, "-----END ", 9) == 0)\n                break;\n            if (i > 65)\n                break;\n            if (!BUF_MEM_grow_clean(dataB, i + bl + 9)) {\n                PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);\n                goto err;\n            }\n            memcpy(&(dataB->data[bl]), buf, i);\n            dataB->data[bl + i] = \'\\0\';\n            bl += i;\n            if (end) {\n                buf[0] = \'\\0\';\n                i = BIO_gets(bp, buf, 254);\n                if (i <= 0)\n                    break;\n                while ((i >= 0) && (buf[i] <= \' \'))\n                    i--;\n                buf[++i] = \'\\n\';\n                buf[++i] = \'\\0\';\n                break;\n            }\n        }\n    } else {\n        tmpB = headerB;\n        headerB = dataB;\n        dataB = tmpB;\n        bl = hl;\n    }\n    i = strlen(nameB->data);\n    if ((strncmp(buf, "-----END ", 9) != 0) ||\n        (strncmp(nameB->data, &(buf[9]), i) != 0) ||\n        (strncmp(&(buf[9 + i]), "-----\\n", 6) != 0)) {\n        PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_END_LINE);\n        goto err;\n    }\n    EVP_DecodeInit(ctx);\n    i = EVP_DecodeUpdate(ctx,\n                         (unsigned char *)dataB->data, &bl,\n                         (unsigned char *)dataB->data, bl);\n    if (i < 0) {\n        PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);\n        goto err;\n    }\n    i = EVP_DecodeFinal(ctx, (unsigned char *)&(dataB->data[bl]), &k);\n    if (i < 0) {\n        PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);\n        goto err;\n    }\n    bl += k;\n    if (bl == 0)\n        goto err;\n    *name = nameB->data;\n    *header = headerB->data;\n    *data = (unsigned char *)dataB->data;\n    *len = bl;\n    OPENSSL_free(nameB);\n    OPENSSL_free(headerB);\n    OPENSSL_free(dataB);\n    EVP_ENCODE_CTX_free(ctx);\n    return (1);\n err:\n    BUF_MEM_free(nameB);\n    BUF_MEM_free(headerB);\n    BUF_MEM_free(dataB);\n    EVP_ENCODE_CTX_free(ctx);\n    return (0);\n}', 'EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,\n                         long length)\n{\n    EVP_PKEY *ret;\n    const unsigned char *p = *pp;\n    if ((a == NULL) || (*a == NULL)) {\n        if ((ret = EVP_PKEY_new()) == NULL) {\n            ASN1err(ASN1_F_D2I_PRIVATEKEY, ERR_R_EVP_LIB);\n            return (NULL);\n        }\n    } else {\n        ret = *a;\n#ifndef OPENSSL_NO_ENGINE\n        if (ret->engine) {\n            ENGINE_finish(ret->engine);\n            ret->engine = NULL;\n        }\n#endif\n    }\n    if (!EVP_PKEY_set_type(ret, type)) {\n        ASN1err(ASN1_F_D2I_PRIVATEKEY, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);\n        goto err;\n    }\n    if (!ret->ameth->old_priv_decode ||\n        !ret->ameth->old_priv_decode(ret, &p, length)) {\n        if (ret->ameth->priv_decode) {\n            PKCS8_PRIV_KEY_INFO *p8 = NULL;\n            p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);\n            if (!p8)\n                goto err;\n            EVP_PKEY_free(ret);\n            ret = EVP_PKCS82PKEY(p8);\n            PKCS8_PRIV_KEY_INFO_free(p8);\n            if (ret == NULL)\n                goto err;\n        } else {\n            ASN1err(ASN1_F_D2I_PRIVATEKEY, ERR_R_ASN1_LIB);\n            goto err;\n        }\n    }\n    *pp = p;\n    if (a != NULL)\n        (*a) = ret;\n    return (ret);\n err:\n    if (a == NULL || *a != ret)\n        EVP_PKEY_free(ret);\n    return (NULL);\n}', 'IMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO)', 'ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,\n                          const unsigned char **in, long len,\n                          const ASN1_ITEM *it)\n{\n    ASN1_TLC c;\n    ASN1_VALUE *ptmpval = NULL;\n    if (!pval)\n        pval = &ptmpval;\n    asn1_tlc_clear_nc(&c);\n    if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)\n        return *pval;\n    return NULL;\n}', 'int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,\n                     const ASN1_ITEM *it,\n                     int tag, int aclass, char opt, ASN1_TLC *ctx)\n{\n    int rv;\n    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx);\n    if (rv <= 0)\n        ASN1_item_ex_free(pval, it);\n    return rv;\n}', 'static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,\n                               long len, const ASN1_ITEM *it,\n                               int tag, int aclass, char opt, ASN1_TLC *ctx)\n{\n    const ASN1_TEMPLATE *tt, *errtt = NULL;\n    const ASN1_EXTERN_FUNCS *ef;\n    const ASN1_AUX *aux = it->funcs;\n    ASN1_aux_cb *asn1_cb;\n    const unsigned char *p = NULL, *q;\n    unsigned char oclass;\n    char seq_eoc, seq_nolen, cst, isopt;\n    long tmplen;\n    int i;\n    int otag;\n    int ret = 0;\n    ASN1_VALUE **pchptr;\n    if (!pval)\n        return 0;\n    if (aux && aux->asn1_cb)\n        asn1_cb = aux->asn1_cb;\n    else\n        asn1_cb = 0;\n    switch (it->itype) {\n    case ASN1_ITYPE_PRIMITIVE:\n        if (it->templates) {\n            if ((tag != -1) || opt) {\n                ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,\n                        ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);\n                goto err;\n            }\n            return asn1_template_ex_d2i(pval, in, len,\n                                        it->templates, opt, ctx);\n        }\n        return asn1_d2i_ex_primitive(pval, in, len, it,\n                                     tag, aclass, opt, ctx);\n    case ASN1_ITYPE_MSTRING:\n        p = *in;\n        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,\n                              &p, len, -1, 0, 1, ctx);\n        if (!ret) {\n            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n            goto err;\n        }\n        if (oclass != V_ASN1_UNIVERSAL) {\n            if (opt)\n                return -1;\n            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);\n            goto err;\n        }\n        if (!(ASN1_tag2bit(otag) & it->utype)) {\n            if (opt)\n                return -1;\n            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);\n            goto err;\n        }\n        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);\n    case ASN1_ITYPE_EXTERN:\n        ef = it->funcs;\n        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);\n    case ASN1_ITYPE_CHOICE:\n        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))\n            goto auxerr;\n        if (*pval) {\n            i = asn1_get_choice_selector(pval, it);\n            if ((i >= 0) && (i < it->tcount)) {\n                tt = it->templates + i;\n                pchptr = asn1_get_field_ptr(pval, tt);\n                asn1_template_free(pchptr, tt);\n                asn1_set_choice_selector(pval, -1, it);\n            }\n        } else if (!ASN1_item_ex_new(pval, it)) {\n            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n            goto err;\n        }\n        p = *in;\n        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {\n            pchptr = asn1_get_field_ptr(pval, tt);\n            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);\n            if (ret == -1)\n                continue;\n            if (ret > 0)\n                break;\n            errtt = tt;\n            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n            goto err;\n        }\n        if (i == it->tcount) {\n            if (opt) {\n                ASN1_item_ex_free(pval, it);\n                return -1;\n            }\n            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);\n            goto err;\n        }\n        asn1_set_choice_selector(pval, i, it);\n        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))\n            goto auxerr;\n        *in = p;\n        return 1;\n    case ASN1_ITYPE_NDEF_SEQUENCE:\n    case ASN1_ITYPE_SEQUENCE:\n        p = *in;\n        tmplen = len;\n        if (tag == -1) {\n            tag = V_ASN1_SEQUENCE;\n            aclass = V_ASN1_UNIVERSAL;\n        }\n        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,\n                              &p, len, tag, aclass, opt, ctx);\n        if (!ret) {\n            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n            goto err;\n        } else if (ret == -1)\n            return -1;\n        if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {\n            len = tmplen - (p - *in);\n            seq_nolen = 1;\n        }\n        else\n            seq_nolen = seq_eoc;\n        if (!cst) {\n            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);\n            goto err;\n        }\n        if (!*pval && !ASN1_item_ex_new(pval, it)) {\n            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n            goto err;\n        }\n        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))\n            goto auxerr;\n        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {\n            if (tt->flags & ASN1_TFLG_ADB_MASK) {\n                const ASN1_TEMPLATE *seqtt;\n                ASN1_VALUE **pseqval;\n                seqtt = asn1_do_adb(pval, tt, 1);\n                pseqval = asn1_get_field_ptr(pval, seqtt);\n                asn1_template_free(pseqval, seqtt);\n            }\n        }\n        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {\n            const ASN1_TEMPLATE *seqtt;\n            ASN1_VALUE **pseqval;\n            seqtt = asn1_do_adb(pval, tt, 1);\n            if (!seqtt)\n                goto err;\n            pseqval = asn1_get_field_ptr(pval, seqtt);\n            if (!len)\n                break;\n            q = p;\n            if (asn1_check_eoc(&p, len)) {\n                if (!seq_eoc) {\n                    ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);\n                    goto err;\n                }\n                len -= p - q;\n                seq_eoc = 0;\n                q = p;\n                break;\n            }\n            if (i == (it->tcount - 1))\n                isopt = 0;\n            else\n                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);\n            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);\n            if (!ret) {\n                errtt = seqtt;\n                goto err;\n            } else if (ret == -1) {\n                asn1_template_free(pseqval, seqtt);\n                continue;\n            }\n            len -= p - q;\n        }\n        if (seq_eoc && !asn1_check_eoc(&p, len)) {\n            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);\n            goto err;\n        }\n        if (!seq_nolen && len) {\n            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);\n            goto err;\n        }\n        for (; i < it->tcount; tt++, i++) {\n            const ASN1_TEMPLATE *seqtt;\n            seqtt = asn1_do_adb(pval, tt, 1);\n            if (!seqtt)\n                goto err;\n            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {\n                ASN1_VALUE **pseqval;\n                pseqval = asn1_get_field_ptr(pval, seqtt);\n                asn1_template_free(pseqval, seqtt);\n            } else {\n                errtt = seqtt;\n                ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);\n                goto err;\n            }\n        }\n        if (!asn1_enc_save(pval, *in, p - *in, it))\n            goto auxerr;\n        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))\n            goto auxerr;\n        *in = p;\n        return 1;\n    default:\n        return 0;\n    }\n auxerr:\n    ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);\n err:\n    if (errtt)\n        ERR_add_error_data(4, "Field=", errtt->field_name,\n                           ", Type=", it->sname);\n    else\n        ERR_add_error_data(2, "Type=", it->sname);\n    return 0;\n}', 'static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,\n                           char *inf, char *cst,\n                           const unsigned char **in, long len,\n                           int exptag, int expclass, char opt, ASN1_TLC *ctx)\n{\n    int i;\n    int ptag, pclass;\n    long plen;\n    const unsigned char *p, *q;\n    p = *in;\n    q = p;\n    if (ctx && ctx->valid) {\n        i = ctx->ret;\n        plen = ctx->plen;\n        pclass = ctx->pclass;\n        ptag = ctx->ptag;\n        p += ctx->hdrlen;\n    } else {\n        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);\n        if (ctx) {\n            ctx->ret = i;\n            ctx->plen = plen;\n            ctx->pclass = pclass;\n            ctx->ptag = ptag;\n            ctx->hdrlen = p - q;\n            ctx->valid = 1;\n            if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {\n                ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);\n                asn1_tlc_clear(ctx);\n                return 0;\n            }\n        }\n    }\n    if (i & 0x80) {\n        ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);\n        asn1_tlc_clear(ctx);\n        return 0;\n    }\n    if (exptag >= 0) {\n        if ((exptag != ptag) || (expclass != pclass)) {\n            if (opt)\n                return -1;\n            asn1_tlc_clear(ctx);\n            ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);\n            return 0;\n        }\n        asn1_tlc_clear(ctx);\n    }\n    if (i & 1)\n        plen = len - (p - q);\n    if (inf)\n        *inf = i & 1;\n    if (cst)\n        *cst = i & V_ASN1_CONSTRUCTED;\n    if (olen)\n        *olen = plen;\n    if (oclass)\n        *oclass = pclass;\n    if (otag)\n        *otag = ptag;\n    *in = p;\n    return 1;\n}', 'int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,\n                    int *pclass, long omax)\n{\n    int i, ret;\n    long l;\n    const unsigned char *p = *pp;\n    int tag, xclass, inf;\n    long max = omax;\n    if (!max)\n        goto err;\n    ret = (*p & V_ASN1_CONSTRUCTED);\n    xclass = (*p & V_ASN1_PRIVATE);\n    i = *p & V_ASN1_PRIMITIVE_TAG;\n    if (i == V_ASN1_PRIMITIVE_TAG) {\n        p++;\n        if (--max == 0)\n            goto err;\n        l = 0;\n        while (*p & 0x80) {\n            l <<= 7L;\n            l |= *(p++) & 0x7f;\n            if (--max == 0)\n                goto err;\n            if (l > (INT_MAX >> 7L))\n                goto err;\n        }\n        l <<= 7L;\n        l |= *(p++) & 0x7f;\n        tag = (int)l;\n        if (--max == 0)\n            goto err;\n    } else {\n        tag = i;\n        p++;\n        if (--max == 0)\n            goto err;\n    }\n    *ptag = tag;\n    *pclass = xclass;\n    if (!asn1_get_length(&p, &inf, plength, (int)max))\n        goto err;\n    if (inf && !(ret & V_ASN1_CONSTRUCTED))\n        goto err;\n    if (*plength > (omax - (p - *pp))) {\n        ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);\n        ret |= 0x80;\n    }\n    *pp = p;\n    return (ret | inf);\n err:\n    ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);\n    return (0x80);\n}', 'static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,\n                           int max)\n{\n    const unsigned char *p = *pp;\n    unsigned long ret = 0;\n    unsigned int i;\n    if (max-- < 1)\n        return (0);\n    if (*p == 0x80) {\n        *inf = 1;\n        ret = 0;\n        p++;\n    } else {\n        *inf = 0;\n        i = *p & 0x7f;\n        if (*(p++) & 0x80) {\n            if (max < (int)i)\n                return 0;\n            while (i && *p == 0) {\n                p++;\n                i--;\n            }\n            if (i > sizeof(long))\n                return 0;\n            while (i-- > 0) {\n                ret <<= 8L;\n                ret |= *(p++);\n            }\n        } else\n            ret = i;\n    }\n    if (ret > LONG_MAX)\n        return 0;\n    *pp = p;\n    *rl = (long)ret;\n    return (1);\n}']