| 1 |
/* ***** BEGIN LICENSE BLOCK ***** |
| 2 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 3 |
* |
| 4 |
* The contents of this file are subject to the Mozilla Public License Version |
| 5 |
* 1.1 (the "License"); you may not use this file except in compliance with |
| 6 |
* the License. You may obtain a copy of the License at |
| 7 |
* http://www.mozilla.org/MPL/ |
| 8 |
* |
| 9 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 10 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 11 |
* for the specific language governing rights and limitations under the |
| 12 |
* License. |
| 13 |
* |
| 14 |
* The Original Code is the Netscape security libraries. |
| 15 |
* |
| 16 |
* The Initial Developer of the Original Code is |
| 17 |
* Netscape Communications Corporation. |
| 18 |
* Portions created by the Initial Developer are Copyright (C) 1994-2000 |
| 19 |
* the Initial Developer. All Rights Reserved. |
| 20 |
* Portions created by Red Hat, Inc, are Copyright (C) 2005 |
| 21 |
* |
| 22 |
* Contributor(s): |
| 23 |
* Rob Crittenden ([email protected]) |
| 24 |
* |
| 25 |
* Alternatively, the contents of this file may be used under the terms of |
| 26 |
* either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 |
* of those above. If you wish to allow use of your version of this file only |
| 30 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 |
* use your version of this file under the terms of the MPL, indicate your |
| 32 |
* decision by deleting the provisions above and replace them with the notice |
| 33 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 |
* the provisions above, a recipient may use your version of this file under |
| 35 |
* the terms of any one of the MPL, the GPL or the LGPL. |
| 36 |
* |
| 37 |
* ***** END LICENSE BLOCK ***** */ |
| 38 |
|
| 39 |
#ifndef CKPEM_H |
| 40 |
#define CKPEM_H |
| 41 |
|
| 42 |
#include "list.h" |
| 43 |
|
| 44 |
#define USE_UTIL_DIRECTLY |
| 45 |
#include <utilrename.h> |
| 46 |
|
| 47 |
#include <blapit.h> |
| 48 |
#include <nssbase.h> |
| 49 |
#include <nssckmdt.h> /* must be before <nssckfw.h> */ |
| 50 |
#include <nssckfw.h> |
| 51 |
#include <nssckfwt.h> |
| 52 |
#include <nssckt.h> |
| 53 |
#include <secasn1.h> |
| 54 |
#include <secder.h> |
| 55 |
#include <secoid.h> |
| 56 |
|
| 57 |
#ifdef HAVE_LOWKEYTI_H |
| 58 |
# include <lowkeyti.h> |
| 59 |
#else |
| 60 |
/* |
| 61 |
* we are dealing with older NSS that does not include the patch |
| 62 |
* for https://bugzilla.mozilla.org/show_bug.cgi?id=1263179 yet |
| 63 |
*/ |
| 64 |
typedef enum { |
| 65 |
NSSLOWKEYNullKey = 0, |
| 66 |
NSSLOWKEYRSAKey = 1, |
| 67 |
NSSLOWKEYDSAKey = 2, |
| 68 |
NSSLOWKEYDHKey = 4, |
| 69 |
NSSLOWKEYECKey = 5 |
| 70 |
} NSSLOWKEYType; |
| 71 |
struct NSSLOWKEYPrivateKeyStr { |
| 72 |
PLArenaPool *arena; |
| 73 |
NSSLOWKEYType keyType; |
| 74 |
union { |
| 75 |
RSAPrivateKey rsa; |
| 76 |
DSAPrivateKey dsa; |
| 77 |
DHPrivateKey dh; |
| 78 |
ECPrivateKey ec; |
| 79 |
} u; |
| 80 |
}; |
| 81 |
typedef struct NSSLOWKEYPrivateKeyStr NSSLOWKEYPrivateKey; |
| 82 |
#endif |
| 83 |
|
| 84 |
#ifndef HAVE_GETSLOTID_FN |
| 85 |
# define NSSCKFWSession_GetFWSlot nssCKFWSession_GetFWSlot |
| 86 |
# define NSSCKFWSlot_GetSlotID nssCKFWSlot_GetSlotID |
| 87 |
NSSCKFWSlot* nssCKFWSession_GetFWSlot(NSSCKFWSession *fwSession); |
| 88 |
CK_SLOT_ID nssCKFWSlot_GetSlotID(NSSCKFWSlot *fwSlot); |
| 89 |
#endif |
| 90 |
|
| 91 |
/* FIXME don't hard-code the number of slots */ |
| 92 |
#define NUM_SLOTS 8 |
| 93 |
|
| 94 |
/* FIXME pem module slot ID's */ |
| 95 |
#define PEM_MIN_USER_SLOT_ID 0 |
| 96 |
#define PEM_MAX_USER_SLOT_ID 8 |
| 97 |
|
| 98 |
/* |
| 99 |
* statically defined raw objects. Allows us to hold data description objects |
| 100 |
* in this PKCS #11 module. |
| 101 |
*/ |
| 102 |
struct pemRawObjectStr { |
| 103 |
CK_ULONG n; |
| 104 |
const CK_ATTRIBUTE_TYPE *types; |
| 105 |
const NSSItem *items; |
| 106 |
}; |
| 107 |
typedef struct pemRawObjectStr pemRawObject; |
| 108 |
|
| 109 |
/* |
| 110 |
* common values needed for both bare keys and cert referenced keys. |
| 111 |
*/ |
| 112 |
struct pemKeyParamsStr { |
| 113 |
NSSItem modulus; |
| 114 |
NSSItem exponent; |
| 115 |
NSSItem privateExponent; |
| 116 |
NSSItem prime1; |
| 117 |
NSSItem prime2; |
| 118 |
NSSItem exponent1; |
| 119 |
NSSItem exponent2; |
| 120 |
NSSItem coefficient; |
| 121 |
/* TODO: split algoritm-specific data out */ |
| 122 |
SECItem *privateKey; |
| 123 |
SECItem *privateKeyOrig; /* deep copy of privateKey until decrypted */ |
| 124 |
void *pubKey; |
| 125 |
}; |
| 126 |
typedef struct pemKeyParamsStr pemKeyParams; |
| 127 |
/* |
| 128 |
* Key objects. Handles bare keys which do not yet have certs associated |
| 129 |
* with them. These are usually short lived, but may exist for several days |
| 130 |
* while the CA is issuing the certificate. |
| 131 |
*/ |
| 132 |
struct pemKeyObjectStr { |
| 133 |
pemKeyParams key; |
| 134 |
char *ivstring; |
| 135 |
int cipher; |
| 136 |
}; |
| 137 |
typedef struct pemKeyObjectStr pemKeyObject; |
| 138 |
|
| 139 |
/* |
| 140 |
* Certificate and certificate referenced keys. |
| 141 |
*/ |
| 142 |
struct pemCertObjectStr { |
| 143 |
const char *certStore; |
| 144 |
NSSItem label; |
| 145 |
NSSItem subject; |
| 146 |
NSSItem issuer; |
| 147 |
NSSItem serial; |
| 148 |
NSSItem derCert; |
| 149 |
unsigned char sha1_hash[SHA1_LENGTH]; |
| 150 |
unsigned char md5_hash[MD5_LENGTH]; |
| 151 |
pemKeyParams key; |
| 152 |
}; |
| 153 |
typedef struct pemCertObjectStr pemCertObject; |
| 154 |
|
| 155 |
/* |
| 156 |
* Trust |
| 157 |
*/ |
| 158 |
struct pemTrustObjectStr { |
| 159 |
char *nickname; |
| 160 |
}; |
| 161 |
typedef struct pemTrustObjectStr pemTrustObject; |
| 162 |
|
| 163 |
typedef enum { |
| 164 |
pemAll = -1, /* matches all types */ |
| 165 |
pemRaw, |
| 166 |
pemCert, |
| 167 |
pemBareKey, |
| 168 |
pemTrust |
| 169 |
} pemObjectType; |
| 170 |
|
| 171 |
typedef struct pemInternalObjectStr pemInternalObject; |
| 172 |
typedef struct pemObjectListItemStr pemObjectListItem; |
| 173 |
|
| 174 |
/* |
| 175 |
* singly-linked list of internal objects |
| 176 |
*/ |
| 177 |
struct pemObjectListItemStr { |
| 178 |
pemInternalObject *io; |
| 179 |
pemObjectListItem *next; |
| 180 |
}; |
| 181 |
|
| 182 |
/* |
| 183 |
* all the various types of objects are abstracted away in cobject and |
| 184 |
* cfind as pemInternalObjects. |
| 185 |
*/ |
| 186 |
struct pemInternalObjectStr { |
| 187 |
pemObjectType type; |
| 188 |
union { |
| 189 |
pemRawObject raw; |
| 190 |
pemCertObject cert; |
| 191 |
pemKeyObject key; |
| 192 |
pemTrustObject trust; |
| 193 |
} u; |
| 194 |
CK_OBJECT_CLASS objClass; |
| 195 |
NSSItem hashKey; |
| 196 |
NSSItem id; |
| 197 |
long objid; |
| 198 |
unsigned char hashKeyData[128]; |
| 199 |
SECItem *derCert; |
| 200 |
char *nickname; |
| 201 |
NSSCKMDObject mdObject; |
| 202 |
CK_SLOT_ID slotID; |
| 203 |
int refCount; |
| 204 |
|
| 205 |
/* all internal objects are linked in a global list */ |
| 206 |
struct list_head gl_list; |
| 207 |
|
| 208 |
/* we represent sparse array as list but keep its elements indexed */ |
| 209 |
long arrayIdx; |
| 210 |
|
| 211 |
/* used by pem_mdFindObjects_Next */ |
| 212 |
CK_BBOOL extRef; |
| 213 |
|
| 214 |
/* If list != NULL, the object contains no useful data except of the list |
| 215 |
* of slave objects */ |
| 216 |
pemObjectListItem *list; |
| 217 |
}; |
| 218 |
|
| 219 |
NSS_EXTERN_DATA struct list_head pem_objs; |
| 220 |
NSS_EXTERN_DATA long pem_nobjs; |
| 221 |
NSS_EXTERN_DATA int token_needsLogin[]; |
| 222 |
NSS_EXTERN_DATA NSSCKMDSlot *lastEventSlot; |
| 223 |
|
| 224 |
struct pemTokenStr { |
| 225 |
PRBool logged_in; |
| 226 |
}; |
| 227 |
typedef struct pemTokenStr pemToken; |
| 228 |
|
| 229 |
NSS_EXTERN_DATA const CK_VERSION pem_CryptokiVersion; |
| 230 |
NSS_EXTERN_DATA const NSSUTF8 * pem_ManufacturerID; |
| 231 |
NSS_EXTERN_DATA const NSSUTF8 * pem_LibraryDescription; |
| 232 |
NSS_EXTERN_DATA const CK_VERSION pem_LibraryVersion; |
| 233 |
NSS_EXTERN_DATA const CK_VERSION pem_HardwareVersion; |
| 234 |
NSS_EXTERN_DATA const CK_VERSION pem_FirmwareVersion; |
| 235 |
NSS_EXTERN_DATA const NSSUTF8 * pem_TokenModel; |
| 236 |
NSS_EXTERN_DATA const NSSUTF8 * pem_TokenSerialNumber; |
| 237 |
|
| 238 |
NSS_EXTERN_DATA const NSSCKMDInstance pem_mdInstance; |
| 239 |
NSS_EXTERN_DATA const NSSCKMDSlot pem_mdSlot; |
| 240 |
NSS_EXTERN_DATA const NSSCKMDToken pem_mdToken; |
| 241 |
NSS_EXTERN_DATA const NSSCKMDMechanism pem_mdMechanismRSA; |
| 242 |
|
| 243 |
NSS_EXTERN NSSCKMDSession * |
| 244 |
pem_CreateSession |
| 245 |
( |
| 246 |
NSSCKFWSession *fwSession, |
| 247 |
CK_RV *pError |
| 248 |
); |
| 249 |
|
| 250 |
NSS_EXTERN NSSCKMDFindObjects * |
| 251 |
pem_FindObjectsInit |
| 252 |
( |
| 253 |
NSSCKFWSession *fwSession, |
| 254 |
CK_ATTRIBUTE_PTR pTemplate, |
| 255 |
CK_ULONG ulAttributeCount, |
| 256 |
CK_RV *pError |
| 257 |
); |
| 258 |
|
| 259 |
NSS_EXTERN NSSCKMDObject * |
| 260 |
pem_CreateMDObject |
| 261 |
( |
| 262 |
NSSArena *arena, |
| 263 |
pemInternalObject *io, |
| 264 |
CK_RV *pError |
| 265 |
); |
| 266 |
|
| 267 |
#define NSS_PEM_ARRAY_SIZE(x) ((sizeof (x))/(sizeof ((x)[0]))) |
| 268 |
|
| 269 |
/* Read DER encoded data from a PEM file or a binary (der-encoded) file. */ |
| 270 |
int ReadDERFromFile(SECItem ***derlist, char *filename, int *cipher, |
| 271 |
char **ivstring, PRBool certsonly); |
| 272 |
|
| 273 |
/* Fetch an attribute of the specified type. */ |
| 274 |
const NSSItem * pem_FetchAttribute ( pemInternalObject *io, CK_ATTRIBUTE_TYPE type, CK_RV *pError); |
| 275 |
|
| 276 |
/* Populate modulus and public exponent of the given internal object */ |
| 277 |
CK_RV pem_PopulateModulusExponent(pemInternalObject *io); |
| 278 |
|
| 279 |
/* Create a pem module object */ |
| 280 |
NSSCKMDObject * pem_CreateObject(NSSCKFWInstance *fwInstance, NSSCKFWSession *fwSession, NSSCKMDToken *mdToken, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, CK_RV *pError); |
| 281 |
|
| 282 |
/* Create a new pem module slot */ |
| 283 |
NSSCKMDSlot *pem_NewSlot( NSSCKFWInstance *fwInstance, CK_RV *pError); |
| 284 |
|
| 285 |
typedef void* (*DynPtrListAllocFunction) (size_t bytes); |
| 286 |
typedef void* (*DynPtrListReallocFunction) (void *ptr, size_t bytes); |
| 287 |
typedef void (*DynPtrListFreeFunction) (void *ptr); |
| 288 |
|
| 289 |
typedef struct DynPtrListStr { |
| 290 |
size_t entries; |
| 291 |
size_t capacity; |
| 292 |
void **pointers; |
| 293 |
DynPtrListAllocFunction alloc_function; |
| 294 |
DynPtrListReallocFunction realloc_function; |
| 295 |
DynPtrListFreeFunction free_function; |
| 296 |
} DynPtrList; |
| 297 |
|
| 298 |
void* pem_InitDynPtrList(DynPtrList *dpl, DynPtrListAllocFunction a, |
| 299 |
DynPtrListReallocFunction r, DynPtrListFreeFunction f); |
| 300 |
void pem_FreeDynPtrList(DynPtrList *dpl); |
| 301 |
void* pem_AddToDynPtrList(DynPtrList *dpl, char *ptr); |
| 302 |
|
| 303 |
PRBool pem_ParseString(const char *inputstring, const char delimiter, |
| 304 |
DynPtrList *returnedstrings); |
| 305 |
|
| 306 |
pemInternalObject * |
| 307 |
AddObjectIfNeeded(CK_OBJECT_CLASS objClass, pemObjectType type, |
| 308 |
SECItem *certDER, SECItem *keyDER, const char *filename, long objid, |
| 309 |
CK_SLOT_ID slotID, PRBool *pAdded); |
| 310 |
|
| 311 |
void pem_DestroyInternalObject (pemInternalObject *io); |
| 312 |
|
| 313 |
|
| 314 |
/* prsa.c */ |
| 315 |
unsigned int pem_PrivateModulusLen(NSSLOWKEYPrivateKey *privk); |
| 316 |
|
| 317 |
/* ptoken.c */ |
| 318 |
NSSCKMDToken * pem_NewToken(NSSCKFWInstance *fwInstance, CK_RV *pError); |
| 319 |
|
| 320 |
/* util.c */ |
| 321 |
void open_nss_pem_log(); |
| 322 |
/* no close_log */ |
| 323 |
void plog(const char *fmt, ...); |
| 324 |
|
| 325 |
#endif /* CKPEM_H */ |