/security/nss/lib/pkcs12/p12creat.c

http://github.com/zpao/v8monkey · C · 254 lines · 162 code · 33 blank · 59 comment · 36 complexity · 28f93ddb508bdd4b0d31d3b1821e17ec MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is the Netscape security libraries.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Netscape Communications Corporation.
  18. * Portions created by the Initial Developer are Copyright (C) 1994-2000
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of
  24. * either the GNU General Public License Version 2 or later (the "GPL"), or
  25. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. * in which case the provisions of the GPL or the LGPL are applicable instead
  27. * of those above. If you wish to allow use of your version of this file only
  28. * under the terms of either the GPL or the LGPL, and not to allow others to
  29. * use your version of this file under the terms of the MPL, indicate your
  30. * decision by deleting the provisions above and replace them with the notice
  31. * and other provisions required by the GPL or the LGPL. If you do not delete
  32. * the provisions above, a recipient may use your version of this file under
  33. * the terms of any one of the MPL, the GPL or the LGPL.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. #include "pkcs12.h"
  37. #include "secitem.h"
  38. #include "secport.h"
  39. #include "secder.h"
  40. #include "secoid.h"
  41. #include "p12local.h"
  42. #include "secerr.h"
  43. /* allocate space for a PFX structure and set up initial
  44. * arena pool. pfx structure is cleared and a pointer to
  45. * the new structure is returned.
  46. */
  47. SEC_PKCS12PFXItem *
  48. sec_pkcs12_new_pfx(void)
  49. {
  50. SEC_PKCS12PFXItem *pfx = NULL;
  51. PRArenaPool *poolp = NULL;
  52. poolp = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE); /* XXX Different size? */
  53. if(poolp == NULL)
  54. goto loser;
  55. pfx = (SEC_PKCS12PFXItem *)PORT_ArenaZAlloc(poolp,
  56. sizeof(SEC_PKCS12PFXItem));
  57. if(pfx == NULL)
  58. goto loser;
  59. pfx->poolp = poolp;
  60. return pfx;
  61. loser:
  62. PORT_FreeArena(poolp, PR_TRUE);
  63. return NULL;
  64. }
  65. /* allocate space for a PFX structure and set up initial
  66. * arena pool. pfx structure is cleared and a pointer to
  67. * the new structure is returned.
  68. */
  69. SEC_PKCS12AuthenticatedSafe *
  70. sec_pkcs12_new_asafe(PRArenaPool *poolp)
  71. {
  72. SEC_PKCS12AuthenticatedSafe *asafe = NULL;
  73. void *mark;
  74. mark = PORT_ArenaMark(poolp);
  75. asafe = (SEC_PKCS12AuthenticatedSafe *)PORT_ArenaZAlloc(poolp,
  76. sizeof(SEC_PKCS12AuthenticatedSafe));
  77. if(asafe == NULL)
  78. goto loser;
  79. asafe->poolp = poolp;
  80. PORT_Memset(&asafe->old_baggage, 0, sizeof(SEC_PKCS7ContentInfo));
  81. PORT_ArenaUnmark(poolp, mark);
  82. return asafe;
  83. loser:
  84. PORT_ArenaRelease(poolp, mark);
  85. return NULL;
  86. }
  87. /* create a safe contents structure with a list of
  88. * length 0 with the first element being NULL
  89. */
  90. SEC_PKCS12SafeContents *
  91. sec_pkcs12_create_safe_contents(PRArenaPool *poolp)
  92. {
  93. SEC_PKCS12SafeContents *safe;
  94. void *mark;
  95. if(poolp == NULL)
  96. return NULL;
  97. /* allocate structure */
  98. mark = PORT_ArenaMark(poolp);
  99. safe = (SEC_PKCS12SafeContents *)PORT_ArenaZAlloc(poolp,
  100. sizeof(SEC_PKCS12SafeContents));
  101. if(safe == NULL)
  102. {
  103. PORT_SetError(SEC_ERROR_NO_MEMORY);
  104. PORT_ArenaRelease(poolp, mark);
  105. return NULL;
  106. }
  107. /* init list */
  108. safe->contents = (SEC_PKCS12SafeBag**)PORT_ArenaZAlloc(poolp,
  109. sizeof(SEC_PKCS12SafeBag *));
  110. if(safe->contents == NULL) {
  111. PORT_SetError(SEC_ERROR_NO_MEMORY);
  112. PORT_ArenaRelease(poolp, mark);
  113. return NULL;
  114. }
  115. safe->contents[0] = NULL;
  116. safe->poolp = poolp;
  117. safe->safe_size = 0;
  118. PORT_ArenaUnmark(poolp, mark);
  119. return safe;
  120. }
  121. /* create a new external bag which is appended onto the list
  122. * of bags in baggage. the bag is created in the same arena
  123. * as baggage
  124. */
  125. SEC_PKCS12BaggageItem *
  126. sec_pkcs12_create_external_bag(SEC_PKCS12Baggage *luggage)
  127. {
  128. void *dummy, *mark;
  129. SEC_PKCS12BaggageItem *bag;
  130. if(luggage == NULL) {
  131. return NULL;
  132. }
  133. mark = PORT_ArenaMark(luggage->poolp);
  134. /* allocate space for null terminated bag list */
  135. if(luggage->bags == NULL) {
  136. luggage->bags=(SEC_PKCS12BaggageItem**)PORT_ArenaZAlloc(luggage->poolp,
  137. sizeof(SEC_PKCS12BaggageItem *));
  138. if(luggage->bags == NULL) {
  139. goto loser;
  140. }
  141. luggage->luggage_size = 0;
  142. }
  143. /* grow the list */
  144. dummy = PORT_ArenaGrow(luggage->poolp, luggage->bags,
  145. sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 1),
  146. sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 2));
  147. if(dummy == NULL) {
  148. goto loser;
  149. }
  150. luggage->bags = (SEC_PKCS12BaggageItem**)dummy;
  151. luggage->bags[luggage->luggage_size] =
  152. (SEC_PKCS12BaggageItem *)PORT_ArenaZAlloc(luggage->poolp,
  153. sizeof(SEC_PKCS12BaggageItem));
  154. if(luggage->bags[luggage->luggage_size] == NULL) {
  155. goto loser;
  156. }
  157. /* create new bag and append it to the end */
  158. bag = luggage->bags[luggage->luggage_size];
  159. bag->espvks = (SEC_PKCS12ESPVKItem **)PORT_ArenaZAlloc(
  160. luggage->poolp,
  161. sizeof(SEC_PKCS12ESPVKItem *));
  162. bag->unencSecrets = (SEC_PKCS12SafeBag **)PORT_ArenaZAlloc(
  163. luggage->poolp,
  164. sizeof(SEC_PKCS12SafeBag *));
  165. if((bag->espvks == NULL) || (bag->unencSecrets == NULL)) {
  166. goto loser;
  167. }
  168. bag->poolp = luggage->poolp;
  169. luggage->luggage_size++;
  170. luggage->bags[luggage->luggage_size] = NULL;
  171. bag->espvks[0] = NULL;
  172. bag->unencSecrets[0] = NULL;
  173. bag->nEspvks = bag->nSecrets = 0;
  174. PORT_ArenaUnmark(luggage->poolp, mark);
  175. return bag;
  176. loser:
  177. PORT_ArenaRelease(luggage->poolp, mark);
  178. PORT_SetError(SEC_ERROR_NO_MEMORY);
  179. return NULL;
  180. }
  181. /* creates a baggage witha NULL terminated 0 length list */
  182. SEC_PKCS12Baggage *
  183. sec_pkcs12_create_baggage(PRArenaPool *poolp)
  184. {
  185. SEC_PKCS12Baggage *luggage;
  186. void *mark;
  187. if(poolp == NULL)
  188. return NULL;
  189. mark = PORT_ArenaMark(poolp);
  190. /* allocate bag */
  191. luggage = (SEC_PKCS12Baggage *)PORT_ArenaZAlloc(poolp,
  192. sizeof(SEC_PKCS12Baggage));
  193. if(luggage == NULL)
  194. {
  195. PORT_SetError(SEC_ERROR_NO_MEMORY);
  196. PORT_ArenaRelease(poolp, mark);
  197. return NULL;
  198. }
  199. /* init list */
  200. luggage->bags = (SEC_PKCS12BaggageItem **)PORT_ArenaZAlloc(poolp,
  201. sizeof(SEC_PKCS12BaggageItem *));
  202. if(luggage->bags == NULL) {
  203. PORT_SetError(SEC_ERROR_NO_MEMORY);
  204. PORT_ArenaRelease(poolp, mark);
  205. return NULL;
  206. }
  207. luggage->bags[0] = NULL;
  208. luggage->luggage_size = 0;
  209. luggage->poolp = poolp;
  210. PORT_ArenaUnmark(poolp, mark);
  211. return luggage;
  212. }
  213. /* free pfx structure and associated items in the arena */
  214. void
  215. SEC_PKCS12DestroyPFX(SEC_PKCS12PFXItem *pfx)
  216. {
  217. if (pfx != NULL && pfx->poolp != NULL)
  218. {
  219. PORT_FreeArena(pfx->poolp, PR_TRUE);
  220. }
  221. }