Exemplo n.º 1
0
static void
dump_strong_name (MonoImage *m)
{
	guint32 size;
	const char *p;

	p = mono_image_get_strong_name (m, &size);
	dump_blob ("\nStrong name:", p, size);
}
Exemplo n.º 2
0
static void
dump_public_key (MonoImage *m)
{
	guint32 size;
	const char *p;

	p = mono_image_get_public_key (m, &size);
	dump_blob ("\nPublic key:", p, size);
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
	char *buf;

	if (argc < 2) {
		fprintf(stderr, "supply input filename\n");
		return 5;
	}

	buf = utilfdt_read(argv[1]);
	if (buf)
		dump_blob(buf);
	else
		return 10;

	return 0;
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{
	const char *file;
	char *buf;
	bool debug = false;
	off_t len;

	file = argv[1];

	/* TODO: I will pass the buf a pointer to the device tree blob
	* instead of reading a binary file. */

	buf = utilfdt_read_len(file, &len);
	if (!buf)
		die("could not read: %s\n", file);

	dump_blob(buf, debug);

	return 0;
}
Exemplo n.º 5
0
/* see brssl.h */
int
run_ssl_engine(br_ssl_engine_context *cc, unsigned long fd, unsigned flags)
{
	int hsdetails;
	int retcode;
	int verbose;
	int trace;
#ifdef _WIN32
	WSAEVENT fd_event;
	int can_send, can_recv;
	HANDLE h_in, h_out;
	in_buffer bb;
#endif

	hsdetails = 0;
	retcode = 0;
	verbose = (flags & RUN_ENGINE_VERBOSE) != 0;
	trace = (flags & RUN_ENGINE_TRACE) != 0;

	/*
	 * Print algorithm details.
	 */
	if (verbose) {
		fprintf(stderr, "Algorithms:\n");
		if (cc->iaes_cbcenc != 0) {
			fprintf(stderr, "   AES/CBC (enc): %s\n",
				get_algo_name(cc->iaes_cbcenc, 0));
		}
		if (cc->iaes_cbcdec != 0) {
			fprintf(stderr, "   AES/CBC (dec): %s\n",
				get_algo_name(cc->iaes_cbcdec, 0));
		}
		if (cc->iaes_ctr != 0) {
			fprintf(stderr, "   AES/CTR:       %s\n",
				get_algo_name(cc->iaes_cbcdec, 0));
		}
		if (cc->ides_cbcenc != 0) {
			fprintf(stderr, "   DES/CBC (enc): %s\n",
				get_algo_name(cc->ides_cbcenc, 0));
		}
		if (cc->ides_cbcdec != 0) {
			fprintf(stderr, "   DES/CBC (dec): %s\n",
				get_algo_name(cc->ides_cbcdec, 0));
		}
		if (cc->ighash != 0) {
			fprintf(stderr, "   GHASH (GCM):   %s\n",
				get_algo_name(cc->ighash, 0));
		}
		if (cc->ichacha != 0) {
			fprintf(stderr, "   ChaCha20:      %s\n",
				get_algo_name(cc->ichacha, 0));
		}
		if (cc->ipoly != 0) {
			fprintf(stderr, "   Poly1305:      %s\n",
				get_algo_name(cc->ipoly, 0));
		}
		if (cc->iec != 0) {
			fprintf(stderr, "   EC:            %s\n",
				get_algo_name(cc->iec, 0));
		}
		if (cc->iecdsa != 0) {
			fprintf(stderr, "   ECDSA:         %s\n",
				get_algo_name(cc->iecdsa, 0));
		}
		if (cc->irsavrfy != 0) {
			fprintf(stderr, "   RSA (vrfy):    %s\n",
				get_algo_name(cc->irsavrfy, 0));
		}
	}

#ifdef _WIN32
	fd_event = WSA_INVALID_EVENT;
	can_send = 0;
	can_recv = 0;
	bb.ptr = bb.len = 0;
#endif

	/*
	 * On Unix systems, we need to follow three descriptors:
	 * standard input (0), standard output (1), and the socket
	 * itself (for both read and write). This is done with a poll()
	 * call.
	 *
	 * On Windows systems, we use WSAEventSelect() to associate
	 * an event handle with the network activity, and we use
	 * WaitForMultipleObjectsEx() on that handle and the standard
	 * input handle, when appropriate. Standard output is assumed
	 * to be always writeable, and standard input to be the console;
	 * this does not work well (or at all) with redirections (to
	 * pipes or files) but it should be enough for a debug tool
	 * (TODO: make something that handles redirections as well).
	 */

#ifdef _WIN32
	fd_event = WSACreateEvent();
	if (fd_event == WSA_INVALID_EVENT) {
		fprintf(stderr, "ERROR: WSACreateEvent() failed with %d\n",
			WSAGetLastError());
		retcode = -2;
		goto engine_exit;
	}
	WSAEventSelect(fd, fd_event, FD_READ | FD_WRITE | FD_CLOSE);
	h_in = GetStdHandle(STD_INPUT_HANDLE);
	h_out = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleMode(h_in, ENABLE_ECHO_INPUT
		| ENABLE_LINE_INPUT
		| ENABLE_PROCESSED_INPUT
		| ENABLE_PROCESSED_OUTPUT
		| ENABLE_WRAP_AT_EOL_OUTPUT);
#else
	/*
	 * Make sure that stdin and stdout are non-blocking.
	 */
	fcntl(0, F_SETFL, O_NONBLOCK);
	fcntl(1, F_SETFL, O_NONBLOCK);
#endif

	/*
	 * Perform the loop.
	 */
	for (;;) {
		unsigned st;
		int sendrec, recvrec, sendapp, recvapp;
#ifdef _WIN32
		HANDLE pfd[2];
		DWORD wt;
#else
		struct pollfd pfd[3];
		int n;
#endif
		size_t u, k_fd, k_in, k_out;
		int sendrec_ok, recvrec_ok, sendapp_ok, recvapp_ok;

		/*
		 * Get current engine state.
		 */
		st = br_ssl_engine_current_state(cc);
		if (st == BR_SSL_CLOSED) {
			int err;

			err = br_ssl_engine_last_error(cc);
			if (err == BR_ERR_OK) {
				if (verbose) {
					fprintf(stderr,
						"SSL closed normally\n");
				}
				retcode = 0;
				goto engine_exit;
			} else {
				fprintf(stderr, "ERROR: SSL error %d", err);
				retcode = err;
				if (err >= BR_ERR_SEND_FATAL_ALERT) {
					err -= BR_ERR_SEND_FATAL_ALERT;
					fprintf(stderr,
						" (sent alert %d)\n", err);
				} else if (err >= BR_ERR_RECV_FATAL_ALERT) {
					err -= BR_ERR_RECV_FATAL_ALERT;
					fprintf(stderr,
						" (received alert %d)\n", err);
				} else {
					const char *ename;

					ename = find_error_name(err, NULL);
					if (ename == NULL) {
						ename = "unknown";
					}
					fprintf(stderr, " (%s)\n", ename);
				}
				goto engine_exit;
			}
		}

		/*
		 * Compute descriptors that must be polled, depending
		 * on engine state.
		 */
		sendrec = ((st & BR_SSL_SENDREC) != 0);
		recvrec = ((st & BR_SSL_RECVREC) != 0);
		sendapp = ((st & BR_SSL_SENDAPP) != 0);
		recvapp = ((st & BR_SSL_RECVAPP) != 0);
		if (verbose && sendapp && !hsdetails) {
			char csn[80];
			const char *pname;

			fprintf(stderr, "Handshake completed\n");
			fprintf(stderr, "   version:               ");
			switch (cc->session.version) {
			case BR_SSL30:
				fprintf(stderr, "SSL 3.0");
				break;
			case BR_TLS10:
				fprintf(stderr, "TLS 1.0");
				break;
			case BR_TLS11:
				fprintf(stderr, "TLS 1.1");
				break;
			case BR_TLS12:
				fprintf(stderr, "TLS 1.2");
				break;
			default:
				fprintf(stderr, "unknown (0x%04X)",
					(unsigned)cc->session.version);
				break;
			}
			fprintf(stderr, "\n");
			get_suite_name_ext(
				cc->session.cipher_suite, csn, sizeof csn);
			fprintf(stderr, "   cipher suite:          %s\n", csn);
			if (uses_ecdhe(cc->session.cipher_suite)) {
				get_curve_name_ext(
					br_ssl_engine_get_ecdhe_curve(cc),
					csn, sizeof csn);
				fprintf(stderr,
					"   ECDHE curve:           %s\n", csn);
			}
			fprintf(stderr, "   secure renegotiation:  %s\n",
				cc->reneg == 1 ? "no" : "yes");
			pname = br_ssl_engine_get_selected_protocol(cc);
			if (pname != NULL) {
				fprintf(stderr,
					"   protocol name (ALPN):  %s\n",
					pname);
			}
			hsdetails = 1;
		}

		k_fd = (size_t)-1;
		k_in = (size_t)-1;
		k_out = (size_t)-1;

		u = 0;
#ifdef _WIN32
		/*
		 * If we recorded that we can send or receive data, and we
		 * want to do exactly that, then we don't wait; we just do
		 * it.
		 */
		recvapp_ok = 0;
		sendrec_ok = 0;
		recvrec_ok = 0;
		sendapp_ok = 0;

		if (sendrec && can_send) {
			sendrec_ok = 1;
		} else if (recvrec && can_recv) {
			recvrec_ok = 1;
		} else if (recvapp) {
			recvapp_ok = 1;
		} else if (sendapp && in_avail_buffered(h_in, &bb)) {
			sendapp_ok = 1;
		} else {
			/*
			 * If we cannot do I/O right away, then we must
			 * wait for some event, and try again.
			 */
			pfd[u] = (HANDLE)fd_event;
			k_fd = u;
			u ++;
			if (sendapp) {
				pfd[u] = h_in;
				k_in = u;
				u ++;
			}
			wt = WaitForMultipleObjectsEx(u, pfd,
				FALSE, INFINITE, FALSE);
			if (wt == WAIT_FAILED) {
				fprintf(stderr, "ERROR:"
					" WaitForMultipleObjectsEx()"
					" failed with 0x%08lX",
					(unsigned long)GetLastError());
				retcode = -2;
				goto engine_exit;
			}
			if (wt == k_fd) {
				WSANETWORKEVENTS e;

				if (WSAEnumNetworkEvents(fd, fd_event, &e)) {
					fprintf(stderr, "ERROR:"
						" WSAEnumNetworkEvents()"
						" failed with %d\n",
						WSAGetLastError());
					retcode = -2;
					goto engine_exit;
				}
				if (e.lNetworkEvents & (FD_WRITE | FD_CLOSE)) {
					can_send = 1;
				}
				if (e.lNetworkEvents & (FD_READ | FD_CLOSE)) {
					can_recv = 1;
				}
			}
			continue;
		}
#else
		if (sendrec || recvrec) {
			pfd[u].fd = fd;
			pfd[u].revents = 0;
			pfd[u].events = 0;
			if (sendrec) {
				pfd[u].events |= POLLOUT;
			}
			if (recvrec) {
				pfd[u].events |= POLLIN;
			}
			k_fd = u;
			u ++;
		}
		if (sendapp) {
			pfd[u].fd = 0;
			pfd[u].revents = 0;
			pfd[u].events = POLLIN;
			k_in = u;
			u ++;
		}
		if (recvapp) {
			pfd[u].fd = 1;
			pfd[u].revents = 0;
			pfd[u].events = POLLOUT;
			k_out = u;
			u ++;
		}
		n = poll(pfd, u, -1);
		if (n < 0) {
			if (errno == EINTR) {
				continue;
			}
			perror("ERROR: poll()");
			retcode = -2;
			goto engine_exit;
		}
		if (n == 0) {
			continue;
		}

		/*
		 * We transform closures/errors into read+write accesses
		 * so as to force the read() or write() call that will
		 * detect the situation.
		 */
		while (u -- > 0) {
			if (pfd[u].revents & (POLLERR | POLLHUP)) {
				pfd[u].revents |= POLLIN | POLLOUT;
			}
		}

		recvapp_ok = recvapp && (pfd[k_out].revents & POLLOUT) != 0;
		sendrec_ok = sendrec && (pfd[k_fd].revents & POLLOUT) != 0;
		recvrec_ok = recvrec && (pfd[k_fd].revents & POLLIN) != 0;
		sendapp_ok = sendapp && (pfd[k_in].revents & POLLIN) != 0;
#endif

		/*
		 * We give preference to outgoing data, on stdout and on
		 * the socket.
		 */
		if (recvapp_ok) {
			unsigned char *buf;
			size_t len;
#ifdef _WIN32
			DWORD wlen;
#else
			ssize_t wlen;
#endif

			buf = br_ssl_engine_recvapp_buf(cc, &len);
#ifdef _WIN32
			if (!WriteFile(h_out, buf, len, &wlen, NULL)) {
				if (verbose) {
					fprintf(stderr, "stdout closed...\n");
				}
				retcode = -2;
				goto engine_exit;
			}
#else
			wlen = write(1, buf, len);
			if (wlen <= 0) {
				if (verbose) {
					fprintf(stderr, "stdout closed...\n");
				}
				retcode = -2;
				goto engine_exit;
			}
#endif
			br_ssl_engine_recvapp_ack(cc, wlen);
			continue;
		}
		if (sendrec_ok) {
			unsigned char *buf;
			size_t len;
			int wlen;

			buf = br_ssl_engine_sendrec_buf(cc, &len);
			wlen = send(fd, buf, len, 0);
			if (wlen <= 0) {
#ifdef _WIN32
				int err;

				err = WSAGetLastError();
				if (err == EWOULDBLOCK
					|| err == WSAEWOULDBLOCK)
				{
					can_send = 0;
					continue;
				}
#else
				if (errno == EINTR || errno == EWOULDBLOCK) {
					continue;
				}
#endif
				if (verbose) {
					fprintf(stderr, "socket closed...\n");
				}
				retcode = -1;
				goto engine_exit;
			}
			if (trace) {
				dump_blob("Outgoing bytes", buf, wlen);
			}
			br_ssl_engine_sendrec_ack(cc, wlen);
			continue;
		}
		if (recvrec_ok) {
			unsigned char *buf;
			size_t len;
			int rlen;

			buf = br_ssl_engine_recvrec_buf(cc, &len);
			rlen = recv(fd, buf, len, 0);
			if (rlen == 0) {
				if (verbose) {
					fprintf(stderr, "socket closed...\n");
				}
				retcode = -1;
				goto engine_exit;
			}
			if (rlen < 0) {
#ifdef _WIN32
				int err;

				err = WSAGetLastError();
				if (err == EWOULDBLOCK
					|| err == WSAEWOULDBLOCK)
				{
					can_recv = 0;
					continue;
				}
#else
				if (errno == EINTR || errno == EWOULDBLOCK) {
					continue;
				}
#endif
				if (verbose) {
					fprintf(stderr, "socket broke...\n");
				}
				retcode = -1;
				goto engine_exit;
			}
			if (trace) {
				dump_blob("Incoming bytes", buf, rlen);
			}
			br_ssl_engine_recvrec_ack(cc, rlen);
			continue;
		}
		if (sendapp_ok) {
			unsigned char *buf;
			size_t len;
#ifdef _WIN32
			int rlen;
#else
			ssize_t rlen;
#endif

			buf = br_ssl_engine_sendapp_buf(cc, &len);
#ifdef _WIN32
			rlen = in_read_buffered(h_in, &bb, buf, len);
#else
			rlen = read(0, buf, len);
#endif
			if (rlen <= 0) {
				if (verbose) {
					fprintf(stderr, "stdin closed...\n");
				}
				br_ssl_engine_close(cc);
			} else if (!run_command(cc, buf, rlen)) {
				br_ssl_engine_sendapp_ack(cc, rlen);
			}
			br_ssl_engine_flush(cc, 0);
			continue;
		}

		/* We should never reach that point. */
		fprintf(stderr, "ERROR: poll() misbehaves\n");
		retcode = -2;
		goto engine_exit;
	}

	/*
	 * Release allocated structures.
	 */
engine_exit:
#ifdef _WIN32
	if (fd_event != WSA_INVALID_EVENT) {
		WSACloseEvent(fd_event);
	}
#endif
	return retcode;
}
Exemplo n.º 6
0
static void
dump_prop(uint32_t prop_id, uint64_t value)
{
	int i;
	drmModePropertyPtr prop;

	prop = drmModeGetProperty(fd, prop_id);

	printf("\t%d", prop_id);
	if (!prop) {
		printf("\n");
		return;
	}

	printf(" %s:\n", prop->name);

	printf("\t\tflags:");
	if (prop->flags & DRM_MODE_PROP_PENDING)
		printf(" pending");
	if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
		printf(" immutable");
	if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
		printf(" signed range");
	if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
		printf(" range");
	if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
		printf(" enum");
	if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
		printf(" bitmask");
	if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
		printf(" blob");
	if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
		printf(" object");
	printf("\n");


	if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
		printf("\t\tvalues:");
		for (i = 0; i < prop->count_values; i++)
			printf(" %"PRId64, U642I64(prop->values[i]));
		printf("\n");
	}

	if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
		printf("\t\tvalues:");
		for (i = 0; i < prop->count_values; i++)
			printf(" %"PRIu64, prop->values[i]);
		printf("\n");
	}

	if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
		printf("\t\tenums:");
		for (i = 0; i < prop->count_enums; i++)
			printf(" %s=%llu", prop->enums[i].name,
			       prop->enums[i].value);
		printf("\n");
	} else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
		printf("\t\tvalues:");
		for (i = 0; i < prop->count_enums; i++)
			printf(" %s=0x%llx", prop->enums[i].name,
			       (1LL << prop->enums[i].value));
		printf("\n");
	} else {
		assert(prop->count_enums == 0);
	}

	if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
		printf("\t\tblobs:\n");
		for (i = 0; i < prop->count_blobs; i++)
			dump_blob(prop->blob_ids[i]);
		printf("\n");
	} else {
		assert(prop->count_blobs == 0);
	}

	printf("\t\tvalue:");
	if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
		dump_blob(value);
	else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
		printf(" %"PRId64"\n", value);
	else
		printf(" %"PRIu64"\n", value);

	drmModeFreeProperty(prop);
}