1 // Copyright 2018 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 /////////////////////////////////////////////////////////////////////////////// 16 #ifndef TINK_SUBTLE_PEM_PARSER_BORINGSSL_H_ 17 #define TINK_SUBTLE_PEM_PARSER_BORINGSSL_H_ 18 19 #include <memory> 20 #include <string> 21 22 #include "absl/strings/string_view.h" 23 #include "tink/internal/rsa_util.h" 24 #include "tink/subtle/subtle_util_boringssl.h" 25 #include "tink/util/statusor.h" 26 27 namespace crypto { 28 namespace tink { 29 namespace subtle { 30 31 // Parses keys in in PEM format (RFC 7468). 32 // This is essentially a wrapper aroung BoringSSL APIs. 33 class PemParser { 34 public: 35 // Parses a given PEM serialized RSA public key `pem_serialized_key` into a 36 // internal::RsaPublicKey. 37 static util::StatusOr<std::unique_ptr<internal::RsaPublicKey>> 38 ParseRsaPublicKey(absl::string_view pem_serialized_key); 39 40 // Parses a given PEM serialized RSA private key `pem_serialized_key` into a 41 // internal::RsaPublicKey. 42 static util::StatusOr<std::unique_ptr<internal::RsaPrivateKey>> 43 ParseRsaPrivateKey(absl::string_view pem_serialized_key); 44 45 // Writes a given internal::RsaPublicKey `rsa_key` into a PEM 46 // serialized RSA public key. 47 static util::StatusOr<std::string> WriteRsaPublicKey( 48 const internal::RsaPublicKey& rsa_public_key); 49 50 // Writes a given internal::RsaPrivateKey `rsa_key` into a PEM 51 // serialized RSA private key. 52 static util::StatusOr<std::string> WriteRsaPrivateKey( 53 const internal::RsaPrivateKey& rsa_private_key); 54 55 // Parses a given PEM serialized EC public key `pem_serialized_key` into a 56 // SubtleUtilBoringSSL::EcKey. 57 static util::StatusOr<std::unique_ptr<SubtleUtilBoringSSL::EcKey>> 58 ParseEcPublicKey(absl::string_view pem_serialized_key); 59 60 // Parses a given PEM serialized EC private key `pem_serialized_key` into a 61 // SubtleUtilBoringSSL::EcKey. 62 static util::StatusOr<std::unique_ptr<SubtleUtilBoringSSL::EcKey>> 63 ParseEcPrivateKey(absl::string_view pem_serialized_key); 64 65 // Writes a given SubtleUtilBoringSSL::EcKey `ec_key` into a PEM serialized 66 // EC public key. 67 static util::StatusOr<std::string> WriteEcPublicKey( 68 const SubtleUtilBoringSSL::EcKey& ec_key); 69 70 // Writes a given SubtleUtilBoringSSL::EcKey `ec_key` into a PEM serialized 71 // EC private key. 72 static util::StatusOr<std::string> WriteEcPrivateKey( 73 const SubtleUtilBoringSSL::EcKey& ec_key); 74 }; 75 76 } // namespace subtle 77 } // namespace tink 78 } // namespace crypto 79 #endif // TINK_SUBTLE_PEM_PARSER_BORINGSSL_H_ 80