Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BUILTINS_H |
michael@0 | 6 | #include "builtins.h" |
michael@0 | 7 | #endif /* BUILTINS_H */ |
michael@0 | 8 | |
michael@0 | 9 | /* |
michael@0 | 10 | * builtins/find.c |
michael@0 | 11 | * |
michael@0 | 12 | * This file implements the NSSCKMDFindObjects object for the |
michael@0 | 13 | * "builtin objects" cryptoki module. |
michael@0 | 14 | */ |
michael@0 | 15 | |
michael@0 | 16 | struct builtinsFOStr { |
michael@0 | 17 | NSSArena *arena; |
michael@0 | 18 | CK_ULONG n; |
michael@0 | 19 | CK_ULONG i; |
michael@0 | 20 | builtinsInternalObject **objs; |
michael@0 | 21 | }; |
michael@0 | 22 | |
michael@0 | 23 | static void |
michael@0 | 24 | builtins_mdFindObjects_Final |
michael@0 | 25 | ( |
michael@0 | 26 | NSSCKMDFindObjects *mdFindObjects, |
michael@0 | 27 | NSSCKFWFindObjects *fwFindObjects, |
michael@0 | 28 | NSSCKMDSession *mdSession, |
michael@0 | 29 | NSSCKFWSession *fwSession, |
michael@0 | 30 | NSSCKMDToken *mdToken, |
michael@0 | 31 | NSSCKFWToken *fwToken, |
michael@0 | 32 | NSSCKMDInstance *mdInstance, |
michael@0 | 33 | NSSCKFWInstance *fwInstance |
michael@0 | 34 | ) |
michael@0 | 35 | { |
michael@0 | 36 | struct builtinsFOStr *fo = (struct builtinsFOStr *)mdFindObjects->etc; |
michael@0 | 37 | NSSArena *arena = fo->arena; |
michael@0 | 38 | |
michael@0 | 39 | nss_ZFreeIf(fo->objs); |
michael@0 | 40 | nss_ZFreeIf(fo); |
michael@0 | 41 | nss_ZFreeIf(mdFindObjects); |
michael@0 | 42 | if ((NSSArena *)NULL != arena) { |
michael@0 | 43 | NSSArena_Destroy(arena); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | return; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | static NSSCKMDObject * |
michael@0 | 50 | builtins_mdFindObjects_Next |
michael@0 | 51 | ( |
michael@0 | 52 | NSSCKMDFindObjects *mdFindObjects, |
michael@0 | 53 | NSSCKFWFindObjects *fwFindObjects, |
michael@0 | 54 | NSSCKMDSession *mdSession, |
michael@0 | 55 | NSSCKFWSession *fwSession, |
michael@0 | 56 | NSSCKMDToken *mdToken, |
michael@0 | 57 | NSSCKFWToken *fwToken, |
michael@0 | 58 | NSSCKMDInstance *mdInstance, |
michael@0 | 59 | NSSCKFWInstance *fwInstance, |
michael@0 | 60 | NSSArena *arena, |
michael@0 | 61 | CK_RV *pError |
michael@0 | 62 | ) |
michael@0 | 63 | { |
michael@0 | 64 | struct builtinsFOStr *fo = (struct builtinsFOStr *)mdFindObjects->etc; |
michael@0 | 65 | builtinsInternalObject *io; |
michael@0 | 66 | |
michael@0 | 67 | if( fo->i == fo->n ) { |
michael@0 | 68 | *pError = CKR_OK; |
michael@0 | 69 | return (NSSCKMDObject *)NULL; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | io = fo->objs[ fo->i ]; |
michael@0 | 73 | fo->i++; |
michael@0 | 74 | |
michael@0 | 75 | return nss_builtins_CreateMDObject(arena, io, pError); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | static int |
michael@0 | 79 | builtins_derUnwrapInt(unsigned char *src, int size, unsigned char **dest) { |
michael@0 | 80 | unsigned char *start = src; |
michael@0 | 81 | int len = 0; |
michael@0 | 82 | |
michael@0 | 83 | if (*src ++ != 2) { |
michael@0 | 84 | return 0; |
michael@0 | 85 | } |
michael@0 | 86 | len = *src++; |
michael@0 | 87 | if (len & 0x80) { |
michael@0 | 88 | int count = len & 0x7f; |
michael@0 | 89 | len =0; |
michael@0 | 90 | |
michael@0 | 91 | if (count+2 > size) { |
michael@0 | 92 | return 0; |
michael@0 | 93 | } |
michael@0 | 94 | while (count-- > 0) { |
michael@0 | 95 | len = (len << 8) | *src++; |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | if (len + (src-start) != size) { |
michael@0 | 99 | return 0; |
michael@0 | 100 | } |
michael@0 | 101 | *dest = src; |
michael@0 | 102 | return len; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | static CK_BBOOL |
michael@0 | 106 | builtins_attrmatch |
michael@0 | 107 | ( |
michael@0 | 108 | CK_ATTRIBUTE_PTR a, |
michael@0 | 109 | const NSSItem *b |
michael@0 | 110 | ) |
michael@0 | 111 | { |
michael@0 | 112 | PRBool prb; |
michael@0 | 113 | |
michael@0 | 114 | if( a->ulValueLen != b->size ) { |
michael@0 | 115 | /* match a decoded serial number */ |
michael@0 | 116 | if ((a->type == CKA_SERIAL_NUMBER) && (a->ulValueLen < b->size)) { |
michael@0 | 117 | int len; |
michael@0 | 118 | unsigned char *data = NULL; |
michael@0 | 119 | |
michael@0 | 120 | len = builtins_derUnwrapInt(b->data,b->size,&data); |
michael@0 | 121 | if (data && |
michael@0 | 122 | (len == a->ulValueLen) && |
michael@0 | 123 | nsslibc_memequal(a->pValue, data, len, (PRStatus *)NULL)) { |
michael@0 | 124 | return CK_TRUE; |
michael@0 | 125 | } |
michael@0 | 126 | } |
michael@0 | 127 | return CK_FALSE; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | prb = nsslibc_memequal(a->pValue, b->data, b->size, (PRStatus *)NULL); |
michael@0 | 131 | |
michael@0 | 132 | if( PR_TRUE == prb ) { |
michael@0 | 133 | return CK_TRUE; |
michael@0 | 134 | } else { |
michael@0 | 135 | return CK_FALSE; |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | |
michael@0 | 140 | static CK_BBOOL |
michael@0 | 141 | builtins_match |
michael@0 | 142 | ( |
michael@0 | 143 | CK_ATTRIBUTE_PTR pTemplate, |
michael@0 | 144 | CK_ULONG ulAttributeCount, |
michael@0 | 145 | builtinsInternalObject *o |
michael@0 | 146 | ) |
michael@0 | 147 | { |
michael@0 | 148 | CK_ULONG i; |
michael@0 | 149 | |
michael@0 | 150 | for( i = 0; i < ulAttributeCount; i++ ) { |
michael@0 | 151 | CK_ULONG j; |
michael@0 | 152 | |
michael@0 | 153 | for( j = 0; j < o->n; j++ ) { |
michael@0 | 154 | if( o->types[j] == pTemplate[i].type ) { |
michael@0 | 155 | if( CK_FALSE == builtins_attrmatch(&pTemplate[i], &o->items[j]) ) { |
michael@0 | 156 | return CK_FALSE; |
michael@0 | 157 | } else { |
michael@0 | 158 | break; |
michael@0 | 159 | } |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | if( j == o->n ) { |
michael@0 | 164 | /* Loop ran to the end: no matching attribute */ |
michael@0 | 165 | return CK_FALSE; |
michael@0 | 166 | } |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | /* Every attribute passed */ |
michael@0 | 170 | return CK_TRUE; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | NSS_IMPLEMENT NSSCKMDFindObjects * |
michael@0 | 174 | nss_builtins_FindObjectsInit |
michael@0 | 175 | ( |
michael@0 | 176 | NSSCKFWSession *fwSession, |
michael@0 | 177 | CK_ATTRIBUTE_PTR pTemplate, |
michael@0 | 178 | CK_ULONG ulAttributeCount, |
michael@0 | 179 | CK_RV *pError |
michael@0 | 180 | ) |
michael@0 | 181 | { |
michael@0 | 182 | /* This could be made more efficient. I'm rather rushed. */ |
michael@0 | 183 | NSSArena *arena; |
michael@0 | 184 | NSSCKMDFindObjects *rv = (NSSCKMDFindObjects *)NULL; |
michael@0 | 185 | struct builtinsFOStr *fo = (struct builtinsFOStr *)NULL; |
michael@0 | 186 | builtinsInternalObject **temp = (builtinsInternalObject **)NULL; |
michael@0 | 187 | PRUint32 i; |
michael@0 | 188 | |
michael@0 | 189 | arena = NSSArena_Create(); |
michael@0 | 190 | if( (NSSArena *)NULL == arena ) { |
michael@0 | 191 | goto loser; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | rv = nss_ZNEW(arena, NSSCKMDFindObjects); |
michael@0 | 195 | if( (NSSCKMDFindObjects *)NULL == rv ) { |
michael@0 | 196 | *pError = CKR_HOST_MEMORY; |
michael@0 | 197 | goto loser; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | fo = nss_ZNEW(arena, struct builtinsFOStr); |
michael@0 | 201 | if( (struct builtinsFOStr *)NULL == fo ) { |
michael@0 | 202 | *pError = CKR_HOST_MEMORY; |
michael@0 | 203 | goto loser; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | fo->arena = arena; |
michael@0 | 207 | /* fo->n and fo->i are already zero */ |
michael@0 | 208 | |
michael@0 | 209 | rv->etc = (void *)fo; |
michael@0 | 210 | rv->Final = builtins_mdFindObjects_Final; |
michael@0 | 211 | rv->Next = builtins_mdFindObjects_Next; |
michael@0 | 212 | rv->null = (void *)NULL; |
michael@0 | 213 | |
michael@0 | 214 | temp = nss_ZNEWARRAY((NSSArena *)NULL, builtinsInternalObject *, |
michael@0 | 215 | nss_builtins_nObjects); |
michael@0 | 216 | if( (builtinsInternalObject **)NULL == temp ) { |
michael@0 | 217 | *pError = CKR_HOST_MEMORY; |
michael@0 | 218 | goto loser; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | for( i = 0; i < nss_builtins_nObjects; i++ ) { |
michael@0 | 222 | builtinsInternalObject *o = (builtinsInternalObject *)&nss_builtins_data[i]; |
michael@0 | 223 | |
michael@0 | 224 | if( CK_TRUE == builtins_match(pTemplate, ulAttributeCount, o) ) { |
michael@0 | 225 | temp[ fo->n ] = o; |
michael@0 | 226 | fo->n++; |
michael@0 | 227 | } |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | fo->objs = nss_ZNEWARRAY(arena, builtinsInternalObject *, fo->n); |
michael@0 | 231 | if( (builtinsInternalObject **)NULL == fo->objs ) { |
michael@0 | 232 | *pError = CKR_HOST_MEMORY; |
michael@0 | 233 | goto loser; |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | (void)nsslibc_memcpy(fo->objs, temp, sizeof(builtinsInternalObject *) * fo->n); |
michael@0 | 237 | nss_ZFreeIf(temp); |
michael@0 | 238 | temp = (builtinsInternalObject **)NULL; |
michael@0 | 239 | |
michael@0 | 240 | return rv; |
michael@0 | 241 | |
michael@0 | 242 | loser: |
michael@0 | 243 | nss_ZFreeIf(temp); |
michael@0 | 244 | nss_ZFreeIf(fo); |
michael@0 | 245 | nss_ZFreeIf(rv); |
michael@0 | 246 | if ((NSSArena *)NULL != arena) { |
michael@0 | 247 | NSSArena_Destroy(arena); |
michael@0 | 248 | } |
michael@0 | 249 | return (NSSCKMDFindObjects *)NULL; |
michael@0 | 250 | } |
michael@0 | 251 |