Shawn Willden | d67afae | 2014-08-19 12:36:27 -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 "asymmetric_key.h" |
| 18 | #include "key_blob.h" |
| 19 | |
| 20 | #include "key.h" |
| 21 | |
| 22 | namespace keymaster { |
| 23 | |
| 24 | Key::Key(const KeyBlob& blob) { |
| 25 | authorizations_.push_back(blob.unenforced()); |
| 26 | authorizations_.push_back(blob.enforced()); |
| 27 | } |
| 28 | |
| 29 | /* static */ |
| 30 | Key* Key::CreateKey(const KeyBlob& blob, keymaster_error_t* error) { |
| 31 | switch (blob.algorithm()) { |
| 32 | case KM_ALGORITHM_RSA: |
| 33 | return new RsaKey(blob, error); |
| 34 | case KM_ALGORITHM_DSA: |
| 35 | return new DsaKey(blob, error); |
| 36 | case KM_ALGORITHM_ECDSA: |
| 37 | return new EcdsaKey(blob, error); |
| 38 | default: |
| 39 | *error = KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 40 | return NULL; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /* static */ |
| 45 | Key* Key::GenerateKey(const AuthorizationSet& key_description, keymaster_error_t* error) { |
| 46 | keymaster_algorithm_t algorithm; |
| 47 | if (!key_description.GetTagValue(TAG_ALGORITHM, &algorithm)) { |
| 48 | *error = KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | switch (algorithm) { |
| 53 | case KM_ALGORITHM_RSA: |
| 54 | return RsaKey::GenerateKey(key_description, error); |
| 55 | case KM_ALGORITHM_DSA: |
| 56 | return DsaKey::GenerateKey(key_description, error); |
| 57 | case KM_ALGORITHM_ECDSA: |
| 58 | return EcdsaKey::GenerateKey(key_description, error); |
| 59 | default: |
| 60 | *error = KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 61 | return NULL; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | } // namespace keymaster |