1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/crmf/crmfdec.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,363 @@ 1.4 +/* -*- Mode: C; tab-width: 8 -*-*/ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +#include "crmf.h" 1.11 +#include "crmfi.h" 1.12 +#include "secitem.h" 1.13 + 1.14 +static CRMFPOPChoice 1.15 +crmf_get_popchoice_from_der(SECItem *derPOP) 1.16 +{ 1.17 + CRMFPOPChoice retChoice; 1.18 + 1.19 + switch (derPOP->data[0] & 0x0f) { 1.20 + case 0: 1.21 + retChoice = crmfRAVerified; 1.22 + break; 1.23 + case 1: 1.24 + retChoice = crmfSignature; 1.25 + break; 1.26 + case 2: 1.27 + retChoice = crmfKeyEncipherment; 1.28 + break; 1.29 + case 3: 1.30 + retChoice = crmfKeyAgreement; 1.31 + break; 1.32 + default: 1.33 + retChoice = crmfNoPOPChoice; 1.34 + break; 1.35 + } 1.36 + return retChoice; 1.37 +} 1.38 + 1.39 +static SECStatus 1.40 +crmf_decode_process_raverified(CRMFCertReqMsg *inCertReqMsg) 1.41 +{ 1.42 + CRMFProofOfPossession *pop; 1.43 + /* Just set up the structure so that the message structure 1.44 + * looks like one that was created using the API 1.45 + */ 1.46 + pop = inCertReqMsg->pop; 1.47 + pop->popChoice.raVerified.data = NULL; 1.48 + pop->popChoice.raVerified.len = 0; 1.49 + return SECSuccess; 1.50 +} 1.51 + 1.52 +static SECStatus 1.53 +crmf_decode_process_signature(CRMFCertReqMsg *inCertReqMsg) 1.54 +{ 1.55 + PORT_Assert(inCertReqMsg->poolp); 1.56 + if (!inCertReqMsg->poolp) { 1.57 + PORT_SetError(SEC_ERROR_INVALID_ARGS); 1.58 + return SECFailure; 1.59 + } 1.60 + return SEC_ASN1Decode(inCertReqMsg->poolp, 1.61 + &inCertReqMsg->pop->popChoice.signature, 1.62 + CRMFPOPOSigningKeyTemplate, 1.63 + (const char*)inCertReqMsg->derPOP.data, 1.64 + inCertReqMsg->derPOP.len); 1.65 +} 1.66 + 1.67 +static CRMFPOPOPrivKeyChoice 1.68 +crmf_get_messagechoice_from_der(SECItem *derPOP) 1.69 +{ 1.70 + CRMFPOPOPrivKeyChoice retChoice; 1.71 + 1.72 + switch (derPOP->data[2] & 0x0f) { 1.73 + case 0: 1.74 + retChoice = crmfThisMessage; 1.75 + break; 1.76 + case 1: 1.77 + retChoice = crmfSubsequentMessage; 1.78 + break; 1.79 + case 2: 1.80 + retChoice = crmfDHMAC; 1.81 + break; 1.82 + default: 1.83 + retChoice = crmfNoMessage; 1.84 + } 1.85 + return retChoice; 1.86 +} 1.87 + 1.88 +static SECStatus 1.89 +crmf_decode_process_popoprivkey(CRMFCertReqMsg *inCertReqMsg) 1.90 +{ 1.91 + /* We've got a union, so a pointer to one POPOPrivKey 1.92 + * struct is the same as having a pointer to the other 1.93 + * one. 1.94 + */ 1.95 + CRMFPOPOPrivKey *popoPrivKey = 1.96 + &inCertReqMsg->pop->popChoice.keyEncipherment; 1.97 + SECItem *derPOP, privKeyDer; 1.98 + SECStatus rv; 1.99 + 1.100 + derPOP = &inCertReqMsg->derPOP; 1.101 + popoPrivKey->messageChoice = crmf_get_messagechoice_from_der(derPOP); 1.102 + if (popoPrivKey->messageChoice == crmfNoMessage) { 1.103 + return SECFailure; 1.104 + } 1.105 + /* If we ever encounter BER encodings of this, we'll get in trouble*/ 1.106 + switch (popoPrivKey->messageChoice) { 1.107 + case crmfThisMessage: 1.108 + case crmfDHMAC: 1.109 + privKeyDer.type = derPOP->type; 1.110 + privKeyDer.data = &derPOP->data[5]; 1.111 + privKeyDer.len = derPOP->len - 5; 1.112 + break; 1.113 + case crmfSubsequentMessage: 1.114 + privKeyDer.type = derPOP->type; 1.115 + privKeyDer.data = &derPOP->data[4]; 1.116 + privKeyDer.len = derPOP->len - 4; 1.117 + break; 1.118 + default: 1.119 + return SECFailure; 1.120 + } 1.121 + 1.122 + rv = SECITEM_CopyItem(inCertReqMsg->poolp, 1.123 + &popoPrivKey->message.subsequentMessage, 1.124 + &privKeyDer); 1.125 + 1.126 + if (rv != SECSuccess) { 1.127 + return rv; 1.128 + } 1.129 + 1.130 + if (popoPrivKey->messageChoice == crmfThisMessage || 1.131 + popoPrivKey->messageChoice == crmfDHMAC) { 1.132 + 1.133 + popoPrivKey->message.thisMessage.len = 1.134 + CRMF_BYTES_TO_BITS(privKeyDer.len) - (int)derPOP->data[4]; 1.135 + 1.136 + } 1.137 + return SECSuccess; 1.138 +} 1.139 + 1.140 +static SECStatus 1.141 +crmf_decode_process_keyagreement(CRMFCertReqMsg *inCertReqMsg) 1.142 +{ 1.143 + return crmf_decode_process_popoprivkey(inCertReqMsg); 1.144 +} 1.145 + 1.146 +static SECStatus 1.147 +crmf_decode_process_keyencipherment(CRMFCertReqMsg *inCertReqMsg) 1.148 +{ 1.149 + SECStatus rv; 1.150 + 1.151 + rv = crmf_decode_process_popoprivkey(inCertReqMsg); 1.152 + if (rv != SECSuccess) { 1.153 + return rv; 1.154 + } 1.155 + if (inCertReqMsg->pop->popChoice.keyEncipherment.messageChoice == 1.156 + crmfDHMAC) { 1.157 + /* Key Encipherment can not use the dhMAC option for 1.158 + * POPOPrivKey. 1.159 + */ 1.160 + return SECFailure; 1.161 + } 1.162 + return SECSuccess; 1.163 +} 1.164 + 1.165 +static SECStatus 1.166 +crmf_decode_process_pop(CRMFCertReqMsg *inCertReqMsg) 1.167 +{ 1.168 + SECItem *derPOP; 1.169 + PLArenaPool *poolp; 1.170 + CRMFProofOfPossession *pop; 1.171 + void *mark; 1.172 + SECStatus rv; 1.173 + 1.174 + derPOP = &inCertReqMsg->derPOP; 1.175 + poolp = inCertReqMsg->poolp; 1.176 + if (derPOP->data == NULL) { 1.177 + /* There is no Proof of Possession field in this message. */ 1.178 + return SECSuccess; 1.179 + } 1.180 + mark = PORT_ArenaMark(poolp); 1.181 + pop = PORT_ArenaZNew(poolp, CRMFProofOfPossession); 1.182 + if (pop == NULL) { 1.183 + goto loser; 1.184 + } 1.185 + pop->popUsed = crmf_get_popchoice_from_der(derPOP); 1.186 + if (pop->popUsed == crmfNoPOPChoice) { 1.187 + /* A bad encoding of CRMF. Not a valid tag was given to the 1.188 + * Proof Of Possession field. 1.189 + */ 1.190 + goto loser; 1.191 + } 1.192 + inCertReqMsg->pop = pop; 1.193 + switch (pop->popUsed) { 1.194 + case crmfRAVerified: 1.195 + rv = crmf_decode_process_raverified(inCertReqMsg); 1.196 + break; 1.197 + case crmfSignature: 1.198 + rv = crmf_decode_process_signature(inCertReqMsg); 1.199 + break; 1.200 + case crmfKeyEncipherment: 1.201 + rv = crmf_decode_process_keyencipherment(inCertReqMsg); 1.202 + break; 1.203 + case crmfKeyAgreement: 1.204 + rv = crmf_decode_process_keyagreement(inCertReqMsg); 1.205 + break; 1.206 + default: 1.207 + rv = SECFailure; 1.208 + } 1.209 + if (rv != SECSuccess) { 1.210 + goto loser; 1.211 + } 1.212 + PORT_ArenaUnmark(poolp, mark); 1.213 + return SECSuccess; 1.214 + 1.215 + loser: 1.216 + PORT_ArenaRelease(poolp, mark); 1.217 + inCertReqMsg->pop = NULL; 1.218 + return SECFailure; 1.219 + 1.220 +} 1.221 + 1.222 +static SECStatus 1.223 +crmf_decode_process_single_control(PLArenaPool *poolp, 1.224 + CRMFControl *inControl) 1.225 +{ 1.226 + const SEC_ASN1Template *asn1Template = NULL; 1.227 + 1.228 + inControl->tag = SECOID_FindOIDTag(&inControl->derTag); 1.229 + asn1Template = crmf_get_pkiarchiveoptions_subtemplate(inControl); 1.230 + 1.231 + PORT_Assert (asn1Template != NULL); 1.232 + PORT_Assert (poolp != NULL); 1.233 + if (!asn1Template || !poolp) { 1.234 + PORT_SetError(SEC_ERROR_INVALID_ARGS); 1.235 + return SECFailure; 1.236 + } 1.237 + /* We've got a union, so passing a pointer to one element of the 1.238 + * union is the same as passing a pointer to any of the other 1.239 + * members of the union. 1.240 + */ 1.241 + return SEC_ASN1Decode(poolp, &inControl->value.archiveOptions, 1.242 + asn1Template, (const char*)inControl->derValue.data, 1.243 + inControl->derValue.len); 1.244 +} 1.245 + 1.246 +static SECStatus 1.247 +crmf_decode_process_controls(CRMFCertReqMsg *inCertReqMsg) 1.248 +{ 1.249 + int i, numControls; 1.250 + SECStatus rv; 1.251 + PLArenaPool *poolp; 1.252 + CRMFControl **controls; 1.253 + 1.254 + numControls = CRMF_CertRequestGetNumControls(inCertReqMsg->certReq); 1.255 + controls = inCertReqMsg->certReq->controls; 1.256 + poolp = inCertReqMsg->poolp; 1.257 + for (i=0; i < numControls; i++) { 1.258 + rv = crmf_decode_process_single_control(poolp, controls[i]); 1.259 + if (rv != SECSuccess) { 1.260 + return SECFailure; 1.261 + } 1.262 + } 1.263 + return SECSuccess; 1.264 +} 1.265 + 1.266 +static SECStatus 1.267 +crmf_decode_process_single_reqmsg(CRMFCertReqMsg *inCertReqMsg) 1.268 +{ 1.269 + SECStatus rv; 1.270 + 1.271 + rv = crmf_decode_process_pop(inCertReqMsg); 1.272 + if (rv != SECSuccess) { 1.273 + goto loser; 1.274 + } 1.275 + 1.276 + rv = crmf_decode_process_controls(inCertReqMsg); 1.277 + if (rv != SECSuccess) { 1.278 + goto loser; 1.279 + } 1.280 + inCertReqMsg->certReq->certTemplate.numExtensions = 1.281 + CRMF_CertRequestGetNumberOfExtensions(inCertReqMsg->certReq); 1.282 + inCertReqMsg->isDecoded = PR_TRUE; 1.283 + rv = SECSuccess; 1.284 + loser: 1.285 + return rv; 1.286 +} 1.287 + 1.288 +CRMFCertReqMsg* 1.289 +CRMF_CreateCertReqMsgFromDER (const char * buf, long len) 1.290 +{ 1.291 + PLArenaPool *poolp; 1.292 + CRMFCertReqMsg *certReqMsg; 1.293 + SECStatus rv; 1.294 + 1.295 + poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE); 1.296 + if (poolp == NULL) { 1.297 + goto loser; 1.298 + } 1.299 + certReqMsg = PORT_ArenaZNew (poolp, CRMFCertReqMsg); 1.300 + if (certReqMsg == NULL) { 1.301 + goto loser; 1.302 + } 1.303 + certReqMsg->poolp = poolp; 1.304 + rv = SEC_ASN1Decode(poolp, certReqMsg, CRMFCertReqMsgTemplate, buf, len); 1.305 + if (rv != SECSuccess) { 1.306 + goto loser; 1.307 + } 1.308 + 1.309 + rv = crmf_decode_process_single_reqmsg(certReqMsg); 1.310 + if (rv != SECSuccess) { 1.311 + goto loser; 1.312 + } 1.313 + 1.314 + return certReqMsg; 1.315 + loser: 1.316 + if (poolp != NULL) { 1.317 + PORT_FreeArena(poolp, PR_FALSE); 1.318 + } 1.319 + return NULL; 1.320 +} 1.321 + 1.322 +CRMFCertReqMessages* 1.323 +CRMF_CreateCertReqMessagesFromDER(const char *buf, long len) 1.324 +{ 1.325 + long arenaSize; 1.326 + int i; 1.327 + SECStatus rv; 1.328 + PLArenaPool *poolp; 1.329 + CRMFCertReqMessages *certReqMsgs; 1.330 + 1.331 + PORT_Assert (buf != NULL); 1.332 + /* Wanna make sure the arena is big enough to store all of the requests 1.333 + * coming in. We'll guestimate according to the length of the buffer. 1.334 + */ 1.335 + arenaSize = len + len/2; 1.336 + poolp = PORT_NewArena(arenaSize); 1.337 + if (poolp == NULL) { 1.338 + return NULL; 1.339 + } 1.340 + certReqMsgs = PORT_ArenaZNew(poolp, CRMFCertReqMessages); 1.341 + if (certReqMsgs == NULL) { 1.342 + goto loser; 1.343 + } 1.344 + certReqMsgs->poolp = poolp; 1.345 + rv = SEC_ASN1Decode(poolp, certReqMsgs, CRMFCertReqMessagesTemplate, 1.346 + buf, len); 1.347 + if (rv != SECSuccess) { 1.348 + goto loser; 1.349 + } 1.350 + for (i=0; certReqMsgs->messages[i] != NULL; i++) { 1.351 + /* The sub-routines expect the individual messages to have 1.352 + * an arena. We'll give them one temporarily. 1.353 + */ 1.354 + certReqMsgs->messages[i]->poolp = poolp; 1.355 + rv = crmf_decode_process_single_reqmsg(certReqMsgs->messages[i]); 1.356 + if (rv != SECSuccess) { 1.357 + goto loser; 1.358 + } 1.359 + certReqMsgs->messages[i]->poolp = NULL; 1.360 + } 1.361 + return certReqMsgs; 1.362 + 1.363 + loser: 1.364 + PORT_FreeArena(poolp, PR_FALSE); 1.365 + return NULL; 1.366 +}