security/nss/lib/certdb/xauthkid.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 /*
michael@0 6 * X.509 v3 Subject Key Usage Extension
michael@0 7 *
michael@0 8 */
michael@0 9
michael@0 10 #include "prtypes.h"
michael@0 11 #include "seccomon.h"
michael@0 12 #include "secdert.h"
michael@0 13 #include "secoidt.h"
michael@0 14 #include "secasn1t.h"
michael@0 15 #include "secasn1.h"
michael@0 16 #include "secport.h"
michael@0 17 #include "certt.h"
michael@0 18 #include "genname.h"
michael@0 19 #include "secerr.h"
michael@0 20
michael@0 21 SEC_ASN1_MKSUB(SEC_IntegerTemplate)
michael@0 22 SEC_ASN1_MKSUB(SEC_OctetStringTemplate)
michael@0 23
michael@0 24 const SEC_ASN1Template CERTAuthKeyIDTemplate[] = {
michael@0 25 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CERTAuthKeyID) },
michael@0 26 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
michael@0 27 offsetof(CERTAuthKeyID,keyID), SEC_ASN1_SUB(SEC_OctetStringTemplate)},
michael@0 28 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
michael@0 29 offsetof(CERTAuthKeyID, DERAuthCertIssuer), CERT_GeneralNamesTemplate},
michael@0 30 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 2,
michael@0 31 offsetof(CERTAuthKeyID,authCertSerialNumber),
michael@0 32 SEC_ASN1_SUB(SEC_IntegerTemplate) },
michael@0 33 { 0 }
michael@0 34 };
michael@0 35
michael@0 36
michael@0 37
michael@0 38 SECStatus CERT_EncodeAuthKeyID (PLArenaPool *arena, CERTAuthKeyID *value, SECItem *encodedValue)
michael@0 39 {
michael@0 40 SECStatus rv = SECFailure;
michael@0 41
michael@0 42 PORT_Assert (value);
michael@0 43 PORT_Assert (arena);
michael@0 44 PORT_Assert (value->DERAuthCertIssuer == NULL);
michael@0 45 PORT_Assert (encodedValue);
michael@0 46
michael@0 47 do {
michael@0 48
michael@0 49 /* If both of the authCertIssuer and the serial number exist, encode
michael@0 50 the name first. Otherwise, it is an error if one exist and the other
michael@0 51 is not.
michael@0 52 */
michael@0 53 if (value->authCertIssuer) {
michael@0 54 if (!value->authCertSerialNumber.data) {
michael@0 55 PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
michael@0 56 break;
michael@0 57 }
michael@0 58
michael@0 59 value->DERAuthCertIssuer = cert_EncodeGeneralNames
michael@0 60 (arena, value->authCertIssuer);
michael@0 61 if (!value->DERAuthCertIssuer) {
michael@0 62 PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
michael@0 63 break;
michael@0 64 }
michael@0 65 }
michael@0 66 else if (value->authCertSerialNumber.data) {
michael@0 67 PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
michael@0 68 break;
michael@0 69 }
michael@0 70
michael@0 71 if (SEC_ASN1EncodeItem (arena, encodedValue, value,
michael@0 72 CERTAuthKeyIDTemplate) == NULL)
michael@0 73 break;
michael@0 74 rv = SECSuccess;
michael@0 75
michael@0 76 } while (0);
michael@0 77 return(rv);
michael@0 78 }
michael@0 79
michael@0 80 CERTAuthKeyID *
michael@0 81 CERT_DecodeAuthKeyID (PLArenaPool *arena, const SECItem *encodedValue)
michael@0 82 {
michael@0 83 CERTAuthKeyID * value = NULL;
michael@0 84 SECStatus rv = SECFailure;
michael@0 85 void * mark;
michael@0 86 SECItem newEncodedValue;
michael@0 87
michael@0 88 PORT_Assert (arena);
michael@0 89
michael@0 90 do {
michael@0 91 mark = PORT_ArenaMark (arena);
michael@0 92 value = (CERTAuthKeyID*)PORT_ArenaZAlloc (arena, sizeof (*value));
michael@0 93 if (value == NULL)
michael@0 94 break;
michael@0 95 value->DERAuthCertIssuer = NULL;
michael@0 96 /* copy the DER into the arena, since Quick DER returns data that points
michael@0 97 into the DER input, which may get freed by the caller */
michael@0 98 rv = SECITEM_CopyItem(arena, &newEncodedValue, encodedValue);
michael@0 99 if ( rv != SECSuccess ) {
michael@0 100 break;
michael@0 101 }
michael@0 102
michael@0 103 rv = SEC_QuickDERDecodeItem
michael@0 104 (arena, value, CERTAuthKeyIDTemplate, &newEncodedValue);
michael@0 105 if (rv != SECSuccess)
michael@0 106 break;
michael@0 107
michael@0 108 value->authCertIssuer = cert_DecodeGeneralNames (arena, value->DERAuthCertIssuer);
michael@0 109 if (value->authCertIssuer == NULL)
michael@0 110 break;
michael@0 111
michael@0 112 /* what if the general name contains other format but not URI ?
michael@0 113 hl
michael@0 114 */
michael@0 115 if ((value->authCertSerialNumber.data && !value->authCertIssuer) ||
michael@0 116 (!value->authCertSerialNumber.data && value->authCertIssuer)){
michael@0 117 PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
michael@0 118 break;
michael@0 119 }
michael@0 120 } while (0);
michael@0 121
michael@0 122 if (rv != SECSuccess) {
michael@0 123 PORT_ArenaRelease (arena, mark);
michael@0 124 return ((CERTAuthKeyID *)NULL);
michael@0 125 }
michael@0 126 PORT_ArenaUnmark(arena, mark);
michael@0 127 return (value);
michael@0 128 }

mercurial