int PublicKeyWrite(const char* filename, const VbPublicKey* key) { VbPublicKey* kcopy; int rv; /* Copy the key, so its data is contiguous with the header */ kcopy = PublicKeyAlloc(key->key_size, 0, 0); if (!kcopy) return 1; if (0 != PublicKeyCopy(kcopy, key)) { free(kcopy); return 1; } /* Write the copy, then free it */ rv = WriteFile(filename, kcopy, kcopy->key_offset + kcopy->key_size); free(kcopy); return rv; }
VbPublicKey* PublicKeyReadKeyb(const char* filename, uint64_t algorithm, uint64_t version) { VbPublicKey* key; uint8_t* key_data; uint64_t key_size; uint64_t expected_key_size; if (algorithm >= kNumAlgorithms) { VBDEBUG(("PublicKeyReadKeyb() called with invalid algorithm!\n")); return NULL; } if (version > 0xFFFF) { /* Currently, TPM only supports 16-bit version */ VBDEBUG(("PublicKeyReadKeyb() called with invalid version!\n")); return NULL; } key_data = ReadFile(filename, &key_size); if (!key_data) return NULL; if (!RSAProcessedKeySize(algorithm, &expected_key_size) || expected_key_size != key_size) { VBDEBUG(("PublicKeyReadKeyb() wrong key size for algorithm\n")); free(key_data); return NULL; } key = PublicKeyAlloc(key_size, algorithm, version); if (!key) { free(key_data); return NULL; } Memcpy(GetPublicKeyData(key), key_data, key_size); free(key_data); return key; }
static void VerifyPublicKeyToRSA(const VbPublicKey *orig_key) { RSAPublicKey *rsa; VbPublicKey *key = PublicKeyAlloc(orig_key->key_size, 0, 0); PublicKeyCopy(key, orig_key); key->algorithm = kNumAlgorithms; TEST_EQ((size_t)PublicKeyToRSA(key), 0, "PublicKeyToRSA() invalid algorithm"); PublicKeyCopy(key, orig_key); key->key_size -= 1; TEST_EQ((size_t)PublicKeyToRSA(key), 0, "PublicKeyToRSA() invalid size"); rsa = PublicKeyToRSA(orig_key); TEST_NEQ((size_t)rsa, 0, "PublicKeyToRSA() ok"); if (rsa) { TEST_EQ((int)rsa->algorithm, (int)key->algorithm, "PublicKeyToRSA() algorithm"); RSAPublicKeyFree(rsa); } }
static int vb1_make_keypair() { VbPrivateKey *privkey = 0; VbPublicKey *pubkey = 0; RSA *rsa_key = 0; uint8_t *keyb_data = 0; uint32_t keyb_size; enum vb2_signature_algorithm sig_alg; uint64_t vb1_algorithm; FILE *fp; int ret = 1; fp = fopen(infile, "rb"); if (!fp) { fprintf(stderr, "Unable to open %s\n", infile); goto done; } rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL); fclose(fp); if (!rsa_key) { fprintf(stderr, "Unable to read RSA key from %s\n", infile); goto done; } sig_alg = vb2_rsa_sig_alg(rsa_key); if (sig_alg == VB2_SIG_INVALID) { fprintf(stderr, "Unsupported sig algorithm in RSA key\n"); goto done; } /* combine the sig_alg with the hash_alg to get the vb1 algorithm */ vb1_algorithm = (sig_alg - VB2_SIG_RSA1024) * 3 + opt_hash_alg - VB2_HASH_SHA1; /* Create the private key */ privkey = (VbPrivateKey *)malloc(sizeof(VbPrivateKey)); if (!privkey) goto done; privkey->rsa_private_key = rsa_key; privkey->algorithm = vb1_algorithm; /* Write it out */ strcpy(outext, ".vbprivk"); if (0 != PrivateKeyWrite(outfile, privkey)) { fprintf(stderr, "unable to write private key\n"); goto done; } fprintf(stderr, "wrote %s\n", outfile); /* Create the public key */ ret = vb_keyb_from_rsa(rsa_key, &keyb_data, &keyb_size); if (ret) { fprintf(stderr, "couldn't extract the public key\n"); goto done; } pubkey = PublicKeyAlloc(keyb_size, vb1_algorithm, opt_version); if (!pubkey) goto done; memcpy(GetPublicKeyData(pubkey), keyb_data, keyb_size); /* Write it out */ strcpy(outext, ".vbpubk"); if (0 != PublicKeyWrite(outfile, pubkey)) { fprintf(stderr, "unable to write public key\n"); goto done; } fprintf(stderr, "wrote %s\n", outfile); ret = 0; done: free(privkey); free(pubkey); free(keyb_data); RSA_free(rsa_key); return ret; }