|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 #ifndef dtls_identity_h__ |
|
7 #define dtls_identity_h__ |
|
8 |
|
9 #include <string> |
|
10 |
|
11 #include "m_cpp_utils.h" |
|
12 #include "mozilla/RefPtr.h" |
|
13 #include "nsISupportsImpl.h" |
|
14 #include "ScopedNSSTypes.h" |
|
15 |
|
16 // All code in this module requires NSS to be live. |
|
17 // Callers must initialize NSS and implement the nsNSSShutdownObject |
|
18 // protocol. |
|
19 namespace mozilla { |
|
20 |
|
21 class DtlsIdentity { |
|
22 public: |
|
23 ~DtlsIdentity(); |
|
24 |
|
25 // Generate an identity with a random name. |
|
26 static TemporaryRef<DtlsIdentity> Generate(); |
|
27 |
|
28 // Note: the following two functions just provide access. They |
|
29 // do not transfer ownership. If you want a pointer that lasts |
|
30 // past the lifetime of the DtlsIdentity, you must make |
|
31 // a copy yourself. |
|
32 CERTCertificate *cert() { return cert_; } |
|
33 SECKEYPrivateKey *privkey() { return privkey_; } |
|
34 |
|
35 std::string GetFormattedFingerprint(const std::string &algorithm = DEFAULT_HASH_ALGORITHM); |
|
36 |
|
37 nsresult ComputeFingerprint(const std::string algorithm, |
|
38 unsigned char *digest, |
|
39 std::size_t size, |
|
40 std::size_t *digest_length); |
|
41 |
|
42 static nsresult ComputeFingerprint(const CERTCertificate *cert, |
|
43 const std::string algorithm, |
|
44 unsigned char *digest, |
|
45 std::size_t size, |
|
46 std::size_t *digest_length); |
|
47 |
|
48 static nsresult ParseFingerprint(const std::string fp, |
|
49 unsigned char *digest, |
|
50 size_t size, size_t *length); |
|
51 |
|
52 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DtlsIdentity) |
|
53 |
|
54 private: |
|
55 DtlsIdentity(SECKEYPrivateKey *privkey, CERTCertificate *cert) |
|
56 : privkey_(privkey), cert_(cert) {} |
|
57 DISALLOW_COPY_ASSIGN(DtlsIdentity); |
|
58 |
|
59 static const std::string DEFAULT_HASH_ALGORITHM; |
|
60 static const size_t HASH_ALGORITHM_MAX_LENGTH; |
|
61 |
|
62 std::string FormatFingerprint(const unsigned char *digest, |
|
63 std::size_t size); |
|
64 |
|
65 ScopedSECKEYPrivateKey privkey_; |
|
66 CERTCertificate *cert_; // TODO: Using a smart pointer here causes link |
|
67 // errors. |
|
68 }; |
|
69 } // close namespace |
|
70 #endif |