int RSAVerifyBinary_f(const uint8_t* key_blob, const RSAPublicKey* key, const uint8_t* buf, int len, const uint8_t* sig, int algorithm) { RSAPublicKey* verification_key = NULL; uint8_t* digest = NULL; int key_size; int sig_size; int success; if (algorithm >= kNumAlgorithms) return 0; /* Invalid algorithm. */ key_size = RSAProcessedKeySize(algorithm); sig_size = siglen_map[algorithm] * sizeof(uint32_t); if (key_blob && !key) verification_key = RSAPublicKeyFromBuf(key_blob, key_size); else if (!key_blob && key) verification_key = (RSAPublicKey*) key; /* Supress const warning. */ else return 0; /* Both can't be NULL or non-NULL. */ digest = DigestBuf(buf, len, algorithm); success = RSA_verify(verification_key, sig, sig_size, algorithm, digest); Free(digest); if (!key) RSAPublicKeyFree(verification_key); /* Only free if we allocated it. */ return success; }
RSAPublicKey *PublicKeyToRSA(const VbPublicKey *key) { RSAPublicKey *rsa; uint64_t key_size; if (kNumAlgorithms <= key->algorithm) { VBDEBUG(("Invalid algorithm.\n")); return NULL; } if (!RSAProcessedKeySize(key->algorithm, &key_size) || key_size != key->key_size) { VBDEBUG(("Wrong key size for algorithm\n")); return NULL; } rsa = RSAPublicKeyFromBuf(GetPublicKeyDataC(key), key->key_size); if (!rsa) return NULL; rsa->algorithm = (unsigned int)key->algorithm; return rsa; }
VbPublicKey* PublicKeyRead(const char* filename) { VbPublicKey* key; uint64_t file_size; uint64_t key_size; key = (VbPublicKey*)ReadFile(filename, &file_size); if (!key) return NULL; do { /* Sanity-check key data */ if (0 != VerifyPublicKeyInside(key, file_size, key)) { VBDEBUG(("PublicKeyRead() not a VbPublicKey\n")); break; } if (key->algorithm >= kNumAlgorithms) { VBDEBUG(("PublicKeyRead() invalid algorithm\n")); break; } if (key->key_version > 0xFFFF) { VBDEBUG(("PublicKeyRead() invalid version\n")); break; /* Currently, TPM only supports 16-bit version */ } if (!RSAProcessedKeySize(key->algorithm, &key_size) || key_size != key->key_size) { VBDEBUG(("PublicKeyRead() wrong key size for algorithm\n")); break; } /* Success */ return key; } while(0); /* Error */ free(key); return NULL; }
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; }