Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
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 | #include "cert.h" |
michael@0 | 6 | #include "base64.h" |
michael@0 | 7 | #include "secitem.h" |
michael@0 | 8 | #include "secder.h" |
michael@0 | 9 | #include "secasn1.h" |
michael@0 | 10 | #include "secoid.h" |
michael@0 | 11 | #include "secerr.h" |
michael@0 | 12 | |
michael@0 | 13 | SEC_ASN1_MKSUB(SEC_AnyTemplate) |
michael@0 | 14 | SEC_ASN1_MKSUB(SEC_SetOfAnyTemplate) |
michael@0 | 15 | |
michael@0 | 16 | typedef struct ContentInfoStr ContentInfo; |
michael@0 | 17 | typedef struct DegenerateSignedDataStr DegenerateSignedData; |
michael@0 | 18 | |
michael@0 | 19 | struct ContentInfoStr { |
michael@0 | 20 | SECOidTag contentTypeTag; /* local; not part of encoding */ |
michael@0 | 21 | SECItem contentType; |
michael@0 | 22 | union { |
michael@0 | 23 | SECItem *data; |
michael@0 | 24 | DegenerateSignedData *signedData; |
michael@0 | 25 | } content; |
michael@0 | 26 | }; |
michael@0 | 27 | |
michael@0 | 28 | struct DegenerateSignedDataStr { |
michael@0 | 29 | SECItem version; |
michael@0 | 30 | SECItem **digestAlgorithms; |
michael@0 | 31 | ContentInfo contentInfo; |
michael@0 | 32 | SECItem **certificates; |
michael@0 | 33 | SECItem **crls; |
michael@0 | 34 | SECItem **signerInfos; |
michael@0 | 35 | }; |
michael@0 | 36 | |
michael@0 | 37 | static const SEC_ASN1Template * |
michael@0 | 38 | choose_content_template(void *src_or_dest, PRBool encoding); |
michael@0 | 39 | |
michael@0 | 40 | static const SEC_ASN1TemplateChooserPtr template_chooser |
michael@0 | 41 | = choose_content_template; |
michael@0 | 42 | |
michael@0 | 43 | static const SEC_ASN1Template ContentInfoTemplate[] = { |
michael@0 | 44 | { SEC_ASN1_SEQUENCE, |
michael@0 | 45 | 0, NULL, sizeof(ContentInfo) }, |
michael@0 | 46 | { SEC_ASN1_OBJECT_ID, |
michael@0 | 47 | offsetof(ContentInfo,contentType) }, |
michael@0 | 48 | { SEC_ASN1_OPTIONAL | SEC_ASN1_DYNAMIC | |
michael@0 | 49 | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0, |
michael@0 | 50 | offsetof(ContentInfo,content), |
michael@0 | 51 | &template_chooser }, |
michael@0 | 52 | { 0 } |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | static const SEC_ASN1Template DegenerateSignedDataTemplate[] = { |
michael@0 | 56 | { SEC_ASN1_SEQUENCE, |
michael@0 | 57 | 0, NULL, sizeof(DegenerateSignedData) }, |
michael@0 | 58 | { SEC_ASN1_INTEGER, |
michael@0 | 59 | offsetof(DegenerateSignedData,version) }, |
michael@0 | 60 | { SEC_ASN1_SET_OF | SEC_ASN1_XTRN, |
michael@0 | 61 | offsetof(DegenerateSignedData,digestAlgorithms), |
michael@0 | 62 | SEC_ASN1_SUB(SEC_AnyTemplate) }, |
michael@0 | 63 | { SEC_ASN1_INLINE, |
michael@0 | 64 | offsetof(DegenerateSignedData,contentInfo), |
michael@0 | 65 | ContentInfoTemplate }, |
michael@0 | 66 | { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | |
michael@0 | 67 | SEC_ASN1_XTRN | 0, |
michael@0 | 68 | offsetof(DegenerateSignedData,certificates), |
michael@0 | 69 | SEC_ASN1_SUB(SEC_SetOfAnyTemplate) }, |
michael@0 | 70 | { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | |
michael@0 | 71 | SEC_ASN1_XTRN | 1, |
michael@0 | 72 | offsetof(DegenerateSignedData,crls), |
michael@0 | 73 | SEC_ASN1_SUB(SEC_SetOfAnyTemplate) }, |
michael@0 | 74 | { SEC_ASN1_SET_OF | SEC_ASN1_XTRN, |
michael@0 | 75 | offsetof(DegenerateSignedData,signerInfos), |
michael@0 | 76 | SEC_ASN1_SUB(SEC_AnyTemplate) }, |
michael@0 | 77 | { 0 } |
michael@0 | 78 | }; |
michael@0 | 79 | |
michael@0 | 80 | static const SEC_ASN1Template PointerToDegenerateSignedDataTemplate[] = { |
michael@0 | 81 | { SEC_ASN1_POINTER, 0, DegenerateSignedDataTemplate } |
michael@0 | 82 | }; |
michael@0 | 83 | |
michael@0 | 84 | static SECOidTag |
michael@0 | 85 | GetContentTypeTag(ContentInfo *cinfo) |
michael@0 | 86 | { |
michael@0 | 87 | if (cinfo->contentTypeTag == SEC_OID_UNKNOWN) |
michael@0 | 88 | cinfo->contentTypeTag = SECOID_FindOIDTag(&cinfo->contentType); |
michael@0 | 89 | return cinfo->contentTypeTag; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | static const SEC_ASN1Template * |
michael@0 | 93 | choose_content_template(void *src_or_dest, PRBool encoding) |
michael@0 | 94 | { |
michael@0 | 95 | const SEC_ASN1Template *theTemplate; |
michael@0 | 96 | ContentInfo *cinfo; |
michael@0 | 97 | SECOidTag kind; |
michael@0 | 98 | |
michael@0 | 99 | PORT_Assert(src_or_dest != NULL); |
michael@0 | 100 | if (src_or_dest == NULL) |
michael@0 | 101 | return NULL; |
michael@0 | 102 | |
michael@0 | 103 | cinfo = (ContentInfo*)src_or_dest; |
michael@0 | 104 | kind = GetContentTypeTag(cinfo); |
michael@0 | 105 | switch (kind) { |
michael@0 | 106 | default: |
michael@0 | 107 | theTemplate = SEC_ASN1_GET(SEC_PointerToAnyTemplate); |
michael@0 | 108 | break; |
michael@0 | 109 | case SEC_OID_PKCS7_DATA: |
michael@0 | 110 | theTemplate = SEC_ASN1_GET(SEC_PointerToOctetStringTemplate); |
michael@0 | 111 | break; |
michael@0 | 112 | case SEC_OID_PKCS7_SIGNED_DATA: |
michael@0 | 113 | theTemplate = PointerToDegenerateSignedDataTemplate; |
michael@0 | 114 | break; |
michael@0 | 115 | } |
michael@0 | 116 | return theTemplate; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | static SECStatus |
michael@0 | 120 | SEC_ReadPKCS7Certs(SECItem *pkcs7Item, CERTImportCertificateFunc f, void *arg) |
michael@0 | 121 | { |
michael@0 | 122 | ContentInfo contentInfo; |
michael@0 | 123 | SECStatus rv; |
michael@0 | 124 | SECItem **certs; |
michael@0 | 125 | int count; |
michael@0 | 126 | PLArenaPool *arena; |
michael@0 | 127 | |
michael@0 | 128 | arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
michael@0 | 129 | if ( arena == NULL ) { |
michael@0 | 130 | return SECFailure; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | PORT_Memset(&contentInfo, 0, sizeof(contentInfo)); |
michael@0 | 134 | rv = SEC_ASN1DecodeItem(arena, &contentInfo, ContentInfoTemplate, |
michael@0 | 135 | pkcs7Item); |
michael@0 | 136 | if ( rv != SECSuccess ) { |
michael@0 | 137 | goto loser; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | if ( GetContentTypeTag(&contentInfo) != SEC_OID_PKCS7_SIGNED_DATA ) { |
michael@0 | 141 | goto loser; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | certs = contentInfo.content.signedData->certificates; |
michael@0 | 145 | if ( certs ) { |
michael@0 | 146 | count = 0; |
michael@0 | 147 | |
michael@0 | 148 | while ( *certs ) { |
michael@0 | 149 | count++; |
michael@0 | 150 | certs++; |
michael@0 | 151 | } |
michael@0 | 152 | rv = (* f)(arg, contentInfo.content.signedData->certificates, count); |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | rv = SECSuccess; |
michael@0 | 156 | |
michael@0 | 157 | goto done; |
michael@0 | 158 | loser: |
michael@0 | 159 | rv = SECFailure; |
michael@0 | 160 | |
michael@0 | 161 | done: |
michael@0 | 162 | if ( arena ) { |
michael@0 | 163 | PORT_FreeArena(arena, PR_FALSE); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | return(rv); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | const SEC_ASN1Template SEC_CertSequenceTemplate[] = { |
michael@0 | 170 | { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_XTRN, 0, SEC_ASN1_SUB(SEC_AnyTemplate) } |
michael@0 | 171 | }; |
michael@0 | 172 | |
michael@0 | 173 | static SECStatus |
michael@0 | 174 | SEC_ReadCertSequence(SECItem *certsItem, CERTImportCertificateFunc f, void *arg) |
michael@0 | 175 | { |
michael@0 | 176 | SECStatus rv; |
michael@0 | 177 | SECItem **certs; |
michael@0 | 178 | int count; |
michael@0 | 179 | SECItem **rawCerts = NULL; |
michael@0 | 180 | PLArenaPool *arena; |
michael@0 | 181 | ContentInfo contentInfo; |
michael@0 | 182 | |
michael@0 | 183 | arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
michael@0 | 184 | if ( arena == NULL ) { |
michael@0 | 185 | return SECFailure; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | PORT_Memset(&contentInfo, 0, sizeof(contentInfo)); |
michael@0 | 189 | rv = SEC_ASN1DecodeItem(arena, &contentInfo, ContentInfoTemplate, |
michael@0 | 190 | certsItem); |
michael@0 | 191 | if ( rv != SECSuccess ) { |
michael@0 | 192 | goto loser; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | if ( GetContentTypeTag(&contentInfo) != SEC_OID_NS_TYPE_CERT_SEQUENCE ) { |
michael@0 | 196 | goto loser; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | rv = SEC_QuickDERDecodeItem(arena, &rawCerts, SEC_CertSequenceTemplate, |
michael@0 | 200 | contentInfo.content.data); |
michael@0 | 201 | |
michael@0 | 202 | if (rv != SECSuccess) { |
michael@0 | 203 | goto loser; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | certs = rawCerts; |
michael@0 | 207 | if ( certs ) { |
michael@0 | 208 | count = 0; |
michael@0 | 209 | |
michael@0 | 210 | while ( *certs ) { |
michael@0 | 211 | count++; |
michael@0 | 212 | certs++; |
michael@0 | 213 | } |
michael@0 | 214 | rv = (* f)(arg, rawCerts, count); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | rv = SECSuccess; |
michael@0 | 218 | |
michael@0 | 219 | goto done; |
michael@0 | 220 | loser: |
michael@0 | 221 | rv = SECFailure; |
michael@0 | 222 | |
michael@0 | 223 | done: |
michael@0 | 224 | if ( arena ) { |
michael@0 | 225 | PORT_FreeArena(arena, PR_FALSE); |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | return(rv); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | CERTCertificate * |
michael@0 | 232 | CERT_ConvertAndDecodeCertificate(char *certstr) |
michael@0 | 233 | { |
michael@0 | 234 | CERTCertificate *cert; |
michael@0 | 235 | SECStatus rv; |
michael@0 | 236 | SECItem der; |
michael@0 | 237 | |
michael@0 | 238 | rv = ATOB_ConvertAsciiToItem(&der, certstr); |
michael@0 | 239 | if (rv != SECSuccess) |
michael@0 | 240 | return NULL; |
michael@0 | 241 | |
michael@0 | 242 | cert = CERT_NewTempCertificate(CERT_GetDefaultCertDB(), |
michael@0 | 243 | &der, NULL, PR_FALSE, PR_TRUE); |
michael@0 | 244 | |
michael@0 | 245 | PORT_Free(der.data); |
michael@0 | 246 | return cert; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | static const char NS_CERT_HEADER[] = "-----BEGIN CERTIFICATE-----"; |
michael@0 | 250 | static const char NS_CERT_TRAILER[] = "-----END CERTIFICATE-----"; |
michael@0 | 251 | #define NS_CERT_HEADER_LEN ((sizeof NS_CERT_HEADER) - 1) |
michael@0 | 252 | #define NS_CERT_TRAILER_LEN ((sizeof NS_CERT_TRAILER) - 1) |
michael@0 | 253 | |
michael@0 | 254 | /* |
michael@0 | 255 | * read an old style ascii or binary certificate chain |
michael@0 | 256 | */ |
michael@0 | 257 | SECStatus |
michael@0 | 258 | CERT_DecodeCertPackage(char *certbuf, |
michael@0 | 259 | int certlen, |
michael@0 | 260 | CERTImportCertificateFunc f, |
michael@0 | 261 | void *arg) |
michael@0 | 262 | { |
michael@0 | 263 | unsigned char *cp; |
michael@0 | 264 | unsigned char *bincert = NULL; |
michael@0 | 265 | char * ascCert = NULL; |
michael@0 | 266 | SECStatus rv; |
michael@0 | 267 | |
michael@0 | 268 | if ( certbuf == NULL ) { |
michael@0 | 269 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
michael@0 | 270 | return(SECFailure); |
michael@0 | 271 | } |
michael@0 | 272 | /* |
michael@0 | 273 | * Make sure certlen is long enough to handle the longest possible |
michael@0 | 274 | * reference in the code below: |
michael@0 | 275 | * 0x30 0x84 l1 l2 l3 l4 + |
michael@0 | 276 | * tag 9 o1 o2 o3 o4 o5 o6 o7 o8 o9 |
michael@0 | 277 | * where 9 is the longest length of the expected oids we are testing. |
michael@0 | 278 | * 6 + 11 = 17. 17 bytes is clearly too small to code any kind of |
michael@0 | 279 | * certificate (a 128 bit ECC certificate contains at least an 8 byte |
michael@0 | 280 | * key and a 16 byte signature, plus coding overhead). Typically a cert |
michael@0 | 281 | * is much larger. So it's safe to require certlen to be at least 17 |
michael@0 | 282 | * bytes. |
michael@0 | 283 | */ |
michael@0 | 284 | if (certlen < 17) { |
michael@0 | 285 | PORT_SetError(SEC_ERROR_INPUT_LEN); |
michael@0 | 286 | return(SECFailure); |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | cp = (unsigned char *)certbuf; |
michael@0 | 290 | |
michael@0 | 291 | /* is a DER encoded certificate of some type? */ |
michael@0 | 292 | if ( ( *cp & 0x1f ) == SEC_ASN1_SEQUENCE ) { |
michael@0 | 293 | SECItem certitem; |
michael@0 | 294 | SECItem *pcertitem = &certitem; |
michael@0 | 295 | int seqLen, seqLenLen; |
michael@0 | 296 | |
michael@0 | 297 | cp++; |
michael@0 | 298 | |
michael@0 | 299 | if ( *cp & 0x80) { |
michael@0 | 300 | /* Multibyte length */ |
michael@0 | 301 | seqLenLen = cp[0] & 0x7f; |
michael@0 | 302 | |
michael@0 | 303 | switch (seqLenLen) { |
michael@0 | 304 | case 4: |
michael@0 | 305 | seqLen = ((unsigned long)cp[1]<<24) | |
michael@0 | 306 | ((unsigned long)cp[2]<<16) | (cp[3]<<8) | cp[4]; |
michael@0 | 307 | break; |
michael@0 | 308 | case 3: |
michael@0 | 309 | seqLen = ((unsigned long)cp[1]<<16) | (cp[2]<<8) | cp[3]; |
michael@0 | 310 | break; |
michael@0 | 311 | case 2: |
michael@0 | 312 | seqLen = (cp[1]<<8) | cp[2]; |
michael@0 | 313 | break; |
michael@0 | 314 | case 1: |
michael@0 | 315 | seqLen = cp[1]; |
michael@0 | 316 | break; |
michael@0 | 317 | case 0: |
michael@0 | 318 | /* indefinite length */ |
michael@0 | 319 | seqLen = 0; |
michael@0 | 320 | break; |
michael@0 | 321 | default: |
michael@0 | 322 | goto notder; |
michael@0 | 323 | } |
michael@0 | 324 | cp += ( seqLenLen + 1 ); |
michael@0 | 325 | |
michael@0 | 326 | } else { |
michael@0 | 327 | seqLenLen = 0; |
michael@0 | 328 | seqLen = *cp; |
michael@0 | 329 | cp++; |
michael@0 | 330 | } |
michael@0 | 331 | |
michael@0 | 332 | /* check entire length if definite length */ |
michael@0 | 333 | if ( seqLen || seqLenLen ) { |
michael@0 | 334 | if ( certlen != ( seqLen + seqLenLen + 2 ) ) { |
michael@0 | 335 | if (certlen > ( seqLen + seqLenLen + 2 )) |
michael@0 | 336 | PORT_SetError(SEC_ERROR_EXTRA_INPUT); |
michael@0 | 337 | else |
michael@0 | 338 | PORT_SetError(SEC_ERROR_INPUT_LEN); |
michael@0 | 339 | goto notder; |
michael@0 | 340 | } |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | /* check the type oid */ |
michael@0 | 344 | if ( cp[0] == SEC_ASN1_OBJECT_ID ) { |
michael@0 | 345 | SECOidData *oiddata; |
michael@0 | 346 | SECItem oiditem; |
michael@0 | 347 | /* XXX - assume DER encoding of OID len!! */ |
michael@0 | 348 | oiditem.len = cp[1]; |
michael@0 | 349 | /* if we add an oid below that is longer than 9 bytes, then we |
michael@0 | 350 | * need to change the certlen check at the top of the function |
michael@0 | 351 | * to prevent a buffer overflow |
michael@0 | 352 | */ |
michael@0 | 353 | if ( oiditem.len > 9 ) { |
michael@0 | 354 | PORT_SetError(SEC_ERROR_UNRECOGNIZED_OID); |
michael@0 | 355 | return(SECFailure); |
michael@0 | 356 | } |
michael@0 | 357 | oiditem.data = (unsigned char *)&cp[2]; |
michael@0 | 358 | oiddata = SECOID_FindOID(&oiditem); |
michael@0 | 359 | if ( oiddata == NULL ) { |
michael@0 | 360 | return(SECFailure); |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | certitem.data = (unsigned char*)certbuf; |
michael@0 | 364 | certitem.len = certlen; |
michael@0 | 365 | |
michael@0 | 366 | switch ( oiddata->offset ) { |
michael@0 | 367 | case SEC_OID_PKCS7_SIGNED_DATA: |
michael@0 | 368 | /* oid: 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02 */ |
michael@0 | 369 | return(SEC_ReadPKCS7Certs(&certitem, f, arg)); |
michael@0 | 370 | break; |
michael@0 | 371 | case SEC_OID_NS_TYPE_CERT_SEQUENCE: |
michael@0 | 372 | /* oid: 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x02, 0x05 */ |
michael@0 | 373 | return(SEC_ReadCertSequence(&certitem, f, arg)); |
michael@0 | 374 | break; |
michael@0 | 375 | default: |
michael@0 | 376 | break; |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | } else { |
michael@0 | 380 | /* it had better be a certificate by now!! */ |
michael@0 | 381 | certitem.data = (unsigned char*)certbuf; |
michael@0 | 382 | certitem.len = certlen; |
michael@0 | 383 | |
michael@0 | 384 | rv = (* f)(arg, &pcertitem, 1); |
michael@0 | 385 | return(rv); |
michael@0 | 386 | } |
michael@0 | 387 | } |
michael@0 | 388 | |
michael@0 | 389 | /* now look for a netscape base64 ascii encoded cert */ |
michael@0 | 390 | notder: |
michael@0 | 391 | { |
michael@0 | 392 | unsigned char *certbegin = NULL; |
michael@0 | 393 | unsigned char *certend = NULL; |
michael@0 | 394 | char *pc; |
michael@0 | 395 | int cl; |
michael@0 | 396 | |
michael@0 | 397 | /* Convert the ASCII data into a nul-terminated string */ |
michael@0 | 398 | ascCert = (char *)PORT_Alloc(certlen + 1); |
michael@0 | 399 | if (!ascCert) { |
michael@0 | 400 | rv = SECFailure; |
michael@0 | 401 | goto loser; |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | PORT_Memcpy(ascCert, certbuf, certlen); |
michael@0 | 405 | ascCert[certlen] = '\0'; |
michael@0 | 406 | |
michael@0 | 407 | pc = PORT_Strchr(ascCert, '\n'); /* find an EOL */ |
michael@0 | 408 | if (!pc) { /* maybe this is a MAC file */ |
michael@0 | 409 | pc = ascCert; |
michael@0 | 410 | while (*pc && NULL != (pc = PORT_Strchr(pc, '\r'))) { |
michael@0 | 411 | *pc++ = '\n'; |
michael@0 | 412 | } |
michael@0 | 413 | } |
michael@0 | 414 | |
michael@0 | 415 | cp = (unsigned char *)ascCert; |
michael@0 | 416 | cl = certlen; |
michael@0 | 417 | |
michael@0 | 418 | /* find the beginning marker */ |
michael@0 | 419 | while ( cl > NS_CERT_HEADER_LEN ) { |
michael@0 | 420 | int found = 0; |
michael@0 | 421 | if ( !PORT_Strncasecmp((char *)cp, NS_CERT_HEADER, |
michael@0 | 422 | NS_CERT_HEADER_LEN) ) { |
michael@0 | 423 | cl -= NS_CERT_HEADER_LEN; |
michael@0 | 424 | cp += NS_CERT_HEADER_LEN; |
michael@0 | 425 | found = 1; |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | /* skip to next eol */ |
michael@0 | 429 | while ( cl && ( *cp != '\n' )) { |
michael@0 | 430 | cp++; |
michael@0 | 431 | cl--; |
michael@0 | 432 | } |
michael@0 | 433 | |
michael@0 | 434 | /* skip all blank lines */ |
michael@0 | 435 | while ( cl && ( *cp == '\n' || *cp == '\r' )) { |
michael@0 | 436 | cp++; |
michael@0 | 437 | cl--; |
michael@0 | 438 | } |
michael@0 | 439 | if (cl && found) { |
michael@0 | 440 | certbegin = cp; |
michael@0 | 441 | break; |
michael@0 | 442 | } |
michael@0 | 443 | } |
michael@0 | 444 | |
michael@0 | 445 | if ( certbegin ) { |
michael@0 | 446 | /* find the ending marker */ |
michael@0 | 447 | while ( cl >= NS_CERT_TRAILER_LEN ) { |
michael@0 | 448 | if ( !PORT_Strncasecmp((char *)cp, NS_CERT_TRAILER, |
michael@0 | 449 | NS_CERT_TRAILER_LEN) ) { |
michael@0 | 450 | certend = cp; |
michael@0 | 451 | break; |
michael@0 | 452 | } |
michael@0 | 453 | |
michael@0 | 454 | /* skip to next eol */ |
michael@0 | 455 | while ( cl && ( *cp != '\n' )) { |
michael@0 | 456 | cp++; |
michael@0 | 457 | cl--; |
michael@0 | 458 | } |
michael@0 | 459 | |
michael@0 | 460 | /* skip all blank lines */ |
michael@0 | 461 | while ( cl && ( *cp == '\n' || *cp == '\r' )) { |
michael@0 | 462 | cp++; |
michael@0 | 463 | cl--; |
michael@0 | 464 | } |
michael@0 | 465 | } |
michael@0 | 466 | } |
michael@0 | 467 | |
michael@0 | 468 | if ( certbegin && certend ) { |
michael@0 | 469 | unsigned int binLen; |
michael@0 | 470 | |
michael@0 | 471 | *certend = 0; |
michael@0 | 472 | /* convert to binary */ |
michael@0 | 473 | bincert = ATOB_AsciiToData((char *)certbegin, &binLen); |
michael@0 | 474 | if (!bincert) { |
michael@0 | 475 | rv = SECFailure; |
michael@0 | 476 | goto loser; |
michael@0 | 477 | } |
michael@0 | 478 | |
michael@0 | 479 | /* now recurse to decode the binary */ |
michael@0 | 480 | rv = CERT_DecodeCertPackage((char *)bincert, binLen, f, arg); |
michael@0 | 481 | |
michael@0 | 482 | } else { |
michael@0 | 483 | PORT_SetError(SEC_ERROR_BAD_DER); |
michael@0 | 484 | rv = SECFailure; |
michael@0 | 485 | } |
michael@0 | 486 | } |
michael@0 | 487 | |
michael@0 | 488 | loser: |
michael@0 | 489 | |
michael@0 | 490 | if ( bincert ) { |
michael@0 | 491 | PORT_Free(bincert); |
michael@0 | 492 | } |
michael@0 | 493 | |
michael@0 | 494 | if ( ascCert ) { |
michael@0 | 495 | PORT_Free(ascCert); |
michael@0 | 496 | } |
michael@0 | 497 | |
michael@0 | 498 | return(rv); |
michael@0 | 499 | } |
michael@0 | 500 | |
michael@0 | 501 | typedef struct { |
michael@0 | 502 | PLArenaPool *arena; |
michael@0 | 503 | SECItem cert; |
michael@0 | 504 | } collect_args; |
michael@0 | 505 | |
michael@0 | 506 | static SECStatus |
michael@0 | 507 | collect_certs(void *arg, SECItem **certs, int numcerts) |
michael@0 | 508 | { |
michael@0 | 509 | SECStatus rv; |
michael@0 | 510 | collect_args *collectArgs; |
michael@0 | 511 | |
michael@0 | 512 | collectArgs = (collect_args *)arg; |
michael@0 | 513 | |
michael@0 | 514 | rv = SECITEM_CopyItem(collectArgs->arena, &collectArgs->cert, *certs); |
michael@0 | 515 | |
michael@0 | 516 | return(rv); |
michael@0 | 517 | } |
michael@0 | 518 | |
michael@0 | 519 | |
michael@0 | 520 | /* |
michael@0 | 521 | * read an old style ascii or binary certificate |
michael@0 | 522 | */ |
michael@0 | 523 | CERTCertificate * |
michael@0 | 524 | CERT_DecodeCertFromPackage(char *certbuf, int certlen) |
michael@0 | 525 | { |
michael@0 | 526 | collect_args collectArgs; |
michael@0 | 527 | SECStatus rv; |
michael@0 | 528 | CERTCertificate *cert = NULL; |
michael@0 | 529 | |
michael@0 | 530 | collectArgs.arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
michael@0 | 531 | |
michael@0 | 532 | rv = CERT_DecodeCertPackage(certbuf, certlen, collect_certs, |
michael@0 | 533 | (void *)&collectArgs); |
michael@0 | 534 | if ( rv == SECSuccess ) { |
michael@0 | 535 | cert = CERT_NewTempCertificate(CERT_GetDefaultCertDB(), |
michael@0 | 536 | &collectArgs.cert, NULL, |
michael@0 | 537 | PR_FALSE, PR_TRUE); |
michael@0 | 538 | } |
michael@0 | 539 | |
michael@0 | 540 | PORT_FreeArena(collectArgs.arena, PR_FALSE); |
michael@0 | 541 | |
michael@0 | 542 | return(cert); |
michael@0 | 543 | } |