takeown.c

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

C
82
字号
/****************************************************************************//*                                                                          *//*                         Test of TPM Take Ownership                       *//*                                                                          *//*                           Written by J. Kravitz                          *//*                                                                          *//*                     IBM Thomas J. Watson Research Center                 *//*                                                                          *//*                               Version 1.1                                *//*                                                                          *//*                         Last Revision 11 Feb 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;	unsigned char pass1hash[20];	unsigned char pass2hash[20];	keydata srk;	RSA *rsa;		/* OpenSSL format Public Key */	FILE *keyfile;		/* output file for public key */	EVP_PKEY pkey;		/* OpenSSL public key */	if (argc < 2) {		fprintf(stderr,			"Usage: takeown <owner password> [<storage root key password>]\n");		exit(1);	}	TPM_setlog(0);		/* turn off verbose output */	/*	 ** use the SHA1 hash of the password string as the Owner Authorization Data	 */	TSS_sha1(argv[1], strlen(argv[1]), pass1hash);	/*	 ** use the SHA1 hash of the password string as the SRK Authorization Data	 */	if (argc > 2) {		TSS_sha1(argv[2], strlen(argv[2]), pass2hash);		ret = TPM_TakeOwnership(pass1hash, pass2hash, &srk);	} else {		ret = TPM_TakeOwnership(pass1hash, NULL, &srk);	}	if (ret != 0) {		fprintf(stderr, "Error %s from TPM_TakeOwnership\n",			TPM_GetErrMsg(ret));		exit(2);	}	/*	 ** convert the returned public key to OpenSSL format and	 ** export it to a file	 */	rsa = TSS_convpubkey(&(srk.pub));	if (rsa == NULL) {		fprintf(stderr, "Error from TSS_convpubkey\n");		exit(3);	}	OpenSSL_add_all_algorithms();	EVP_PKEY_assign_RSA(&pkey, rsa);	keyfile = fopen("srk.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);	}	fclose(keyfile);	RSA_free(rsa);	exit(0);}

⌨️ 快捷键说明

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