Beispiel #1
0
QRimageResultType QRimage_writeImage(
	QRcode *qrcode,
	const char *outfile,
	QRimageType imagetype
	)
{
	if (!qrcode) {
		return QR_IMG_QRCODE_IS_NULL;
	}
	switch(imagetype) {
		case QR_IMG_PNG:
			return writePNG(qrcode, outfile);
		case QR_IMG_EPS:
			return writeEPS(qrcode, outfile);
		case QR_IMG_SVG:
			return writeSVG(qrcode, outfile);
		case QR_IMG_ANSI:
			return writeANSI(qrcode, outfile, 0);
		case QR_IMG_ANSI256:
			return writeANSI(qrcode, outfile, 1);
		case QR_IMG_ASCIIi:
			return writeASCII(qrcode, outfile,  1);
		case QR_IMG_ASCII:
			return writeASCII(qrcode, outfile,  0);
		case QR_IMG_UTF8:
			return writeUTF8(qrcode, outfile, 0);
		case QR_IMG_ANSIUTF8:
			return writeUTF8(qrcode, outfile, 1);
		default:
			return QR_IMG_UNKNOW_IMAGE_TYPE;
	}
	return QR_IMG_SUCCESS;
}
Beispiel #2
0
static void qrencode(const unsigned char *intext, int length, const char *outfile)
{
	QRcode *qrcode;
	
	qrcode = encode(intext, length);
	if(qrcode == NULL) {
		perror("Failed to encode the input data");
		exit(EXIT_FAILURE);
	}

	if(verbose) {
		fprintf(stderr, "File: %s, Version: %d\n", (outfile!=NULL)?outfile:"(stdout)", qrcode->version);
	}

	switch(image_type) {
		case PNG_TYPE:
			writePNG(qrcode, outfile);
			break;
		case EPS_TYPE:
			writeEPS(qrcode, outfile);
			break;
		case SVG_TYPE:
			writeSVG(qrcode, outfile);
			break;
		case ANSI_TYPE:
		case ANSI256_TYPE:
			writeANSI(qrcode, outfile);
			break;
		case ASCIIi_TYPE:
			writeASCII(qrcode, outfile,  1);
			break;
		case ASCII_TYPE:
			writeASCII(qrcode, outfile,  0);
			break;
		case UTF8_TYPE:
			writeUTF8(qrcode, outfile, 0);
			break;
		case ANSIUTF8_TYPE:
			writeUTF8(qrcode, outfile, 1);
			break;
		default:
			fprintf(stderr, "Unknown image type.\n");
			exit(EXIT_FAILURE);
	}

	QRcode_free(qrcode);
}
Beispiel #3
0
void verifyne::QR::printAnsi(std::string data, std::ostream &out)
{
    //std::cout << "[+] QR code following ..." << std::endl << data << std::endl;

    QRcode *q = QRcode_encodeString(data.c_str(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);

    if(q == nullptr)
    {
        throw std::runtime_error("Failed to generate QR code");
    }

    //std::cout << "[+] QR code has width " << q->width << std::endl;

    writeANSI(q, out);

    free(q);
}
Beispiel #4
0
static void qrencodeStructured(const unsigned char *intext, int length, const char *outfile)
{
	QRcode_List *qrlist, *p;
	char filename[FILENAME_MAX];
	char *base, *q, *suffix = NULL;
	const char *type_suffix;
	int i = 1;
	size_t suffix_size;

	switch(image_type) {
		case PNG_TYPE:
			type_suffix = ".png";
			break;
		case EPS_TYPE:
			type_suffix = ".eps";
			break;
		case SVG_TYPE:
			type_suffix = ".svg";
			break;
		case ANSI_TYPE:
		case ANSI256_TYPE:
		case ASCII_TYPE:
		case UTF8_TYPE:
		case ANSIUTF8_TYPE:
			type_suffix = ".txt";
			break;
		default:
			fprintf(stderr, "Unknown image type.\n");
			exit(EXIT_FAILURE);
	}

	if(outfile == NULL) {
		fprintf(stderr, "An output filename must be specified to store the structured images.\n");
		exit(EXIT_FAILURE);
	}
	base = strdup(outfile);
	if(base == NULL) {
		fprintf(stderr, "Failed to allocate memory.\n");
		exit(EXIT_FAILURE);
	}
	suffix_size = strlen(type_suffix);
	if(strlen(base) > suffix_size) {
		q = base + strlen(base) - suffix_size;
		if(strcasecmp(type_suffix, q) == 0) {
			suffix = strdup(q);
			*q = '\0';
		}
	}
	
	qrlist = encodeStructured(intext, length);
	if(qrlist == NULL) {
		perror("Failed to encode the input data");
		exit(EXIT_FAILURE);
	}

	for(p = qrlist; p != NULL; p = p->next) {
		if(p->code == NULL) {
			fprintf(stderr, "Failed to encode the input data.\n");
			exit(EXIT_FAILURE);
		}
		if(suffix) {
			snprintf(filename, FILENAME_MAX, "%s-%02d%s", base, i, suffix);
		} else {
			snprintf(filename, FILENAME_MAX, "%s-%02d", base, i);
		}

		if(verbose) {
			fprintf(stderr, "File: %s, Version: %d\n", filename, p->code->version);
		}

		switch(image_type) {
			case PNG_TYPE: 
				writePNG(p->code, filename);
				break;
			case EPS_TYPE: 
				writeEPS(p->code, filename);
				break;
			case SVG_TYPE: 
				writeSVG(p->code, filename);
				break;
			case ANSI_TYPE:
			case ANSI256_TYPE:
				writeANSI(p->code, filename);
				break;
			case ASCIIi_TYPE:
				writeASCII(p->code, filename, 1);
				break;
			case ASCII_TYPE:
				writeASCII(p->code, filename, 0);
				break;
			case UTF8_TYPE:
				writeUTF8(p->code, filename, 0);
				break;
			case ANSIUTF8_TYPE:
				writeUTF8(p->code, filename, 0);
				break;

			default:
				fprintf(stderr, "Unknown image type.\n");
				exit(EXIT_FAILURE);
		}
		i++;
	}

	free(base);
	if(suffix) {
		free(suffix);
	}

	QRcode_List_free(qrlist);
}
Beispiel #5
0
int main(int argc, char *argv[])
{
	int ret;
	char base32_key[BASE32_LEN(keylen)+1];
	unsigned char uefiblob[4100];
	unsigned char blob[4096];	/* resulting sealed blob */
	unsigned int bloblen;	/* blob length */
	unsigned char wellknown[20] = {0};
	unsigned char totpstring[64];
	uint32_t pcrmask =  0x000003BF; // PCRs 0-5 and 7-9
	const char * hostname = "TPMTOTP";

	char *outfile_name;
	FILE *infile;
	FILE *outfile;
	QRcode *qrcode;
#ifdef CONFIG_TSS
	TSS_HCONTEXT context;
#endif

	if (generate_key()) {
		return -1;
	}

	pcrvals = malloc(NUM_PCRS * sizeof(uint8_t*));
	CHECK_MALLOC(pcrvals);

	static int base32_flag = 0;
	static int qr_flag = 1;
	static int nvram_flag = 0;
	int option_index = 0;

	static struct option long_options[] = {
	     {"pcrs",  required_argument, 0, 'p'},
	     {"nvram", no_argument, &nvram_flag, 1},
	     {"no-qrcode", no_argument, &qr_flag, 0},
	     {"base32", no_argument, &base32_flag, 1},
	     {"help", no_argument, 0, 'h'},
	     {0,0,0,0}

	};

	if (argc == 1) {
	     print_help();
	     return -1;
	}

	int c;

	while ((c = getopt_long(argc, argv, "p:nbh", long_options, &option_index)) != -1) {

		switch (c) {
		case 0: // Flag only
			break;

		case 'p':
			pcrmask = parse_pcrs(optarg);
			if (pcrmask == (uint32_t) -1)
				return -1;
			break;

		case 'h':
			print_help();
			return 0;

		case 'b':
			base32_flag = 1;
			break;

		case 'n':
			nvram_flag = 1;
			break;

		case 's':
			qr_flag = 0;
			break;

		case '?': // Unrecognized Option
			print_help();
			return -1;

		default:
			print_help();
		}

	}
	
	if (!nvram_flag) {
		if (optind == argc) {
			fprintf(stderr, "Output name required!\n");
			print_help();
			return -1;
		} else {
			outfile_name = argv[optind];
		}
	}

	base32_encode(key, keylen, base32_key);
	base32_key[BASE32_LEN(keylen)] = NULL;

#ifdef CONFIG_TSS
	ret = Tspi_Context_Create(&context);
	if (ret != TSS_SUCCESS) {
		fprintf(stderr, "Unable to create TPM context\n");
		return -1;
	}
	ret = Tspi_Context_Connect(context, NULL);
	if (ret != TSS_SUCCESS) {
		fprintf(stderr, "Unable to connect to TPM\n");
		return -1;
	}
	ret = TSPI_SealCurrPCR(context, // context
			      0x40000000, // SRK
			      pcrmask,
			      wellknown,  // Well-known SRK secret
			      wellknown,  // Well-known SEAL secret
			      key, keylen,	/* data to be sealed */
			      blob, &bloblen);	/* buffer to receive result */
#else
	ret = TPM_SealCurrPCR(
			      0x40000000, // SRK
			      pcrmask,
			      wellknown,  // Well-known SRK secret
			      wellknown,  // Well-known SEAL secret
			      key, keylen,	/* data to be sealed */
			      blob, &bloblen);	/* buffer to receive result */
#endif
	if (ret != 0) {
		fprintf(stderr, "Error %s from TPM_Seal\n",
			TPM_GetErrMsg(ret));
		//goto out;
	}

	sprintf(totpstring, "otpauth://totp/%s?secret=%s", hostname, base32_key);
	//sprintf(totpstring, "%s", base32_key);

	if (base32_flag) {
		printf("%s\n", base32_key);
	}

	printf("%s\n", totpstring);
	if (qr_flag) {
		qrcode = QRcode_encodeString(totpstring, 0, QR_ECLEVEL_L,
					     QR_MODE_8, 1);
		writeANSI(qrcode);
	}

	if (nvram_flag) {
		uint32_t nvindex = 0x00004d47;
		uint32_t permissions = TPM_NV_PER_OWNERREAD|TPM_NV_PER_OWNERWRITE;
#ifdef CONFIG_TSS
		TSS_HNVSTORE nvObject;
		TSS_FLAG nvattrs = 0;

		ret = Tspi_Context_CreateObject(context, TSS_OBJECT_TYPE_NV,
						nvattrs, &nvObject);
		if (ret != TSS_SUCCESS) {
			fprintf(stderr, "Unable to create nvram object: %x\n",
				ret);
			goto out;
		}
		ret = Tspi_SetAttribUint32(nvObject, TSS_TSPATTRIB_NV_INDEX, 0,
					   nvindex);
		if (ret != TSS_SUCCESS) {
			fprintf(stderr, "Unable to set index\n");
			goto out;
		}
		ret = Tspi_SetAttribUint32(nvObject,
					   TSS_TSPATTRIB_NV_PERMISSIONS, 0,
				   permissions);
		if (ret != TSS_SUCCESS) {
			fprintf(stderr, "Unable to set permissions\n");
			goto out;
		}
		ret = Tspi_SetAttribUint32(nvObject, TSS_TSPATTRIB_NV_DATASIZE,
					   0, bloblen);
		if (ret != TSS_SUCCESS) {
			fprintf(stderr, "Unable to set size\n");
			goto out;
		}
		ret = Tspi_NV_DefineSpace(nvObject, NULL, NULL);
		if (ret != TSS_SUCCESS && ret != (0x3000 | TSS_E_NV_AREA_EXIST)) {
			fprintf(stderr, "Unable to define space: %x\n", ret);
			goto out;
		}
		ret = Tspi_NV_WriteValue(nvObject, 0, bloblen, blob);
		if (ret != TSS_SUCCESS) {
			fprintf(stderr, "Unable to write to nvram\n");
			goto out;
		}
#else
		unsigned char * ownauth = wellknown;
		unsigned char * areaauth = wellknown;
		TPM_PCR_INFO_SHORT *pcrInfoRead = NULL;
		TPM_PCR_INFO_SHORT *pcrInfoWrite = NULL;

		ret = TPM_NV_DefineSpace2(
			ownauth,
			nvindex,
			bloblen,
			permissions,
			areaauth,
			pcrInfoRead,
			pcrInfoWrite);
		//if (ret != TPM_SUCCESS && ret != (0x3000 | TSS_E_NV_AREA_EXIST)) {
		if (ret != TPM_SUCCESS) {
			fprintf(stderr, "Unable to define space: %x\n", ret);
			goto out;
		}

		ret = TPM_NV_WriteValue(
			nvindex,
			0,
			blob,
			bloblen,
			ownauth
		);
		if (ret != TPM_SUCCESS) {
			fprintf(stderr, "Unable to write to nvram\n");
			goto out;
		}
#endif
	} else {
		outfile = fopen(outfile_name, "w");
		if (outfile == NULL) {
			fprintf(stderr, "Unable to open output file '%s'\n",
				outfile_name);
			goto out;
		}
		if (strncmp(outfile_name, efivarfs, strlen(efivarfs)) == 0) {
			int attributes = 7; // NV, RT, BS
			memcpy(uefiblob, &attributes, sizeof(int));
			memcpy(uefiblob + sizeof(int), blob, bloblen);
			bloblen += sizeof(int);
			ret = fwrite(uefiblob, 1, bloblen, outfile);
		} else {
			ret = fwrite(blob, 1, bloblen, outfile);
		}
		if (ret != bloblen) {
			fprintf(stderr,
				"I/O Error while writing output file '%s'\n",
				outfile_name);
			goto out;
		}
	}

out:

#ifdef CONFIG_TSS
	Tspi_Context_FreeMemory(context, NULL);
	Tspi_Context_Close(context);
#endif

	exit(ret);
}