Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(682)

Side by Side Diff: net/cert/internal/signature_algorithm.h

Issue 1218753002: Add DER parsing of AlgorithmId for signatures. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "net/base/net_export.h"
12
13 namespace net {
14
15 namespace der {
16 class Input;
17 }
Ryan Sleevi 2015/07/07 13:55:15 nit: } // namespace der
eroman 2015/07/08 01:09:34 Done.
18
19 // Enumeration that describes a (parameter-less) hashing algorithm.
Ryan Sleevi 2015/07/07 13:55:15 nit: This comment doesn't seem to really add much.
eroman 2015/07/08 01:09:34 Done.
20 enum class DigestAlgorithm {
21 Sha1,
22 Sha256,
23 Sha384,
24 Sha512,
25 };
26
27 // Enumeration that describes a class of algorithm (the parameters to the
28 // algorithm are specified separately).
Ryan Sleevi 2015/07/07 13:55:15 // The signature scheme used within a signature. P
eroman 2015/07/08 01:09:34 Done.
29 enum class SignatureAlgorithmId {
30 RsaPkcs1, // RSA PKCS#1 v1.5
31 RsaPss, // RSASSA-PSS
32 Ecdsa, // ECDSA
33 };
34
35 // Base class for describing algorithm parameters.
36 class NET_EXPORT SignatureAlgorithmParameters {
37 public:
38 SignatureAlgorithmParameters() {}
39 virtual ~SignatureAlgorithmParameters(){};
40
41 // Returns true if the two parameters are identical. This must only be called
42 // with instances of the same type (so it is safe for implementations to
43 // cast).
44 virtual bool Equals(const SignatureAlgorithmParameters* other) const = 0;
45
46 private:
47 DISALLOW_COPY_AND_ASSIGN(SignatureAlgorithmParameters);
48 };
49
50 // Parameters for an RSASSA-PSS signature algorithm.
51 //
52 // The trailer is assumed to be 1, and the mask gen to be MGF1.
Ryan Sleevi 2015/07/07 13:55:15 Why?
eroman 2015/07/08 01:09:34 Why not? I don't see any support in BoringSSL for
Ryan Sleevi 2015/07/08 11:44:01 Sorry for being opaque, especially with the round-
eroman 2015/07/14 20:31:39 Done.
53 class NET_EXPORT RsaPssParameters : public SignatureAlgorithmParameters {
54 public:
55 RsaPssParameters(DigestAlgorithm mgf1_hash, uint32_t salt_length);
56
57 bool Equals(const SignatureAlgorithmParameters* other) const override;
58
59 DigestAlgorithm mgf1_hash() const { return mgf1_hash_; }
60 uint32_t salt_length() const { return salt_length_; }
61
62 private:
63 const DigestAlgorithm mgf1_hash_;
64 const uint32_t salt_length_;
65 };
66
67 // SignatureAlgorithm describes a signature algorithm and its parameters. This
68 // corresponds to "AlgorithmIdentifier" from RFC 5280.
69 class NET_EXPORT SignatureAlgorithm {
70 public:
71 // Creates an empty (invalid) signature algorithm. Callers should use one of
72 // the Assign* methods to initialize it.
73 SignatureAlgorithm();
Ryan Sleevi 2015/07/07 13:55:15 Why init+Assign vs a factory create? Is the assump
eroman 2015/07/08 01:09:34 Using factory methods creates the need to support
Ryan Sleevi 2015/07/08 11:49:05 Sorry, by factory method I meant something like s
eroman 2015/07/14 20:31:39 Done. (Except renamed ParseDer --> CreateFromDer()
74 ~SignatureAlgorithm();
75
76 // Returns true if the SignatureAlgorithm has been initialized. Accessors
77 // should not be called on invalid SignatureAlgorithms.
78 //
79 // Note that IsValid() does not imply that the algorithm and its parameters
80 // are valid in a cryptographic sense, just that the object itself has been
81 // initialized.
82 bool IsValid() const WARN_UNUSED_RESULT;
83
84 SignatureAlgorithmId algorithm() const;
85 DigestAlgorithm digest() const;
86
87 // Assigns the SignatureAlgorithm by parsing a DER-encoded
88 // "AlgorithmIdentifier" (RFC 5280). Returns true on success.
89 bool ParseDer(const der::Input& algorithm_identifier) WARN_UNUSED_RESULT;
90
91 // Sets the SignatureAlgorithm to the given type and parameters.
92 void AssignRsaPkcs1(DigestAlgorithm digest);
93 void AssignEcdsa(DigestAlgorithm digest);
94 void AssignRsaPss(DigestAlgorithm digest,
95 DigestAlgorithm mgf1_hash,
96 uint32_t salt_length);
97
98 // Returns true if |*this| is equivalent to |other|. This compares both the
99 // algorithm ID and each parameter for equality.
100 bool Equals(const SignatureAlgorithm& other) const WARN_UNUSED_RESULT;
101
102 // The following methods retrieve the parameters for the signature algorithm.
103 //
104 // The correct parameters should be chosen based on the algorithm ID. For
105 // instance a SignatureAlgorithm with |algorithm() == RsaPss| should retrieve
106 // parameters via ParametersForRsaPss().
107 //
108 // The returned pointer is non-owned, and will be valid only as long as the
109 // parameters remain alive. This correspond with the lifetime of the
110 // SignatureAlgorithm, provided there are no intervening calls to Assign*() or
111 // Parse*().
112 const RsaPssParameters* ParamsForRsaPss() const;
113
114 private:
115 // Clears the SignatureAlgorithm, rendering it in an invalid state as if it
116 // had just been default-constructed.
117 void AssignInvalid();
118
119 SignatureAlgorithmId algorithm_;
120 DigestAlgorithm digest_;
121 scoped_ptr<SignatureAlgorithmParameters> params_;
122
123 DISALLOW_COPY_AND_ASSIGN(SignatureAlgorithm);
124 };
125
126 } // namespace net
127
128 #endif // NET_CERT_INTERNAL_SIGNATURE_ALGORITHM_H_
OLDNEW
« no previous file with comments | « no previous file | net/cert/internal/signature_algorithm.cc » ('j') | net/cert/internal/signature_algorithm.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698