Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C; tab-width: 8 -*-*/ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * This file will contain all routines dealing with creating a |
michael@0 | 8 | * CMMFCertRepContent structure through Create/Set functions. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "cmmf.h" |
michael@0 | 12 | #include "cmmfi.h" |
michael@0 | 13 | #include "crmf.h" |
michael@0 | 14 | #include "crmfi.h" |
michael@0 | 15 | #include "secitem.h" |
michael@0 | 16 | #include "secder.h" |
michael@0 | 17 | |
michael@0 | 18 | CMMFCertRepContent* |
michael@0 | 19 | CMMF_CreateCertRepContent(void) |
michael@0 | 20 | { |
michael@0 | 21 | CMMFCertRepContent *retCertRep; |
michael@0 | 22 | PLArenaPool *poolp; |
michael@0 | 23 | |
michael@0 | 24 | poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE); |
michael@0 | 25 | if (poolp == NULL) { |
michael@0 | 26 | goto loser; |
michael@0 | 27 | } |
michael@0 | 28 | retCertRep = PORT_ArenaZNew(poolp, CMMFCertRepContent); |
michael@0 | 29 | if (retCertRep == NULL) { |
michael@0 | 30 | goto loser; |
michael@0 | 31 | } |
michael@0 | 32 | retCertRep->poolp = poolp; |
michael@0 | 33 | return retCertRep; |
michael@0 | 34 | loser: |
michael@0 | 35 | if (poolp != NULL) { |
michael@0 | 36 | PORT_FreeArena(poolp, PR_FALSE); |
michael@0 | 37 | } |
michael@0 | 38 | return NULL; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | SECStatus |
michael@0 | 42 | cmmf_CertOrEncCertSetCertificate(CMMFCertOrEncCert *certOrEncCert, |
michael@0 | 43 | PLArenaPool *poolp, |
michael@0 | 44 | CERTCertificate *inCert) |
michael@0 | 45 | { |
michael@0 | 46 | SECItem *derDest = NULL; |
michael@0 | 47 | SECStatus rv = SECFailure; |
michael@0 | 48 | |
michael@0 | 49 | if (inCert->derCert.data == NULL) { |
michael@0 | 50 | derDest = SEC_ASN1EncodeItem(NULL, NULL, inCert, |
michael@0 | 51 | CMMFCertOrEncCertCertificateTemplate); |
michael@0 | 52 | if (derDest == NULL) { |
michael@0 | 53 | goto loser; |
michael@0 | 54 | } |
michael@0 | 55 | } else { |
michael@0 | 56 | derDest = SECITEM_DupItem(&inCert->derCert); |
michael@0 | 57 | if (derDest == NULL) { |
michael@0 | 58 | goto loser; |
michael@0 | 59 | } |
michael@0 | 60 | } |
michael@0 | 61 | PORT_Assert(certOrEncCert->cert.certificate == NULL); |
michael@0 | 62 | certOrEncCert->cert.certificate = CERT_DupCertificate(inCert); |
michael@0 | 63 | certOrEncCert->choice = cmmfCertificate; |
michael@0 | 64 | if (poolp != NULL) { |
michael@0 | 65 | rv = SECITEM_CopyItem(poolp, &certOrEncCert->derValue, derDest); |
michael@0 | 66 | if (rv != SECSuccess) { |
michael@0 | 67 | goto loser; |
michael@0 | 68 | } |
michael@0 | 69 | } else { |
michael@0 | 70 | certOrEncCert->derValue = *derDest; |
michael@0 | 71 | } |
michael@0 | 72 | PORT_Free(derDest); |
michael@0 | 73 | return SECSuccess; |
michael@0 | 74 | loser: |
michael@0 | 75 | if (derDest != NULL) { |
michael@0 | 76 | SECITEM_FreeItem(derDest, PR_TRUE); |
michael@0 | 77 | } |
michael@0 | 78 | return rv; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | SECStatus |
michael@0 | 82 | cmmf_ExtractCertsFromList(CERTCertList *inCertList, |
michael@0 | 83 | PLArenaPool *poolp, |
michael@0 | 84 | CERTCertificate ***certArray) |
michael@0 | 85 | { |
michael@0 | 86 | CERTCertificate **arrayLocalCopy; |
michael@0 | 87 | CERTCertListNode *node; |
michael@0 | 88 | int numNodes = 0, i; |
michael@0 | 89 | |
michael@0 | 90 | for (node = CERT_LIST_HEAD(inCertList); !CERT_LIST_END(node, inCertList); |
michael@0 | 91 | node = CERT_LIST_NEXT(node)) { |
michael@0 | 92 | numNodes++; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | arrayLocalCopy = *certArray = (poolp == NULL) ? |
michael@0 | 96 | PORT_NewArray(CERTCertificate*, (numNodes+1)) : |
michael@0 | 97 | PORT_ArenaNewArray(poolp, CERTCertificate*, (numNodes+1)); |
michael@0 | 98 | if (arrayLocalCopy == NULL) { |
michael@0 | 99 | return SECFailure; |
michael@0 | 100 | } |
michael@0 | 101 | for (node = CERT_LIST_HEAD(inCertList), i=0; |
michael@0 | 102 | !CERT_LIST_END(node, inCertList); |
michael@0 | 103 | node = CERT_LIST_NEXT(node), i++) { |
michael@0 | 104 | arrayLocalCopy[i] = CERT_DupCertificate(node->cert); |
michael@0 | 105 | if (arrayLocalCopy[i] == NULL) { |
michael@0 | 106 | int j; |
michael@0 | 107 | |
michael@0 | 108 | for (j=0; j<i; j++) { |
michael@0 | 109 | CERT_DestroyCertificate(arrayLocalCopy[j]); |
michael@0 | 110 | } |
michael@0 | 111 | if (poolp == NULL) { |
michael@0 | 112 | PORT_Free(arrayLocalCopy); |
michael@0 | 113 | } |
michael@0 | 114 | *certArray = NULL; |
michael@0 | 115 | return SECFailure; |
michael@0 | 116 | } |
michael@0 | 117 | } |
michael@0 | 118 | arrayLocalCopy[numNodes] = NULL; |
michael@0 | 119 | return SECSuccess; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | SECStatus |
michael@0 | 123 | CMMF_CertRepContentSetCertResponses(CMMFCertRepContent *inCertRepContent, |
michael@0 | 124 | CMMFCertResponse **inCertResponses, |
michael@0 | 125 | int inNumResponses) |
michael@0 | 126 | { |
michael@0 | 127 | PLArenaPool *poolp; |
michael@0 | 128 | CMMFCertResponse **respArr, *newResp; |
michael@0 | 129 | void *mark; |
michael@0 | 130 | SECStatus rv; |
michael@0 | 131 | int i; |
michael@0 | 132 | |
michael@0 | 133 | PORT_Assert (inCertRepContent != NULL && |
michael@0 | 134 | inCertResponses != NULL && |
michael@0 | 135 | inNumResponses > 0); |
michael@0 | 136 | if (inCertRepContent == NULL || |
michael@0 | 137 | inCertResponses == NULL || |
michael@0 | 138 | inCertRepContent->response != NULL) { |
michael@0 | 139 | return SECFailure; |
michael@0 | 140 | } |
michael@0 | 141 | poolp = inCertRepContent->poolp; |
michael@0 | 142 | mark = PORT_ArenaMark(poolp); |
michael@0 | 143 | respArr = inCertRepContent->response = |
michael@0 | 144 | PORT_ArenaZNewArray(poolp, CMMFCertResponse*, (inNumResponses+1)); |
michael@0 | 145 | if (respArr == NULL) { |
michael@0 | 146 | goto loser; |
michael@0 | 147 | } |
michael@0 | 148 | for (i=0; i<inNumResponses; i++) { |
michael@0 | 149 | newResp = PORT_ArenaZNew(poolp, CMMFCertResponse); |
michael@0 | 150 | if (newResp == NULL) { |
michael@0 | 151 | goto loser; |
michael@0 | 152 | } |
michael@0 | 153 | rv = cmmf_CopyCertResponse(poolp, newResp, inCertResponses[i]); |
michael@0 | 154 | if (rv != SECSuccess) { |
michael@0 | 155 | goto loser; |
michael@0 | 156 | } |
michael@0 | 157 | respArr[i] = newResp; |
michael@0 | 158 | } |
michael@0 | 159 | respArr[inNumResponses] = NULL; |
michael@0 | 160 | PORT_ArenaUnmark(poolp, mark); |
michael@0 | 161 | return SECSuccess; |
michael@0 | 162 | |
michael@0 | 163 | loser: |
michael@0 | 164 | PORT_ArenaRelease(poolp, mark); |
michael@0 | 165 | return SECFailure; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | CMMFCertResponse* |
michael@0 | 169 | CMMF_CreateCertResponse(long inCertReqId) |
michael@0 | 170 | { |
michael@0 | 171 | SECItem *dummy; |
michael@0 | 172 | CMMFCertResponse *newResp; |
michael@0 | 173 | |
michael@0 | 174 | newResp = PORT_ZNew(CMMFCertResponse); |
michael@0 | 175 | if (newResp == NULL) { |
michael@0 | 176 | goto loser; |
michael@0 | 177 | } |
michael@0 | 178 | dummy = SEC_ASN1EncodeInteger(NULL, &newResp->certReqId, inCertReqId); |
michael@0 | 179 | if (dummy != &newResp->certReqId) { |
michael@0 | 180 | goto loser; |
michael@0 | 181 | } |
michael@0 | 182 | return newResp; |
michael@0 | 183 | |
michael@0 | 184 | loser: |
michael@0 | 185 | if (newResp != NULL) { |
michael@0 | 186 | CMMF_DestroyCertResponse(newResp); |
michael@0 | 187 | } |
michael@0 | 188 | return NULL; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | SECStatus |
michael@0 | 192 | CMMF_CertResponseSetPKIStatusInfoStatus(CMMFCertResponse *inCertResp, |
michael@0 | 193 | CMMFPKIStatus inPKIStatus) |
michael@0 | 194 | { |
michael@0 | 195 | PORT_Assert (inCertResp != NULL && inPKIStatus >= cmmfGranted |
michael@0 | 196 | && inPKIStatus < cmmfNumPKIStatus); |
michael@0 | 197 | |
michael@0 | 198 | if (inCertResp == NULL) { |
michael@0 | 199 | return SECFailure; |
michael@0 | 200 | } |
michael@0 | 201 | return cmmf_PKIStatusInfoSetStatus(&inCertResp->status, NULL, |
michael@0 | 202 | inPKIStatus); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | SECStatus |
michael@0 | 206 | CMMF_CertResponseSetCertificate (CMMFCertResponse *inCertResp, |
michael@0 | 207 | CERTCertificate *inCertificate) |
michael@0 | 208 | { |
michael@0 | 209 | CMMFCertifiedKeyPair *keyPair = NULL; |
michael@0 | 210 | SECStatus rv = SECFailure; |
michael@0 | 211 | |
michael@0 | 212 | PORT_Assert(inCertResp != NULL && inCertificate != NULL); |
michael@0 | 213 | if (inCertResp == NULL || inCertificate == NULL) { |
michael@0 | 214 | return SECFailure; |
michael@0 | 215 | } |
michael@0 | 216 | if (inCertResp->certifiedKeyPair == NULL) { |
michael@0 | 217 | keyPair = inCertResp->certifiedKeyPair = |
michael@0 | 218 | PORT_ZNew(CMMFCertifiedKeyPair); |
michael@0 | 219 | } else { |
michael@0 | 220 | keyPair = inCertResp->certifiedKeyPair; |
michael@0 | 221 | } |
michael@0 | 222 | if (keyPair == NULL) { |
michael@0 | 223 | goto loser; |
michael@0 | 224 | } |
michael@0 | 225 | rv = cmmf_CertOrEncCertSetCertificate(&keyPair->certOrEncCert, NULL, |
michael@0 | 226 | inCertificate); |
michael@0 | 227 | if (rv != SECSuccess) { |
michael@0 | 228 | goto loser; |
michael@0 | 229 | } |
michael@0 | 230 | return SECSuccess; |
michael@0 | 231 | loser: |
michael@0 | 232 | if (keyPair) { |
michael@0 | 233 | if (keyPair->certOrEncCert.derValue.data) { |
michael@0 | 234 | PORT_Free(keyPair->certOrEncCert.derValue.data); |
michael@0 | 235 | } |
michael@0 | 236 | PORT_Free(keyPair); |
michael@0 | 237 | } |
michael@0 | 238 | return rv; |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | |
michael@0 | 242 | SECStatus |
michael@0 | 243 | CMMF_CertRepContentSetCAPubs(CMMFCertRepContent *inCertRepContent, |
michael@0 | 244 | CERTCertList *inCAPubs) |
michael@0 | 245 | { |
michael@0 | 246 | PLArenaPool *poolp; |
michael@0 | 247 | void *mark; |
michael@0 | 248 | SECStatus rv; |
michael@0 | 249 | |
michael@0 | 250 | PORT_Assert(inCertRepContent != NULL && |
michael@0 | 251 | inCAPubs != NULL && |
michael@0 | 252 | inCertRepContent->caPubs == NULL); |
michael@0 | 253 | |
michael@0 | 254 | if (inCertRepContent == NULL || |
michael@0 | 255 | inCAPubs == NULL || inCertRepContent == NULL) { |
michael@0 | 256 | return SECFailure; |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | poolp = inCertRepContent->poolp; |
michael@0 | 260 | mark = PORT_ArenaMark(poolp); |
michael@0 | 261 | |
michael@0 | 262 | rv = cmmf_ExtractCertsFromList(inCAPubs, poolp, |
michael@0 | 263 | &inCertRepContent->caPubs); |
michael@0 | 264 | |
michael@0 | 265 | if (rv != SECSuccess) { |
michael@0 | 266 | PORT_ArenaRelease(poolp, mark); |
michael@0 | 267 | } else { |
michael@0 | 268 | PORT_ArenaUnmark(poolp, mark); |
michael@0 | 269 | } |
michael@0 | 270 | return rv; |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | CERTCertificate* |
michael@0 | 274 | CMMF_CertifiedKeyPairGetCertificate(CMMFCertifiedKeyPair *inCertKeyPair, |
michael@0 | 275 | CERTCertDBHandle *inCertdb) |
michael@0 | 276 | { |
michael@0 | 277 | PORT_Assert(inCertKeyPair != NULL); |
michael@0 | 278 | if (inCertKeyPair == NULL) { |
michael@0 | 279 | return NULL; |
michael@0 | 280 | } |
michael@0 | 281 | return cmmf_CertOrEncCertGetCertificate(&inCertKeyPair->certOrEncCert, |
michael@0 | 282 | inCertdb); |
michael@0 | 283 | } |