1 // Copyright 2015 The Android Open Source Project
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 #ifndef WEBSERVER_WEBSERVD_UTILS_H_
16 #define WEBSERVER_WEBSERVD_UTILS_H_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 #include <openssl/ossl_typ.h>
22 
23 #include <base/files/file_path.h>
24 #include <base/time/time.h>
25 #include <brillo/secure_blob.h>
26 
27 namespace webservd {
28 
29 using X509Ptr = std::unique_ptr<X509, void(*)(X509*)>;
30 
31 // Creates a new X509 certificate.
32 X509Ptr CreateCertificate(int serial_number,
33                           const base::TimeDelta& cert_expiration,
34                           const std::string& common_name);
35 
36 // Generates an RSA public-private key pair of the specified strength.
37 std::unique_ptr<RSA, void(*)(RSA*)> GenerateRSAKeyPair(int key_length_bits);
38 
39 // Serializes a private key from the key pair into a PEM string and returns
40 // it as a binary blob.
41 brillo::SecureBlob StoreRSAPrivateKey(RSA* rsa_key_pair);
42 
43 // Checks if the buffer |key| contains a valid RSA private key.
44 bool ValidateRSAPrivateKey(const brillo::SecureBlob& key);
45 
46 // Serializes an X509 certificate using PEM format.
47 brillo::Blob StoreCertificate(X509* cert);
48 
49 // Stores/loads an X509 certificate to/from a file (in PEM format).
50 bool StoreCertificate(X509* cert, const base::FilePath& file);
51 X509Ptr LoadAndValidateCertificate(const base::FilePath& file);
52 
53 // Same as openssl x509 -fingerprint -sha256.
54 brillo::Blob GetSha256Fingerprint(X509* cert);
55 
56 // Creates a socket bound to a specified network interface.
57 // Returns a socket file descriptor or -1 on error.
58 int CreateNetworkInterfaceSocket(const std::string& if_name);
59 
60 }  // namespace webservd
61 
62 #endif  // WEBSERVER_WEBSERVD_UTILS_H_
63