1 /**
2 * PK Key Types
3 * 
4 * Copyright:
5 * (C) 1999-2007 Jack Lloyd
6 * (C) 2014-2015 Etienne Cimon
7 *
8 * License:
9 * Botan is released under the Simplified BSD License (see LICENSE.md)
10 */
11 module botan.pubkey.pk_keys;
12 
13 import botan.constants;
14 public import botan.asn1.alg_id;
15 import memutils.vector;
16 import botan.asn1.asn1_oid;
17 import botan.rng.rng;
18 import botan.asn1.der_enc;
19 import botan.asn1.oids;
20 import botan.utils.types;
21 
22 /**
23 * Public Key Base Class.
24 */
25 interface PublicKey
26 {
27 public:
28     /**
29     * Get the name of the underlying public key scheme.
30     * Returns: name of the public key scheme
31     */
32     abstract @property string algoName() const;
33 
34     /**
35     * Return the estimated strength of the underlying key against
36     * the best currently known attack. Note that this ignores anything
37     * but pure attacks against the key itself and do not take into
38     * account padding schemes, usage mistakes, etc which might reduce
39     * the strength. However it does suffice to provide an upper bound.
40     *
41     * Returns: estimated strength in bits
42     */
43     abstract size_t estimatedStrength() const;
44 
45     /**
46     * Get the OID of the underlying public key scheme.
47     * Returns: OID of the public key scheme
48     */
49     final OID getOid() const
50     {
51         try {
52             return OIDS.lookup(algoName);
53         }
54         catch(LookupError)
55         {
56             throw new LookupError("PK algo " ~ algoName ~ " has no defined OIDs");
57         }
58     }
59 
60 
61     /**
62     * Test the key values for consistency.
63     *
64     * Params:
65     *  rng = rng to use
66     *  strong = whether to perform strong and lengthy version
67     * of the test
68     * Returns: true if the test is passed
69     */
70     abstract bool checkKey(RandomNumberGenerator rng, bool strong) const;
71 
72     /**
73     * Find out the number of message parts supported by this scheme.
74     * Returns: number of message parts
75     */
76     abstract size_t messageParts() const;
77 
78     /**
79     * Find out the message part size supported by this scheme/key.
80     * Returns: size of the message parts in bits
81     */
82     abstract size_t messagePartSize() const; 
83 
84     /**
85     * Get the maximum message size in bits supported by this public key.
86     * Returns: maximum message size in bits
87     */
88     abstract size_t maxInputBits() const;
89 
90     /**
91     * Returns: X.509 AlgorithmIdentifier for this key
92     */
93     abstract AlgorithmIdentifier algorithmIdentifier() const;
94 
95     /**
96     * Returns: X.509 subject key encoding for this key object
97     */
98     abstract Vector!ubyte x509SubjectPublicKey() const;
99 
100     /**
101     * Self-test after loading a key
102     * Params:
103     *  rng = a random number generator
104     */
105     final void loadCheck(RandomNumberGenerator rng) const
106     {
107         if (!checkKey(rng, BOTAN_PUBLIC_KEY_STRONG_CHECKS_ON_LOAD))
108             throw new InvalidArgument(algoName ~ ": Invalid public key");
109     }
110 }
111 
112 /**
113 * Private Key Base Class
114 */
115 interface PrivateKey : PublicKey
116 {
117 public:
118     /**
119     * Returns: PKCS #8 private key encoding for this key object
120     */
121     abstract SecureVector!ubyte pkcs8PrivateKey() const;
122 
123     /**
124     * Returns: PKCS #8 AlgorithmIdentifier for this key
125     * Might be different from the X.509 identifier, but normally is not
126     */
127     abstract AlgorithmIdentifier pkcs8AlgorithmIdentifier() const;
128 
129     /**
130     * Self-test after loading a key
131     * Params:
132     *  rng = a random number generator
133     */
134     final void loadCheck(RandomNumberGenerator rng) const
135     {
136         if (!checkKey(rng, BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_LOAD))
137             throw new InvalidArgument(algoName ~ ": Invalid private key");
138     }
139 
140     /**
141     * Self-test after generating a key
142     * Params:
143     *  rng = a random number generator
144     */
145     final void genCheck(RandomNumberGenerator rng) const
146     {
147         if (!checkKey(rng, BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_GENERATE))
148             throw new SelfTestFailure(algoName ~ " private key generation failed");
149     }
150 }
151 
152 /**
153 * PK Secret Value Derivation Key
154 */
155 interface PKKeyAgreementKey : PrivateKey
156 {
157 public:
158     /*
159     * Returns: public component of this key
160     */
161     abstract Vector!ubyte publicValue() const;
162 
163 }
164 
165 /*
166 * Typedefs
167 */
168 alias X509PublicKey = PublicKey;
169 alias PKCS8PrivateKey = PrivateKey;
170 
171 template UnConst(T) {
172     static if (is(T U == const(U))) {
173         alias UnConst = U;
174     } else static if (is(T V == immutable(V))) {
175         alias UnConst = V;
176     } else static if (is(T W == inout(W))) {
177         alias UnConst = W;
178     } else alias UnConst = T;
179 }