DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (31ec81b5d7bb)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
/* -*- Mode: C; tab-width: 8 -*-*/
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


#include "crmf.h"
#include "crmfi.h"
#include "secitem.h"

static CRMFPOPChoice
crmf_get_popchoice_from_der(SECItem *derPOP)
{
    CRMFPOPChoice retChoice;

    switch (derPOP->data[0] & 0x0f) {
    case 0:
        retChoice = crmfRAVerified;
	break;
    case 1:
        retChoice = crmfSignature;
	break;
    case 2:
        retChoice = crmfKeyEncipherment;
	break;
    case 3:
        retChoice = crmfKeyAgreement;
	break;
    default:
        retChoice = crmfNoPOPChoice;
	break;
    }
    return retChoice;
}

static SECStatus
crmf_decode_process_raverified(CRMFCertReqMsg *inCertReqMsg)
{   
    CRMFProofOfPossession *pop;
    /* Just set up the structure so that the message structure
     * looks like one that was created using the API
     */
    pop = inCertReqMsg->pop;
    pop->popChoice.raVerified.data = NULL;
    pop->popChoice.raVerified.len  = 0;
    return SECSuccess;
}

static SECStatus
crmf_decode_process_signature(CRMFCertReqMsg *inCertReqMsg)
{
    PORT_Assert(inCertReqMsg->poolp);
    if (!inCertReqMsg->poolp) {
    	PORT_SetError(SEC_ERROR_INVALID_ARGS);
	return SECFailure;
    }
    return SEC_ASN1Decode(inCertReqMsg->poolp,
			  &inCertReqMsg->pop->popChoice.signature,
			  CRMFPOPOSigningKeyTemplate, 
			  (const char*)inCertReqMsg->derPOP.data,
			  inCertReqMsg->derPOP.len);
}

static CRMFPOPOPrivKeyChoice
crmf_get_messagechoice_from_der(SECItem *derPOP)
{
    CRMFPOPOPrivKeyChoice retChoice;

    switch (derPOP->data[2] & 0x0f) {
    case 0:
        retChoice = crmfThisMessage;
	break;
    case 1:
        retChoice = crmfSubsequentMessage;
	break;
    case 2:
        retChoice = crmfDHMAC;
	break;
    default:
        retChoice = crmfNoMessage;
    }
    return retChoice;
}

static SECStatus
crmf_decode_process_popoprivkey(CRMFCertReqMsg *inCertReqMsg)
{
    /* We've got a union, so a pointer to one POPOPrivKey
     * struct is the same as having a pointer to the other 
     * one.
     */
    CRMFPOPOPrivKey *popoPrivKey = 
                    &inCertReqMsg->pop->popChoice.keyEncipherment;
    SECItem         *derPOP, privKeyDer;
    SECStatus        rv;

    derPOP = &inCertReqMsg->derPOP;
    popoPrivKey->messageChoice = crmf_get_messagechoice_from_der(derPOP);
    if (popoPrivKey->messageChoice == crmfNoMessage) {
        return SECFailure;
    }
    /* If we ever encounter BER encodings of this, we'll get in trouble*/
    switch (popoPrivKey->messageChoice) {
    case crmfThisMessage:
    case crmfDHMAC:
        privKeyDer.type = derPOP->type;
        privKeyDer.data = &derPOP->data[5];
	privKeyDer.len  = derPOP->len - 5;
	break;
    case crmfSubsequentMessage:
        privKeyDer.type = derPOP->type;
        privKeyDer.data = &derPOP->data[4];
	privKeyDer.len  = derPOP->len - 4;
	break;
    default:
        return SECFailure;
    }

    rv = SECITEM_CopyItem(inCertReqMsg->poolp, 
			  &popoPrivKey->message.subsequentMessage,
			  &privKeyDer);

    if (rv != SECSuccess) {
        return rv;
    }

    if (popoPrivKey->messageChoice == crmfThisMessage ||
	popoPrivKey->messageChoice == crmfDHMAC) {

        popoPrivKey->message.thisMessage.len = 
	    CRMF_BYTES_TO_BITS(privKeyDer.len) - (int)derPOP->data[4];
        
    }
    return SECSuccess;    
}

static SECStatus
crmf_decode_process_keyagreement(CRMFCertReqMsg *inCertReqMsg)
{
    return crmf_decode_process_popoprivkey(inCertReqMsg);
}

static SECStatus
crmf_decode_process_keyencipherment(CRMFCertReqMsg *inCertReqMsg)
{
    SECStatus rv;

    rv = crmf_decode_process_popoprivkey(inCertReqMsg);
    if (rv != SECSuccess) {
        return rv;
    }
    if (inCertReqMsg->pop->popChoice.keyEncipherment.messageChoice == 
	crmfDHMAC) {
        /* Key Encipherment can not use the dhMAC option for
	 * POPOPrivKey. 
	 */
        return SECFailure;
    }
    return SECSuccess;
}

static SECStatus
crmf_decode_process_pop(CRMFCertReqMsg *inCertReqMsg)
{
     SECItem               *derPOP;
     PLArenaPool           *poolp;
     CRMFProofOfPossession *pop;
     void                  *mark;
     SECStatus              rv;

     derPOP = &inCertReqMsg->derPOP;
     poolp  = inCertReqMsg->poolp;
     if (derPOP->data == NULL) {
         /* There is no Proof of Possession field in this message. */
         return SECSuccess;
     }
     mark = PORT_ArenaMark(poolp);
     pop = PORT_ArenaZNew(poolp, CRMFProofOfPossession);
     if (pop == NULL) {
         goto loser;
     }
     pop->popUsed = crmf_get_popchoice_from_der(derPOP);
     if (pop->popUsed == crmfNoPOPChoice) {
         /* A bad encoding of CRMF.  Not a valid tag was given to the
	  * Proof Of Possession field.
	  */
         goto loser;
     }
     inCertReqMsg->pop = pop;
     switch (pop->popUsed) {
     case crmfRAVerified:
         rv = crmf_decode_process_raverified(inCertReqMsg);
	 break;
     case crmfSignature:
         rv = crmf_decode_process_signature(inCertReqMsg);
	 break;
     case crmfKeyEncipherment:
         rv = crmf_decode_process_keyencipherment(inCertReqMsg);
	 break;
     case crmfKeyAgreement:
         rv = crmf_decode_process_keyagreement(inCertReqMsg);
	 break;
     default:
         rv = SECFailure;
     }
     if (rv != SECSuccess) {
         goto loser;
     }
     PORT_ArenaUnmark(poolp, mark);
     return SECSuccess;

 loser:
     PORT_ArenaRelease(poolp, mark);
     inCertReqMsg->pop = NULL;
     return SECFailure;
     
}

static SECStatus
crmf_decode_process_single_control(PLArenaPool *poolp,
				   CRMFControl *inControl)
{
    const SEC_ASN1Template *asn1Template = NULL;

    inControl->tag = SECOID_FindOIDTag(&inControl->derTag);
    asn1Template = crmf_get_pkiarchiveoptions_subtemplate(inControl);

    PORT_Assert (asn1Template != NULL);
    PORT_Assert (poolp != NULL);
    if (!asn1Template || !poolp) {
    	PORT_SetError(SEC_ERROR_INVALID_ARGS);
	return SECFailure;
    }
    /* We've got a union, so passing a pointer to one element of the
     * union is the same as passing a pointer to any of the other
     * members of the union.
     */
    return SEC_ASN1Decode(poolp, &inControl->value.archiveOptions, 
			  asn1Template, (const char*)inControl->derValue.data,
			  inControl->derValue.len);
}

static SECStatus 
crmf_decode_process_controls(CRMFCertReqMsg *inCertReqMsg)
{
    int           i, numControls;
    SECStatus     rv;
    PLArenaPool  *poolp;
    CRMFControl **controls;
    
    numControls = CRMF_CertRequestGetNumControls(inCertReqMsg->certReq);
    controls = inCertReqMsg->certReq->controls;
    poolp    = inCertReqMsg->poolp;
    for (i=0; i < numControls; i++) {
        rv = crmf_decode_process_single_control(poolp, controls[i]);
	if (rv != SECSuccess) {
	    return SECFailure;
	}
    }
    return SECSuccess;
}

static SECStatus
crmf_decode_process_single_reqmsg(CRMFCertReqMsg *inCertReqMsg)
{
    SECStatus rv;

    rv = crmf_decode_process_pop(inCertReqMsg);
    if (rv != SECSuccess) {
        goto loser;
    }

    rv = crmf_decode_process_controls(inCertReqMsg);
    if (rv != SECSuccess) {
        goto loser;
    }
    inCertReqMsg->certReq->certTemplate.numExtensions = 
        CRMF_CertRequestGetNumberOfExtensions(inCertReqMsg->certReq);
    inCertReqMsg->isDecoded = PR_TRUE;
    rv = SECSuccess;
 loser:
    return rv;
}

CRMFCertReqMsg*
CRMF_CreateCertReqMsgFromDER (const char * buf, long len)
{
    PLArenaPool    *poolp;
    CRMFCertReqMsg *certReqMsg;
    SECStatus       rv;

    poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
    if (poolp == NULL) {
        goto loser;
    }
    certReqMsg = PORT_ArenaZNew (poolp, CRMFCertReqMsg);
    if (certReqMsg == NULL) {
        goto loser;
    }
    certReqMsg->poolp = poolp;
    rv = SEC_ASN1Decode(poolp, certReqMsg, CRMFCertReqMsgTemplate, buf, len);
    if (rv != SECSuccess) {
        goto loser;
    }

    rv = crmf_decode_process_single_reqmsg(certReqMsg);
    if (rv != SECSuccess) {
        goto loser;
    }

    return certReqMsg;
 loser:
    if (poolp != NULL) {
        PORT_FreeArena(poolp, PR_FALSE);
    }
    return NULL;
}

CRMFCertReqMessages*
CRMF_CreateCertReqMessagesFromDER(const char *buf, long len)
{
    long                 arenaSize;
    int                  i;
    SECStatus            rv;
    PLArenaPool         *poolp;
    CRMFCertReqMessages *certReqMsgs;

    PORT_Assert (buf != NULL);
    /* Wanna make sure the arena is big enough to store all of the requests
     * coming in.  We'll guestimate according to the length of the buffer.
     */
    arenaSize = len + len/2;
    poolp = PORT_NewArena(arenaSize);
    if (poolp == NULL) {
        return NULL;
    }
    certReqMsgs = PORT_ArenaZNew(poolp, CRMFCertReqMessages);
    if (certReqMsgs == NULL) {
        goto loser;
    }
    certReqMsgs->poolp = poolp;
    rv = SEC_ASN1Decode(poolp, certReqMsgs, CRMFCertReqMessagesTemplate,
			buf, len);
    if (rv != SECSuccess) {
        goto loser;
    }
    for (i=0; certReqMsgs->messages[i] != NULL; i++) {
        /* The sub-routines expect the individual messages to have 
	 * an arena.  We'll give them one temporarily.
	 */
        certReqMsgs->messages[i]->poolp = poolp;
        rv = crmf_decode_process_single_reqmsg(certReqMsgs->messages[i]);
	if (rv != SECSuccess) {
	    goto loser;
	}
        certReqMsgs->messages[i]->poolp = NULL;
    }
    return certReqMsgs;

 loser:
    PORT_FreeArena(poolp, PR_FALSE);
    return NULL;
}