| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 * | 4 * |
| 5 * Host functions for keys. | 5 * Host functions for keys. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 /* TODO: change all 'return 0', 'return 1' into meaningful return codes */ | 8 /* TODO: change all 'return 0', 'return 1' into meaningful return codes */ |
| 9 | 9 |
| 10 #define OPENSSL_NO_SHA | 10 #define OPENSSL_NO_SHA |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 65 |
| 66 void PrivateKeyFree(VbPrivateKey* key) { | 66 void PrivateKeyFree(VbPrivateKey* key) { |
| 67 if (!key) | 67 if (!key) |
| 68 return; | 68 return; |
| 69 if (key->rsa_private_key) | 69 if (key->rsa_private_key) |
| 70 RSA_free(key->rsa_private_key); | 70 RSA_free(key->rsa_private_key); |
| 71 Free(key); | 71 Free(key); |
| 72 } | 72 } |
| 73 | 73 |
| 74 | 74 |
| 75 void PublicKeyInit(VbPublicKey* key, uint8_t* key_data, uint64_t key_size) { | |
| 76 key->key_offset = OffsetOf(key, key_data); | |
| 77 key->key_size = key_size; | |
| 78 key->algorithm = kNumAlgorithms; /* Key not present yet */ | |
| 79 key->key_version = 0; | |
| 80 } | |
| 81 | |
| 82 | |
| 83 /* Allocate a new public key with space for a [key_size] byte key. */ | 75 /* Allocate a new public key with space for a [key_size] byte key. */ |
| 84 VbPublicKey* PublicKeyAlloc(uint64_t key_size, uint64_t algorithm, | 76 VbPublicKey* PublicKeyAlloc(uint64_t key_size, uint64_t algorithm, |
| 85 uint64_t version) { | 77 uint64_t version) { |
| 86 VbPublicKey* key = (VbPublicKey*)Malloc(sizeof(VbPublicKey) + key_size); | 78 VbPublicKey* key = (VbPublicKey*)Malloc(sizeof(VbPublicKey) + key_size); |
| 87 if (!key) | 79 if (!key) |
| 88 return NULL; | 80 return NULL; |
| 89 | 81 |
| 90 key->algorithm = algorithm; | 82 key->algorithm = algorithm; |
| 91 key->key_version = version; | 83 key->key_version = version; |
| 92 key->key_size = key_size; | 84 key->key_size = key_size; |
| 93 key->key_offset = sizeof(VbPublicKey); | 85 key->key_offset = sizeof(VbPublicKey); |
| 94 return key; | 86 return key; |
| 95 } | 87 } |
| 96 | 88 |
| 97 | 89 |
| 98 /* Copy a public key from [src] to [dest]. | |
| 99 * | |
| 100 * Returns zero if success, non-zero if error. */ | |
| 101 int PublicKeyCopy(VbPublicKey* dest, const VbPublicKey* src) { | |
| 102 if (dest->key_size < src->key_size) | |
| 103 return 1; | |
| 104 | |
| 105 dest->key_size = src->key_size; | |
| 106 dest->algorithm = src->algorithm; | |
| 107 dest->key_version = src->key_version; | |
| 108 Memcpy(GetPublicKeyData(dest), GetPublicKeyDataC(src), src->key_size); | |
| 109 return 0; | |
| 110 } | |
| 111 | |
| 112 | |
| 113 VbPublicKey* PublicKeyReadKeyb(const char* filename, uint64_t algorithm, | 90 VbPublicKey* PublicKeyReadKeyb(const char* filename, uint64_t algorithm, |
| 114 uint64_t version) { | 91 uint64_t version) { |
| 115 VbPublicKey* key; | 92 VbPublicKey* key; |
| 116 uint8_t* key_data; | 93 uint8_t* key_data; |
| 117 uint64_t key_size; | 94 uint64_t key_size; |
| 118 | 95 |
| 119 if (algorithm >= kNumAlgorithms) { | 96 if (algorithm >= kNumAlgorithms) { |
| 120 debug("PublicKeyReadKeyb() called with invalid algorithm!\n"); | 97 debug("PublicKeyReadKeyb() called with invalid algorithm!\n"); |
| 121 return NULL; | 98 return NULL; |
| 122 } | 99 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 if (0 != PublicKeyCopy(kcopy, key)) { | 174 if (0 != PublicKeyCopy(kcopy, key)) { |
| 198 Free(kcopy); | 175 Free(kcopy); |
| 199 return 1; | 176 return 1; |
| 200 } | 177 } |
| 201 | 178 |
| 202 /* Write the copy, then free it */ | 179 /* Write the copy, then free it */ |
| 203 rv = WriteFile(filename, kcopy, kcopy->key_offset + kcopy->key_size); | 180 rv = WriteFile(filename, kcopy, kcopy->key_offset + kcopy->key_size); |
| 204 Free(kcopy); | 181 Free(kcopy); |
| 205 return rv; | 182 return rv; |
| 206 } | 183 } |
| OLD | NEW |