//////////////////////////////////////////////////////////////////////////////// // Copyright 2019 Google LLC. // // This software is licensed under the terms defined in the Widevine Master // License Agreement. For a copy of this agreement, please contact // widevine-licensing@google.com. //////////////////////////////////////////////////////////////////////////////// // // Description: // Utility functions to deserialize and serialize EC keys. #ifndef COMMON_EC_UTIL_H_ #define COMMON_EC_UTIL_H_ #include #include "openssl/ec.h" #include "common/ec_key.h" namespace widevine { namespace ec_util { // Serialize EC private key into DER encoded RFC5915 std::string. // Parameter |private_key| is the EC private key to be serialized. // Parameter |serialized_private_key| will hold the serialized private key. // Caller retains ownership of all pointers and they cannot be nullptr. // Returns true if successful, false otherwise. bool SerializeECPrivateKey(const EC_KEY* private_key, std::string* serialized_private_key); // Deserialized DER encoded RFC5915 std::string into an EC private key. // Parameter |serialized_private_key| contains the serialized private key. // Parameter |private_key| will hold the deserialized EC private key. // Caller retains ownership of all pointers and they cannot be nullptr. // Returns true if successful, false otherwise. bool DeserializeECPrivateKey(const std::string& serialized_private_key, EC_KEY** private_key); // Serialize EC public key into DER encoded RFC5208 std::string. // Parameter |public_key| is the EC public key to be serialized. // Parameter |serialized_public_key| will hold the serialized public key. // Caller retains ownership of all pointers and they cannot be nullptr. // Returns true if successful, false otherwise. bool SerializeECPublicKey(const EC_KEY* public_key, std::string* serialized_public_key); // Deserialized DER encoded RFC5208 std::string into an EC private key. // Parameter |serialized_public_key| contains the serialized public key. // Parameter |public_key| will hold the deserialized EC public key. // Caller retains ownership of all pointers and they cannot be nullptr. // Returns true if successful, false otherwise. bool DeserializeECPublicKey(const std::string& serialized_public_key, EC_KEY** public_key); // Returns the expected size of the encoded public key keypoint for the given // key |curve|. size_t GetPublicKeyPointSize(ECPrivateKey::EllipticCurve curve); // Returns the public key from the |ec_key| encoded per X9.62. bool GetPublicKeyPoint(const EC_KEY* ec_key, std::string* encoded_key); // Returns a |public_key| for the given curve, deserialized from the // X9.62 |key_point|. |key_point| is required to be in uncompressed format and // the correct size for the given curve. bool GetPublicKeyFromKeyPoint(ECPrivateKey::EllipticCurve curve, const std::string& key_point, EC_KEY** public_key); // Convert |nid| to its corresponding EllipticCurve value if supported. // Parameter |nid| corresponds to one of the macros in nid.h. // Returns a valid EllipticCurve if |nid| is supported and UNDEFINED_CURVE // otherwise. ECPrivateKey::EllipticCurve NidToCurve(int nid); // Convert |curve| to its corresponding NID value. // Parameter |curve| is an accepted curve value. // Returns the NID that corresponds to the curve in nid.h if it exists and // NID_undef otherwise. int CurveToNid(ECPrivateKey::EllipticCurve curve); // Creates a new ECKey using the provided |curve|. ScopedECKEY GenerateKeyWithCurve(ECPrivateKey::EllipticCurve curve); } // namespace ec_util } // namespace widevine #endif // COMMON_EC_UTIL_H_