Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <openssl/rsa.h> |
| 18 | #include <openssl/evp.h> |
| 19 | |
| 20 | #include "rsa_operation.h" |
| 21 | |
| 22 | namespace keymaster { |
| 23 | |
| 24 | struct EVP_PKEY_Delete { |
| 25 | void operator()(EVP_PKEY* p) const { |
| 26 | EVP_PKEY_free(p); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | RsaOperation::RsaOperation(keymaster_purpose_t purpose, const KeyBlob& key) |
| 31 | : Operation(purpose), rsa_key_(NULL) { |
| 32 | assert(key.algorithm() == KM_ALGORITHM_RSA); |
| 33 | |
| 34 | if ((!key.enforced().GetTagValue(TAG_DIGEST, &digest_) && |
| 35 | !key.unenforced().GetTagValue(TAG_DIGEST, &digest_)) || |
| 36 | digest_ != KM_DIGEST_NONE) { |
| 37 | error_ = KM_ERROR_UNSUPPORTED_DIGEST; |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | if ((!key.enforced().GetTagValue(TAG_PADDING, &padding_) && |
| 42 | !key.unenforced().GetTagValue(TAG_PADDING, &padding_)) || |
| 43 | padding_ != KM_PAD_NONE) { |
| 44 | error_ = KM_ERROR_UNSUPPORTED_PADDING_MODE; |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> evp_key(EVP_PKEY_new()); |
| 49 | if (evp_key.get() == NULL) { |
| 50 | error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | EVP_PKEY* tmp_pkey = evp_key.get(); |
| 55 | const uint8_t* key_material = key.key_material(); |
| 56 | if (d2i_PrivateKey(EVP_PKEY_RSA, &tmp_pkey, &key_material, key.key_material_length()) == NULL) { |
| 57 | error_ = KM_ERROR_INVALID_KEY_BLOB; |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | rsa_key_ = EVP_PKEY_get1_RSA(evp_key.get()); |
| 62 | if (rsa_key_ == NULL) { |
| 63 | error_ = KM_ERROR_UNKNOWN_ERROR; |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | // Since we're not using a digest function, we just need to store the text, up to the key |
| 68 | // size, until Finish is called, so we allocate a place to put it. |
| 69 | if (!data_to_sign_.Reinitialize(RSA_size(rsa_key_))) { |
| 70 | error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 71 | return; |
| 72 | } |
| 73 | error_ = KM_ERROR_OK; |
| 74 | } |
| 75 | |
| 76 | RsaOperation::~RsaOperation() { |
| 77 | if (rsa_key_ != NULL) |
| 78 | RSA_free(rsa_key_); |
| 79 | } |
| 80 | |
| 81 | keymaster_error_t RsaOperation::Update(const Buffer& input, Buffer* output) { |
| 82 | switch (purpose()) { |
| 83 | default: |
| 84 | return KM_ERROR_UNIMPLEMENTED; |
| 85 | case KM_PURPOSE_SIGN: |
| 86 | return Sign(input, output); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | keymaster_error_t RsaOperation::Sign(const Buffer& input, Buffer* /* output */) { |
| 91 | if (!data_to_sign_.write(input.peek_read(), input.available_read())) |
| 92 | return KM_ERROR_INVALID_INPUT_LENGTH; |
| 93 | return KM_ERROR_OK; |
| 94 | } |
| 95 | |
| 96 | keymaster_error_t RsaOperation::Finish(Buffer* signature, Buffer* /* output */) { |
| 97 | switch (purpose()) { |
| 98 | case KM_PURPOSE_SIGN: { |
| 99 | signature->Reinitialize(RSA_size(rsa_key_)); |
| 100 | if (data_to_sign_.available_read() != signature->buffer_size()) |
| 101 | return KM_ERROR_INVALID_INPUT_LENGTH; |
| 102 | |
| 103 | int bytes_encrypted = |
| 104 | RSA_private_encrypt(data_to_sign_.available_read(), data_to_sign_.peek_read(), |
| 105 | signature->peek_write(), rsa_key_, RSA_NO_PADDING); |
| 106 | if (bytes_encrypted < 0) |
| 107 | return KM_ERROR_UNKNOWN_ERROR; |
| 108 | assert(bytes_encrypted == RSA_size(rsa_key_)); |
| 109 | signature->advance_write(bytes_encrypted); |
| 110 | return KM_ERROR_OK; |
| 111 | } |
| 112 | default: |
| 113 | return KM_ERROR_UNIMPLEMENTED; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } // namespace keymaster |