servget.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /* -*- Mode: C; tab-width: 8 -*-*/
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "cmmf.h"
  6. #include "cmmfi.h"
  7. #include "secitem.h"
  8. #include "keyhi.h"
  9. #include "secder.h"
  10. CRMFEncryptedKeyChoice
  11. CRMF_EncryptedKeyGetChoice(CRMFEncryptedKey *inEncrKey)
  12. {
  13. PORT_Assert(inEncrKey != NULL);
  14. if (inEncrKey == NULL) {
  15. return crmfNoEncryptedKeyChoice;
  16. }
  17. return inEncrKey->encKeyChoice;
  18. }
  19. CRMFEncryptedValue *
  20. CRMF_EncryptedKeyGetEncryptedValue(CRMFEncryptedKey *inEncrKey)
  21. {
  22. CRMFEncryptedValue *newEncrValue = NULL;
  23. SECStatus rv;
  24. PORT_Assert(inEncrKey != NULL);
  25. if (inEncrKey == NULL ||
  26. CRMF_EncryptedKeyGetChoice(inEncrKey) != crmfEncryptedValueChoice) {
  27. goto loser;
  28. }
  29. newEncrValue = PORT_ZNew(CRMFEncryptedValue);
  30. if (newEncrValue == NULL) {
  31. goto loser;
  32. }
  33. rv = crmf_copy_encryptedvalue(NULL, &inEncrKey->value.encryptedValue,
  34. newEncrValue);
  35. if (rv != SECSuccess) {
  36. goto loser;
  37. }
  38. return newEncrValue;
  39. loser:
  40. if (newEncrValue != NULL) {
  41. CRMF_DestroyEncryptedValue(newEncrValue);
  42. }
  43. return NULL;
  44. }
  45. static SECItem *
  46. crmf_get_encvalue_bitstring(SECItem *srcItem)
  47. {
  48. SECItem *newItem = NULL;
  49. SECStatus rv;
  50. if (srcItem->data == NULL) {
  51. return NULL;
  52. }
  53. newItem = PORT_ZNew(SECItem);
  54. if (newItem == NULL) {
  55. goto loser;
  56. }
  57. rv = crmf_make_bitstring_copy(NULL, newItem, srcItem);
  58. if (rv != SECSuccess) {
  59. goto loser;
  60. }
  61. return newItem;
  62. loser:
  63. if (newItem != NULL) {
  64. SECITEM_FreeItem(newItem, PR_TRUE);
  65. }
  66. return NULL;
  67. }
  68. SECItem *
  69. CRMF_EncryptedValueGetEncSymmKey(CRMFEncryptedValue *inEncValue)
  70. {
  71. if (inEncValue == NULL) {
  72. return NULL;
  73. }
  74. return crmf_get_encvalue_bitstring(&inEncValue->encSymmKey);
  75. }
  76. SECItem *
  77. CRMF_EncryptedValueGetEncValue(CRMFEncryptedValue *inEncrValue)
  78. {
  79. if (inEncrValue == NULL || inEncrValue->encValue.data == NULL) {
  80. return NULL;
  81. }
  82. return crmf_get_encvalue_bitstring(&inEncrValue->encValue);
  83. }
  84. static SECAlgorithmID *
  85. crmf_get_encvalue_algid(SECAlgorithmID *srcAlg)
  86. {
  87. SECStatus rv;
  88. SECAlgorithmID *newAlgID;
  89. if (srcAlg == NULL) {
  90. return NULL;
  91. }
  92. rv = crmf_copy_encryptedvalue_secalg(NULL, srcAlg, &newAlgID);
  93. if (rv != SECSuccess) {
  94. return NULL;
  95. }
  96. return newAlgID;
  97. }
  98. SECAlgorithmID *
  99. CRMF_EncryptedValueGetIntendedAlg(CRMFEncryptedValue *inEncValue)
  100. {
  101. if (inEncValue == NULL) {
  102. return NULL;
  103. }
  104. return crmf_get_encvalue_algid(inEncValue->intendedAlg);
  105. }
  106. SECAlgorithmID *
  107. CRMF_EncryptedValueGetKeyAlg(CRMFEncryptedValue *inEncValue)
  108. {
  109. if (inEncValue == NULL) {
  110. return NULL;
  111. }
  112. return crmf_get_encvalue_algid(inEncValue->keyAlg);
  113. }
  114. SECAlgorithmID *
  115. CRMF_EncryptedValueGetSymmAlg(CRMFEncryptedValue *inEncValue)
  116. {
  117. if (inEncValue == NULL) {
  118. return NULL;
  119. }
  120. return crmf_get_encvalue_algid(inEncValue->symmAlg);
  121. }
  122. SECItem *
  123. CRMF_EncryptedValueGetValueHint(CRMFEncryptedValue *inEncValue)
  124. {
  125. if (inEncValue == NULL || inEncValue->valueHint.data == NULL) {
  126. return NULL;
  127. }
  128. return SECITEM_DupItem(&inEncValue->valueHint);
  129. }
  130. SECStatus
  131. CRMF_PKIArchiveOptionsGetArchiveRemGenPrivKey(CRMFPKIArchiveOptions *inOpt,
  132. PRBool *destVal)
  133. {
  134. if (inOpt == NULL || destVal == NULL ||
  135. CRMF_PKIArchiveOptionsGetOptionType(inOpt) != crmfArchiveRemGenPrivKey) {
  136. return SECFailure;
  137. }
  138. *destVal = (inOpt->option.archiveRemGenPrivKey.data[0] == hexFalse)
  139. ? PR_FALSE
  140. : PR_TRUE;
  141. return SECSuccess;
  142. }
  143. CRMFEncryptedKey *
  144. CRMF_PKIArchiveOptionsGetEncryptedPrivKey(CRMFPKIArchiveOptions *inOpts)
  145. {
  146. CRMFEncryptedKey *newEncrKey = NULL;
  147. SECStatus rv;
  148. PORT_Assert(inOpts != NULL);
  149. if (inOpts == NULL ||
  150. CRMF_PKIArchiveOptionsGetOptionType(inOpts) != crmfEncryptedPrivateKey) {
  151. return NULL;
  152. }
  153. newEncrKey = PORT_ZNew(CRMFEncryptedKey);
  154. if (newEncrKey == NULL) {
  155. goto loser;
  156. }
  157. rv = crmf_copy_encryptedkey(NULL, &inOpts->option.encryptedKey,
  158. newEncrKey);
  159. if (rv != SECSuccess) {
  160. goto loser;
  161. }
  162. return newEncrKey;
  163. loser:
  164. if (newEncrKey != NULL) {
  165. CRMF_DestroyEncryptedKey(newEncrKey);
  166. }
  167. return NULL;
  168. }
  169. SECItem *
  170. CRMF_PKIArchiveOptionsGetKeyGenParameters(CRMFPKIArchiveOptions *inOptions)
  171. {
  172. if (inOptions == NULL ||
  173. CRMF_PKIArchiveOptionsGetOptionType(inOptions) != crmfKeyGenParameters ||
  174. inOptions->option.keyGenParameters.data == NULL) {
  175. return NULL;
  176. }
  177. return SECITEM_DupItem(&inOptions->option.keyGenParameters);
  178. }
  179. CRMFPKIArchiveOptionsType
  180. CRMF_PKIArchiveOptionsGetOptionType(CRMFPKIArchiveOptions *inOptions)
  181. {
  182. PORT_Assert(inOptions != NULL);
  183. if (inOptions == NULL) {
  184. return crmfNoArchiveOptions;
  185. }
  186. return inOptions->archOption;
  187. }
  188. static SECStatus
  189. crmf_extract_long_from_item(SECItem *intItem, long *destLong)
  190. {
  191. *destLong = DER_GetInteger(intItem);
  192. return (*destLong == -1) ? SECFailure : SECSuccess;
  193. }
  194. SECStatus
  195. CRMF_POPOPrivGetKeySubseqMess(CRMFPOPOPrivKey *inKey,
  196. CRMFSubseqMessOptions *destOpt)
  197. {
  198. long value;
  199. SECStatus rv;
  200. PORT_Assert(inKey != NULL);
  201. if (inKey == NULL ||
  202. inKey->messageChoice != crmfSubsequentMessage) {
  203. return SECFailure;
  204. }
  205. rv = crmf_extract_long_from_item(&inKey->message.subsequentMessage, &value);
  206. if (rv != SECSuccess) {
  207. return SECFailure;
  208. }
  209. switch (value) {
  210. case 0:
  211. *destOpt = crmfEncrCert;
  212. break;
  213. case 1:
  214. *destOpt = crmfChallengeResp;
  215. break;
  216. default:
  217. rv = SECFailure;
  218. }
  219. if (rv != SECSuccess) {
  220. return rv;
  221. }
  222. return SECSuccess;
  223. }
  224. CRMFPOPOPrivKeyChoice
  225. CRMF_POPOPrivKeyGetChoice(CRMFPOPOPrivKey *inPrivKey)
  226. {
  227. PORT_Assert(inPrivKey != NULL);
  228. if (inPrivKey != NULL) {
  229. return inPrivKey->messageChoice;
  230. }
  231. return crmfNoMessage;
  232. }
  233. SECStatus
  234. CRMF_POPOPrivKeyGetDHMAC(CRMFPOPOPrivKey *inKey, SECItem *destMAC)
  235. {
  236. PORT_Assert(inKey != NULL);
  237. if (inKey == NULL || inKey->message.dhMAC.data == NULL) {
  238. return SECFailure;
  239. }
  240. return crmf_make_bitstring_copy(NULL, destMAC, &inKey->message.dhMAC);
  241. }
  242. SECStatus
  243. CRMF_POPOPrivKeyGetThisMessage(CRMFPOPOPrivKey *inKey,
  244. SECItem *destString)
  245. {
  246. PORT_Assert(inKey != NULL);
  247. if (inKey == NULL ||
  248. inKey->messageChoice != crmfThisMessage) {
  249. return SECFailure;
  250. }
  251. return crmf_make_bitstring_copy(NULL, destString,
  252. &inKey->message.thisMessage);
  253. }
  254. SECAlgorithmID *
  255. CRMF_POPOSigningKeyGetAlgID(CRMFPOPOSigningKey *inSignKey)
  256. {
  257. SECAlgorithmID *newAlgId = NULL;
  258. SECStatus rv;
  259. PORT_Assert(inSignKey != NULL);
  260. if (inSignKey == NULL) {
  261. return NULL;
  262. }
  263. newAlgId = PORT_ZNew(SECAlgorithmID);
  264. if (newAlgId == NULL) {
  265. goto loser;
  266. }
  267. rv = SECOID_CopyAlgorithmID(NULL, newAlgId,
  268. inSignKey->algorithmIdentifier);
  269. if (rv != SECSuccess) {
  270. goto loser;
  271. }
  272. return newAlgId;
  273. loser:
  274. if (newAlgId != NULL) {
  275. SECOID_DestroyAlgorithmID(newAlgId, PR_TRUE);
  276. }
  277. return NULL;
  278. }
  279. SECItem *
  280. CRMF_POPOSigningKeyGetInput(CRMFPOPOSigningKey *inSignKey)
  281. {
  282. PORT_Assert(inSignKey != NULL);
  283. if (inSignKey == NULL || inSignKey->derInput.data == NULL) {
  284. return NULL;
  285. }
  286. return SECITEM_DupItem(&inSignKey->derInput);
  287. }
  288. SECItem *
  289. CRMF_POPOSigningKeyGetSignature(CRMFPOPOSigningKey *inSignKey)
  290. {
  291. SECItem *newSig = NULL;
  292. SECStatus rv;
  293. PORT_Assert(inSignKey != NULL);
  294. if (inSignKey == NULL) {
  295. return NULL;
  296. }
  297. newSig = PORT_ZNew(SECItem);
  298. if (newSig == NULL) {
  299. goto loser;
  300. }
  301. rv = crmf_make_bitstring_copy(NULL, newSig, &inSignKey->signature);
  302. if (rv != SECSuccess) {
  303. goto loser;
  304. }
  305. return newSig;
  306. loser:
  307. if (newSig != NULL) {
  308. SECITEM_FreeItem(newSig, PR_TRUE);
  309. }
  310. return NULL;
  311. }
  312. static SECStatus
  313. crmf_copy_poposigningkey(PLArenaPool *poolp,
  314. CRMFPOPOSigningKey *inPopoSignKey,
  315. CRMFPOPOSigningKey *destPopoSignKey)
  316. {
  317. SECStatus rv;
  318. /* We don't support use of the POPOSigningKeyInput, so we'll only
  319. * store away the DER encoding.
  320. */
  321. if (inPopoSignKey->derInput.data != NULL) {
  322. rv = SECITEM_CopyItem(poolp, &destPopoSignKey->derInput,
  323. &inPopoSignKey->derInput);
  324. if (rv != SECSuccess) {
  325. goto loser;
  326. }
  327. }
  328. destPopoSignKey->algorithmIdentifier = (poolp == NULL) ? PORT_ZNew(SECAlgorithmID)
  329. : PORT_ArenaZNew(poolp, SECAlgorithmID);
  330. if (destPopoSignKey->algorithmIdentifier == NULL) {
  331. goto loser;
  332. }
  333. rv = SECOID_CopyAlgorithmID(poolp, destPopoSignKey->algorithmIdentifier,
  334. inPopoSignKey->algorithmIdentifier);
  335. if (rv != SECSuccess) {
  336. goto loser;
  337. }
  338. rv = crmf_make_bitstring_copy(poolp, &destPopoSignKey->signature,
  339. &inPopoSignKey->signature);
  340. if (rv != SECSuccess) {
  341. goto loser;
  342. }
  343. return SECSuccess;
  344. loser:
  345. if (poolp == NULL) {
  346. CRMF_DestroyPOPOSigningKey(destPopoSignKey);
  347. }
  348. return SECFailure;
  349. }
  350. static SECStatus
  351. crmf_copy_popoprivkey(PLArenaPool *poolp,
  352. CRMFPOPOPrivKey *srcPrivKey,
  353. CRMFPOPOPrivKey *destPrivKey)
  354. {
  355. SECStatus rv;
  356. destPrivKey->messageChoice = srcPrivKey->messageChoice;
  357. switch (destPrivKey->messageChoice) {
  358. case crmfThisMessage:
  359. case crmfDHMAC:
  360. /* I've got a union, so taking the address of one, will also give
  361. * me a pointer to the other (eg, message.dhMAC)
  362. */
  363. rv = crmf_make_bitstring_copy(poolp, &destPrivKey->message.thisMessage,
  364. &srcPrivKey->message.thisMessage);
  365. break;
  366. case crmfSubsequentMessage:
  367. rv = SECITEM_CopyItem(poolp, &destPrivKey->message.subsequentMessage,
  368. &srcPrivKey->message.subsequentMessage);
  369. break;
  370. default:
  371. rv = SECFailure;
  372. }
  373. if (rv != SECSuccess && poolp == NULL) {
  374. CRMF_DestroyPOPOPrivKey(destPrivKey);
  375. }
  376. return rv;
  377. }
  378. static CRMFProofOfPossession *
  379. crmf_copy_pop(PLArenaPool *poolp, CRMFProofOfPossession *srcPOP)
  380. {
  381. CRMFProofOfPossession *newPOP;
  382. SECStatus rv;
  383. /*
  384. * Proof Of Possession structures are always part of the Request
  385. * message, so there will always be an arena for allocating memory.
  386. */
  387. if (poolp == NULL) {
  388. return NULL;
  389. }
  390. newPOP = PORT_ArenaZNew(poolp, CRMFProofOfPossession);
  391. if (newPOP == NULL) {
  392. return NULL;
  393. }
  394. switch (srcPOP->popUsed) {
  395. case crmfRAVerified:
  396. newPOP->popChoice.raVerified.data = NULL;
  397. newPOP->popChoice.raVerified.len = 0;
  398. break;
  399. case crmfSignature:
  400. rv = crmf_copy_poposigningkey(poolp, &srcPOP->popChoice.signature,
  401. &newPOP->popChoice.signature);
  402. if (rv != SECSuccess) {
  403. goto loser;
  404. }
  405. break;
  406. case crmfKeyEncipherment:
  407. case crmfKeyAgreement:
  408. /* We've got a union, so a pointer to one, is a pointer to the
  409. * other one.
  410. */
  411. rv = crmf_copy_popoprivkey(poolp, &srcPOP->popChoice.keyEncipherment,
  412. &newPOP->popChoice.keyEncipherment);
  413. if (rv != SECSuccess) {
  414. goto loser;
  415. }
  416. break;
  417. default:
  418. goto loser;
  419. }
  420. newPOP->popUsed = srcPOP->popUsed;
  421. return newPOP;
  422. loser:
  423. return NULL;
  424. }
  425. static CRMFCertReqMsg *
  426. crmf_copy_cert_req_msg(CRMFCertReqMsg *srcReqMsg)
  427. {
  428. CRMFCertReqMsg *newReqMsg;
  429. PLArenaPool *poolp;
  430. poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
  431. if (poolp == NULL) {
  432. return NULL;
  433. }
  434. newReqMsg = PORT_ArenaZNew(poolp, CRMFCertReqMsg);
  435. if (newReqMsg == NULL) {
  436. PORT_FreeArena(poolp, PR_TRUE);
  437. return NULL;
  438. }
  439. newReqMsg->poolp = poolp;
  440. newReqMsg->certReq = crmf_copy_cert_request(poolp, srcReqMsg->certReq);
  441. if (newReqMsg->certReq == NULL) {
  442. goto loser;
  443. }
  444. newReqMsg->pop = crmf_copy_pop(poolp, srcReqMsg->pop);
  445. if (newReqMsg->pop == NULL) {
  446. goto loser;
  447. }
  448. /* None of my set/get routines operate on the regInfo field, so
  449. * for now, that won't get copied over.
  450. */
  451. return newReqMsg;
  452. loser:
  453. CRMF_DestroyCertReqMsg(newReqMsg);
  454. return NULL;
  455. }
  456. CRMFCertReqMsg *
  457. CRMF_CertReqMessagesGetCertReqMsgAtIndex(CRMFCertReqMessages *inReqMsgs,
  458. int index)
  459. {
  460. int numMsgs;
  461. PORT_Assert(inReqMsgs != NULL && index >= 0);
  462. if (inReqMsgs == NULL) {
  463. return NULL;
  464. }
  465. numMsgs = CRMF_CertReqMessagesGetNumMessages(inReqMsgs);
  466. if (index < 0 || index >= numMsgs) {
  467. return NULL;
  468. }
  469. return crmf_copy_cert_req_msg(inReqMsgs->messages[index]);
  470. }
  471. int
  472. CRMF_CertReqMessagesGetNumMessages(CRMFCertReqMessages *inCertReqMsgs)
  473. {
  474. int numMessages = 0;
  475. PORT_Assert(inCertReqMsgs != NULL);
  476. if (inCertReqMsgs == NULL) {
  477. return 0;
  478. }
  479. while (inCertReqMsgs->messages[numMessages] != NULL) {
  480. numMessages++;
  481. }
  482. return numMessages;
  483. }
  484. CRMFCertRequest *
  485. CRMF_CertReqMsgGetCertRequest(CRMFCertReqMsg *inCertReqMsg)
  486. {
  487. PLArenaPool *poolp = NULL;
  488. CRMFCertRequest *newCertReq = NULL;
  489. PORT_Assert(inCertReqMsg != NULL);
  490. poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
  491. if (poolp == NULL) {
  492. goto loser;
  493. }
  494. newCertReq = crmf_copy_cert_request(poolp, inCertReqMsg->certReq);
  495. if (newCertReq == NULL) {
  496. goto loser;
  497. }
  498. newCertReq->poolp = poolp;
  499. return newCertReq;
  500. loser:
  501. if (poolp != NULL) {
  502. PORT_FreeArena(poolp, PR_FALSE);
  503. }
  504. return NULL;
  505. }
  506. SECStatus
  507. CRMF_CertReqMsgGetID(CRMFCertReqMsg *inCertReqMsg, long *destID)
  508. {
  509. PORT_Assert(inCertReqMsg != NULL && destID != NULL);
  510. if (inCertReqMsg == NULL || inCertReqMsg->certReq == NULL) {
  511. return SECFailure;
  512. }
  513. return crmf_extract_long_from_item(&inCertReqMsg->certReq->certReqId,
  514. destID);
  515. }
  516. SECStatus
  517. CRMF_CertReqMsgGetPOPKeyAgreement(CRMFCertReqMsg *inCertReqMsg,
  518. CRMFPOPOPrivKey **destKey)
  519. {
  520. PORT_Assert(inCertReqMsg != NULL && destKey != NULL);
  521. if (inCertReqMsg == NULL || destKey == NULL ||
  522. CRMF_CertReqMsgGetPOPType(inCertReqMsg) != crmfKeyAgreement) {
  523. return SECFailure;
  524. }
  525. *destKey = PORT_ZNew(CRMFPOPOPrivKey);
  526. if (*destKey == NULL) {
  527. return SECFailure;
  528. }
  529. return crmf_copy_popoprivkey(NULL,
  530. &inCertReqMsg->pop->popChoice.keyAgreement,
  531. *destKey);
  532. }
  533. SECStatus
  534. CRMF_CertReqMsgGetPOPKeyEncipherment(CRMFCertReqMsg *inCertReqMsg,
  535. CRMFPOPOPrivKey **destKey)
  536. {
  537. PORT_Assert(inCertReqMsg != NULL && destKey != NULL);
  538. if (inCertReqMsg == NULL || destKey == NULL ||
  539. CRMF_CertReqMsgGetPOPType(inCertReqMsg) != crmfKeyEncipherment) {
  540. return SECFailure;
  541. }
  542. *destKey = PORT_ZNew(CRMFPOPOPrivKey);
  543. if (*destKey == NULL) {
  544. return SECFailure;
  545. }
  546. return crmf_copy_popoprivkey(NULL,
  547. &inCertReqMsg->pop->popChoice.keyEncipherment,
  548. *destKey);
  549. }
  550. SECStatus
  551. CRMF_CertReqMsgGetPOPOSigningKey(CRMFCertReqMsg *inCertReqMsg,
  552. CRMFPOPOSigningKey **destKey)
  553. {
  554. CRMFProofOfPossession *pop;
  555. PORT_Assert(inCertReqMsg != NULL);
  556. if (inCertReqMsg == NULL) {
  557. return SECFailure;
  558. }
  559. pop = inCertReqMsg->pop;
  560. ;
  561. if (pop->popUsed != crmfSignature) {
  562. return SECFailure;
  563. }
  564. *destKey = PORT_ZNew(CRMFPOPOSigningKey);
  565. if (*destKey == NULL) {
  566. return SECFailure;
  567. }
  568. return crmf_copy_poposigningkey(NULL, &pop->popChoice.signature, *destKey);
  569. }
  570. static SECStatus
  571. crmf_copy_name(CERTName *destName, CERTName *srcName)
  572. {
  573. PLArenaPool *poolp = NULL;
  574. SECStatus rv;
  575. if (destName->arena != NULL) {
  576. poolp = destName->arena;
  577. } else {
  578. poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
  579. }
  580. if (poolp == NULL) {
  581. return SECFailure;
  582. }
  583. /* Need to do this so that CERT_CopyName doesn't free out
  584. * the arena from underneath us.
  585. */
  586. destName->arena = NULL;
  587. rv = CERT_CopyName(poolp, destName, srcName);
  588. destName->arena = poolp;
  589. return rv;
  590. }
  591. SECStatus
  592. CRMF_CertRequestGetCertTemplateIssuer(CRMFCertRequest *inCertReq,
  593. CERTName *destIssuer)
  594. {
  595. PORT_Assert(inCertReq != NULL);
  596. if (inCertReq == NULL) {
  597. return SECFailure;
  598. }
  599. if (CRMF_DoesRequestHaveField(inCertReq, crmfIssuer)) {
  600. return crmf_copy_name(destIssuer,
  601. inCertReq->certTemplate.issuer);
  602. }
  603. return SECFailure;
  604. }
  605. SECStatus
  606. CRMF_CertRequestGetCertTemplateIssuerUID(CRMFCertRequest *inCertReq,
  607. SECItem *destIssuerUID)
  608. {
  609. PORT_Assert(inCertReq != NULL);
  610. if (inCertReq == NULL) {
  611. return SECFailure;
  612. }
  613. if (CRMF_DoesRequestHaveField(inCertReq, crmfIssuerUID)) {
  614. return crmf_make_bitstring_copy(NULL, destIssuerUID,
  615. &inCertReq->certTemplate.issuerUID);
  616. }
  617. return SECFailure;
  618. }
  619. SECStatus
  620. CRMF_CertRequestGetCertTemplatePublicKey(CRMFCertRequest *inCertReq,
  621. CERTSubjectPublicKeyInfo *destPublicKey)
  622. {
  623. PORT_Assert(inCertReq != NULL);
  624. if (inCertReq == NULL) {
  625. return SECFailure;
  626. }
  627. if (CRMF_DoesRequestHaveField(inCertReq, crmfPublicKey)) {
  628. return SECKEY_CopySubjectPublicKeyInfo(NULL, destPublicKey,
  629. inCertReq->certTemplate.publicKey);
  630. }
  631. return SECFailure;
  632. }
  633. SECStatus
  634. CRMF_CertRequestGetCertTemplateSerialNumber(CRMFCertRequest *inCertReq,
  635. long *serialNumber)
  636. {
  637. PORT_Assert(inCertReq != NULL);
  638. if (inCertReq == NULL) {
  639. return SECFailure;
  640. }
  641. if (CRMF_DoesRequestHaveField(inCertReq, crmfSerialNumber)) {
  642. return crmf_extract_long_from_item(&inCertReq->certTemplate.serialNumber,
  643. serialNumber);
  644. }
  645. return SECFailure;
  646. }
  647. SECStatus
  648. CRMF_CertRequestGetCertTemplateSigningAlg(CRMFCertRequest *inCertReq,
  649. SECAlgorithmID *destAlg)
  650. {
  651. PORT_Assert(inCertReq != NULL);
  652. if (inCertReq == NULL) {
  653. return SECFailure;
  654. }
  655. if (CRMF_DoesRequestHaveField(inCertReq, crmfSigningAlg)) {
  656. return SECOID_CopyAlgorithmID(NULL, destAlg,
  657. inCertReq->certTemplate.signingAlg);
  658. }
  659. return SECFailure;
  660. }
  661. SECStatus
  662. CRMF_CertRequestGetCertTemplateSubject(CRMFCertRequest *inCertReq,
  663. CERTName *destSubject)
  664. {
  665. PORT_Assert(inCertReq != NULL);
  666. if (inCertReq == NULL) {
  667. return SECFailure;
  668. }
  669. if (CRMF_DoesRequestHaveField(inCertReq, crmfSubject)) {
  670. return crmf_copy_name(destSubject, inCertReq->certTemplate.subject);
  671. }
  672. return SECFailure;
  673. }
  674. SECStatus
  675. CRMF_CertRequestGetCertTemplateSubjectUID(CRMFCertRequest *inCertReq,
  676. SECItem *destSubjectUID)
  677. {
  678. PORT_Assert(inCertReq != NULL);
  679. if (inCertReq == NULL) {
  680. return SECFailure;
  681. }
  682. if (CRMF_DoesRequestHaveField(inCertReq, crmfSubjectUID)) {
  683. return crmf_make_bitstring_copy(NULL, destSubjectUID,
  684. &inCertReq->certTemplate.subjectUID);
  685. }
  686. return SECFailure;
  687. }
  688. SECStatus
  689. CRMF_CertRequestGetCertTemplateVersion(CRMFCertRequest *inCertReq,
  690. long *version)
  691. {
  692. PORT_Assert(inCertReq != NULL);
  693. if (inCertReq == NULL) {
  694. return SECFailure;
  695. }
  696. if (CRMF_DoesRequestHaveField(inCertReq, crmfVersion)) {
  697. return crmf_extract_long_from_item(&inCertReq->certTemplate.version,
  698. version);
  699. }
  700. return SECFailure;
  701. }
  702. static SECStatus
  703. crmf_copy_validity(CRMFGetValidity *destValidity,
  704. CRMFOptionalValidity *src)
  705. {
  706. SECStatus rv;
  707. destValidity->notBefore = destValidity->notAfter = NULL;
  708. if (src->notBefore.data != NULL) {
  709. rv = crmf_create_prtime(&src->notBefore,
  710. &destValidity->notBefore);
  711. if (rv != SECSuccess) {
  712. return rv;
  713. }
  714. }
  715. if (src->notAfter.data != NULL) {
  716. rv = crmf_create_prtime(&src->notAfter,
  717. &destValidity->notAfter);
  718. if (rv != SECSuccess) {
  719. return rv;
  720. }
  721. }
  722. return SECSuccess;
  723. }
  724. SECStatus
  725. CRMF_CertRequestGetCertTemplateValidity(CRMFCertRequest *inCertReq,
  726. CRMFGetValidity *destValidity)
  727. {
  728. PORT_Assert(inCertReq != NULL);
  729. if (inCertReq == NULL) {
  730. return SECFailure;
  731. }
  732. if (CRMF_DoesRequestHaveField(inCertReq, crmfValidity)) {
  733. return crmf_copy_validity(destValidity,
  734. inCertReq->certTemplate.validity);
  735. }
  736. return SECFailure;
  737. }
  738. CRMFControl *
  739. CRMF_CertRequestGetControlAtIndex(CRMFCertRequest *inCertReq, int index)
  740. {
  741. CRMFControl *newControl, *srcControl;
  742. int numControls;
  743. SECStatus rv;
  744. PORT_Assert(inCertReq != NULL);
  745. if (inCertReq == NULL) {
  746. return NULL;
  747. }
  748. numControls = CRMF_CertRequestGetNumControls(inCertReq);
  749. if (index >= numControls || index < 0) {
  750. return NULL;
  751. }
  752. newControl = PORT_ZNew(CRMFControl);
  753. if (newControl == NULL) {
  754. return NULL;
  755. }
  756. srcControl = inCertReq->controls[index];
  757. newControl->tag = srcControl->tag;
  758. rv = SECITEM_CopyItem(NULL, &newControl->derTag, &srcControl->derTag);
  759. if (rv != SECSuccess) {
  760. goto loser;
  761. }
  762. rv = SECITEM_CopyItem(NULL, &newControl->derValue,
  763. &srcControl->derValue);
  764. if (rv != SECSuccess) {
  765. goto loser;
  766. }
  767. /* Copy over the PKIArchiveOptions stuff */
  768. switch (srcControl->tag) {
  769. case SEC_OID_PKIX_REGCTRL_REGTOKEN:
  770. case SEC_OID_PKIX_REGCTRL_AUTHENTICATOR:
  771. /* No further processing necessary for these types. */
  772. rv = SECSuccess;
  773. break;
  774. case SEC_OID_PKIX_REGCTRL_OLD_CERT_ID:
  775. case SEC_OID_PKIX_REGCTRL_PKIPUBINFO:
  776. case SEC_OID_PKIX_REGCTRL_PROTOCOL_ENC_KEY:
  777. /* These aren't supported yet, so no post-processing will
  778. * be done at this time. But we don't want to fail in case
  779. * we read in DER that has one of these options.
  780. */
  781. rv = SECSuccess;
  782. break;
  783. case SEC_OID_PKIX_REGCTRL_PKI_ARCH_OPTIONS:
  784. rv = crmf_copy_pkiarchiveoptions(NULL,
  785. &newControl->value.archiveOptions,
  786. &srcControl->value.archiveOptions);
  787. break;
  788. default:
  789. rv = SECFailure;
  790. }
  791. if (rv != SECSuccess) {
  792. goto loser;
  793. }
  794. return newControl;
  795. loser:
  796. CRMF_DestroyControl(newControl);
  797. return NULL;
  798. }
  799. static SECItem *
  800. crmf_copy_control_value(CRMFControl *inControl)
  801. {
  802. return SECITEM_DupItem(&inControl->derValue);
  803. }
  804. SECItem *
  805. CRMF_ControlGetAuthenticatorControlValue(CRMFControl *inControl)
  806. {
  807. PORT_Assert(inControl != NULL);
  808. if (inControl == NULL ||
  809. CRMF_ControlGetControlType(inControl) != crmfAuthenticatorControl) {
  810. return NULL;
  811. }
  812. return crmf_copy_control_value(inControl);
  813. }
  814. CRMFControlType
  815. CRMF_ControlGetControlType(CRMFControl *inControl)
  816. {
  817. CRMFControlType retType;
  818. PORT_Assert(inControl != NULL);
  819. switch (inControl->tag) {
  820. case SEC_OID_PKIX_REGCTRL_REGTOKEN:
  821. retType = crmfRegTokenControl;
  822. break;
  823. case SEC_OID_PKIX_REGCTRL_AUTHENTICATOR:
  824. retType = crmfAuthenticatorControl;
  825. break;
  826. case SEC_OID_PKIX_REGCTRL_PKIPUBINFO:
  827. retType = crmfPKIPublicationInfoControl;
  828. break;
  829. case SEC_OID_PKIX_REGCTRL_PKI_ARCH_OPTIONS:
  830. retType = crmfPKIArchiveOptionsControl;
  831. break;
  832. case SEC_OID_PKIX_REGCTRL_OLD_CERT_ID:
  833. retType = crmfOldCertIDControl;
  834. break;
  835. case SEC_OID_PKIX_REGCTRL_PROTOCOL_ENC_KEY:
  836. retType = crmfProtocolEncrKeyControl;
  837. break;
  838. default:
  839. retType = crmfNoControl;
  840. }
  841. return retType;
  842. }
  843. CRMFPKIArchiveOptions *
  844. CRMF_ControlGetPKIArchiveOptions(CRMFControl *inControl)
  845. {
  846. CRMFPKIArchiveOptions *newOpt = NULL;
  847. SECStatus rv;
  848. PORT_Assert(inControl != NULL);
  849. if (inControl == NULL ||
  850. CRMF_ControlGetControlType(inControl) != crmfPKIArchiveOptionsControl) {
  851. goto loser;
  852. }
  853. newOpt = PORT_ZNew(CRMFPKIArchiveOptions);
  854. if (newOpt == NULL) {
  855. goto loser;
  856. }
  857. rv = crmf_copy_pkiarchiveoptions(NULL, newOpt,
  858. &inControl->value.archiveOptions);
  859. if (rv != SECSuccess) {
  860. goto loser;
  861. }
  862. loser:
  863. if (newOpt != NULL) {
  864. CRMF_DestroyPKIArchiveOptions(newOpt);
  865. }
  866. return NULL;
  867. }
  868. SECItem *
  869. CRMF_ControlGetRegTokenControlValue(CRMFControl *inControl)
  870. {
  871. PORT_Assert(inControl != NULL);
  872. if (inControl == NULL ||
  873. CRMF_ControlGetControlType(inControl) != crmfRegTokenControl) {
  874. return NULL;
  875. }
  876. return crmf_copy_control_value(inControl);
  877. ;
  878. }
  879. CRMFCertExtension *
  880. CRMF_CertRequestGetExtensionAtIndex(CRMFCertRequest *inCertReq,
  881. int index)
  882. {
  883. int numExtensions;
  884. PORT_Assert(inCertReq != NULL);
  885. numExtensions = CRMF_CertRequestGetNumberOfExtensions(inCertReq);
  886. if (index >= numExtensions || index < 0) {
  887. return NULL;
  888. }
  889. return crmf_copy_cert_extension(NULL,
  890. inCertReq->certTemplate.extensions[index]);
  891. }