OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_CERT_INTERNAL_SIGNATURE_ALGORITHM_H_ | |
6 #define NET_CERT_INTERNAL_SIGNATURE_ALGORITHM_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/compiler_specific.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "net/base/net_export.h" | |
14 | |
15 namespace net { | |
16 | |
17 namespace der { | |
18 class Input; | |
19 } // namespace der | |
20 | |
21 // The digest algorithm used within a signature. | |
22 enum class DigestAlgorithm { | |
23 Sha1, | |
24 Sha256, | |
25 Sha384, | |
26 Sha512, | |
27 }; | |
28 | |
29 // The signature scheme used within a signature. Parameters are specified | |
30 // separately. | |
31 enum class SignatureAlgorithmId { | |
32 RsaPkcs1, // RSA PKCS#1 v1.5 | |
33 RsaPss, // RSASSA-PSS | |
34 Ecdsa, // ECDSA | |
35 }; | |
36 | |
37 // Base class for describing algorithm parameters. | |
38 class NET_EXPORT SignatureAlgorithmParameters { | |
39 public: | |
40 SignatureAlgorithmParameters() {} | |
41 virtual ~SignatureAlgorithmParameters(){}; | |
42 | |
43 // Returns true if the two parameters are identical. This must only be called | |
44 // with instances of the same type (so it is safe for implementations to | |
45 // cast). | |
Ryan Sleevi
2015/07/16 03:46:37
Still feels like we're stuck between a footgun ("y
eroman
2015/07/16 05:23:06
I removed the virtual in favor of a different appr
| |
46 virtual bool Equals(const SignatureAlgorithmParameters* other) const = 0; | |
47 | |
48 private: | |
49 DISALLOW_COPY_AND_ASSIGN(SignatureAlgorithmParameters); | |
50 }; | |
51 | |
52 // Parameters for an RSASSA-PSS signature algorithm. | |
53 // | |
54 // The trailer is assumed to be 1 and the mask generation algorithm to be MGF1, | |
55 // as that is all that is implemented, and any other values while parsing the | |
56 // AlgorithmIdentifier will thus be rejected. | |
57 class NET_EXPORT RsaPssParameters : public SignatureAlgorithmParameters { | |
58 public: | |
59 RsaPssParameters(DigestAlgorithm mgf1_hash, uint32_t salt_length); | |
60 | |
61 bool Equals(const SignatureAlgorithmParameters* other) const override; | |
62 | |
63 DigestAlgorithm mgf1_hash() const { return mgf1_hash_; } | |
64 uint32_t salt_length() const { return salt_length_; } | |
65 | |
66 private: | |
67 const DigestAlgorithm mgf1_hash_; | |
68 const uint32_t salt_length_; | |
69 }; | |
70 | |
71 // SignatureAlgorithm describes a signature algorithm and its parameters. This | |
72 // corresponds to "AlgorithmIdentifier" from RFC 5280. | |
73 class NET_EXPORT SignatureAlgorithm { | |
74 public: | |
75 ~SignatureAlgorithm(); | |
76 | |
77 SignatureAlgorithmId algorithm() const { return algorithm_; } | |
78 DigestAlgorithm digest() const { return digest_; } | |
79 | |
80 // Creates a SignatureAlgorithm by parsing a DER-encoded "AlgorithmIdentifier" | |
81 // (RFC 5280). Returns nullptr on failure. | |
82 static scoped_ptr<SignatureAlgorithm> CreateFromDer( | |
83 const der::Input& algorithm_identifier); | |
84 | |
85 // Creates a new SignatureAlgorithm with the given type and parameters. | |
86 static scoped_ptr<SignatureAlgorithm> CreateRsaPkcs1(DigestAlgorithm digest); | |
87 static scoped_ptr<SignatureAlgorithm> CreateEcdsa(DigestAlgorithm digest); | |
88 static scoped_ptr<SignatureAlgorithm> CreateRsaPss(DigestAlgorithm digest, | |
89 DigestAlgorithm mgf1_hash, | |
90 uint32_t salt_length); | |
91 | |
92 // Returns true if |*this| is equivalent to |other|. This compares both the | |
93 // algorithm ID and each parameter for equality. | |
94 bool Equals(const SignatureAlgorithm& other) const WARN_UNUSED_RESULT; | |
95 | |
96 // The following methods retrieve the parameters for the signature algorithm. | |
97 // | |
98 // The correct parameters should be chosen based on the algorithm ID. For | |
99 // instance a SignatureAlgorithm with |algorithm() == RsaPss| should retrieve | |
100 // parameters via ParametersForRsaPss(). | |
101 // | |
102 // The returned pointer is non-owned, and has the same lifetime as |this|. | |
103 const RsaPssParameters* ParamsForRsaPss() const; | |
104 | |
105 private: | |
106 SignatureAlgorithm(SignatureAlgorithmId algorithm, | |
107 DigestAlgorithm digest, | |
108 scoped_ptr<SignatureAlgorithmParameters> params); | |
109 | |
110 const SignatureAlgorithmId algorithm_; | |
111 const DigestAlgorithm digest_; | |
112 const scoped_ptr<SignatureAlgorithmParameters> params_; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(SignatureAlgorithm); | |
115 }; | |
116 | |
117 } // namespace net | |
118 | |
119 #endif // NET_CERT_INTERNAL_SIGNATURE_ALGORITHM_H_ | |
OLD | NEW |