Back to index

lightning-sunbird  0.9+nobinonly
keyhi.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Dr Stephen Henson <stephen.henson@gemplus.com>
00023  *   Dr Vipul Gupta <vipul.gupta@sun.com>, Sun Microsystems Laboratories
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 /* $Id: keyhi.h,v 1.13.2.2 2006/06/08 05:42:36 wtchang%redhat.com Exp $ */
00039 
00040 #ifndef _KEYHI_H_
00041 #define _KEYHI_H_
00042 
00043 #include "plarena.h"
00044 
00045 #include "seccomon.h"
00046 #include "secoidt.h"
00047 #include "secdert.h"
00048 #include "keythi.h"
00049 #include "certt.h"
00050 /*#include "secpkcs5.h" */
00051 
00052 SEC_BEGIN_PROTOS
00053 
00054 
00055 /*
00056 ** Destroy a subject-public-key-info object.
00057 */
00058 extern void SECKEY_DestroySubjectPublicKeyInfo(CERTSubjectPublicKeyInfo *spki);
00059 
00060 /*
00061 ** Copy subject-public-key-info "src" to "dst". "dst" is filled in
00062 ** appropriately (memory is allocated for each of the sub objects).
00063 */
00064 extern SECStatus SECKEY_CopySubjectPublicKeyInfo(PRArenaPool *arena,
00065                                         CERTSubjectPublicKeyInfo *dst,
00066                                         CERTSubjectPublicKeyInfo *src);
00067 
00068 /*
00069 ** Update the PQG parameters for a cert's public key.
00070 ** Only done for DSA and Fortezza certs
00071 */
00072 extern SECStatus
00073 SECKEY_UpdateCertPQG(CERTCertificate * subjectCert);
00074 
00075 
00076 /* Compare the KEA parameters of two public keys.  
00077  * Only used by fortezza.      */
00078 
00079 extern SECStatus
00080 SECKEY_KEAParamCompare(CERTCertificate *cert1,CERTCertificate *cert2);
00081 
00082 /*
00083 ** Return the strength of the public key in bytes
00084 */
00085 extern unsigned SECKEY_PublicKeyStrength(SECKEYPublicKey *pubk);
00086 
00087 /*
00088 ** Return the strength of the public key in bits
00089 */
00090 extern unsigned SECKEY_PublicKeyStrengthInBits(SECKEYPublicKey *pubk);
00091 
00092 /*
00093 ** Return the length of the signature in bytes
00094 */
00095 extern unsigned SECKEY_SignatureLen(const SECKEYPublicKey *pubk);
00096 
00097 /*
00098 ** Make a copy of the private key "privKey"
00099 */
00100 extern SECKEYPrivateKey *SECKEY_CopyPrivateKey(SECKEYPrivateKey *privKey);
00101 
00102 /*
00103 ** Make a copy of the public key "pubKey"
00104 */
00105 extern SECKEYPublicKey *SECKEY_CopyPublicKey(SECKEYPublicKey *pubKey);
00106 
00107 /*
00108 ** Convert a private key "privateKey" into a public key
00109 */
00110 extern SECKEYPublicKey *SECKEY_ConvertToPublicKey(SECKEYPrivateKey *privateKey);
00111 
00112 /*
00113  * create a new RSA key pair. The private Key is returned...
00114  */
00115 SECKEYPrivateKey *SECKEY_CreateRSAPrivateKey(int keySizeInBits,
00116                                       SECKEYPublicKey **pubk, void *cx);
00117        
00118 /*
00119  * create a new DH key pair. The private Key is returned...
00120  */
00121 SECKEYPrivateKey *SECKEY_CreateDHPrivateKey(SECKEYDHParams *param,
00122                                       SECKEYPublicKey **pubk, void *cx);
00123 
00124 /*
00125  * create a new EC key pair. The private Key is returned...
00126  */
00127 SECKEYPrivateKey *SECKEY_CreateECPrivateKey(SECKEYECParams *param,
00128                                            SECKEYPublicKey **pubk, void *cx);
00129 
00130 /*
00131 ** Create a subject-public-key-info based on a public key.
00132 */
00133 extern CERTSubjectPublicKeyInfo *
00134 SECKEY_CreateSubjectPublicKeyInfo(SECKEYPublicKey *k);
00135 
00136 /*
00137 ** Decode a DER encoded public key into an SECKEYPublicKey structure.
00138 */
00139 extern SECKEYPublicKey *SECKEY_DecodeDERPublicKey(SECItem *pubkder);
00140 
00141 /*
00142 ** Convert a base64 ascii encoded DER public key to our internal format.
00143 */
00144 extern SECKEYPublicKey *SECKEY_ConvertAndDecodePublicKey(char *pubkstr);
00145 
00146 /*
00147 ** Convert a base64 ascii encoded DER public key and challenge to spki,
00148 ** and verify the signature and challenge data are correct
00149 */
00150 extern CERTSubjectPublicKeyInfo *
00151 SECKEY_ConvertAndDecodePublicKeyAndChallenge(char *pkacstr, char *challenge,
00152                                                         void *cx);
00153 
00154 /*
00155 ** Encode a  CERTSubjectPublicKeyInfo structure. into a
00156 ** DER encoded subject public key info. 
00157 */
00158 SECItem *
00159 SECKEY_EncodeDERSubjectPublicKeyInfo(SECKEYPublicKey *pubk);
00160 
00161 /*
00162 ** Decode a DER encoded subject public key info into a
00163 ** CERTSubjectPublicKeyInfo structure.
00164 */
00165 extern CERTSubjectPublicKeyInfo *
00166 SECKEY_DecodeDERSubjectPublicKeyInfo(SECItem *spkider);
00167 
00168 /*
00169 ** Convert a base64 ascii encoded DER subject public key info to our
00170 ** internal format.
00171 */
00172 extern CERTSubjectPublicKeyInfo *
00173 SECKEY_ConvertAndDecodeSubjectPublicKeyInfo(char *spkistr);
00174 
00175 /*
00176  * extract the public key from a subject Public Key info structure.
00177  * (used by JSS).
00178  */
00179 extern SECKEYPublicKey *
00180 SECKEY_ExtractPublicKey(CERTSubjectPublicKeyInfo *);
00181 
00182 /*
00183 ** Destroy a private key object.
00184 **     "key" the object
00185 */
00186 extern void SECKEY_DestroyPrivateKey(SECKEYPrivateKey *key);
00187 
00188 
00189 /*
00190 ** Destroy a public key object.
00191 **     "key" the object
00192 */
00193 extern void SECKEY_DestroyPublicKey(SECKEYPublicKey *key);
00194 
00195 /* Destroy and zero out a private key info structure.  for now this
00196  * function zero's out memory allocated in an arena for the key 
00197  * since PORT_FreeArena does not currently do this.  
00198  *
00199  * NOTE -- If a private key info is allocated in an arena, one should 
00200  * not call this function with freeit = PR_FALSE.  The function should 
00201  * destroy the arena.  
00202  */
00203 extern void
00204 SECKEY_DestroyPrivateKeyInfo(SECKEYPrivateKeyInfo *pvk, PRBool freeit);
00205 
00206 /* Destroy and zero out an encrypted private key info.
00207  *
00208  * NOTE -- If a encrypted private key info is allocated in an arena, one should 
00209  * not call this function with freeit = PR_FALSE.  The function should 
00210  * destroy the arena.  
00211  */
00212 extern void
00213 SECKEY_DestroyEncryptedPrivateKeyInfo(SECKEYEncryptedPrivateKeyInfo *epki,
00214                                   PRBool freeit);
00215 
00216 /* Copy private key info structure.  
00217  *  poolp is the arena into which the contents of from is to be copied.
00218  *     NULL is a valid entry.
00219  *  to is the destination private key info
00220  *  from is the source private key info
00221  * if either from or to is NULL or an error occurs, SECFailure is 
00222  * returned.  otherwise, SECSuccess is returned.
00223  */
00224 extern SECStatus
00225 SECKEY_CopyPrivateKeyInfo(PRArenaPool *poolp,
00226                        SECKEYPrivateKeyInfo *to,
00227                        SECKEYPrivateKeyInfo *from);
00228 
00229 extern SECStatus
00230 SECKEY_CacheStaticFlags(SECKEYPrivateKey* key);
00231 
00232 /* Copy encrypted private key info structure.  
00233  *  poolp is the arena into which the contents of from is to be copied.
00234  *     NULL is a valid entry.
00235  *  to is the destination encrypted private key info
00236  *  from is the source encrypted private key info
00237  * if either from or to is NULL or an error occurs, SECFailure is 
00238  * returned.  otherwise, SECSuccess is returned.
00239  */
00240 extern SECStatus
00241 SECKEY_CopyEncryptedPrivateKeyInfo(PRArenaPool *poolp,
00242                                SECKEYEncryptedPrivateKeyInfo *to,
00243                                SECKEYEncryptedPrivateKeyInfo *from);
00244 /*
00245  * Accessor functions for key type of public and private keys.
00246  */
00247 KeyType SECKEY_GetPrivateKeyType(SECKEYPrivateKey *privKey);
00248 KeyType SECKEY_GetPublicKeyType(SECKEYPublicKey *pubKey);
00249 
00250 /*
00251  * Creates a PublicKey from its DER encoding.
00252  * Currently only supports RSA and DSA keys.
00253  */
00254 SECKEYPublicKey*
00255 SECKEY_ImportDERPublicKey(SECItem *derKey, CK_KEY_TYPE type);
00256 
00257 SECKEYPrivateKeyList*
00258 SECKEY_NewPrivateKeyList(void);
00259 
00260 void
00261 SECKEY_DestroyPrivateKeyList(SECKEYPrivateKeyList *keys);
00262 
00263 void
00264 SECKEY_RemovePrivateKeyListNode(SECKEYPrivateKeyListNode *node);
00265 
00266 SECStatus
00267 SECKEY_AddPrivateKeyToListTail( SECKEYPrivateKeyList *list,
00268                                 SECKEYPrivateKey *key);
00269 
00270 #define PRIVKEY_LIST_HEAD(l) ((SECKEYPrivateKeyListNode*)PR_LIST_HEAD(&l->list))
00271 #define PRIVKEY_LIST_NEXT(n) ((SECKEYPrivateKeyListNode *)n->links.next)
00272 #define PRIVKEY_LIST_END(n,l) (((void *)n) == ((void *)&l->list))
00273 
00274 SECKEYPublicKeyList*
00275 SECKEY_NewPublicKeyList(void);
00276 
00277 void
00278 SECKEY_DestroyPublicKeyList(SECKEYPublicKeyList *keys);
00279 
00280 void
00281 SECKEY_RemovePublicKeyListNode(SECKEYPublicKeyListNode *node);
00282 
00283 SECStatus
00284 SECKEY_AddPublicKeyToListTail( SECKEYPublicKeyList *list,
00285                                 SECKEYPublicKey *key);
00286 
00287 #define PUBKEY_LIST_HEAD(l) ((SECKEYPublicKeyListNode*)PR_LIST_HEAD(&l->list))
00288 #define PUBKEY_LIST_NEXT(n) ((SECKEYPublicKeyListNode *)n->links.next)
00289 #define PUBKEY_LIST_END(n,l) (((void *)n) == ((void *)&l->list))
00290 
00291 /*
00292  * Length in bits of the EC's field size.  This is also the length of
00293  * the x and y coordinates of EC points, such as EC public keys and
00294  * base points.
00295  *
00296  * Return 0 on failure (unknown EC domain parameters).
00297  */
00298 extern int SECKEY_ECParamsToKeySize(const SECItem *params);
00299 
00300 /*
00301  * Length in bits of the EC base point order, usually denoted n.  This
00302  * is also the length of EC private keys and ECDSA signature components
00303  * r and s.
00304  *
00305  * Return 0 on failure (unknown EC domain parameters).
00306  */
00307 extern int SECKEY_ECParamsToBasePointOrderLen(const SECItem *params);
00308 
00309 SEC_END_PROTOS
00310 
00311 #endif /* _KEYHI_H_ */