project
stringclasses
633 values
commit_id
stringlengths
7
81
target
int64
0
1
func
stringlengths
5
484k
cwe
stringclasses
131 values
big_vul_idx
float64
0
189k
idx
int64
0
522k
hash
stringlengths
34
39
size
float64
1
24k
message
stringlengths
0
11.5k
dataset
stringclasses
1 value
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static const char *connect_to_host(Ssh ssh, const char *host, int port, char **realhost, int nodelay, int keepalive) { static const struct plug_function_table fn_table = { ssh_socket_log, ssh_closing, ssh_receive, ssh_sent, NULL }; SockAddr addr; const char *err; char *loghost; int addressfamily, sshprot; ssh_hostport_setup(host, port, ssh->conf, &ssh->savedhost, &ssh->savedport, &loghost); ssh->fn = &fn_table; /* make 'ssh' usable as a Plug */ /* * Try connection-sharing, in case that means we don't open a * socket after all. ssh_connection_sharing_init will connect to a * previously established upstream if it can, and failing that, * establish a listening socket for _us_ to be the upstream. In * the latter case it will return NULL just as if it had done * nothing, because here we only need to care if we're a * downstream and need to do our connection setup differently. */ ssh->connshare = NULL; ssh->attempting_connshare = TRUE; /* affects socket logging behaviour */ ssh->s = ssh_connection_sharing_init(ssh->savedhost, ssh->savedport, ssh->conf, ssh, &ssh->connshare); ssh->attempting_connshare = FALSE; if (ssh->s != NULL) { /* * We are a downstream. */ ssh->bare_connection = TRUE; ssh->do_ssh_init = do_ssh_connection_init; ssh->fullhostname = NULL; *realhost = dupstr(host); /* best we can do */ } else { /* * We're not a downstream, so open a normal socket. */ ssh->do_ssh_init = do_ssh_init; /* * Try to find host. */ addressfamily = conf_get_int(ssh->conf, CONF_addressfamily); addr = name_lookup(host, port, realhost, ssh->conf, addressfamily, ssh->frontend, "SSH connection"); if ((err = sk_addr_error(addr)) != NULL) { sk_addr_free(addr); return err; } ssh->fullhostname = dupstr(*realhost); /* save in case of GSSAPI */ ssh->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive, (Plug) ssh, ssh->conf); if ((err = sk_socket_error(ssh->s)) != NULL) { ssh->s = NULL; notify_remote_exit(ssh->frontend); return err; } } /* * The SSH version number is always fixed (since we no longer support * fallback between versions), so set it now, and if it's SSH-2, * send the version string now too. */ sshprot = conf_get_int(ssh->conf, CONF_sshprot); assert(sshprot == 0 || sshprot == 3); if (sshprot == 0) /* SSH-1 only */ ssh->version = 1; if (sshprot == 3 && !ssh->bare_connection) { /* SSH-2 only */ ssh->version = 2; ssh_send_verstring(ssh, "SSH-", NULL); } /* * loghost, if configured, overrides realhost. */ if (*loghost) { sfree(*realhost); *realhost = dupstr(loghost); } return NULL; }
CWE-119
8,510
15,884
206078356279546680965182307763584455668
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void defer_packet(Ssh ssh, int pkttype, ...) { struct Packet *pkt; va_list ap; va_start(ap, pkttype); pkt = construct_packet(ssh, pkttype, ap); va_end(ap); s_wrpkt_defer(ssh, pkt); }
CWE-119
8,512
15,885
275342575540975488082146690754968182759
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int do_ssh_connection_init(Ssh ssh, unsigned char c) { /* * Ordinary SSH begins with the banner "SSH-x.y-...". This is just * the ssh-connection part, extracted and given a trivial binary * packet protocol, so we replace 'SSH-' at the start with a new * name. In proper SSH style (though of course this part of the * proper SSH protocol _isn't_ subject to this kind of * DNS-domain-based extension), we define the new name in our * extension space. */ static const char protoname[] = "SSHCONNECTION@putty.projects.tartarus.org-"; struct do_ssh_connection_init_state { int crLine; int vslen; char version[10]; char *vstring; int vstrsize; int i; }; crState(do_ssh_connection_init_state); crBeginState; /* Search for a line beginning with the protocol name prefix in * the input. */ for (;;) { for (s->i = 0; protoname[s->i]; s->i++) { if ((char)c != protoname[s->i]) goto no; crReturn(1); } break; no: while (c != '\012') crReturn(1); crReturn(1); } s->vstrsize = sizeof(protoname) + 16; s->vstring = snewn(s->vstrsize, char); strcpy(s->vstring, protoname); s->vslen = strlen(protoname); s->i = 0; while (1) { if (s->vslen >= s->vstrsize - 1) { s->vstrsize += 16; s->vstring = sresize(s->vstring, s->vstrsize, char); } s->vstring[s->vslen++] = c; if (s->i >= 0) { if (c == '-') { s->version[s->i] = '\0'; s->i = -1; } else if (s->i < sizeof(s->version) - 1) s->version[s->i++] = c; } else if (c == '\012') break; crReturn(1); /* get another char */ } ssh->agentfwd_enabled = FALSE; ssh->rdpkt2_bare_state.incoming_sequence = 0; s->vstring[s->vslen] = 0; s->vstring[strcspn(s->vstring, "\015\012")] = '\0';/* remove EOL chars */ logeventf(ssh, "Server version: %s", s->vstring); ssh_detect_bugs(ssh, s->vstring); /* * Decide which SSH protocol version to support. This is easy in * bare ssh-connection mode: only 2.0 is legal. */ if (ssh_versioncmp(s->version, "2.0") < 0) { bombout(("Server announces compatibility with SSH-1 in bare ssh-connection protocol")); crStop(0); } if (conf_get_int(ssh->conf, CONF_sshprot) == 0) { bombout(("Bare ssh-connection protocol cannot be run in SSH-1-only mode")); crStop(0); } ssh->version = 2; logeventf(ssh, "Using bare ssh-connection protocol"); /* Send the version string, if we haven't already */ ssh_send_verstring(ssh, protoname, s->version); /* * Initialise bare connection protocol. */ ssh->protocol = ssh2_bare_connection_protocol; ssh2_bare_connection_protocol_setup(ssh); ssh->s_rdpkt = ssh2_bare_connection_rdpkt; update_specials_menu(ssh->frontend); ssh->state = SSH_STATE_BEFORE_SIZE; ssh->pinger = pinger_new(ssh->conf, &ssh_backend, ssh); /* * Get authconn (really just conn) under way. */ do_ssh2_authconn(ssh, NULL, 0, NULL); sfree(s->vstring); crFinish(0); }
CWE-119
8,513
15,886
271432698616096435031295089205213841560
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int do_ssh_init(Ssh ssh, unsigned char c) { static const char protoname[] = "SSH-"; struct do_ssh_init_state { int crLine; int vslen; char version[10]; char *vstring; int vstrsize; int i; int proto1, proto2; }; crState(do_ssh_init_state); crBeginState; /* Search for a line beginning with the protocol name prefix in * the input. */ for (;;) { for (s->i = 0; protoname[s->i]; s->i++) { if ((char)c != protoname[s->i]) goto no; crReturn(1); } break; no: while (c != '\012') crReturn(1); crReturn(1); } ssh->session_started = TRUE; s->vstrsize = sizeof(protoname) + 16; s->vstring = snewn(s->vstrsize, char); strcpy(s->vstring, protoname); s->vslen = strlen(protoname); s->i = 0; while (1) { if (s->vslen >= s->vstrsize - 1) { s->vstrsize += 16; s->vstring = sresize(s->vstring, s->vstrsize, char); } s->vstring[s->vslen++] = c; if (s->i >= 0) { if (c == '-') { s->version[s->i] = '\0'; s->i = -1; } else if (s->i < sizeof(s->version) - 1) s->version[s->i++] = c; } else if (c == '\012') break; crReturn(1); /* get another char */ } ssh->agentfwd_enabled = FALSE; ssh->rdpkt2_state.incoming_sequence = 0; s->vstring[s->vslen] = 0; s->vstring[strcspn(s->vstring, "\015\012")] = '\0';/* remove EOL chars */ logeventf(ssh, "Server version: %s", s->vstring); ssh_detect_bugs(ssh, s->vstring); /* * Decide which SSH protocol version to support. */ /* Anything strictly below "2.0" means protocol 1 is supported. */ s->proto1 = ssh_versioncmp(s->version, "2.0") < 0; /* Anything greater or equal to "1.99" means protocol 2 is supported. */ s->proto2 = ssh_versioncmp(s->version, "1.99") >= 0; if (conf_get_int(ssh->conf, CONF_sshprot) == 0) { if (!s->proto1) { bombout(("SSH protocol version 1 required by our configuration " "but not provided by server")); crStop(0); } } else if (conf_get_int(ssh->conf, CONF_sshprot) == 3) { if (!s->proto2) { bombout(("SSH protocol version 2 required by our configuration " "but server only provides (old, insecure) SSH-1")); crStop(0); } } else { /* No longer support values 1 or 2 for CONF_sshprot */ assert(!"Unexpected value for CONF_sshprot"); } if (s->proto2 && (conf_get_int(ssh->conf, CONF_sshprot) >= 2 || !s->proto1)) ssh->version = 2; else ssh->version = 1; logeventf(ssh, "Using SSH protocol version %d", ssh->version); /* Send the version string, if we haven't already */ if (conf_get_int(ssh->conf, CONF_sshprot) != 3) ssh_send_verstring(ssh, protoname, s->version); if (ssh->version == 2) { size_t len; /* * Record their version string. */ len = strcspn(s->vstring, "\015\012"); ssh->v_s = snewn(len + 1, char); memcpy(ssh->v_s, s->vstring, len); ssh->v_s[len] = 0; /* * Initialise SSH-2 protocol. */ ssh->protocol = ssh2_protocol; ssh2_protocol_setup(ssh); ssh->s_rdpkt = ssh2_rdpkt; } else { /* * Initialise SSH-1 protocol. */ ssh->protocol = ssh1_protocol; ssh1_protocol_setup(ssh); ssh->s_rdpkt = ssh1_rdpkt; } if (ssh->version == 2) do_ssh2_transport(ssh, NULL, -1, NULL); update_specials_menu(ssh->frontend); ssh->state = SSH_STATE_BEFORE_SIZE; ssh->pinger = pinger_new(ssh->conf, &ssh_backend, ssh); sfree(s->vstring); crFinish(0); }
CWE-119
8,514
15,887
40061463117260604047354388230242361992
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void free_rportfwd(struct ssh_rportfwd *pf) { if (pf) { sfree(pf->sportdesc); sfree(pf->shost); sfree(pf->dhost); sfree(pf); } }
CWE-119
8,515
15,888
100508878295100865964140346245830534214
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void hash_mpint(const struct ssh_hash *h, void *s, Bignum b) { unsigned char *p; int len; p = ssh2_mpint_fmt(b, &len); hash_string(h, s, p, len); sfree(p); }
CWE-119
8,516
15,889
91407153664652524008243217991573889065
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void hash_string(const struct ssh_hash *h, void *s, void *str, int len) { unsigned char lenblk[4]; PUT_32BIT(lenblk, len); h->bytes(s, lenblk, 4); h->bytes(s, str, len); }
CWE-119
8,517
15,890
174426121073554114015365085296594085606
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void hash_uint32(const struct ssh_hash *h, void *s, unsigned i) { unsigned char intblk[4]; PUT_32BIT(intblk, i); h->bytes(s, intblk, 4); }
CWE-119
8,518
15,891
22269431165647128712299226388462866679
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void logeventf(Ssh ssh, const char *fmt, ...) { va_list ap; char *buf; va_start(ap, fmt); buf = dupvprintf(fmt, ap); va_end(ap); logevent(buf); sfree(buf); }
CWE-119
8,519
15,892
239679867056778340200046638261136005543
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int nullstrcmp(const char *a, const char *b) { if (a == NULL && b == NULL) return 0; if (a == NULL) return -1; if (b == NULL) return +1; return strcmp(a, b); }
CWE-119
8,520
15,893
318637567403470851959133824858440549152
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void parse_ttymodes(Ssh ssh, void (*do_mode)(void *data, const struct ssh_ttymode *mode, char *val), void *data) { int i; const struct ssh_ttymode *mode; char *val; char default_val[2]; strcpy(default_val, "A"); for (i = 0; i < lenof(ssh_ttymodes); i++) { mode = ssh_ttymodes + i; val = conf_get_str_str_opt(ssh->conf, CONF_ttymodes, mode->mode); if (!val) val = default_val; /* * val[0] is either 'V', indicating that an explicit value * follows it, or 'A' indicating that we should pass the * value through from the local environment via get_ttymode. */ if (val[0] == 'A') { val = get_ttymode(ssh->frontend, mode->mode); if (val) { do_mode(data, mode, val); sfree(val); } } else do_mode(data, mode, val + 1); /* skip the 'V' */ } }
CWE-119
8,521
15,894
287264871383643594821028952753771749805
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void s_wrpkt(Ssh ssh, struct Packet *pkt) { int len, backlog, offset; len = s_wrpkt_prepare(ssh, pkt, &offset); backlog = s_write(ssh, pkt->data + offset, len); if (backlog > SSH_MAX_BACKLOG) ssh_throttle_all(ssh, 1, backlog); ssh_free_packet(pkt); }
CWE-119
8,523
15,895
3826000372827367747793165846169435652
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void s_wrpkt_defer(Ssh ssh, struct Packet *pkt) { int len, offset; len = s_wrpkt_prepare(ssh, pkt, &offset); if (ssh->deferred_len + len > ssh->deferred_size) { ssh->deferred_size = ssh->deferred_len + len + 128; ssh->deferred_send_data = sresize(ssh->deferred_send_data, ssh->deferred_size, unsigned char); } memcpy(ssh->deferred_send_data + ssh->deferred_len, pkt->data + offset, len); ssh->deferred_len += len; ssh_free_packet(pkt); }
CWE-119
8,524
15,896
98269245982075011968499530114826373589
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int s_wrpkt_prepare(Ssh ssh, struct Packet *pkt, int *offset_p) { int pad, biglen, i, pktoffs; unsigned long crc; #ifdef __SC__ /* * XXX various versions of SC (including 8.8.4) screw up the * register allocation in this function and use the same register * (D6) for len and as a temporary, with predictable results. The * following sledgehammer prevents this. */ volatile #endif int len; if (ssh->logctx) ssh1_log_outgoing_packet(ssh, pkt); if (ssh->v1_compressing) { unsigned char *compblk; int complen; zlib_compress_block(ssh->cs_comp_ctx, pkt->data + 12, pkt->length - 12, &compblk, &complen); ssh_pkt_ensure(pkt, complen + 2); /* just in case it's got bigger */ memcpy(pkt->data + 12, compblk, complen); sfree(compblk); pkt->length = complen + 12; } ssh_pkt_ensure(pkt, pkt->length + 4); /* space for CRC */ pkt->length += 4; len = pkt->length - 4 - 8; /* len(type+data+CRC) */ pad = 8 - (len % 8); pktoffs = 8 - pad; biglen = len + pad; /* len(padding+type+data+CRC) */ for (i = pktoffs; i < 4+8; i++) pkt->data[i] = random_byte(); crc = crc32_compute(pkt->data + pktoffs + 4, biglen - 4); /* all ex len */ PUT_32BIT(pkt->data + pktoffs + 4 + biglen - 4, crc); PUT_32BIT(pkt->data + pktoffs, len); if (ssh->cipher) ssh->cipher->encrypt(ssh->v1_cipher_ctx, pkt->data + pktoffs + 4, biglen); if (offset_p) *offset_p = pktoffs; return biglen + 4; /* len(length+padding+type+data+CRC) */ }
CWE-119
8,525
15,897
137851645625319283837494708321255579028
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void send_packet(Ssh ssh, int pkttype, ...) { struct Packet *pkt; va_list ap; va_start(ap, pkttype); pkt = construct_packet(ssh, pkttype, ap); va_end(ap); s_wrpkt(ssh, pkt); }
CWE-119
8,526
15,898
76142150243788377969654647038915995137
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh1_log_incoming_packet(Ssh ssh, struct Packet *pkt) { int nblanks = 0; struct logblank_t blanks[4]; char *str; int slen; pkt->savedpos = 0; if (ssh->logomitdata && (pkt->type == SSH1_SMSG_STDOUT_DATA || pkt->type == SSH1_SMSG_STDERR_DATA || pkt->type == SSH1_MSG_CHANNEL_DATA)) { /* "Session data" packets - omit the data string. */ if (pkt->type == SSH1_MSG_CHANNEL_DATA) ssh_pkt_getuint32(pkt); /* skip channel id */ blanks[nblanks].offset = pkt->savedpos + 4; blanks[nblanks].type = PKTLOG_OMIT; ssh_pkt_getstring(pkt, &str, &slen); if (str) { blanks[nblanks].len = slen; nblanks++; } } log_packet(ssh->logctx, PKT_INCOMING, pkt->type, ssh1_pkt_type(pkt->type), pkt->body, pkt->length, nblanks, blanks, NULL, 0, NULL); }
CWE-119
8,527
15,899
235785568874700230712708047038066330115
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh1_log_outgoing_packet(Ssh ssh, struct Packet *pkt) { int nblanks = 0; struct logblank_t blanks[4]; char *str; int slen; /* * For outgoing packets, pkt->length represents the length of the * whole packet starting at pkt->data (including some header), and * pkt->body refers to the point within that where the log-worthy * payload begins. However, incoming packets expect pkt->length to * represent only the payload length (that is, it's measured from * pkt->body not from pkt->data). Temporarily adjust our outgoing * packet to conform to the incoming-packet semantics, so that we * can analyse it with the ssh_pkt_get functions. */ pkt->length -= (pkt->body - pkt->data); pkt->savedpos = 0; if (ssh->logomitdata && (pkt->type == SSH1_CMSG_STDIN_DATA || pkt->type == SSH1_MSG_CHANNEL_DATA)) { /* "Session data" packets - omit the data string. */ if (pkt->type == SSH1_MSG_CHANNEL_DATA) ssh_pkt_getuint32(pkt); /* skip channel id */ blanks[nblanks].offset = pkt->savedpos + 4; blanks[nblanks].type = PKTLOG_OMIT; ssh_pkt_getstring(pkt, &str, &slen); if (str) { blanks[nblanks].len = slen; nblanks++; } } if ((pkt->type == SSH1_CMSG_AUTH_PASSWORD || pkt->type == SSH1_CMSG_AUTH_TIS_RESPONSE || pkt->type == SSH1_CMSG_AUTH_CCARD_RESPONSE) && conf_get_int(ssh->conf, CONF_logomitpass)) { /* If this is a password or similar packet, blank the password(s). */ blanks[nblanks].offset = 0; blanks[nblanks].len = pkt->length; blanks[nblanks].type = PKTLOG_BLANK; nblanks++; } else if (pkt->type == SSH1_CMSG_X11_REQUEST_FORWARDING && conf_get_int(ssh->conf, CONF_logomitpass)) { /* * If this is an X forwarding request packet, blank the fake * auth data. * * Note that while we blank the X authentication data here, we * don't take any special action to blank the start of an X11 * channel, so using MIT-MAGIC-COOKIE-1 and actually opening * an X connection without having session blanking enabled is * likely to leak your cookie into the log. */ pkt->savedpos = 0; ssh_pkt_getstring(pkt, &str, &slen); blanks[nblanks].offset = pkt->savedpos; blanks[nblanks].type = PKTLOG_BLANK; ssh_pkt_getstring(pkt, &str, &slen); if (str) { blanks[nblanks].len = pkt->savedpos - blanks[nblanks].offset; nblanks++; } } log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[12], ssh1_pkt_type(pkt->data[12]), pkt->body, pkt->length, nblanks, blanks, NULL, 0, NULL); /* * Undo the above adjustment of pkt->length, to put the packet * back in the state we found it. */ pkt->length += (pkt->body - pkt->data); }
CWE-119
8,528
15,900
305747542266126807478779403782893195047
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static Bignum ssh1_pkt_getmp(struct Packet *pkt) { int j; Bignum b; j = ssh1_read_bignum(pkt->body + pkt->savedpos, pkt->length - pkt->savedpos, &b); if (j < 0) return NULL; pkt->savedpos += j; return b; }
CWE-119
8,530
15,901
138435134252459431864327139537992580944
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static struct Packet *ssh1_pkt_init(int pkt_type) { struct Packet *pkt = ssh_new_packet(); pkt->length = 4 + 8; /* space for length + max padding */ ssh_pkt_addbyte(pkt, pkt_type); pkt->body = pkt->data + pkt->length; pkt->type = pkt_type; pkt->downstream_id = 0; pkt->additional_log_text = NULL; return pkt; }
CWE-119
8,532
15,902
111996490057896546138771717023842815585
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static struct Packet *ssh1_rdpkt(Ssh ssh, const unsigned char **data, int *datalen) { struct rdpkt1_state_tag *st = &ssh->rdpkt1_state; crBegin(ssh->ssh1_rdpkt_crstate); st->pktin = ssh_new_packet(); st->pktin->type = 0; st->pktin->length = 0; for (st->i = st->len = 0; st->i < 4; st->i++) { while ((*datalen) == 0) crReturn(NULL); st->len = (st->len << 8) + **data; (*data)++, (*datalen)--; } st->pad = 8 - (st->len % 8); st->biglen = st->len + st->pad; st->pktin->length = st->len - 5; if (st->biglen < 0) { bombout(("Extremely large packet length from server suggests" " data stream corruption")); ssh_free_packet(st->pktin); crStop(NULL); } st->pktin->maxlen = st->biglen; st->pktin->data = snewn(st->biglen + APIEXTRA, unsigned char); st->to_read = st->biglen; st->p = st->pktin->data; while (st->to_read > 0) { st->chunk = st->to_read; while ((*datalen) == 0) crReturn(NULL); if (st->chunk > (*datalen)) st->chunk = (*datalen); memcpy(st->p, *data, st->chunk); *data += st->chunk; *datalen -= st->chunk; st->p += st->chunk; st->to_read -= st->chunk; } if (ssh->cipher && detect_attack(ssh->crcda_ctx, st->pktin->data, st->biglen, NULL)) { bombout(("Network attack (CRC compensation) detected!")); ssh_free_packet(st->pktin); crStop(NULL); } if (ssh->cipher) ssh->cipher->decrypt(ssh->v1_cipher_ctx, st->pktin->data, st->biglen); st->realcrc = crc32_compute(st->pktin->data, st->biglen - 4); st->gotcrc = GET_32BIT(st->pktin->data + st->biglen - 4); if (st->gotcrc != st->realcrc) { bombout(("Incorrect CRC received on packet")); ssh_free_packet(st->pktin); crStop(NULL); } st->pktin->body = st->pktin->data + st->pad + 1; if (ssh->v1_compressing) { unsigned char *decompblk; int decomplen; if (!zlib_decompress_block(ssh->sc_comp_ctx, st->pktin->body - 1, st->pktin->length + 1, &decompblk, &decomplen)) { bombout(("Zlib decompression encountered invalid data")); ssh_free_packet(st->pktin); crStop(NULL); } if (st->pktin->maxlen < st->pad + decomplen) { st->pktin->maxlen = st->pad + decomplen; st->pktin->data = sresize(st->pktin->data, st->pktin->maxlen + APIEXTRA, unsigned char); st->pktin->body = st->pktin->data + st->pad + 1; } memcpy(st->pktin->body - 1, decompblk, decomplen); sfree(decompblk); st->pktin->length = decomplen - 1; } st->pktin->type = st->pktin->body[-1]; /* * Now pktin->body and pktin->length identify the semantic content * of the packet, excluding the initial type byte. */ if (ssh->logctx) ssh1_log_incoming_packet(ssh, st->pktin); st->pktin->savedpos = 0; crFinish(st->pktin); }
CWE-119
8,533
15,903
47155452858001689927475382973965009636
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh2_add_sigblob(Ssh ssh, struct Packet *pkt, void *pkblob_v, int pkblob_len, void *sigblob_v, int sigblob_len) { unsigned char *pkblob = (unsigned char *)pkblob_v; unsigned char *sigblob = (unsigned char *)sigblob_v; /* dmemdump(pkblob, pkblob_len); */ /* dmemdump(sigblob, sigblob_len); */ /* * See if this is in fact an ssh-rsa signature and a buggy * server; otherwise we can just do this the easy way. */ if ((ssh->remote_bugs & BUG_SSH2_RSA_PADDING) && pkblob_len > 4+7+4 && (GET_32BIT(pkblob) == 7 && !memcmp(pkblob+4, "ssh-rsa", 7))) { int pos, len, siglen; /* * Find the byte length of the modulus. */ pos = 4+7; /* skip over "ssh-rsa" */ len = toint(GET_32BIT(pkblob+pos)); /* get length of exponent */ if (len < 0 || len > pkblob_len - pos - 4) goto give_up; pos += 4 + len; /* skip over exponent */ if (pkblob_len - pos < 4) goto give_up; len = toint(GET_32BIT(pkblob+pos)); /* find length of modulus */ if (len < 0 || len > pkblob_len - pos - 4) goto give_up; pos += 4; /* find modulus itself */ while (len > 0 && pkblob[pos] == 0) len--, pos++; /* debug(("modulus length is %d\n", len)); */ /* * Now find the signature integer. */ pos = 4+7; /* skip over "ssh-rsa" */ if (sigblob_len < pos+4) goto give_up; siglen = toint(GET_32BIT(sigblob+pos)); if (siglen != sigblob_len - pos - 4) goto give_up; /* debug(("signature length is %d\n", siglen)); */ if (len != siglen) { unsigned char newlen[4]; ssh2_pkt_addstring_start(pkt); ssh2_pkt_addstring_data(pkt, (char *)sigblob, pos); /* dmemdump(sigblob, pos); */ pos += 4; /* point to start of actual sig */ PUT_32BIT(newlen, len); ssh2_pkt_addstring_data(pkt, (char *)newlen, 4); /* dmemdump(newlen, 4); */ newlen[0] = 0; while (len-- > siglen) { ssh2_pkt_addstring_data(pkt, (char *)newlen, 1); /* dmemdump(newlen, 1); */ } ssh2_pkt_addstring_data(pkt, (char *)(sigblob+pos), siglen); /* dmemdump(sigblob+pos, siglen); */ return; } /* Otherwise fall through and do it the easy way. We also come * here as a fallback if we discover above that the key blob * is misformatted in some way. */ give_up:; } ssh2_pkt_addstring_start(pkt); ssh2_pkt_addstring_data(pkt, (char *)sigblob, sigblob_len); }
CWE-119
8,534
15,904
295403029222771811542799371665880079397
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static struct Packet *ssh2_bare_connection_rdpkt(Ssh ssh, const unsigned char **data, int *datalen) { struct rdpkt2_bare_state_tag *st = &ssh->rdpkt2_bare_state; crBegin(ssh->ssh2_bare_rdpkt_crstate); /* * Read the packet length field. */ for (st->i = 0; st->i < 4; st->i++) { while ((*datalen) == 0) crReturn(NULL); st->length[st->i] = *(*data)++; (*datalen)--; } st->packetlen = toint(GET_32BIT_MSB_FIRST(st->length)); if (st->packetlen <= 0 || st->packetlen >= OUR_V2_PACKETLIMIT) { bombout(("Invalid packet length received")); crStop(NULL); } st->pktin = ssh_new_packet(); st->pktin->data = snewn(st->packetlen, unsigned char); st->pktin->encrypted_len = st->packetlen; st->pktin->sequence = st->incoming_sequence++; /* * Read the remainder of the packet. */ for (st->i = 0; st->i < st->packetlen; st->i++) { while ((*datalen) == 0) crReturn(NULL); st->pktin->data[st->i] = *(*data)++; (*datalen)--; } /* * pktin->body and pktin->length should identify the semantic * content of the packet, excluding the initial type byte. */ st->pktin->type = st->pktin->data[0]; st->pktin->body = st->pktin->data + 1; st->pktin->length = st->packetlen - 1; /* * Log incoming packet, possibly omitting sensitive fields. */ if (ssh->logctx) ssh2_log_incoming_packet(ssh, st->pktin); st->pktin->savedpos = 0; crFinish(st->pktin); }
CWE-119
8,535
15,905
269741594725008029547070705953926626480
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh2_log_incoming_packet(Ssh ssh, struct Packet *pkt) { int nblanks = 0; struct logblank_t blanks[4]; char *str; int slen; pkt->savedpos = 0; if (ssh->logomitdata && (pkt->type == SSH2_MSG_CHANNEL_DATA || pkt->type == SSH2_MSG_CHANNEL_EXTENDED_DATA)) { /* "Session data" packets - omit the data string. */ ssh_pkt_getuint32(pkt); /* skip channel id */ if (pkt->type == SSH2_MSG_CHANNEL_EXTENDED_DATA) ssh_pkt_getuint32(pkt); /* skip extended data type */ blanks[nblanks].offset = pkt->savedpos + 4; blanks[nblanks].type = PKTLOG_OMIT; ssh_pkt_getstring(pkt, &str, &slen); if (str) { blanks[nblanks].len = slen; nblanks++; } } log_packet(ssh->logctx, PKT_INCOMING, pkt->type, ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, pkt->type), pkt->body, pkt->length, nblanks, blanks, &pkt->sequence, 0, NULL); }
CWE-119
8,536
15,906
19891660967097009691179946206998746674
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh2_log_outgoing_packet(Ssh ssh, struct Packet *pkt) { int nblanks = 0; struct logblank_t blanks[4]; char *str; int slen; /* * For outgoing packets, pkt->length represents the length of the * whole packet starting at pkt->data (including some header), and * pkt->body refers to the point within that where the log-worthy * payload begins. However, incoming packets expect pkt->length to * represent only the payload length (that is, it's measured from * pkt->body not from pkt->data). Temporarily adjust our outgoing * packet to conform to the incoming-packet semantics, so that we * can analyse it with the ssh_pkt_get functions. */ pkt->length -= (pkt->body - pkt->data); pkt->savedpos = 0; if (ssh->logomitdata && (pkt->type == SSH2_MSG_CHANNEL_DATA || pkt->type == SSH2_MSG_CHANNEL_EXTENDED_DATA)) { /* "Session data" packets - omit the data string. */ ssh_pkt_getuint32(pkt); /* skip channel id */ if (pkt->type == SSH2_MSG_CHANNEL_EXTENDED_DATA) ssh_pkt_getuint32(pkt); /* skip extended data type */ blanks[nblanks].offset = pkt->savedpos + 4; blanks[nblanks].type = PKTLOG_OMIT; ssh_pkt_getstring(pkt, &str, &slen); if (str) { blanks[nblanks].len = slen; nblanks++; } } if (pkt->type == SSH2_MSG_USERAUTH_REQUEST && conf_get_int(ssh->conf, CONF_logomitpass)) { /* If this is a password packet, blank the password(s). */ pkt->savedpos = 0; ssh_pkt_getstring(pkt, &str, &slen); ssh_pkt_getstring(pkt, &str, &slen); ssh_pkt_getstring(pkt, &str, &slen); if (slen == 8 && !memcmp(str, "password", 8)) { ssh2_pkt_getbool(pkt); /* Blank the password field. */ blanks[nblanks].offset = pkt->savedpos; blanks[nblanks].type = PKTLOG_BLANK; ssh_pkt_getstring(pkt, &str, &slen); if (str) { blanks[nblanks].len = pkt->savedpos - blanks[nblanks].offset; nblanks++; /* If there's another password field beyond it (change of * password), blank that too. */ ssh_pkt_getstring(pkt, &str, &slen); if (str) blanks[nblanks-1].len = pkt->savedpos - blanks[nblanks].offset; } } } else if (ssh->pkt_actx == SSH2_PKTCTX_KBDINTER && pkt->type == SSH2_MSG_USERAUTH_INFO_RESPONSE && conf_get_int(ssh->conf, CONF_logomitpass)) { /* If this is a keyboard-interactive response packet, blank * the responses. */ pkt->savedpos = 0; ssh_pkt_getuint32(pkt); blanks[nblanks].offset = pkt->savedpos; blanks[nblanks].type = PKTLOG_BLANK; while (1) { ssh_pkt_getstring(pkt, &str, &slen); if (!str) break; } blanks[nblanks].len = pkt->savedpos - blanks[nblanks].offset; nblanks++; } else if (pkt->type == SSH2_MSG_CHANNEL_REQUEST && conf_get_int(ssh->conf, CONF_logomitpass)) { /* * If this is an X forwarding request packet, blank the fake * auth data. * * Note that while we blank the X authentication data here, we * don't take any special action to blank the start of an X11 * channel, so using MIT-MAGIC-COOKIE-1 and actually opening * an X connection without having session blanking enabled is * likely to leak your cookie into the log. */ pkt->savedpos = 0; ssh_pkt_getuint32(pkt); ssh_pkt_getstring(pkt, &str, &slen); if (slen == 7 && !memcmp(str, "x11-req", 0)) { ssh2_pkt_getbool(pkt); ssh2_pkt_getbool(pkt); ssh_pkt_getstring(pkt, &str, &slen); blanks[nblanks].offset = pkt->savedpos; blanks[nblanks].type = PKTLOG_BLANK; ssh_pkt_getstring(pkt, &str, &slen); if (str) { blanks[nblanks].len = pkt->savedpos - blanks[nblanks].offset; nblanks++; } } } log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[5], ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, pkt->data[5]), pkt->body, pkt->length, nblanks, blanks, &ssh->v2_outgoing_sequence, pkt->downstream_id, pkt->additional_log_text); /* * Undo the above adjustment of pkt->length, to put the packet * back in the state we found it. */ pkt->length += (pkt->body - pkt->data); }
CWE-119
8,537
15,907
28952928007105642961246364674291556975
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static unsigned char *ssh2_mpint_fmt(Bignum b, int *len) { unsigned char *p; int i, n = (bignum_bitcount(b) + 7) / 8; p = snewn(n + 1, unsigned char); p[0] = 0; for (i = 1; i <= n; i++) p[i] = bignum_byte(b, n - i); i = 0; while (i <= n && p[i] == 0 && (p[i + 1] & 0x80) == 0) i++; memmove(p, p + i, n + 1 - i); *len = n + 1 - i; return p; }
CWE-119
8,538
15,908
218783200766290158982789690918171438100
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh2_pkt_addbool(struct Packet *pkt, unsigned char value) { ssh_pkt_adddata(pkt, &value, 1); }
CWE-119
8,539
15,909
171303735622802171037782529228916219213
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh2_pkt_addmp(struct Packet *pkt, Bignum b) { unsigned char *p; int len; p = ssh2_mpint_fmt(b, &len); ssh_pkt_addstring_start(pkt); ssh_pkt_addstring_data(pkt, (char *)p, len); sfree(p); }
CWE-119
8,540
15,910
73486966960067117433184892401044485002
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int ssh2_pkt_construct(Ssh ssh, struct Packet *pkt) { int cipherblk, maclen, padding, unencrypted_prefix, i; if (ssh->logctx) ssh2_log_outgoing_packet(ssh, pkt); if (ssh->bare_connection) { /* * Trivial packet construction for the bare connection * protocol. */ PUT_32BIT(pkt->data + 1, pkt->length - 5); pkt->body = pkt->data + 1; ssh->v2_outgoing_sequence++; /* only for diagnostics, really */ return pkt->length - 1; } /* * Compress packet payload. */ { unsigned char *newpayload; int newlen; if (ssh->cscomp && ssh->cscomp->compress(ssh->cs_comp_ctx, pkt->data + 5, pkt->length - 5, &newpayload, &newlen)) { pkt->length = 5; ssh2_pkt_adddata(pkt, newpayload, newlen); sfree(newpayload); } } /* * Add padding. At least four bytes, and must also bring total * length (minus MAC) up to a multiple of the block size. * If pkt->forcepad is set, make sure the packet is at least that size * after padding. */ cipherblk = ssh->cscipher ? ssh->cscipher->blksize : 8; /* block size */ cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */ padding = 4; unencrypted_prefix = (ssh->csmac && ssh->csmac_etm) ? 4 : 0; if (pkt->length + padding < pkt->forcepad) padding = pkt->forcepad - pkt->length; padding += (cipherblk - (pkt->length - unencrypted_prefix + padding) % cipherblk) % cipherblk; assert(padding <= 255); maclen = ssh->csmac ? ssh->csmac->len : 0; ssh2_pkt_ensure(pkt, pkt->length + padding + maclen); pkt->data[4] = padding; for (i = 0; i < padding; i++) pkt->data[pkt->length + i] = random_byte(); PUT_32BIT(pkt->data, pkt->length + padding - 4); /* Encrypt length if the scheme requires it */ if (ssh->cscipher && (ssh->cscipher->flags & SSH_CIPHER_SEPARATE_LENGTH)) { ssh->cscipher->encrypt_length(ssh->cs_cipher_ctx, pkt->data, 4, ssh->v2_outgoing_sequence); } if (ssh->csmac && ssh->csmac_etm) { /* * OpenSSH-defined encrypt-then-MAC protocol. */ if (ssh->cscipher) ssh->cscipher->encrypt(ssh->cs_cipher_ctx, pkt->data + 4, pkt->length + padding - 4); ssh->csmac->generate(ssh->cs_mac_ctx, pkt->data, pkt->length + padding, ssh->v2_outgoing_sequence); } else { /* * SSH-2 standard protocol. */ if (ssh->csmac) ssh->csmac->generate(ssh->cs_mac_ctx, pkt->data, pkt->length + padding, ssh->v2_outgoing_sequence); if (ssh->cscipher) ssh->cscipher->encrypt(ssh->cs_cipher_ctx, pkt->data, pkt->length + padding); } ssh->v2_outgoing_sequence++; /* whether or not we MACed */ pkt->encrypted_len = pkt->length + padding; /* Ready-to-send packet starts at pkt->data. We return length. */ pkt->body = pkt->data; return pkt->length + padding + maclen; }
CWE-119
8,541
15,911
275938596052769069179386423045028369175
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh2_pkt_defer(Ssh ssh, struct Packet *pkt) { if (ssh->queueing) ssh2_pkt_queue(ssh, pkt); else ssh2_pkt_defer_noqueue(ssh, pkt, FALSE); }
CWE-119
8,542
15,912
94432288796183507323298127942191671833
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh2_pkt_defer_noqueue(Ssh ssh, struct Packet *pkt, int noignore) { int len; if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC) && ssh->deferred_len == 0 && !noignore && !(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) { /* * Interpose an SSH_MSG_IGNORE to ensure that user data don't * get encrypted with a known IV. */ struct Packet *ipkt = ssh2_pkt_init(SSH2_MSG_IGNORE); ssh2_pkt_addstring_start(ipkt); ssh2_pkt_defer_noqueue(ssh, ipkt, TRUE); } len = ssh2_pkt_construct(ssh, pkt); if (ssh->deferred_len + len > ssh->deferred_size) { ssh->deferred_size = ssh->deferred_len + len + 128; ssh->deferred_send_data = sresize(ssh->deferred_send_data, ssh->deferred_size, unsigned char); } memcpy(ssh->deferred_send_data + ssh->deferred_len, pkt->body, len); ssh->deferred_len += len; ssh->deferred_data_size += pkt->encrypted_len; ssh_free_packet(pkt); }
CWE-119
8,543
15,913
220827973249406596179515473909962384771
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int ssh2_pkt_getbool(struct Packet *pkt) { unsigned long value; if (pkt->length - pkt->savedpos < 1) return 0; /* arrgh, no way to decline (FIXME?) */ value = pkt->body[pkt->savedpos] != 0; pkt->savedpos++; return value; }
CWE-119
8,544
15,914
303278995874562253277936722244266056767
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static struct Packet *ssh2_pkt_init(int pkt_type) { struct Packet *pkt = ssh_new_packet(); pkt->length = 5; /* space for packet length + padding length */ pkt->forcepad = 0; pkt->type = pkt_type; ssh_pkt_addbyte(pkt, (unsigned char) pkt_type); pkt->body = pkt->data + pkt->length; /* after packet type */ pkt->downstream_id = 0; pkt->additional_log_text = NULL; return pkt; }
CWE-119
8,546
15,915
326870129455951485677747719126381733170
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh2_pkt_queue(Ssh ssh, struct Packet *pkt) { assert(ssh->queueing); if (ssh->queuelen >= ssh->queuesize) { ssh->queuesize = ssh->queuelen + 32; ssh->queue = sresize(ssh->queue, ssh->queuesize, struct Packet *); } ssh->queue[ssh->queuelen++] = pkt; }
CWE-119
8,547
15,916
75010161448844036531781951167931958259
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh2_pkt_queuesend(Ssh ssh) { int i; assert(!ssh->queueing); for (i = 0; i < ssh->queuelen; i++) ssh2_pkt_defer_noqueue(ssh, ssh->queue[i], FALSE); ssh->queuelen = 0; ssh_pkt_defersend(ssh); }
CWE-119
8,548
15,917
212573312958899835631852695564631863999
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh2_pkt_send(Ssh ssh, struct Packet *pkt) { if (ssh->queueing) ssh2_pkt_queue(ssh, pkt); else ssh2_pkt_send_noqueue(ssh, pkt); }
CWE-119
8,549
15,918
328827467313238354655529078844049305486
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh2_pkt_send_noqueue(Ssh ssh, struct Packet *pkt) { int len; int backlog; if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC)) { /* We need to send two packets, so use the deferral mechanism. */ ssh2_pkt_defer_noqueue(ssh, pkt, FALSE); ssh_pkt_defersend(ssh); return; } len = ssh2_pkt_construct(ssh, pkt); backlog = s_write(ssh, pkt->body, len); if (backlog > SSH_MAX_BACKLOG) ssh_throttle_all(ssh, 1, backlog); ssh->outgoing_data_size += pkt->encrypted_len; if (!ssh->kex_in_progress && !ssh->bare_connection && ssh->max_data_size != 0 && ssh->outgoing_data_size > ssh->max_data_size) do_ssh2_transport(ssh, "too much data sent", -1, NULL); ssh_free_packet(pkt); }
CWE-119
8,550
15,919
289194957054072927725618821886112688096
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh2_pkt_send_with_padding(Ssh ssh, struct Packet *pkt, int padsize) { #if 0 if (0) { /* * The simplest way to do this is to adjust the * variable-length padding field in the outgoing packet. * * Currently compiled out, because some Cisco SSH servers * don't like excessively padded packets (bah, why's it * always Cisco?) */ pkt->forcepad = padsize; ssh2_pkt_send(ssh, pkt); } else #endif { /* * If we can't do that, however, an alternative approach is * to use the pkt_defer mechanism to bundle the packet * tightly together with an SSH_MSG_IGNORE such that their * combined length is a constant. So first we construct the * final form of this packet and defer its sending. */ ssh2_pkt_defer(ssh, pkt); /* * Now construct an SSH_MSG_IGNORE which includes a string * that's an exact multiple of the cipher block size. (If * the cipher is NULL so that the block size is * unavailable, we don't do this trick at all, because we * gain nothing by it.) */ if (ssh->cscipher && !(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) { int stringlen, i; stringlen = (256 - ssh->deferred_len); stringlen += ssh->cscipher->blksize - 1; stringlen -= (stringlen % ssh->cscipher->blksize); if (ssh->cscomp) { /* * Temporarily disable actual compression, so we * can guarantee to get this string exactly the * length we want it. The compression-disabling * routine should return an integer indicating how * many bytes we should adjust our string length * by. */ stringlen -= ssh->cscomp->disable_compression(ssh->cs_comp_ctx); } pkt = ssh2_pkt_init(SSH2_MSG_IGNORE); ssh2_pkt_addstring_start(pkt); for (i = 0; i < stringlen; i++) { char c = (char) random_byte(); ssh2_pkt_addstring_data(pkt, &c, 1); } ssh2_pkt_defer(ssh, pkt); } ssh_pkt_defersend(ssh); } }
CWE-119
8,551
15,920
330715098131383827482364737505483044467
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static const char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type) { translatea(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,SSH2_PKTCTX_GSSAPI); translatea(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,SSH2_PKTCTX_GSSAPI); translatea(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,SSH2_PKTCTX_GSSAPI); translatea(SSH2_MSG_USERAUTH_GSSAPI_ERROR,SSH2_PKTCTX_GSSAPI); translatea(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,SSH2_PKTCTX_GSSAPI); translatea(SSH2_MSG_USERAUTH_GSSAPI_MIC, SSH2_PKTCTX_GSSAPI); translate(SSH2_MSG_DISCONNECT); translate(SSH2_MSG_IGNORE); translate(SSH2_MSG_UNIMPLEMENTED); translate(SSH2_MSG_DEBUG); translate(SSH2_MSG_SERVICE_REQUEST); translate(SSH2_MSG_SERVICE_ACCEPT); translate(SSH2_MSG_KEXINIT); translate(SSH2_MSG_NEWKEYS); translatek(SSH2_MSG_KEXDH_INIT, SSH2_PKTCTX_DHGROUP); translatek(SSH2_MSG_KEXDH_REPLY, SSH2_PKTCTX_DHGROUP); translatek(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD, SSH2_PKTCTX_DHGEX); translatek(SSH2_MSG_KEX_DH_GEX_REQUEST, SSH2_PKTCTX_DHGEX); translatek(SSH2_MSG_KEX_DH_GEX_GROUP, SSH2_PKTCTX_DHGEX); translatek(SSH2_MSG_KEX_DH_GEX_INIT, SSH2_PKTCTX_DHGEX); translatek(SSH2_MSG_KEX_DH_GEX_REPLY, SSH2_PKTCTX_DHGEX); translatek(SSH2_MSG_KEXRSA_PUBKEY, SSH2_PKTCTX_RSAKEX); translatek(SSH2_MSG_KEXRSA_SECRET, SSH2_PKTCTX_RSAKEX); translatek(SSH2_MSG_KEXRSA_DONE, SSH2_PKTCTX_RSAKEX); translatek(SSH2_MSG_KEX_ECDH_INIT, SSH2_PKTCTX_ECDHKEX); translatek(SSH2_MSG_KEX_ECDH_REPLY, SSH2_PKTCTX_ECDHKEX); translate(SSH2_MSG_USERAUTH_REQUEST); translate(SSH2_MSG_USERAUTH_FAILURE); translate(SSH2_MSG_USERAUTH_SUCCESS); translate(SSH2_MSG_USERAUTH_BANNER); translatea(SSH2_MSG_USERAUTH_PK_OK, SSH2_PKTCTX_PUBLICKEY); translatea(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, SSH2_PKTCTX_PASSWORD); translatea(SSH2_MSG_USERAUTH_INFO_REQUEST, SSH2_PKTCTX_KBDINTER); translatea(SSH2_MSG_USERAUTH_INFO_RESPONSE, SSH2_PKTCTX_KBDINTER); translate(SSH2_MSG_GLOBAL_REQUEST); translate(SSH2_MSG_REQUEST_SUCCESS); translate(SSH2_MSG_REQUEST_FAILURE); translate(SSH2_MSG_CHANNEL_OPEN); translate(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); translate(SSH2_MSG_CHANNEL_OPEN_FAILURE); translate(SSH2_MSG_CHANNEL_WINDOW_ADJUST); translate(SSH2_MSG_CHANNEL_DATA); translate(SSH2_MSG_CHANNEL_EXTENDED_DATA); translate(SSH2_MSG_CHANNEL_EOF); translate(SSH2_MSG_CHANNEL_CLOSE); translate(SSH2_MSG_CHANNEL_REQUEST); translate(SSH2_MSG_CHANNEL_SUCCESS); translate(SSH2_MSG_CHANNEL_FAILURE); return "unknown"; }
CWE-119
8,552
15,921
61087804134680965527155923482538836142
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static struct Packet *ssh2_rdpkt(Ssh ssh, const unsigned char **data, int *datalen) { struct rdpkt2_state_tag *st = &ssh->rdpkt2_state; crBegin(ssh->ssh2_rdpkt_crstate); st->pktin = ssh_new_packet(); st->pktin->type = 0; st->pktin->length = 0; if (ssh->sccipher) st->cipherblk = ssh->sccipher->blksize; else st->cipherblk = 8; if (st->cipherblk < 8) st->cipherblk = 8; st->maclen = ssh->scmac ? ssh->scmac->len : 0; if (ssh->sccipher && (ssh->sccipher->flags & SSH_CIPHER_IS_CBC) && ssh->scmac && !ssh->scmac_etm) { /* * When dealing with a CBC-mode cipher, we want to avoid the * possibility of an attacker's tweaking the ciphertext stream * so as to cause us to feed the same block to the block * cipher more than once and thus leak information * (VU#958563). The way we do this is not to take any * decisions on the basis of anything we've decrypted until * we've verified it with a MAC. That includes the packet * length, so we just read data and check the MAC repeatedly, * and when the MAC passes, see if the length we've got is * plausible. * * This defence is unnecessary in OpenSSH ETM mode, because * the whole point of ETM mode is that the attacker can't * tweak the ciphertext stream at all without the MAC * detecting it before we decrypt anything. */ /* May as well allocate the whole lot now. */ st->pktin->data = snewn(OUR_V2_PACKETLIMIT + st->maclen + APIEXTRA, unsigned char); /* Read an amount corresponding to the MAC. */ for (st->i = 0; st->i < st->maclen; st->i++) { while ((*datalen) == 0) crReturn(NULL); st->pktin->data[st->i] = *(*data)++; (*datalen)--; } st->packetlen = 0; { unsigned char seq[4]; ssh->scmac->start(ssh->sc_mac_ctx); PUT_32BIT(seq, st->incoming_sequence); ssh->scmac->bytes(ssh->sc_mac_ctx, seq, 4); } for (;;) { /* Once around this loop per cipher block. */ /* Read another cipher-block's worth, and tack it onto the end. */ for (st->i = 0; st->i < st->cipherblk; st->i++) { while ((*datalen) == 0) crReturn(NULL); st->pktin->data[st->packetlen+st->maclen+st->i] = *(*data)++; (*datalen)--; } /* Decrypt one more block (a little further back in the stream). */ ssh->sccipher->decrypt(ssh->sc_cipher_ctx, st->pktin->data + st->packetlen, st->cipherblk); /* Feed that block to the MAC. */ ssh->scmac->bytes(ssh->sc_mac_ctx, st->pktin->data + st->packetlen, st->cipherblk); st->packetlen += st->cipherblk; /* See if that gives us a valid packet. */ if (ssh->scmac->verresult(ssh->sc_mac_ctx, st->pktin->data + st->packetlen) && ((st->len = toint(GET_32BIT(st->pktin->data))) == st->packetlen-4)) break; if (st->packetlen >= OUR_V2_PACKETLIMIT) { bombout(("No valid incoming packet found")); ssh_free_packet(st->pktin); crStop(NULL); } } st->pktin->maxlen = st->packetlen + st->maclen; st->pktin->data = sresize(st->pktin->data, st->pktin->maxlen + APIEXTRA, unsigned char); } else if (ssh->scmac && ssh->scmac_etm) { st->pktin->data = snewn(4 + APIEXTRA, unsigned char); /* * OpenSSH encrypt-then-MAC mode: the packet length is * unencrypted, unless the cipher supports length encryption. */ for (st->i = st->len = 0; st->i < 4; st->i++) { while ((*datalen) == 0) crReturn(NULL); st->pktin->data[st->i] = *(*data)++; (*datalen)--; } /* Cipher supports length decryption, so do it */ if (ssh->sccipher && (ssh->sccipher->flags & SSH_CIPHER_SEPARATE_LENGTH)) { /* Keep the packet the same though, so the MAC passes */ unsigned char len[4]; memcpy(len, st->pktin->data, 4); ssh->sccipher->decrypt_length(ssh->sc_cipher_ctx, len, 4, st->incoming_sequence); st->len = toint(GET_32BIT(len)); } else { st->len = toint(GET_32BIT(st->pktin->data)); } /* * _Completely_ silly lengths should be stomped on before they * do us any more damage. */ if (st->len < 0 || st->len > OUR_V2_PACKETLIMIT || st->len % st->cipherblk != 0) { bombout(("Incoming packet length field was garbled")); ssh_free_packet(st->pktin); crStop(NULL); } /* * So now we can work out the total packet length. */ st->packetlen = st->len + 4; /* * Allocate memory for the rest of the packet. */ st->pktin->maxlen = st->packetlen + st->maclen; st->pktin->data = sresize(st->pktin->data, st->pktin->maxlen + APIEXTRA, unsigned char); /* * Read the remainder of the packet. */ for (st->i = 4; st->i < st->packetlen + st->maclen; st->i++) { while ((*datalen) == 0) crReturn(NULL); st->pktin->data[st->i] = *(*data)++; (*datalen)--; } /* * Check the MAC. */ if (ssh->scmac && !ssh->scmac->verify(ssh->sc_mac_ctx, st->pktin->data, st->len + 4, st->incoming_sequence)) { bombout(("Incorrect MAC received on packet")); ssh_free_packet(st->pktin); crStop(NULL); } /* Decrypt everything between the length field and the MAC. */ if (ssh->sccipher) ssh->sccipher->decrypt(ssh->sc_cipher_ctx, st->pktin->data + 4, st->packetlen - 4); } else { st->pktin->data = snewn(st->cipherblk + APIEXTRA, unsigned char); /* * Acquire and decrypt the first block of the packet. This will * contain the length and padding details. */ for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) { while ((*datalen) == 0) crReturn(NULL); st->pktin->data[st->i] = *(*data)++; (*datalen)--; } if (ssh->sccipher) ssh->sccipher->decrypt(ssh->sc_cipher_ctx, st->pktin->data, st->cipherblk); /* * Now get the length figure. */ st->len = toint(GET_32BIT(st->pktin->data)); /* * _Completely_ silly lengths should be stomped on before they * do us any more damage. */ if (st->len < 0 || st->len > OUR_V2_PACKETLIMIT || (st->len + 4) % st->cipherblk != 0) { bombout(("Incoming packet was garbled on decryption")); ssh_free_packet(st->pktin); crStop(NULL); } /* * So now we can work out the total packet length. */ st->packetlen = st->len + 4; /* * Allocate memory for the rest of the packet. */ st->pktin->maxlen = st->packetlen + st->maclen; st->pktin->data = sresize(st->pktin->data, st->pktin->maxlen + APIEXTRA, unsigned char); /* * Read and decrypt the remainder of the packet. */ for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen; st->i++) { while ((*datalen) == 0) crReturn(NULL); st->pktin->data[st->i] = *(*data)++; (*datalen)--; } /* Decrypt everything _except_ the MAC. */ if (ssh->sccipher) ssh->sccipher->decrypt(ssh->sc_cipher_ctx, st->pktin->data + st->cipherblk, st->packetlen - st->cipherblk); /* * Check the MAC. */ if (ssh->scmac && !ssh->scmac->verify(ssh->sc_mac_ctx, st->pktin->data, st->len + 4, st->incoming_sequence)) { bombout(("Incorrect MAC received on packet")); ssh_free_packet(st->pktin); crStop(NULL); } } /* Get and sanity-check the amount of random padding. */ st->pad = st->pktin->data[4]; if (st->pad < 4 || st->len - st->pad < 1) { bombout(("Invalid padding length on received packet")); ssh_free_packet(st->pktin); crStop(NULL); } /* * This enables us to deduce the payload length. */ st->payload = st->len - st->pad - 1; st->pktin->length = st->payload + 5; st->pktin->encrypted_len = st->packetlen; st->pktin->sequence = st->incoming_sequence++; st->pktin->length = st->packetlen - st->pad; assert(st->pktin->length >= 0); /* * Decompress packet payload. */ { unsigned char *newpayload; int newlen; if (ssh->sccomp && ssh->sccomp->decompress(ssh->sc_comp_ctx, st->pktin->data + 5, st->pktin->length - 5, &newpayload, &newlen)) { if (st->pktin->maxlen < newlen + 5) { st->pktin->maxlen = newlen + 5; st->pktin->data = sresize(st->pktin->data, st->pktin->maxlen + APIEXTRA, unsigned char); } st->pktin->length = 5 + newlen; memcpy(st->pktin->data + 5, newpayload, newlen); sfree(newpayload); } } /* * RFC 4253 doesn't explicitly say that completely empty packets * with no type byte are forbidden, so treat them as deserving * an SSH_MSG_UNIMPLEMENTED. */ if (st->pktin->length <= 5) { /* == 5 we hope, but robustness */ ssh2_msg_something_unimplemented(ssh, st->pktin); crStop(NULL); } /* * pktin->body and pktin->length should identify the semantic * content of the packet, excluding the initial type byte. */ st->pktin->type = st->pktin->data[5]; st->pktin->body = st->pktin->data + 6; st->pktin->length -= 6; assert(st->pktin->length >= 0); /* one last double-check */ if (ssh->logctx) ssh2_log_incoming_packet(ssh, st->pktin); st->pktin->savedpos = 0; crFinish(st->pktin); }
CWE-119
8,553
15,922
16959874014768988310772760555927561227
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int ssh_channelfind(void *av, void *bv) { unsigned *a = (unsigned *) av; struct ssh_channel *b = (struct ssh_channel *) bv; if (*a < b->localid) return -1; if (*a > b->localid) return +1; return 0; }
CWE-119
8,555
15,923
179229902878804666950341280492920398519
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int ssh_closing(Plug plug, const char *error_msg, int error_code, int calling_back) { Ssh ssh = (Ssh) plug; int need_notify = ssh_do_close(ssh, FALSE); if (!error_msg) { if (!ssh->close_expected) error_msg = "Server unexpectedly closed network connection"; else error_msg = "Server closed network connection"; } if (ssh->close_expected && ssh->clean_exit && ssh->exitcode < 0) ssh->exitcode = 0; if (need_notify) notify_remote_exit(ssh->frontend); if (error_msg) logevent(error_msg); if (!ssh->close_expected || !ssh->clean_exit) connection_fatal(ssh->frontend, "%s", error_msg); return 0; }
CWE-119
8,556
15,924
63542602278606942380914366651544388381
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int ssh_comp_none_block(void *handle, unsigned char *block, int len, unsigned char **outblock, int *outlen) { return 0; }
CWE-119
8,557
15,925
277141377856308184734188025596738375074
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_comp_none_cleanup(void *handle) { }
CWE-119
8,558
15,926
136871537284858747086761193553742291745
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int ssh_comp_none_disable(void *handle) { return 0; }
CWE-119
8,559
15,927
137967881984128301590046764136149602313
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void *ssh_comp_none_init(void) { return NULL; }
CWE-119
8,560
15,928
168116434342731647055074714962622514516
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
void ssh_connshare_log(Ssh ssh, int event, const char *logtext, const char *ds_err, const char *us_err) { if (event == SHARE_NONE) { /* In this case, 'logtext' is an error message indicating a * reason why connection sharing couldn't be set up _at all_. * Failing that, ds_err and us_err indicate why we couldn't be * a downstream and an upstream respectively. */ if (logtext) { logeventf(ssh, "Could not set up connection sharing: %s", logtext); } else { if (ds_err) logeventf(ssh, "Could not set up connection sharing" " as downstream: %s", ds_err); if (us_err) logeventf(ssh, "Could not set up connection sharing" " as upstream: %s", us_err); } } else if (event == SHARE_DOWNSTREAM) { /* In this case, 'logtext' is a local endpoint address */ logeventf(ssh, "Using existing shared connection at %s", logtext); /* Also we should mention this in the console window to avoid * confusing users as to why this window doesn't behave the * usual way. */ if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) { c_write_str(ssh,"Reusing a shared connection to this server.\r\n"); } } else if (event == SHARE_UPSTREAM) { /* In this case, 'logtext' is a local endpoint address too */ logeventf(ssh, "Sharing this connection at %s", logtext); } }
CWE-119
8,561
15,929
181398193300213563727695058059432390400
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_detect_bugs(Ssh ssh, char *vstring) { char *imp; /* pointer to implementation part */ imp = vstring; imp += strcspn(imp, "-"); if (*imp) imp++; imp += strcspn(imp, "-"); if (*imp) imp++; ssh->remote_bugs = 0; /* * General notes on server version strings: * - Not all servers reporting "Cisco-1.25" have all the bugs listed * here -- in particular, we've heard of one that's perfectly happy * with SSH1_MSG_IGNOREs -- but this string never seems to change, * so we can't distinguish them. */ if (conf_get_int(ssh->conf, CONF_sshbug_ignore1) == FORCE_ON || (conf_get_int(ssh->conf, CONF_sshbug_ignore1) == AUTO && (!strcmp(imp, "1.2.18") || !strcmp(imp, "1.2.19") || !strcmp(imp, "1.2.20") || !strcmp(imp, "1.2.21") || !strcmp(imp, "1.2.22") || !strcmp(imp, "Cisco-1.25") || !strcmp(imp, "OSU_1.4alpha3") || !strcmp(imp, "OSU_1.5alpha4")))) { /* * These versions don't support SSH1_MSG_IGNORE, so we have * to use a different defence against password length * sniffing. */ ssh->remote_bugs |= BUG_CHOKES_ON_SSH1_IGNORE; logevent("We believe remote version has SSH-1 ignore bug"); } if (conf_get_int(ssh->conf, CONF_sshbug_plainpw1) == FORCE_ON || (conf_get_int(ssh->conf, CONF_sshbug_plainpw1) == AUTO && (!strcmp(imp, "Cisco-1.25") || !strcmp(imp, "OSU_1.4alpha3")))) { /* * These versions need a plain password sent; they can't * handle having a null and a random length of data after * the password. */ ssh->remote_bugs |= BUG_NEEDS_SSH1_PLAIN_PASSWORD; logevent("We believe remote version needs a plain SSH-1 password"); } if (conf_get_int(ssh->conf, CONF_sshbug_rsa1) == FORCE_ON || (conf_get_int(ssh->conf, CONF_sshbug_rsa1) == AUTO && (!strcmp(imp, "Cisco-1.25")))) { /* * These versions apparently have no clue whatever about * RSA authentication and will panic and die if they see * an AUTH_RSA message. */ ssh->remote_bugs |= BUG_CHOKES_ON_RSA; logevent("We believe remote version can't handle SSH-1 RSA authentication"); } if (conf_get_int(ssh->conf, CONF_sshbug_hmac2) == FORCE_ON || (conf_get_int(ssh->conf, CONF_sshbug_hmac2) == AUTO && !wc_match("* VShell", imp) && (wc_match("2.1.0*", imp) || wc_match("2.0.*", imp) || wc_match("2.2.0*", imp) || wc_match("2.3.0*", imp) || wc_match("2.1 *", imp)))) { /* * These versions have the HMAC bug. */ ssh->remote_bugs |= BUG_SSH2_HMAC; logevent("We believe remote version has SSH-2 HMAC bug"); } if (conf_get_int(ssh->conf, CONF_sshbug_derivekey2) == FORCE_ON || (conf_get_int(ssh->conf, CONF_sshbug_derivekey2) == AUTO && !wc_match("* VShell", imp) && (wc_match("2.0.0*", imp) || wc_match("2.0.10*", imp) ))) { /* * These versions have the key-derivation bug (failing to * include the literal shared secret in the hashes that * generate the keys). */ ssh->remote_bugs |= BUG_SSH2_DERIVEKEY; logevent("We believe remote version has SSH-2 key-derivation bug"); } if (conf_get_int(ssh->conf, CONF_sshbug_rsapad2) == FORCE_ON || (conf_get_int(ssh->conf, CONF_sshbug_rsapad2) == AUTO && (wc_match("OpenSSH_2.[5-9]*", imp) || wc_match("OpenSSH_3.[0-2]*", imp) || wc_match("mod_sftp/0.[0-8]*", imp) || wc_match("mod_sftp/0.9.[0-8]", imp)))) { /* * These versions have the SSH-2 RSA padding bug. */ ssh->remote_bugs |= BUG_SSH2_RSA_PADDING; logevent("We believe remote version has SSH-2 RSA padding bug"); } if (conf_get_int(ssh->conf, CONF_sshbug_pksessid2) == FORCE_ON || (conf_get_int(ssh->conf, CONF_sshbug_pksessid2) == AUTO && wc_match("OpenSSH_2.[0-2]*", imp))) { /* * These versions have the SSH-2 session-ID bug in * public-key authentication. */ ssh->remote_bugs |= BUG_SSH2_PK_SESSIONID; logevent("We believe remote version has SSH-2 public-key-session-ID bug"); } if (conf_get_int(ssh->conf, CONF_sshbug_rekey2) == FORCE_ON || (conf_get_int(ssh->conf, CONF_sshbug_rekey2) == AUTO && (wc_match("DigiSSH_2.0", imp) || wc_match("OpenSSH_2.[0-4]*", imp) || wc_match("OpenSSH_2.5.[0-3]*", imp) || wc_match("Sun_SSH_1.0", imp) || wc_match("Sun_SSH_1.0.1", imp) || /* All versions <= 1.2.6 (they changed their format in 1.2.7) */ wc_match("WeOnlyDo-*", imp)))) { /* * These versions have the SSH-2 rekey bug. */ ssh->remote_bugs |= BUG_SSH2_REKEY; logevent("We believe remote version has SSH-2 rekey bug"); } if (conf_get_int(ssh->conf, CONF_sshbug_maxpkt2) == FORCE_ON || (conf_get_int(ssh->conf, CONF_sshbug_maxpkt2) == AUTO && (wc_match("1.36_sshlib GlobalSCAPE", imp) || wc_match("1.36 sshlib: GlobalScape", imp)))) { /* * This version ignores our makpkt and needs to be throttled. */ ssh->remote_bugs |= BUG_SSH2_MAXPKT; logevent("We believe remote version ignores SSH-2 maximum packet size"); } if (conf_get_int(ssh->conf, CONF_sshbug_ignore2) == FORCE_ON) { /* * Servers that don't support SSH2_MSG_IGNORE. Currently, * none detected automatically. */ ssh->remote_bugs |= BUG_CHOKES_ON_SSH2_IGNORE; logevent("We believe remote version has SSH-2 ignore bug"); } if (conf_get_int(ssh->conf, CONF_sshbug_oldgex2) == FORCE_ON || (conf_get_int(ssh->conf, CONF_sshbug_oldgex2) == AUTO && (wc_match("OpenSSH_2.[235]*", imp)))) { /* * These versions only support the original (pre-RFC4419) * SSH-2 GEX request, and disconnect with a protocol error if * we use the newer version. */ ssh->remote_bugs |= BUG_SSH2_OLDGEX; logevent("We believe remote version has outdated SSH-2 GEX"); } if (conf_get_int(ssh->conf, CONF_sshbug_winadj) == FORCE_ON) { /* * Servers that don't support our winadj request for one * reason or another. Currently, none detected automatically. */ ssh->remote_bugs |= BUG_CHOKES_ON_WINADJ; logevent("We believe remote version has winadj bug"); } if (conf_get_int(ssh->conf, CONF_sshbug_chanreq) == FORCE_ON || (conf_get_int(ssh->conf, CONF_sshbug_chanreq) == AUTO && (wc_match("OpenSSH_[2-5].*", imp) || wc_match("OpenSSH_6.[0-6]*", imp) || wc_match("dropbear_0.[2-4][0-9]*", imp) || wc_match("dropbear_0.5[01]*", imp)))) { /* * These versions have the SSH-2 channel request bug. * OpenSSH 6.7 and above do not: * https://bugzilla.mindrot.org/show_bug.cgi?id=1818 * dropbear_0.52 and above do not: * https://secure.ucc.asn.au/hg/dropbear/rev/cd02449b709c */ ssh->remote_bugs |= BUG_SENDS_LATE_REQUEST_REPLY; logevent("We believe remote version has SSH-2 channel request bug"); } }
CWE-119
8,562
15,930
16688036074493196405684931165425667437
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int ssh_do_close(Ssh ssh, int notify_exit) { int ret = 0; struct ssh_channel *c; ssh->state = SSH_STATE_CLOSED; expire_timer_context(ssh); if (ssh->s) { sk_close(ssh->s); ssh->s = NULL; if (notify_exit) notify_remote_exit(ssh->frontend); else ret = 1; } /* * Now we must shut down any port- and X-forwarded channels going * through this connection. */ if (ssh->channels) { while (NULL != (c = index234(ssh->channels, 0))) { ssh_channel_close_local(c, NULL); del234(ssh->channels, c); /* moving next one to index 0 */ if (ssh->version == 2) bufchain_clear(&c->v.v2.outbuffer); sfree(c); } } /* * Go through port-forwardings, and close any associated * listening sockets. */ if (ssh->portfwds) { struct ssh_portfwd *pf; while (NULL != (pf = index234(ssh->portfwds, 0))) { /* Dispose of any listening socket. */ if (pf->local) pfl_terminate(pf->local); del234(ssh->portfwds, pf); /* moving next one to index 0 */ free_portfwd(pf); } freetree234(ssh->portfwds); ssh->portfwds = NULL; } /* * Also stop attempting to connection-share. */ if (ssh->connshare) { sharestate_free(ssh->connshare); ssh->connshare = NULL; } return ret; }
CWE-119
8,563
15,931
26559473794661968887588769912721446824
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_fix_verstring(char *str) { /* Eat "<protoversion>-". */ while (*str && *str != '-') str++; assert(*str == '-'); str++; /* Convert minus signs and spaces in the remaining string into * underscores. */ while (*str) { if (*str == '-' || *str == ' ') *str = '_'; str++; } }
CWE-119
8,564
15,932
132079971942704045124682724392090256618
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_free_packet(struct Packet *pkt) { sfree(pkt->data); sfree(pkt); }
CWE-119
8,565
15,933
282036113130683845071205301138341992776
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_gotdata(Ssh ssh, const unsigned char *data, int datalen) { /* Log raw data, if we're in that mode. */ if (ssh->logctx) log_packet(ssh->logctx, PKT_INCOMING, -1, NULL, data, datalen, 0, NULL, NULL, 0, NULL); crBegin(ssh->ssh_gotdata_crstate); /* * To begin with, feed the characters one by one to the * protocol initialisation / selection function do_ssh_init(). * When that returns 0, we're done with the initial greeting * exchange and can move on to packet discipline. */ while (1) { int ret; /* need not be kept across crReturn */ if (datalen == 0) crReturnV; /* more data please */ ret = ssh->do_ssh_init(ssh, *data); data++; datalen--; if (ret == 0) break; } /* * We emerge from that loop when the initial negotiation is * over and we have selected an s_rdpkt function. Now pass * everything to s_rdpkt, and then pass the resulting packets * to the proper protocol handler. */ while (1) { while (bufchain_size(&ssh->queued_incoming_data) > 0 || datalen > 0) { if (ssh->frozen) { ssh_queue_incoming_data(ssh, &data, &datalen); /* This uses up all data and cannot cause anything interesting * to happen; indeed, for anything to happen at all, we must * return, so break out. */ break; } else if (bufchain_size(&ssh->queued_incoming_data) > 0) { /* This uses up some or all data, and may freeze the * session. */ ssh_process_queued_incoming_data(ssh); } else { /* This uses up some or all data, and may freeze the * session. */ ssh_process_incoming_data(ssh, &data, &datalen); } /* FIXME this is probably EBW. */ if (ssh->state == SSH_STATE_CLOSED) return; } /* We're out of data. Go and get some more. */ crReturnV; } crFinishV; }
CWE-119
8,566
15,934
271392582219673837637097888552032060141
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_hostport_setup(const char *host, int port, Conf *conf, char **savedhost, int *savedport, char **loghost_ret) { char *loghost = conf_get_str(conf, CONF_loghost); if (loghost_ret) *loghost_ret = loghost; if (*loghost) { char *tmphost; char *colon; tmphost = dupstr(loghost); *savedport = 22; /* default ssh port */ /* * A colon suffix on the hostname string also lets us affect * savedport. (Unless there are multiple colons, in which case * we assume this is an unbracketed IPv6 literal.) */ colon = host_strrchr(tmphost, ':'); if (colon && colon == host_strchr(tmphost, ':')) { *colon++ = '\0'; if (*colon) *savedport = atoi(colon); } *savedhost = host_strduptrim(tmphost); sfree(tmphost); } else { *savedhost = host_strduptrim(host); if (port < 0) port = 22; /* default ssh port */ *savedport = port; } }
CWE-119
8,567
15,935
278715791568813773533371732149640147667
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static struct Packet *ssh_new_packet(void) { struct Packet *pkt = snew(struct Packet); pkt->body = pkt->data = NULL; pkt->maxlen = 0; return pkt; }
CWE-119
8,568
15,936
158007946012211653187465917877374359155
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_pkt_addstring(struct Packet *pkt, const char *data) { ssh_pkt_addstring_start(pkt); ssh_pkt_addstring_str(pkt, data); }
CWE-119
8,571
15,937
74660900349737269525228724271190309115
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_pkt_addstring_start(struct Packet *pkt) { ssh_pkt_adduint32(pkt, 0); pkt->savedpos = pkt->length; }
CWE-119
8,573
15,938
49395045714738786897114439137647861733
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_pkt_addstring_str(struct Packet *pkt, const char *data) { ssh_pkt_addstring_data(pkt, data, strlen(data)); }
CWE-119
8,574
15,939
52632729765945584908695751540015779809
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_pkt_adduint32(struct Packet *pkt, unsigned long value) { unsigned char x[4]; PUT_32BIT(x, value); ssh_pkt_adddata(pkt, x, 4); }
CWE-119
8,575
15,940
192273665588630959690164066606325320605
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_pkt_defersend(Ssh ssh) { int backlog; backlog = s_write(ssh, ssh->deferred_send_data, ssh->deferred_len); ssh->deferred_len = ssh->deferred_size = 0; sfree(ssh->deferred_send_data); ssh->deferred_send_data = NULL; if (backlog > SSH_MAX_BACKLOG) ssh_throttle_all(ssh, 1, backlog); if (ssh->version == 2) { ssh->outgoing_data_size += ssh->deferred_data_size; ssh->deferred_data_size = 0; if (!ssh->kex_in_progress && !ssh->bare_connection && ssh->max_data_size != 0 && ssh->outgoing_data_size > ssh->max_data_size) do_ssh2_transport(ssh, "too much data sent", -1, NULL); } }
CWE-119
8,576
15,941
76839359403083738306160506389110730612
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_pkt_ensure(struct Packet *pkt, int length) { if (pkt->maxlen < length) { unsigned char *body = pkt->body; int offset = body ? body - pkt->data : 0; pkt->maxlen = length + 256; pkt->data = sresize(pkt->data, pkt->maxlen + APIEXTRA, unsigned char); if (body) pkt->body = pkt->data + offset; } }
CWE-119
8,577
15,942
289847091193334489999082425533952132635
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length) { int len; *p = NULL; *length = 0; if (pkt->length - pkt->savedpos < 4) return; len = toint(GET_32BIT(pkt->body + pkt->savedpos)); if (len < 0) return; *length = len; pkt->savedpos += 4; if (pkt->length - pkt->savedpos < *length) return; *p = (char *)(pkt->body + pkt->savedpos); pkt->savedpos += *length; }
CWE-119
8,579
15,943
151401641392909003224538040591826942794
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static unsigned long ssh_pkt_getuint32(struct Packet *pkt) { unsigned long value; if (pkt->length - pkt->savedpos < 4) return 0; /* arrgh, no way to decline (FIXME?) */ value = GET_32BIT(pkt->body + pkt->savedpos); pkt->savedpos += 4; return value; }
CWE-119
8,580
15,944
225178251622248264304328477013407180465
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static const char *ssh_pkt_type(Ssh ssh, int type) { if (ssh->version == 1) return ssh1_pkt_type(type); else return ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, type); }
CWE-119
8,581
15,945
18837290965495418195356028423221436827
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int ssh_portcmp(void *av, void *bv) { struct ssh_portfwd *a = (struct ssh_portfwd *) av; struct ssh_portfwd *b = (struct ssh_portfwd *) bv; int i; if (a->type > b->type) return +1; if (a->type < b->type) return -1; if (a->addressfamily > b->addressfamily) return +1; if (a->addressfamily < b->addressfamily) return -1; if ( (i = nullstrcmp(a->saddr, b->saddr)) != 0) return i < 0 ? -1 : +1; if (a->sport > b->sport) return +1; if (a->sport < b->sport) return -1; if (a->type != 'D') { if ( (i = nullstrcmp(a->daddr, b->daddr)) != 0) return i < 0 ? -1 : +1; if (a->dport > b->dport) return +1; if (a->dport < b->dport) return -1; } return 0; }
CWE-119
8,582
15,946
255188720706790389657578712126288804589
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_process_incoming_data(Ssh ssh, const unsigned char **data, int *datalen) { struct Packet *pktin; pktin = ssh->s_rdpkt(ssh, data, datalen); if (pktin) { ssh->protocol(ssh, NULL, 0, pktin); ssh_free_packet(pktin); } }
CWE-119
8,583
15,947
133053065281277132507577187066787834060
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_process_queued_incoming_data(Ssh ssh) { void *vdata; const unsigned char *data; int len, origlen; while (!ssh->frozen && bufchain_size(&ssh->queued_incoming_data)) { bufchain_prefix(&ssh->queued_incoming_data, &vdata, &len); data = vdata; origlen = len; while (!ssh->frozen && len > 0) ssh_process_incoming_data(ssh, &data, &len); if (origlen > len) bufchain_consume(&ssh->queued_incoming_data, origlen - len); } }
CWE-119
8,584
15,948
220307873025183977705228152237897561700
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_queue_incoming_data(Ssh ssh, const unsigned char **data, int *datalen) { bufchain_add(&ssh->queued_incoming_data, *data, *datalen); *data += *datalen; *datalen = 0; }
CWE-119
8,585
15,949
11985463837882270223376259837488036104
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int ssh_receive(Plug plug, int urgent, char *data, int len) { Ssh ssh = (Ssh) plug; ssh_gotdata(ssh, (unsigned char *)data, len); if (ssh->state == SSH_STATE_CLOSED) { ssh_do_close(ssh, TRUE); return 0; } return 1; }
CWE-119
8,586
15,950
275860688972757442213742612376958863669
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int ssh_rportcmp_ssh1(void *av, void *bv) { struct ssh_rportfwd *a = (struct ssh_rportfwd *) av; struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv; int i; if ( (i = strcmp(a->dhost, b->dhost)) != 0) return i < 0 ? -1 : +1; if (a->dport > b->dport) return +1; if (a->dport < b->dport) return -1; return 0; }
CWE-119
8,587
15,951
136873118504627476070579779022782491207
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int ssh_rportcmp_ssh2(void *av, void *bv) { struct ssh_rportfwd *a = (struct ssh_rportfwd *) av; struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv; int i; if ( (i = strcmp(a->shost, b->shost)) != 0) return i < 0 ? -1 : +1; if (a->sport > b->sport) return +1; if (a->sport < b->sport) return -1; return 0; }
CWE-119
8,588
15,952
339918513357860015862526734380718900119
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_send_verstring(Ssh ssh, const char *protoname, char *svers) { char *verstring; if (ssh->version == 2) { /* * Construct a v2 version string. */ verstring = dupprintf("%s2.0-%s\015\012", protoname, sshver); } else { /* * Construct a v1 version string. */ assert(!strcmp(protoname, "SSH-")); /* no v1 bare connection protocol */ verstring = dupprintf("SSH-%s-%s\012", (ssh_versioncmp(svers, "1.5") <= 0 ? svers : "1.5"), sshver); } ssh_fix_verstring(verstring + strlen(protoname)); #ifdef FUZZING /* FUZZING make PuTTY insecure, so make live use difficult. */ verstring[0] = 'I'; #endif if (ssh->version == 2) { size_t len; /* * Record our version string. */ len = strcspn(verstring, "\015\012"); ssh->v_c = snewn(len + 1, char); memcpy(ssh->v_c, verstring, len); ssh->v_c[len] = 0; } logeventf(ssh, "We claim version: %.*s", strcspn(verstring, "\015\012"), verstring); s_write(ssh, verstring, strlen(verstring)); sfree(verstring); }
CWE-119
8,589
15,953
120628138213746654151666215850592247021
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_sent(Plug plug, int bufsize) { Ssh ssh = (Ssh) plug; /* * If the send backlog on the SSH socket itself clears, we * should unthrottle the whole world if it was throttled. */ if (bufsize < SSH_MAX_BACKLOG) ssh_throttle_all(ssh, 0, bufsize); }
CWE-119
8,590
15,954
186484141585362802101962931811937153770
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_set_frozen(Ssh ssh, int frozen) { if (ssh->s) sk_set_frozen(ssh->s, frozen); ssh->frozen = frozen; }
CWE-119
8,591
15,955
152663698814473838723880144332130669949
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static int ssh_test_for_upstream(const char *host, int port, Conf *conf) { char *savedhost; int savedport; int ret; random_ref(); /* platform may need this to determine share socket name */ ssh_hostport_setup(host, port, conf, &savedhost, &savedport, NULL); ret = ssh_share_test_for_upstream(savedhost, savedport, conf); sfree(savedhost); random_unref(); return ret; }
CWE-119
8,593
15,956
156978669351908846372543786027828521181
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static void ssh_throttle_conn(Ssh ssh, int adjust) { int old_count = ssh->conn_throttle_count; ssh->conn_throttle_count += adjust; assert(ssh->conn_throttle_count >= 0); if (ssh->conn_throttle_count && !old_count) { } }
CWE-119
8,594
15,957
128690798958080982954607477439470590768
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
static unsigned int ssh_tty_parse_specchar(char *s) { unsigned int ret; if (*s) { char *next = NULL; ret = ctrlparse(s, &next); if (!next) ret = s[0]; } else { ret = 255; /* special value meaning "don't set" */ } return ret; }
CWE-119
8,596
15,958
304397307944611222711604518262183569049
null
null
null
tartarus
4ff22863d895cb7ebfced4cf923a012a614adaa8
0
int agent_exists(void) { HWND hwnd; hwnd = FindWindow("Pageant", "Pageant"); if (!hwnd) return FALSE; else return TRUE; }
CWE-119
8,599
15,959
168458141818786916704159499767532016959
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer, EVP_PKEY *skey, const EVP_MD *md, unsigned int flags) { X509_CRL *crl = NULL; int i; STACK_OF(X509_REVOKED) *revs = NULL; /* CRLs can't be delta already */ if (base->base_crl_number || newer->base_crl_number) { X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_ALREADY_DELTA); return NULL; } /* Base and new CRL must have a CRL number */ if (!base->crl_number || !newer->crl_number) { X509err(X509_F_X509_CRL_DIFF, X509_R_NO_CRL_NUMBER); return NULL; } /* Issuer names must match */ if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) { X509err(X509_F_X509_CRL_DIFF, X509_R_ISSUER_MISMATCH); return NULL; } /* AKID and IDP must match */ if (!crl_extension_match(base, newer, NID_authority_key_identifier)) { X509err(X509_F_X509_CRL_DIFF, X509_R_AKID_MISMATCH); return NULL; } if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) { X509err(X509_F_X509_CRL_DIFF, X509_R_IDP_MISMATCH); return NULL; } /* Newer CRL number must exceed full CRL number */ if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) { X509err(X509_F_X509_CRL_DIFF, X509_R_NEWER_CRL_NOT_NEWER); return NULL; } /* CRLs must verify */ if (skey && (X509_CRL_verify(base, skey) <= 0 || X509_CRL_verify(newer, skey) <= 0)) { X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_VERIFY_FAILURE); return NULL; } /* Create new CRL */ crl = X509_CRL_new(); if (!crl || !X509_CRL_set_version(crl, 1)) goto memerr; /* Set issuer name */ if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer))) goto memerr; if (!X509_CRL_set_lastUpdate(crl, X509_CRL_get_lastUpdate(newer))) goto memerr; if (!X509_CRL_set_nextUpdate(crl, X509_CRL_get_nextUpdate(newer))) goto memerr; /* Set base CRL number: must be critical */ if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0)) goto memerr; /* * Copy extensions across from newest CRL to delta: this will set CRL * number to correct value too. */ for (i = 0; i < X509_CRL_get_ext_count(newer); i++) { X509_EXTENSION *ext; ext = X509_CRL_get_ext(newer, i); if (!X509_CRL_add_ext(crl, ext, -1)) goto memerr; } /* Go through revoked entries, copying as needed */ revs = X509_CRL_get_REVOKED(newer); for (i = 0; i < sk_X509_REVOKED_num(revs); i++) { X509_REVOKED *rvn, *rvtmp; rvn = sk_X509_REVOKED_value(revs, i); /* * Add only if not also in base. TODO: need something cleverer here * for some more complex CRLs covering multiple CAs. */ if (!X509_CRL_get0_by_serial(base, &rvtmp, rvn->serialNumber)) { rvtmp = X509_REVOKED_dup(rvn); if (!rvtmp) goto memerr; if (!X509_CRL_add0_revoked(crl, rvtmp)) { X509_REVOKED_free(rvtmp); goto memerr; } } } /* TODO: optionally prune deleted entries */ if (skey && md && !X509_CRL_sign(crl, skey, md)) goto memerr; return crl; memerr: X509err(X509_F_X509_CRL_DIFF, ERR_R_MALLOC_FAILURE); if (crl) X509_CRL_free(crl); return NULL; }
CWE-476
8,720
15,996
237396656118435283793758931510813869191
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx) { /* * We need to be idempotent because, unfortunately, free() also calls * cleanup(), so the natural call sequence new(), init(), cleanup(), free() * calls cleanup() for the same object twice! Thus we must zero the * pointers below after they're freed! */ /* Seems to always be 0 in OpenSSL, do this at most once. */ if (ctx->cleanup != NULL) { ctx->cleanup(ctx); ctx->cleanup = NULL; } if (ctx->param != NULL) { if (ctx->parent == NULL) X509_VERIFY_PARAM_free(ctx->param); ctx->param = NULL; } if (ctx->tree != NULL) { X509_policy_tree_free(ctx->tree); ctx->tree = NULL; } if (ctx->chain != NULL) { sk_X509_pop_free(ctx->chain, X509_free); ctx->chain = NULL; } CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data)); memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA)); }
CWE-476
8,721
15,997
290525848851403349343296573750453536503
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx) { if (!ctx->chain) return NULL; return X509_chain_up_ref(ctx->chain); }
CWE-476
8,722
15,998
37008237058353947733198250534470314054
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
int X509_verify_cert(X509_STORE_CTX *ctx) { X509 *x, *xtmp, *xtmp2, *chain_ss = NULL; int bad_chain = 0; X509_VERIFY_PARAM *param = ctx->param; int depth, i, ok = 0; int num, j, retry; int (*cb) (int xok, X509_STORE_CTX *xctx); STACK_OF(X509) *sktmp = NULL; int trust = X509_TRUST_UNTRUSTED; int err; if (ctx->cert == NULL) { X509err(X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY); ctx->error = X509_V_ERR_INVALID_CALL; return -1; } if (ctx->chain != NULL) { /* * This X509_STORE_CTX has already been used to verify a cert. We * cannot do another one. */ X509err(X509_F_X509_VERIFY_CERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); ctx->error = X509_V_ERR_INVALID_CALL; return -1; } cb = ctx->verify_cb; /* * first we make sure the chain we are going to build is present and that * the first entry is in place */ if (((ctx->chain = sk_X509_new_null()) == NULL) || (!sk_X509_push(ctx->chain, ctx->cert))) { X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE); ctx->error = X509_V_ERR_OUT_OF_MEM; ok = -1; goto err; } CRYPTO_add(&ctx->cert->references, 1, CRYPTO_LOCK_X509); ctx->last_untrusted = 1; /* We use a temporary STACK so we can chop and hack at it */ if (ctx->untrusted != NULL && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) { X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE); ctx->error = X509_V_ERR_OUT_OF_MEM; ok = -1; goto err; } num = sk_X509_num(ctx->chain); x = sk_X509_value(ctx->chain, num - 1); depth = param->depth; for (;;) { /* If we have enough, we break */ if (depth < num) break; /* FIXME: If this happens, we should take * note of it and, if appropriate, use the * X509_V_ERR_CERT_CHAIN_TOO_LONG error code * later. */ /* If we are self signed, we break */ if (cert_self_signed(x)) break; /* * If asked see if we can find issuer in trusted store first */ if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) { ok = ctx->get_issuer(&xtmp, ctx, x); if (ok < 0) { ctx->error = X509_V_ERR_STORE_LOOKUP; goto err; } /* * If successful for now free up cert so it will be picked up * again later. */ if (ok > 0) { X509_free(xtmp); break; } } /* If we were passed a cert chain, use it first */ if (ctx->untrusted != NULL) { xtmp = find_issuer(ctx, sktmp, x); if (xtmp != NULL) { if (!sk_X509_push(ctx->chain, xtmp)) { X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE); ctx->error = X509_V_ERR_OUT_OF_MEM; ok = -1; goto err; } CRYPTO_add(&xtmp->references, 1, CRYPTO_LOCK_X509); (void)sk_X509_delete_ptr(sktmp, xtmp); ctx->last_untrusted++; x = xtmp; num++; /* * reparse the full chain for the next one */ continue; } } break; } /* Remember how many untrusted certs we have */ j = num; /* * at this point, chain should contain a list of untrusted certificates. * We now need to add at least one trusted one, if possible, otherwise we * complain. */ do { /* * Examine last certificate in chain and see if it is self signed. */ i = sk_X509_num(ctx->chain); x = sk_X509_value(ctx->chain, i - 1); if (cert_self_signed(x)) { /* we have a self signed certificate */ if (sk_X509_num(ctx->chain) == 1) { /* * We have a single self signed certificate: see if we can * find it in the store. We must have an exact match to avoid * possible impersonation. */ ok = ctx->get_issuer(&xtmp, ctx, x); if ((ok <= 0) || X509_cmp(x, xtmp)) { ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT; ctx->current_cert = x; ctx->error_depth = i - 1; if (ok == 1) X509_free(xtmp); bad_chain = 1; ok = cb(0, ctx); if (!ok) goto err; } else { /* * We have a match: replace certificate with store * version so we get any trust settings. */ X509_free(x); x = xtmp; (void)sk_X509_set(ctx->chain, i - 1, x); ctx->last_untrusted = 0; } } else { /* * extract and save self signed certificate for later use */ chain_ss = sk_X509_pop(ctx->chain); ctx->last_untrusted--; num--; j--; x = sk_X509_value(ctx->chain, num - 1); } } /* We now lookup certs from the certificate store */ for (;;) { /* If we have enough, we break */ if (depth < num) break; /* If we are self signed, we break */ if (cert_self_signed(x)) break; ok = ctx->get_issuer(&xtmp, ctx, x); if (ok < 0) { ctx->error = X509_V_ERR_STORE_LOOKUP; goto err; } if (ok == 0) break; x = xtmp; if (!sk_X509_push(ctx->chain, x)) { X509_free(xtmp); X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE); ctx->error = X509_V_ERR_OUT_OF_MEM; ok = -1; goto err; } num++; } /* we now have our chain, lets check it... */ if ((trust = check_trust(ctx)) == X509_TRUST_REJECTED) { /* Callback already issued */ ok = 0; goto err; } /* * If it's not explicitly trusted then check if there is an alternative * chain that could be used. We only do this if we haven't already * checked via TRUSTED_FIRST and the user hasn't switched off alternate * chain checking */ retry = 0; if (trust != X509_TRUST_TRUSTED && !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) && !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) { while (j-- > 1) { xtmp2 = sk_X509_value(ctx->chain, j - 1); ok = ctx->get_issuer(&xtmp, ctx, xtmp2); if (ok < 0) { ctx->error = X509_V_ERR_STORE_LOOKUP; goto err; } /* Check if we found an alternate chain */ if (ok > 0) { /* * Free up the found cert we'll add it again later */ X509_free(xtmp); /* * Dump all the certs above this point - we've found an * alternate chain */ while (num > j) { xtmp = sk_X509_pop(ctx->chain); X509_free(xtmp); num--; } ctx->last_untrusted = sk_X509_num(ctx->chain); retry = 1; break; } } } } while (retry); /* * If not explicitly trusted then indicate error unless it's a single * self signed certificate in which case we've indicated an error already * and set bad_chain == 1 */ if (trust != X509_TRUST_TRUSTED && !bad_chain) { if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) { if (ctx->last_untrusted >= num) ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY; else ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT; ctx->current_cert = x; } else { sk_X509_push(ctx->chain, chain_ss); num++; ctx->last_untrusted = num; ctx->current_cert = chain_ss; ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; chain_ss = NULL; } ctx->error_depth = num - 1; bad_chain = 1; ok = cb(0, ctx); if (!ok) goto err; } /* We have the chain complete: now we need to check its purpose */ ok = check_chain_extensions(ctx); if (!ok) goto err; /* Check name constraints */ ok = check_name_constraints(ctx); if (!ok) goto err; ok = check_id(ctx); if (!ok) goto err; /* We may as well copy down any DSA parameters that are required */ X509_get_pubkey_parameters(NULL, ctx->chain); /* * Check revocation status: we do this after copying parameters because * they may be needed for CRL signature verification. */ ok = ctx->check_revocation(ctx); if (!ok) goto err; err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain, ctx->param->flags); if (err != X509_V_OK) { ctx->error = err; ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth); ok = cb(0, ctx); if (!ok) goto err; } /* At this point, we have a chain and need to verify it */ if (ctx->verify != NULL) ok = ctx->verify(ctx); else ok = internal_verify(ctx); if (!ok) goto err; #ifndef OPENSSL_NO_RFC3779 /* RFC 3779 path validation, now that CRL check has been done */ ok = v3_asid_validate_path(ctx); if (!ok) goto err; ok = v3_addr_validate_path(ctx); if (!ok) goto err; #endif /* If we get this far evaluate policies */ if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK)) ok = ctx->check_policy(ctx); if (!ok) goto err; if (0) { err: /* Ensure we return an error */ if (ok > 0) ok = 0; X509_get_pubkey_parameters(NULL, ctx->chain); } if (sktmp != NULL) sk_X509_free(sktmp); if (chain_ss != NULL) X509_free(chain_ss); /* Safety net, error returns must set ctx->error */ if (ok <= 0 && ctx->error == X509_V_OK) ctx->error = X509_V_ERR_UNSPECIFIED; return ok; }
CWE-476
8,723
15,999
27021596997414419769888083545338220657
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
static int check_cert(X509_STORE_CTX *ctx) { X509_CRL *crl = NULL, *dcrl = NULL; X509 *x; int ok, cnum; unsigned int last_reasons; cnum = ctx->error_depth; x = sk_X509_value(ctx->chain, cnum); ctx->current_cert = x; ctx->current_issuer = NULL; ctx->current_crl_score = 0; ctx->current_reasons = 0; if (x->ex_flags & EXFLAG_PROXY) return 1; while (ctx->current_reasons != CRLDP_ALL_REASONS) { last_reasons = ctx->current_reasons; /* Try to retrieve relevant CRL */ if (ctx->get_crl) ok = ctx->get_crl(ctx, &crl, x); else ok = get_crl_delta(ctx, &crl, &dcrl, x); /* * If error looking up CRL, nothing we can do except notify callback */ if (!ok) { ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL; ok = ctx->verify_cb(0, ctx); goto err; } ctx->current_crl = crl; ok = ctx->check_crl(ctx, crl); if (!ok) goto err; if (dcrl) { ok = ctx->check_crl(ctx, dcrl); if (!ok) goto err; ok = ctx->cert_crl(ctx, dcrl, x); if (!ok) goto err; } else ok = 1; /* Don't look in full CRL if delta reason is removefromCRL */ if (ok != 2) { ok = ctx->cert_crl(ctx, crl, x); if (!ok) goto err; } X509_CRL_free(crl); X509_CRL_free(dcrl); crl = NULL; dcrl = NULL; /* * If reasons not updated we wont get anywhere by another iteration, * so exit loop. */ if (last_reasons == ctx->current_reasons) { ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL; ok = ctx->verify_cb(0, ctx); goto err; } } err: X509_CRL_free(crl); X509_CRL_free(dcrl); ctx->current_crl = NULL; return ok; }
CWE-476
8,725
16,000
296914209746112447604708215338076564700
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
static int check_chain_extensions(X509_STORE_CTX *ctx) { #ifdef OPENSSL_NO_CHAIN_VERIFY return 1; #else int i, ok = 0, must_be_ca, plen = 0; X509 *x; int (*cb) (int xok, X509_STORE_CTX *xctx); int proxy_path_length = 0; int purpose; int allow_proxy_certs; cb = ctx->verify_cb; /*- * must_be_ca can have 1 of 3 values: * -1: we accept both CA and non-CA certificates, to allow direct * use of self-signed certificates (which are marked as CA). * 0: we only accept non-CA certificates. This is currently not * used, but the possibility is present for future extensions. * 1: we only accept CA certificates. This is currently used for * all certificates in the chain except the leaf certificate. */ must_be_ca = -1; /* CRL path validation */ if (ctx->parent) { allow_proxy_certs = 0; purpose = X509_PURPOSE_CRL_SIGN; } else { allow_proxy_certs = ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS); /* * A hack to keep people who don't want to modify their software * happy */ if (getenv("OPENSSL_ALLOW_PROXY_CERTS")) allow_proxy_certs = 1; purpose = ctx->param->purpose; } /* Check all untrusted certificates */ for (i = 0; i < ctx->last_untrusted; i++) { int ret; x = sk_X509_value(ctx->chain, i); if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) && (x->ex_flags & EXFLAG_CRITICAL)) { ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) { ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } ret = X509_check_ca(x); switch (must_be_ca) { case -1: if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) && (ret != 1) && (ret != 0)) { ret = 0; ctx->error = X509_V_ERR_INVALID_CA; } else ret = 1; break; case 0: if (ret != 0) { ret = 0; ctx->error = X509_V_ERR_INVALID_NON_CA; } else ret = 1; break; default: if ((ret == 0) || ((ctx->param->flags & X509_V_FLAG_X509_STRICT) && (ret != 1))) { ret = 0; ctx->error = X509_V_ERR_INVALID_CA; } else ret = 1; break; } if (ret == 0) { ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } if (ctx->param->purpose > 0) { ret = X509_check_purpose(x, purpose, must_be_ca > 0); if ((ret == 0) || ((ctx->param->flags & X509_V_FLAG_X509_STRICT) && (ret != 1))) { ctx->error = X509_V_ERR_INVALID_PURPOSE; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } } /* Check pathlen if not self issued */ if ((i > 1) && !(x->ex_flags & EXFLAG_SI) && (x->ex_pathlen != -1) && (plen > (x->ex_pathlen + proxy_path_length + 1))) { ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } /* Increment path length if not self issued */ if (!(x->ex_flags & EXFLAG_SI)) plen++; /* * If this certificate is a proxy certificate, the next certificate * must be another proxy certificate or a EE certificate. If not, * the next certificate must be a CA certificate. */ if (x->ex_flags & EXFLAG_PROXY) { /* * RFC3820, 4.1.3 (b)(1) stipulates that if pCPathLengthConstraint * is less than max_path_length, the former should be copied to * the latter, and 4.1.4 (a) stipulates that max_path_length * should be verified to be larger than zero and decrement it. * * Because we're checking the certs in the reverse order, we start * with verifying that proxy_path_length isn't larger than pcPLC, * and copy the latter to the former if it is, and finally, * increment proxy_path_length. */ if (x->ex_pcpathlen != -1) { if (proxy_path_length > x->ex_pcpathlen) { ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } proxy_path_length = x->ex_pcpathlen; } proxy_path_length++; must_be_ca = 0; } else must_be_ca = 1; } ok = 1; end: return ok; #endif }
CWE-476
8,726
16,001
331669087567090221108664324296582591336
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) { X509 *issuer = NULL; EVP_PKEY *ikey = NULL; int ok = 0, chnum, cnum; cnum = ctx->error_depth; chnum = sk_X509_num(ctx->chain) - 1; /* if we have an alternative CRL issuer cert use that */ if (ctx->current_issuer) issuer = ctx->current_issuer; /* * Else find CRL issuer: if not last certificate then issuer is next * certificate in chain. */ else if (cnum < chnum) issuer = sk_X509_value(ctx->chain, cnum + 1); else { issuer = sk_X509_value(ctx->chain, chnum); /* If not self signed, can't check signature */ if (!ctx->check_issued(ctx, issuer, issuer)) { ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER; ok = ctx->verify_cb(0, ctx); if (!ok) goto err; } } if (issuer) { /* * Skip most tests for deltas because they have already been done */ if (!crl->base_crl_number) { /* Check for cRLSign bit if keyUsage present */ if ((issuer->ex_flags & EXFLAG_KUSAGE) && !(issuer->ex_kusage & KU_CRL_SIGN)) { ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN; ok = ctx->verify_cb(0, ctx); if (!ok) goto err; } if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) { ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE; ok = ctx->verify_cb(0, ctx); if (!ok) goto err; } if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) { if (check_crl_path(ctx, ctx->current_issuer) <= 0) { ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR; ok = ctx->verify_cb(0, ctx); if (!ok) goto err; } } if (crl->idp_flags & IDP_INVALID) { ctx->error = X509_V_ERR_INVALID_EXTENSION; ok = ctx->verify_cb(0, ctx); if (!ok) goto err; } } if (!(ctx->current_crl_score & CRL_SCORE_TIME)) { ok = check_crl_time(ctx, crl, 1); if (!ok) goto err; } /* Attempt to get issuer certificate public key */ ikey = X509_get_pubkey(issuer); if (!ikey) { ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; ok = ctx->verify_cb(0, ctx); if (!ok) goto err; } else { int rv; rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags); if (rv != X509_V_OK) { ctx->error = rv; ok = ctx->verify_cb(0, ctx); if (!ok) goto err; } /* Verify CRL signature */ if (X509_CRL_verify(crl, ikey) <= 0) { ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE; ok = ctx->verify_cb(0, ctx); if (!ok) goto err; } } } ok = 1; err: EVP_PKEY_free(ikey); return ok; }
CWE-476
8,727
16,002
254139821483245343592037008476024199291
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
static int check_hosts(X509 *x, X509_VERIFY_PARAM_ID *id) { int i; int n = sk_OPENSSL_STRING_num(id->hosts); char *name; if (id->peername != NULL) { OPENSSL_free(id->peername); id->peername = NULL; } for (i = 0; i < n; ++i) { name = sk_OPENSSL_STRING_value(id->hosts, i); if (X509_check_host(x, name, 0, id->hostflags, &id->peername) > 0) return 1; } return n == 0; }
CWE-476
8,728
16,003
297191557374608091998007996867943641640
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
static int check_id(X509_STORE_CTX *ctx) { X509_VERIFY_PARAM *vpm = ctx->param; X509_VERIFY_PARAM_ID *id = vpm->id; X509 *x = ctx->cert; if (id->hosts && check_hosts(x, id) <= 0) { if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH)) return 0; } if (id->email && X509_check_email(x, id->email, id->emaillen, 0) <= 0) { if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH)) return 0; } if (id->ip && X509_check_ip(x, id->ip, id->iplen, 0) <= 0) { if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH)) return 0; } return 1; }
CWE-476
8,729
16,004
122316311069463854658189220068673502060
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
static int check_id_error(X509_STORE_CTX *ctx, int errcode) { ctx->error = errcode; ctx->current_cert = ctx->cert; ctx->error_depth = 0; return ctx->verify_cb(0, ctx); }
CWE-476
8,730
16,005
114592313188918363317952020501306999052
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer) { int ret; ret = X509_check_issued(issuer, x); if (ret == X509_V_OK) return 1; /* If we haven't asked for issuer errors don't set ctx */ if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK)) return 0; ctx->error = ret; ctx->current_cert = x; ctx->current_issuer = issuer; return ctx->verify_cb(0, ctx); }
CWE-476
8,731
16,006
186806565681330308714823934466729093244
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
static int check_policy(X509_STORE_CTX *ctx) { int ret; if (ctx->parent) return 1; ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain, ctx->param->policies, ctx->param->flags); if (ret == 0) { X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE); ctx->error = X509_V_ERR_OUT_OF_MEM; return 0; } /* Invalid or inconsistent extensions */ if (ret == -1) { /* * Locate certificates with bad extensions and notify callback. */ X509 *x; int i; for (i = 1; i < sk_X509_num(ctx->chain); i++) { x = sk_X509_value(ctx->chain, i); if (!(x->ex_flags & EXFLAG_INVALID_POLICY)) continue; ctx->current_cert = x; ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION; if (!ctx->verify_cb(0, ctx)) return 0; } return 1; } if (ret == -2) { ctx->current_cert = NULL; ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY; return ctx->verify_cb(0, ctx); } if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) { ctx->current_cert = NULL; /* * Verification errors need to be "sticky", a callback may have allowed * an SSL handshake to continue despite an error, and we must then * remain in an error state. Therefore, we MUST NOT clear earlier * verification errors by setting the error to X509_V_OK. */ if (!ctx->verify_cb(2, ctx)) return 0; } return 1; }
CWE-476
8,732
16,007
25771525378933339246077198902750732858
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
static int check_trust(X509_STORE_CTX *ctx) { int i, ok; X509 *x = NULL; int (*cb) (int xok, X509_STORE_CTX *xctx); cb = ctx->verify_cb; /* Check all trusted certificates in chain */ for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) { x = sk_X509_value(ctx->chain, i); ok = X509_check_trust(x, ctx->param->trust, 0); /* If explicitly trusted return trusted */ if (ok == X509_TRUST_TRUSTED) return X509_TRUST_TRUSTED; /* * If explicitly rejected notify callback and reject if not * overridden. */ if (ok == X509_TRUST_REJECTED) { ctx->error_depth = i; ctx->current_cert = x; ctx->error = X509_V_ERR_CERT_REJECTED; ok = cb(0, ctx); if (!ok) return X509_TRUST_REJECTED; } } /* * If we accept partial chains and have at least one trusted certificate * return success. */ if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) { X509 *mx; if (ctx->last_untrusted < sk_X509_num(ctx->chain)) return X509_TRUST_TRUSTED; x = sk_X509_value(ctx->chain, 0); mx = lookup_cert_match(ctx, x); if (mx) { (void)sk_X509_set(ctx->chain, 0, mx); X509_free(x); ctx->last_untrusted = 0; return X509_TRUST_TRUSTED; } } /* * If no trusted certs in chain at all return untrusted and allow * standard (no issuer cert) etc errors to be indicated. */ return X509_TRUST_UNTRUSTED; }
CWE-476
8,733
16,008
256072379840855417891389319639683188185
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
static int internal_verify(X509_STORE_CTX *ctx) { int ok = 0, n; X509 *xs, *xi; EVP_PKEY *pkey = NULL; int (*cb) (int xok, X509_STORE_CTX *xctx); cb = ctx->verify_cb; n = sk_X509_num(ctx->chain); ctx->error_depth = n - 1; n--; xi = sk_X509_value(ctx->chain, n); if (ctx->check_issued(ctx, xi, xi)) xs = xi; else { if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) { xs = xi; goto check_cert; } if (n <= 0) { ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE; ctx->current_cert = xi; ok = cb(0, ctx); goto end; } else { n--; ctx->error_depth = n; xs = sk_X509_value(ctx->chain, n); } } /* ctx->error=0; not needed */ while (n >= 0) { ctx->error_depth = n; /* * Skip signature check for self signed certificates unless * explicitly asked for. It doesn't add any security and just wastes * time. */ if (!xs->valid && (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) { if ((pkey = X509_get_pubkey(xi)) == NULL) { ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; ctx->current_cert = xi; ok = (*cb) (0, ctx); if (!ok) goto end; } else if (X509_verify(xs, pkey) <= 0) { ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE; ctx->current_cert = xs; ok = (*cb) (0, ctx); if (!ok) { EVP_PKEY_free(pkey); goto end; } } EVP_PKEY_free(pkey); pkey = NULL; } xs->valid = 1; check_cert: ok = check_cert_time(ctx, xs); if (!ok) goto end; /* The last error (if any) is still in the error value */ ctx->current_issuer = xi; ctx->current_cert = xs; ok = (*cb) (1, ctx); if (!ok) goto end; n--; if (n >= 0) { xi = xs; xs = sk_X509_value(ctx->chain, n); } } ok = 1; end: return ok; }
CWE-476
8,734
16,009
146943588074155177796507075183135877633
null
null
null
openssl
6e629b5be45face20b4ca71c4fcbfed78b864a2e
0
static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x) { STACK_OF(X509) *certs; X509 *xtmp = NULL; int i; /* Lookup all certs with matching subject name */ certs = ctx->lookup_certs(ctx, X509_get_subject_name(x)); if (certs == NULL) return NULL; /* Look for exact match */ for (i = 0; i < sk_X509_num(certs); i++) { xtmp = sk_X509_value(certs, i); if (!X509_cmp(xtmp, x)) break; } if (i < sk_X509_num(certs)) CRYPTO_add(&xtmp->references, 1, CRYPTO_LOCK_X509); else xtmp = NULL; sk_X509_pop_free(certs, X509_free); return xtmp; }
CWE-476
8,735
16,010
315841031545923908286960573621771059888
null
null
null
spice
9113dc6a303604a2d9812ac70c17d076ef11886c
0
vcard7816_file_system_process_apdu(VCard *card, VCardAPDU *apdu, VCardResponse **response) { /* TODO: if we want to support a virtual file system card, we do it here. * It would probably be a pkcs #15 card type */ *response = vcard_make_response( VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED); return VCARD_DONE; }
CWE-772
8,736
16,011
181235954283776116591417631039990506391
null
null
null
spice
9113dc6a303604a2d9812ac70c17d076ef11886c
0
vcard_apdu_set_class(VCardAPDU *apdu) { apdu->a_channel = 0; apdu->a_secure_messaging = 0; apdu->a_type = apdu->a_cla & 0xf0; apdu->a_gen_type = VCARD_7816_ISO; /* parse the class tables 8 & 9 of the 7816-4 Part 4 spec */ switch (apdu->a_type) { /* we only support the basic types */ case 0x00: case 0x80: case 0x90: case 0xa0: apdu->a_channel = apdu->a_cla & 3; apdu->a_secure_messaging = apdu->a_cla & 0xe; break; case 0xb0: case 0xc0: break; case 0x10: case 0x20: case 0x30: case 0x40: case 0x50: case 0x60: case 0x70: /* Reserved for future use */ apdu->a_gen_type = VCARD_7816_RFU; break; case 0xd0: case 0xe0: case 0xf0: default: apdu->a_gen_type = (apdu->a_cla == 0xff) ? VCARD_7816_PTS : VCARD_7816_PROPRIETARY; break; } return VCARD7816_STATUS_SUCCESS; }
CWE-772
8,738
16,012
303515655899765826077838845891872864881
null
null
null
spice
9113dc6a303604a2d9812ac70c17d076ef11886c
0
vcard_apdu_set_length(VCardAPDU *apdu) { int L, Le; /* process according to table 5 of the 7816-4 Part 4 spec. * variable names match the variables in the spec */ L = apdu->a_len-4; /* fixed APDU header */ apdu->a_Lc = 0; apdu->a_Le = 0; apdu->a_body = NULL; switch (L) { case 0: /* 1 minimal apdu */ return VCARD7816_STATUS_SUCCESS; case 1: /* 2S only return values apdu */ /* zero maps to 256 here */ apdu->a_Le = apdu->a_header->ah_Le ? apdu->a_header->ah_Le : 256; return VCARD7816_STATUS_SUCCESS; default: /* if the ah_Le byte is zero and we have more than * 1 byte in the header, then we must be using extended Le and Lc. * process the extended now. */ if (apdu->a_header->ah_Le == 0) { if (L < 3) { /* coding error, need at least 3 bytes */ return VCARD7816_STATUS_ERROR_WRONG_LENGTH; } /* calculate the first extended value. Could be either Le or Lc */ Le = (apdu->a_header->ah_body[0] << 8) | apdu->a_header->ah_body[1]; if (L == 3) { /* 2E extended, return data only */ /* zero maps to 65536 */ apdu->a_Le = Le ? Le : 65536; return VCARD7816_STATUS_SUCCESS; } if (Le == 0) { /* reserved for future use, probably for next time we need * to extend the lengths */ return VCARD7816_STATUS_ERROR_WRONG_LENGTH; } /* we know that the first extended value is Lc now */ apdu->a_Lc = Le; apdu->a_body = &apdu->a_header->ah_body[2]; if (L == Le+3) { /* 3E extended, only body parameters */ return VCARD7816_STATUS_SUCCESS; } if (L == Le+5) { /* 4E extended, parameters and return data */ Le = (apdu->a_data[apdu->a_len-2] << 8) | apdu->a_data[apdu->a_len-1]; apdu->a_Le = Le ? Le : 65536; return VCARD7816_STATUS_SUCCESS; } return VCARD7816_STATUS_ERROR_WRONG_LENGTH; } /* not extended */ apdu->a_Lc = apdu->a_header->ah_Le; apdu->a_body = &apdu->a_header->ah_body[0]; if (L == apdu->a_Lc + 1) { /* 3S only body parameters */ return VCARD7816_STATUS_SUCCESS; } if (L == apdu->a_Lc + 2) { /* 4S parameters and return data */ Le = apdu->a_data[apdu->a_len-1]; apdu->a_Le = Le ? Le : 256; return VCARD7816_STATUS_SUCCESS; } break; } return VCARD7816_STATUS_ERROR_WRONG_LENGTH; }
CWE-772
8,739
16,013
192010135003170452601999294080542039643
null
null
null
spice
9113dc6a303604a2d9812ac70c17d076ef11886c
0
vcard_make_response(vcard_7816_status_t status) { VCardResponse *response; switch (status) { /* known 7816 response codes */ case VCARD7816_STATUS_SUCCESS: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_SUCCESS); case VCARD7816_STATUS_WARNING: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_WARNING); case VCARD7816_STATUS_WARNING_RET_CORUPT: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_WARNING_RET_CORUPT); case VCARD7816_STATUS_WARNING_BUF_END_BEFORE_LE: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_WARNING_BUF_END_BEFORE_LE); case VCARD7816_STATUS_WARNING_INVALID_FILE_SELECTED: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_WARNING_INVALID_FILE_SELECTED); case VCARD7816_STATUS_WARNING_FCI_FORMAT_INVALID: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_WARNING_FCI_FORMAT_INVALID); case VCARD7816_STATUS_WARNING_CHANGE: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_WARNING_CHANGE); case VCARD7816_STATUS_WARNING_FILE_FILLED: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_WARNING_FILE_FILLED); case VCARD7816_STATUS_EXC_ERROR: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_EXC_ERROR); case VCARD7816_STATUS_EXC_ERROR_CHANGE: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_EXC_ERROR_CHANGE); case VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE); case VCARD7816_STATUS_ERROR_WRONG_LENGTH: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_WRONG_LENGTH); case VCARD7816_STATUS_ERROR_CLA_NOT_SUPPORTED: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_CLA_NOT_SUPPORTED); case VCARD7816_STATUS_ERROR_CHANNEL_NOT_SUPPORTED: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_CHANNEL_NOT_SUPPORTED); case VCARD7816_STATUS_ERROR_SECURE_NOT_SUPPORTED: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_SECURE_NOT_SUPPORTED); case VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED); case VCARD7816_STATUS_ERROR_COMMAND_INCOMPATIBLE_WITH_FILE: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_COMMAND_INCOMPATIBLE_WITH_FILE); case VCARD7816_STATUS_ERROR_SECURITY_NOT_SATISFIED: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_SECURITY_NOT_SATISFIED); case VCARD7816_STATUS_ERROR_AUTHENTICATION_BLOCKED: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_AUTHENTICATION_BLOCKED); case VCARD7816_STATUS_ERROR_DATA_INVALID: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_DATA_INVALID); case VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED); case VCARD7816_STATUS_ERROR_DATA_NO_EF: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_DATA_NO_EF); case VCARD7816_STATUS_ERROR_SM_OBJECT_MISSING: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_SM_OBJECT_MISSING); case VCARD7816_STATUS_ERROR_SM_OBJECT_INCORRECT: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_SM_OBJECT_INCORRECT); case VCARD7816_STATUS_ERROR_WRONG_PARAMETERS: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_WRONG_PARAMETERS); case VCARD7816_STATUS_ERROR_WRONG_PARAMETERS_IN_DATA: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_WRONG_PARAMETERS_IN_DATA); case VCARD7816_STATUS_ERROR_FUNCTION_NOT_SUPPORTED: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_FUNCTION_NOT_SUPPORTED); case VCARD7816_STATUS_ERROR_FILE_NOT_FOUND: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_FILE_NOT_FOUND); case VCARD7816_STATUS_ERROR_RECORD_NOT_FOUND: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_RECORD_NOT_FOUND); case VCARD7816_STATUS_ERROR_NO_SPACE_FOR_FILE: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_NO_SPACE_FOR_FILE); case VCARD7816_STATUS_ERROR_LC_TLV_INCONSISTENT: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_LC_TLV_INCONSISTENT); case VCARD7816_STATUS_ERROR_P1_P2_INCORRECT: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_P1_P2_INCORRECT); case VCARD7816_STATUS_ERROR_LC_P1_P2_INCONSISTENT: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_LC_P1_P2_INCONSISTENT); case VCARD7816_STATUS_ERROR_DATA_NOT_FOUND: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_DATA_NOT_FOUND); case VCARD7816_STATUS_ERROR_WRONG_PARAMETERS_2: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_WRONG_PARAMETERS_2); case VCARD7816_STATUS_ERROR_INS_CODE_INVALID: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_INS_CODE_INVALID); case VCARD7816_STATUS_ERROR_CLA_INVALID: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_CLA_INVALID); case VCARD7816_STATUS_ERROR_GENERAL: return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_ERROR_GENERAL); default: /* we don't know this status code, create a response buffer to * hold it */ response = vcard_response_new_status(status); if (response == NULL) { /* couldn't allocate the buffer, return memmory error */ return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE); } return response; } }
CWE-772
8,741
16,014
97470289627486950774655567205189327396
null
null
null
spice
9113dc6a303604a2d9812ac70c17d076ef11886c
0
vcard_process_apdu(VCard *card, VCardAPDU *apdu, VCardResponse **response) { VCardStatus status; VCardBufferResponse *buffer_response; /* first handle any PTS commands, which aren't really APDU's */ if (apdu->a_type == VCARD_7816_PTS) { /* the PTS responses aren't really responses either */ *response = vcard_response_new_data(apdu->a_data, apdu->a_len); /* PTS responses have no status bytes */ (*response)->b_total_len = (*response)->b_len; return VCARD_DONE; } buffer_response = vcard_get_buffer_response(card); if (buffer_response && apdu->a_ins != VCARD7816_INS_GET_RESPONSE) { /* clear out buffer_response, do not return an error */ vcard_set_buffer_response(card, NULL); vcard_buffer_response_delete(buffer_response); } status = vcard_process_applet_apdu(card, apdu, response); if (status != VCARD_NEXT) { return status; } switch (vcard_get_type(card)) { case VCARD_FILE_SYSTEM: return vcard7816_file_system_process_apdu(card, apdu, response); case VCARD_VM: return vcard7816_vm_process_apdu(card, apdu, response); case VCARD_DIRECT: /* if we are type direct, then the applet should handle everything */ assert(!"VCARD_DIRECT: applet failure"); break; default: g_warn_if_reached(); } *response = vcard_make_response(VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED); return VCARD_DONE; }
CWE-772
8,742
16,015
157812423111175314263622798856635374153
null
null
null
spice
9113dc6a303604a2d9812ac70c17d076ef11886c
0
vcard_response_delete(VCardResponse *response) { if (response == NULL) { return; } switch (response->b_type) { case VCARD_MALLOC: /* everything was malloc'ed */ g_free(response->b_data); g_free(response); break; case VCARD_MALLOC_DATA: /* only the data buffer was malloc'ed */ g_free(response->b_data); break; case VCARD_MALLOC_STRUCT: /* only the structure was malloc'ed */ g_free(response); break; case VCARD_STATIC: break; default: g_warn_if_reached(); } }
CWE-772
8,743
16,016
178513110078084645557823807469054339973
null
null
null
spice
9113dc6a303604a2d9812ac70c17d076ef11886c
0
vcard_response_new(VCard *card, unsigned char *buf, int len, int Le, vcard_7816_status_t status) { VCardResponse *new_response; if (len > Le) { return vcard_init_buffer_response(card, buf, len); } new_response = vcard_response_new_data(buf, len); if (new_response == NULL) { return NULL; } vcard_response_set_status(new_response, status); return new_response; }
CWE-772
8,744
16,017
147723823737923442439571639569368421945
null
null
null
spice
9113dc6a303604a2d9812ac70c17d076ef11886c
0
vcard_response_new_bytes(VCard *card, unsigned char *buf, int len, int Le, unsigned char sw1, unsigned char sw2) { VCardResponse *new_response; if (len > Le) { return vcard_init_buffer_response(card, buf, len); } new_response = vcard_response_new_data(buf, len); if (new_response == NULL) { return NULL; } vcard_response_set_status_bytes(new_response, sw1, sw2); return new_response; }
CWE-772
8,745
16,018
120088951222124060626370615557698368814
null
null
null
spice
9113dc6a303604a2d9812ac70c17d076ef11886c
0
vcard_response_new_data(unsigned char *buf, int len) { VCardResponse *new_response; new_response = g_new(VCardResponse, 1); new_response->b_data = g_malloc(len + 2); memcpy(new_response->b_data, buf, len); new_response->b_total_len = len+2; new_response->b_len = len; new_response->b_type = VCARD_MALLOC; return new_response; }
CWE-772
8,746
16,019
218247377203831787433413014241545256624
null
null
null