getpubek.c

来自「IBM开发的TPM的驱动, 有少量的例子可以供参考」· C语言 代码 · 共 86 行

C
86
字号
/****************************************************************************//*                                                                          *//*                         Read Endorsement Public Key                      *//*                                                                          *//*                           Written by J. Kravitz                          *//*                                                                          *//*                     IBM Thomas J. Watson Research Center                 *//*                                                                          *//*                               Version 1.0                                *//*                                                                          *//*                         Last Revision 15 Mar 2004                        *//*                                                                          *//*                           Copyright (C) 2004 IBM                         *//*                                                                          *//****************************************************************************/#include <stdio.h>#include <string.h>#include "tpmfunc.h"#include <openssl/rsa.h>#include <openssl/pem.h>#include <openssl/evp.h>int main(int argc, char *argv[]){	int ret, i;	unsigned char pass1hash[20];	pubkeydata pubek;	RSA *rsa;		/* OpenSSL format Public Key */	FILE *keyfile;		/* output file for public key */	EVP_PKEY pkey;		/* OpenSSL public key */	TPM_setlog(0);		/* turn off verbose output */	if (argc > 1) {		/* if password is specified, use OwnerReadKey */		TSS_sha1(argv[1], strlen(argv[1]), pass1hash);		ret = TPM_OwnerReadPubek(pass1hash, &pubek);		if (ret != 0) {			fprintf(stderr,				"Error %s from TPM_OwnerReadPubek\n",				TPM_GetErrMsg(ret));			exit(2);		}	} else {		/* if no password specified, use ReadEKey */		ret = TPM_ReadPubek(&pubek);		if (ret != 0) {			fprintf(stderr,				"Error %s from TPM_ReadPubek: try getpubek <owner password>\n",				TPM_GetErrMsg(ret));			exit(2);		}	}	/*	 ** convert the returned public key to OpenSSL format and	 ** export it to a file	 */	rsa = TSS_convpubkey(&pubek);	if (rsa == NULL) {		fprintf(stderr, "Error from TSS_convpubkey\n");		exit(3);	}	OpenSSL_add_all_algorithms();	EVP_PKEY_assign_RSA(&pkey, rsa);	keyfile = fopen("pubek.pem", "w");	if (keyfile == NULL) {		fprintf(stderr, "Unable to create public key file\n");		exit(4);	}	ret = PEM_write_PUBKEY(keyfile, &pkey);	if (ret == 0) {		fprintf(stderr, "Unable to write public key file\n");		exit(5);	}	printf("pubek.pem successfully written\n");	printf("Pubek keylength %d\nModulus:", pubek.keylength);	for (i = 0; i < pubek.keylength; i++) {		if (!(i % 16))			printf("\n");		printf("%02X ", pubek.modulus[i]);	}	printf("\n");	fclose(keyfile);	RSA_free(rsa);	exit(0);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?