First public contribution.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * X.509 key classes and utility classes for key encoding/decoding.
26 #if !defined (__X509KEYS_H__)
27 #define __X509KEYS_H__
31 #include <asymmetrickeys.h>
32 #include <asymmetric.h>
37 // Forward declarations
39 class CASN1EncContainer;
40 class CASN1EncSequence;
41 class CASN1EncBitString;
44 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS
45 #include <x509keyencoder.h>
48 class CX509RSAPublicKey : public CRSAPublicKey
49 /** Adds the capability to decode DER-encoded RSA public keys.
51 * Adds a commitment to a specific encoding scheme allowing X.509 RSA public key
52 * superclasses to remain encoding-independent.
59 /** Creates a new RSA Public key object from the specified buffer containing the
60 * encoded binary representation.
62 * Initialises the object from its encoded binary form into an internal representation.
64 * @param aBinaryData The encoded binary representation.
65 * @return A pointer to the new CX509RSAPublicKey object. */
66 IMPORT_C static CX509RSAPublicKey* NewL(const TDesC8& aBinaryData);
68 /** Creates a new RSA Public Key object from the specified buffer containing the
69 * encoded binary representation, and puts a pointer to it onto the cleanup stack.
71 * @param aBinaryData The encoded binary representation.
72 * @return A pointer to the new CX509RSAPublicKey object. */
73 IMPORT_C static CX509RSAPublicKey* NewLC(const TDesC8& aBinaryData);
75 /** Creates a new RSA Public key object from the specified buffer containing the
76 * encoded binary representation, starting at the specified offset.
78 * @param aBinaryData The encoded binary representation.
79 * @param aPos The offset position from which to start decoding.
80 * @return A pointer to the new CX509RSAPublicKey object. */
81 IMPORT_C static CX509RSAPublicKey* NewL(const TDesC8& aBinaryData, TInt& aPos);
83 /** Creates a new RSA Public key object from the specified buffer containing the
84 * encoded binary representation, starting at the specified offset, and puts
85 * a pointer to it onto the cleanup stack.
87 * @param aBinaryData The encoded binary representation.
88 * @param aPos The offset position from which to start decoding.
89 * @return A pointer to the new CX509RSAPublicKey object. */
90 IMPORT_C static CX509RSAPublicKey* NewLC(const TDesC8& aBinaryData, TInt& aPos);
92 void ConstructL(const TDesC8& aBinaryData, TInt& aPos);
96 class TASN1EncRSAPublicKey
98 * Class for encoding RSA public keys to ASN.1 encoding.
105 * Encodes the supplied public key into buffer in DER format ASN.1:
112 * @param aKey Key to encode.
113 * @return Allocated buffer containing DER encoding of
114 * the supplied key aKey.
116 IMPORT_C HBufC8* EncodeDERL(const CRSAPublicKey& aKey) const;
119 class TASN1DecRSAPublicKey
121 * Class for decoding RSA public keys from ASN.1 DER encoding.
128 * Decodes an RSA key from the supplied buffer starting at the
129 * specified position.
131 * @param aDER Buffer containing DER ASN.1 encoding of the key.
132 * @param aPos Starting position in the buffer (updated on exit).
133 * @return A pointer to the new CRSAPublicKey object.
135 IMPORT_C CRSAPublicKey* DecodeDERL(const TDesC8& aDER, TInt& aPos) const;
138 class TASN1DecRSAKeyPair
140 * Class for decoding RSA key pairs from ASN.1 DER encoding.
147 * Decodes an RSA key pair from buffer containing ASN.1
148 * DER-encoded private key. The encoding of a private key
149 * contains public key components as well.
151 * @param aDER DER-encoded private key.
152 * @param aPos Position in the buffer to start decoding
154 * @param aPublicKey On return, the RSA public key object
155 * @param aPrivateKey On return, the RSA private key object
156 * @param aKeyType Key type, default is @c EStandardCRT
158 IMPORT_C void DecodeDERL(const TDesC8& aDER, TInt& aPos,
159 CRSAPublicKey*& aPublicKey,
160 CRSAPrivateKey*& aPrivateKey,
161 TRSAPrivateKeyType aKeyType = EStandardCRT);
164 class CX509DSAPublicKey : public CDSAPublicKey
165 /** Encapsulates the X.509 DSA public key.
167 * Adds a commitment to a specific encoding scheme allowing superclasses to remain
168 * encoding-independent.
172 // DSA public key, params, signature.
175 /** Creates a new X.509 DSA public key object.
177 * @param aParamsData A non-modifiable descriptor representing the entire encoding.
178 * @param aBinaryData The encoded binary representation.
179 * @return A pointer to the new CX509DSAPublicKey object. */
180 IMPORT_C static CX509DSAPublicKey* NewL(const TDesC8& aParamsData, const TDesC8& aBinaryData);
182 /** Creates a new X.509 DSA public key object, and puts a pointer to it onto the cleanup stack.
184 * @param aParamsData A non-modifiable descriptor representing the entire encoding.
185 * @param aBinaryData The encoded binary representation.
186 * @return A pointer to the new CX509DSAPublicKey object. */
187 IMPORT_C static CX509DSAPublicKey* NewLC(const TDesC8& aParamsData, const TDesC8& aBinaryData);
189 /** Creates a new X.509 DSA public key object.
191 * @param aParams The DSA parameters.
192 * @param aBinaryData The encoded binary representation.
193 * @return A pointer to the new CX509DSAPublicKey object. */
194 IMPORT_C static CX509DSAPublicKey* NewL(const CDSAParameters& aParams, const TDesC8& aBinaryData);
196 /** Creates a new X.509 DSA public key object, and puts a pointer to it onto the cleanup stack.
198 * @param aParams The DSA parameters.
199 * @param aBinaryData The encoded binary representation.
200 * @return A pointer to the new CX509DSAPublicKey object. */
201 IMPORT_C static CX509DSAPublicKey* NewLC(const CDSAParameters& aParams, const TDesC8& aBinaryData);
203 /** Creates a new X.509 DSA public key object.
205 * @param aParamsData A non-modifiable descriptor representing the entire encoding.
206 * @param aBinaryData The encoded binary representation.
207 * @param aPos The position from which to start decoding.
208 * @return A pointer to the new CX509DSAPublicKey object. */
209 IMPORT_C static CX509DSAPublicKey* NewL(const TDesC8& aParamsData, const TDesC8& aBinaryData,TInt& aPos);
211 /** Creates a new X.509 DSA public key object, and puts a pointer to it onto the cleanup stack.
213 * @param aParamsData A non-modifiable descriptor representing the entire encoding.
214 * @param aBinaryData The encoded binary representation.
215 * @param aPos The position from which to start decoding.
216 * @return A pointer to the new CX509DSAPublicKey object. */
217 IMPORT_C static CX509DSAPublicKey* NewLC(const TDesC8& aParamsData, const TDesC8& aBinaryData, TInt& aPos);
219 /** Creates a new X.509 DSA public key object.
221 * @param aParams The DSA parameters.
222 * @param aBinaryData The encoded binary representation.
223 * @param aPos The position from which to start decoding.
224 * @return A pointer to the new CX509DSAPublicKey object. */
225 IMPORT_C static CX509DSAPublicKey* NewL(const CDSAParameters& aParams, const TDesC8& aBinaryData, TInt& aPos);
227 /** Creates a new X.509 DSA public key object, and puts a pointer to it onto the cleanup stack.
229 * @param aParams The DSA parameters.
230 * @param aBinaryData The encoded binary representation.
231 * @param aPos The position from which to start decoding.
232 * @return A pointer to the new CX509DSAPublicKey object. */
233 IMPORT_C static CX509DSAPublicKey* NewLC(const CDSAParameters& aParams, const TDesC8& aBinaryData, TInt& aPos);
235 /** Gets the DSA parameters from the encoding key.
237 * @param aParamsData A non-modifiable descriptor representing the entire encoding.
238 * @return The DSA parameters. */
239 IMPORT_C static CDSAParameters* DSAParametersL(const TDesC8& aParamsData);
241 /** @internalComponent */
242 void ConstructL(const TDesC8& aParamsData, const TDesC8& aBinaryData, TInt& aPos);
243 /** @internalComponent */
244 void ConstructL(const CDSAParameters& aParams, const TDesC8& aBinaryData, TInt& aPos);
245 /** @internalComponent */
249 class TASN1DecDSAKeyPair
251 * Class for decoding DSA key pairs from ASN.1 DER encoding.
258 * Decodes a DSA key pair from a buffer containing an ASN.1
259 * DER-encoded private key.
261 * The encoding of the private key contains public key components as well.
263 * The DER encoding has the following format:
266 * INTEGER version (==0, ignored)
267 * INTEGER p (public prime)
268 * INTEGER q (160-bit public subprime, q | p-1)
269 * INTEGER g (public generator of subgroup)
270 * INTEGER x (private key)
271 * INTEGER y (public key y=g^x)
274 * @param aDER DER-encoded private key.
275 * @param aPos Position in the buffer to start decoding
277 * @param aPublicKey On return, the DSA public key object
278 * @param aPrivateKey On return, the DSA private key object
280 IMPORT_C void DecodeDERL(const TDesC8& aDER, TInt& aPos,
281 CDSAPublicKey*& aPublicKey, CDSAPrivateKey*& aPrivateKey);
285 * Class for encoding DSA public keys to ASN.1 encoding.
289 class TASN1EncDSAPublicKey
293 * Encodes the supplied public key into a buffer in DER format.
295 * Note that the encoding has the following format:
302 * BIT STRING (encoded INTEGER public value)
305 * @param aKey Key to encode.
306 * @return Sequence containing public key information.
308 IMPORT_C CASN1EncSequence* EncodeDERL(const CDSAPublicKey& aKey) const;
311 * Encodes DSA parameters into an ASN.1 encoding structure suitable for
312 * inclusion into other objects, like a PKCS#10 certificate request.
314 * Note that the encoding has the following form:
322 * @param aKey DSA public key.
323 * @return ASN.1 encoding structure on the cleanup stack.
325 IMPORT_C CASN1EncSequence* EncodeParamsLC(const CDSAPublicKey& aKey) const;
328 * Encodes a public key as a bit string.
330 * @param aKey DSA public key.
331 * @return ASN.1 bit string (public key). This is left on the cleanup stack.
334 IMPORT_C CASN1EncBitString* EncodePublicValueLC(const CDSAPublicKey& aKey) const;
337 class CX509DSASignature : public CDSASignature
338 /** Encapsulates the X.509 DSA signature.
340 * Adds a commitment to a specific encoding scheme allowing superclasses to remain
341 * encoding-independent.
346 /** Creates a new DSA Signature object from the specified buffer containing the
347 * encoded binary representation.
349 * @param aBinaryData The encoded binary representation.
350 * @return A pointer to the new CX509DSASignature object. */
351 IMPORT_C static CX509DSASignature* NewL(const TDesC8& aBinaryData);
353 /** Creates a new DSA Signature object from the specified buffer containing the
354 * encoded binary representation, and puts a pointer to it onto the cleanup stack.
356 * @param aBinaryData The encoded binary representation.
357 * @return A pointer to the new CX509DSASignature object. */
358 IMPORT_C static CX509DSASignature* NewLC(const TDesC8& aBinaryData);
360 /** Creates a new DSA Signature object from the specified buffer containing the
361 * encoded binary representation, starting at the specified offset.
363 * @param aBinaryData The encoded binary representation.
364 * @param aPos The offset position from which to start decoding.
365 * @return A pointer to the new CX509DSASignature object. */
366 IMPORT_C static CX509DSASignature* NewL(const TDesC8& aBinaryData, TInt& aPos);
368 /** Creates a new DSA Signature object from the specified buffer containing the
369 * encoded binary representation, starting at the specified offset, and puts
370 * a pointer to it onto the cleanup stack.
372 * @param aBinaryData The encoded binary representation.
373 * @param aPos The offset position from which to start decoding.
374 * @return A pointer to the new CX509DSASignature object. */
375 IMPORT_C static CX509DSASignature* NewLC(const TDesC8& aBinaryData, TInt& aPos);
377 void ConstructL(const TDesC8& aBinaryData, TInt& aPos);
381 class CX509DHPublicKey : public CDHPublicKey
382 /** Provides clients with the information they need for Diffie-Hellman key exchange
388 /** Creates a new CX509DHPublicKey object from the specified buffer containing the encoded
389 * binary representation.
391 * @param aParamsData A non-modifiable descriptor representing the entire encoding.
393 * @return A pointer to the new CX509DHPublicKey object.*/
394 IMPORT_C static CX509DHPublicKey* NewL(const TDesC8& aParamsData, const TDesC8& aKeyData);
396 /** Creates a new CX509DHPublicKey object from the specified buffer containing the encoded
397 * binary representation, and puts a pointer to it onto the cleanup stack.
399 * @param aParamsData A non-modifiable descriptor representing the entire encoding.
401 * @return A pointer to the new CX509DHPublicKey object.*/
402 IMPORT_C static CX509DHPublicKey* NewLC(const TDesC8& aParamsData, const TDesC8& aKeyData);
406 * Frees all resources owned by the object, prior to its destruction. */
407 IMPORT_C virtual ~CX509DHPublicKey();
409 /** @internalComponent */
411 /** @internalComponent */
412 void ConstructL(const TDesC8& aParamsData, const TDesC8& aKeyData);
415 class CX509DHKeyPair : public CDHKeyPair
416 /** This class represents the Diffie-Hellman Key Pair.
421 /** Creates a new DH key pair object from the specified buffer containing
422 * the encoded binary representation .
424 * @param aParamsData A non-modifiable descriptor representing the entire encoding.
425 * @return A pointer to the new CX509DHKeyPair object.
427 IMPORT_C static CX509DHKeyPair* NewL(const TDesC8& aParamsData);
429 /** Creates a new DH Key Pair object from the specified buffer containing the encoded binary
430 * representation, and puts a pointer to it onto the cleanup stack.
432 * @param aParamsData A non-modifiable descriptor representing the entire encoding.
433 * @return A pointer to the new CX509DHKeyPair object.
435 IMPORT_C static CX509DHKeyPair* NewLC(const TDesC8& aParamsData);
438 /** Virtual Destructor.
439 * Frees all resources owned by the object, prior to its destruction.
442 IMPORT_C virtual ~CX509DHKeyPair();
444 /** @internalComponent */
446 /** @internalComponent */
447 void ConstructL(const TDesC8& aParamsData);
450 class CX509DHValidationParams : public CBase
451 /** Validates Diffie-Hellman (DH) Domain parameters.
453 * Provides access to the DH Validation Parameters, which are used to determine
454 * if the DH Public Key has been generated in conformance with the algorithm
455 * specified in ESDH (see RFC 2631).
460 /** Creates a new DH Validation parameters object from the specified buffer containing
461 * the encoded binary representation.
463 * @param aBinaryData The encoded binary representation.
464 * @return A pointer to the new CX509DHValidationParams object. */
465 IMPORT_C static CX509DHValidationParams* NewL(const TDesC8& aBinaryData);
467 /** Creates a new DH Validation parameters object from the specified buffer containing
468 * the encoded binary representation, and puts a pointer to it onto the cleanup stack.
470 * @param aBinaryData The encoded binary representation.
471 * @return A pointer to the new CX509DHValidationParams object. */
472 IMPORT_C static CX509DHValidationParams* NewLC(const TDesC8& aBinaryData);
474 /** Creates a new DH Validation parameters object from the specified buffer containing
475 * the encoded binary representation, starting at the specified offset.
477 * @param aBinaryData The encoded binary representation.
478 * @param aPos The offset position from which to start decoding.
479 * @return A pointer to the new CX509DHValidationParams object. */
480 IMPORT_C static CX509DHValidationParams* NewL(const TDesC8& aBinaryData, TInt& aPos);
482 /** Creates a new DH Validation parameters object from the specified buffer containing
483 * the encoded binary representation, starting at the specified offset, and puts
484 * a pointer to it onto the cleanup stack.
486 * @param aBinaryData The encoded binary representation.
487 * @param aPos The offset position from which to start decoding.
488 * @return A pointer to the new CX509DHValidationParams object. */
489 IMPORT_C static CX509DHValidationParams* NewLC(const TDesC8& aBinaryData, TInt& aPos);
491 /** Gets a DSA prime generation seed.
493 * @return The bit string parameter used as the seed. */
494 IMPORT_C const TPtrC8 Seed() const;
496 /** Gets the output from a DSA prime generation counter.
498 * @return The integer value output. */
499 IMPORT_C const TInteger& PGenCounter() const;
503 * Frees all resources owned by the object, prior to its destruction. */
504 virtual ~CX509DHValidationParams();
506 /** @internalComponent */
507 CX509DHValidationParams();
508 /** @internalComponent */
509 void ConstructL(const TDesC8& aBinaryData, TInt& aPos);
511 RInteger iPGenCounter;
514 class CX509DHDomainParams : public CBase
515 /** Encapsulates the compulsory Diffie-Hellman domain parameter values P and G
521 /** Creates a new DH Domain parameters object from the specified buffer containing
522 * the encoded binary representation.
524 * @param aBinaryData The encoded binary representation.
525 * @return A pointer to the new CX509DHDomainParams object. */
526 IMPORT_C static CX509DHDomainParams* NewL(const TDesC8& aBinaryData);
528 /** Creates a new DH Domain parameters object from the specified buffer containing
529 * the encoded binary representation, and puts a pointer to it onto the cleanup stack.
531 * @param aBinaryData The encoded binary representation.
532 * @return A pointer to the new CX509DHDomainParams object. */
533 IMPORT_C static CX509DHDomainParams* NewLC(const TDesC8& aBinaryData);
535 /** Creates a new DH Domain parameters object from the specified buffer containing
536 * the encoded binary representation, starting at the specified offset.
538 * @param aBinaryData The encoded binary representation.
539 * @param aPos The offset position from which to start decoding.
540 * @return A pointer to the new CX509DHDomainParams object. */
541 IMPORT_C static CX509DHDomainParams* NewL(const TDesC8& aBinaryData, TInt& aPos);
543 /** Creates a new DH Domain parameters object from the specified buffer containing
544 * the encoded binary representation, starting at the specified offset, and puts
545 * a pointer to it onto the cleanup stack.
547 * @param aBinaryData The encoded binary representation.
548 * @param aPos The offset position from which to start decoding.
549 * @return A pointer to the new CX509DHDomainParams object. */
550 IMPORT_C static CX509DHDomainParams* NewLC(const TDesC8& aBinaryData, TInt& aPos);
552 /** Gets the compulsory parameter value P.
554 * @return The compulsory parameter value P. */
555 IMPORT_C const TInteger& P() const;
557 /** Gets the compulsory parameter value G.
559 * @return The compulsory parameter value G. */
560 IMPORT_C const TInteger& G() const;
562 //the next 3 members are optional, in which case NULL is returned
563 //the returned objects remain the property of this object
565 //N.B. according to RFC 2459 the Q member is *not* optional,
566 //however it is not essential for doing DH, and empirical studies
567 //suggest it doesn't get included much, so I'm relaxing the spec here
568 //to permit DomainParams objects which contain no Q.
570 /** Gets the optional value Q.
572 * @return The optional value Q. */
573 IMPORT_C const TInteger& Q() const;
575 /** Gets the optional value J.
577 * @return The optional value J. */
578 IMPORT_C const TInteger& J() const;
580 /** Gets the optional validation parameters.
582 * @return The optional validation parameters. */
583 IMPORT_C const CX509DHValidationParams* ValidationParams() const;
587 * Frees all resources owned by the object, prior to its destruction. */
588 virtual ~CX509DHDomainParams();
590 /** @internalComponent */
591 CX509DHDomainParams();
592 /** @internalComponent */
593 void ConstructL(const TDesC8& aBinaryData, TInt& aPos);
598 CX509DHValidationParams* iValidationParams;