/security/nss/lib/pkcs12/p12tmpl.c

http://github.com/zpao/v8monkey · C · 323 lines · 243 code · 45 blank · 35 comment · 22 complexity · dc9836abc084a911e03ee6857f419279 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 "plarena.h"
  37. #include "secitem.h"
  38. #include "secoid.h"
  39. #include "seccomon.h"
  40. #include "secport.h"
  41. #include "cert.h"
  42. #include "secpkcs7.h"
  43. #include "secasn1.h"
  44. #include "p12t.h"
  45. SEC_ASN1_MKSUB(SEC_AnyTemplate)
  46. SEC_ASN1_MKSUB(sgn_DigestInfoTemplate)
  47. static const SEC_ASN1Template *
  48. sec_pkcs12_choose_safe_bag_type(void *src_or_dest, PRBool encoding)
  49. {
  50. const SEC_ASN1Template *theTemplate;
  51. sec_PKCS12SafeBag *safeBag;
  52. SECOidData *oiddata;
  53. if (src_or_dest == NULL) {
  54. return NULL;
  55. }
  56. safeBag = (sec_PKCS12SafeBag*)src_or_dest;
  57. oiddata = SECOID_FindOID(&safeBag->safeBagType);
  58. if(oiddata == NULL) {
  59. return SEC_ASN1_GET(SEC_AnyTemplate);
  60. }
  61. switch (oiddata->offset) {
  62. default:
  63. theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
  64. break;
  65. case SEC_OID_PKCS12_V1_KEY_BAG_ID:
  66. theTemplate = SEC_ASN1_GET(SECKEY_PointerToPrivateKeyInfoTemplate);
  67. break;
  68. case SEC_OID_PKCS12_V1_CERT_BAG_ID:
  69. theTemplate = sec_PKCS12PointerToCertBagTemplate;
  70. break;
  71. case SEC_OID_PKCS12_V1_CRL_BAG_ID:
  72. theTemplate = sec_PKCS12PointerToCRLBagTemplate;
  73. break;
  74. case SEC_OID_PKCS12_V1_SECRET_BAG_ID:
  75. theTemplate = sec_PKCS12PointerToSecretBagTemplate;
  76. break;
  77. case SEC_OID_PKCS12_V1_PKCS8_SHROUDED_KEY_BAG_ID:
  78. theTemplate =
  79. SEC_ASN1_GET(SECKEY_PointerToEncryptedPrivateKeyInfoTemplate);
  80. break;
  81. case SEC_OID_PKCS12_V1_SAFE_CONTENTS_BAG_ID:
  82. if(encoding) {
  83. theTemplate = sec_PKCS12PointerToSafeContentsTemplate;
  84. } else {
  85. theTemplate = SEC_ASN1_GET(SEC_PointerToAnyTemplate);
  86. }
  87. break;
  88. }
  89. return theTemplate;
  90. }
  91. static const SEC_ASN1Template *
  92. sec_pkcs12_choose_crl_bag_type(void *src_or_dest, PRBool encoding)
  93. {
  94. const SEC_ASN1Template *theTemplate;
  95. sec_PKCS12CRLBag *crlbag;
  96. SECOidData *oiddata;
  97. if (src_or_dest == NULL) {
  98. return NULL;
  99. }
  100. crlbag = (sec_PKCS12CRLBag*)src_or_dest;
  101. oiddata = SECOID_FindOID(&crlbag->bagID);
  102. if(oiddata == NULL) {
  103. return SEC_ASN1_GET(SEC_AnyTemplate);
  104. }
  105. switch (oiddata->offset) {
  106. default:
  107. theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
  108. break;
  109. case SEC_OID_PKCS9_X509_CRL:
  110. theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate);
  111. break;
  112. }
  113. return theTemplate;
  114. }
  115. static const SEC_ASN1Template *
  116. sec_pkcs12_choose_cert_bag_type(void *src_or_dest, PRBool encoding)
  117. {
  118. const SEC_ASN1Template *theTemplate;
  119. sec_PKCS12CertBag *certbag;
  120. SECOidData *oiddata;
  121. if (src_or_dest == NULL) {
  122. return NULL;
  123. }
  124. certbag = (sec_PKCS12CertBag*)src_or_dest;
  125. oiddata = SECOID_FindOID(&certbag->bagID);
  126. if(oiddata == NULL) {
  127. return SEC_ASN1_GET(SEC_AnyTemplate);
  128. }
  129. switch (oiddata->offset) {
  130. default:
  131. theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
  132. break;
  133. case SEC_OID_PKCS9_X509_CERT:
  134. theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate);
  135. break;
  136. case SEC_OID_PKCS9_SDSI_CERT:
  137. theTemplate = SEC_ASN1_GET(SEC_IA5StringTemplate);
  138. break;
  139. }
  140. return theTemplate;
  141. }
  142. static const SEC_ASN1Template *
  143. sec_pkcs12_choose_attr_type(void *src_or_dest, PRBool encoding)
  144. {
  145. const SEC_ASN1Template *theTemplate;
  146. sec_PKCS12Attribute *attr;
  147. SECOidData *oiddata;
  148. if (src_or_dest == NULL) {
  149. return NULL;
  150. }
  151. attr = (sec_PKCS12Attribute*)src_or_dest;
  152. oiddata = SECOID_FindOID(&attr->attrType);
  153. if(oiddata == NULL) {
  154. return SEC_ASN1_GET(SEC_AnyTemplate);
  155. }
  156. switch (oiddata->offset) {
  157. default:
  158. theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
  159. break;
  160. case SEC_OID_PKCS9_FRIENDLY_NAME:
  161. theTemplate = SEC_ASN1_GET(SEC_BMPStringTemplate);
  162. break;
  163. case SEC_OID_PKCS9_LOCAL_KEY_ID:
  164. theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate);
  165. break;
  166. case SEC_OID_PKCS12_KEY_USAGE:
  167. theTemplate = SEC_ASN1_GET(SEC_BitStringTemplate);
  168. break;
  169. }
  170. return theTemplate;
  171. }
  172. const SEC_ASN1Template sec_PKCS12PointerToContentInfoTemplate[] = {
  173. { SEC_ASN1_POINTER | SEC_ASN1_MAY_STREAM, 0, sec_PKCS7ContentInfoTemplate }
  174. };
  175. static const SEC_ASN1TemplateChooserPtr sec_pkcs12_crl_bag_chooser =
  176. sec_pkcs12_choose_crl_bag_type;
  177. static const SEC_ASN1TemplateChooserPtr sec_pkcs12_cert_bag_chooser =
  178. sec_pkcs12_choose_cert_bag_type;
  179. static const SEC_ASN1TemplateChooserPtr sec_pkcs12_safe_bag_chooser =
  180. sec_pkcs12_choose_safe_bag_type;
  181. static const SEC_ASN1TemplateChooserPtr sec_pkcs12_attr_chooser =
  182. sec_pkcs12_choose_attr_type;
  183. const SEC_ASN1Template sec_PKCS12PointerToCertBagTemplate[] = {
  184. { SEC_ASN1_POINTER, 0, sec_PKCS12CertBagTemplate }
  185. };
  186. const SEC_ASN1Template sec_PKCS12PointerToCRLBagTemplate[] = {
  187. { SEC_ASN1_POINTER, 0, sec_PKCS12CRLBagTemplate }
  188. };
  189. const SEC_ASN1Template sec_PKCS12PointerToSecretBagTemplate[] = {
  190. { SEC_ASN1_POINTER, 0, sec_PKCS12SecretBagTemplate }
  191. };
  192. const SEC_ASN1Template sec_PKCS12PointerToSafeContentsTemplate[] = {
  193. { SEC_ASN1_POINTER, 0, sec_PKCS12SafeContentsTemplate }
  194. };
  195. const SEC_ASN1Template sec_PKCS12PFXItemTemplate[] = {
  196. { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM, 0, NULL,
  197. sizeof(sec_PKCS12PFXItem) },
  198. { SEC_ASN1_OPTIONAL | SEC_ASN1_INTEGER,
  199. offsetof(sec_PKCS12PFXItem, version) },
  200. { SEC_ASN1_ANY | SEC_ASN1_MAY_STREAM,
  201. offsetof(sec_PKCS12PFXItem, encodedAuthSafe) },
  202. { SEC_ASN1_ANY | SEC_ASN1_MAY_STREAM,
  203. offsetof(sec_PKCS12PFXItem, encodedMacData) },
  204. { 0 }
  205. };
  206. const SEC_ASN1Template sec_PKCS12MacDataTemplate[] = {
  207. { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12MacData) },
  208. { SEC_ASN1_INLINE | SEC_ASN1_XTRN , offsetof(sec_PKCS12MacData, safeMac),
  209. SEC_ASN1_SUB(sgn_DigestInfoTemplate) },
  210. { SEC_ASN1_OCTET_STRING, offsetof(sec_PKCS12MacData, macSalt) },
  211. { SEC_ASN1_OPTIONAL | SEC_ASN1_INTEGER, offsetof(sec_PKCS12MacData, iter) },
  212. { 0 }
  213. };
  214. const SEC_ASN1Template sec_PKCS12AuthenticatedSafeTemplate[] = {
  215. { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN ,
  216. offsetof(sec_PKCS12AuthenticatedSafe, encodedSafes),
  217. SEC_ASN1_SUB(SEC_AnyTemplate) }
  218. };
  219. const SEC_ASN1Template sec_PKCS12SafeBagTemplate[] = {
  220. { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM, 0, NULL,
  221. sizeof(sec_PKCS12SafeBag) },
  222. { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12SafeBag, safeBagType) },
  223. { SEC_ASN1_EXPLICIT | SEC_ASN1_DYNAMIC | SEC_ASN1_CONSTRUCTED |
  224. SEC_ASN1_MAY_STREAM | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  225. offsetof(sec_PKCS12SafeBag, safeBagContent),
  226. &sec_pkcs12_safe_bag_chooser },
  227. { SEC_ASN1_SET_OF | SEC_ASN1_OPTIONAL, offsetof(sec_PKCS12SafeBag, attribs),
  228. sec_PKCS12AttributeTemplate },
  229. { 0 }
  230. };
  231. const SEC_ASN1Template sec_PKCS12SafeContentsTemplate[] = {
  232. { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM,
  233. offsetof(sec_PKCS12SafeContents, safeBags),
  234. sec_PKCS12SafeBagTemplate }
  235. };
  236. const SEC_ASN1Template sec_PKCS12SequenceOfAnyTemplate[] = {
  237. { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN , 0,
  238. SEC_ASN1_SUB(SEC_AnyTemplate) }
  239. };
  240. const SEC_ASN1Template sec_PKCS12NestedSafeContentsDecodeTemplate[] = {
  241. { SEC_ASN1_EXPLICIT | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 0,
  242. offsetof(sec_PKCS12SafeContents, encodedSafeBags),
  243. sec_PKCS12SequenceOfAnyTemplate }
  244. };
  245. const SEC_ASN1Template sec_PKCS12SafeContentsDecodeTemplate[] = {
  246. { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN ,
  247. offsetof(sec_PKCS12SafeContents, encodedSafeBags),
  248. SEC_ASN1_SUB(SEC_AnyTemplate) }
  249. };
  250. const SEC_ASN1Template sec_PKCS12CRLBagTemplate[] = {
  251. { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12CRLBag) },
  252. { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12CRLBag, bagID) },
  253. { SEC_ASN1_DYNAMIC | SEC_ASN1_POINTER,
  254. offsetof(sec_PKCS12CRLBag, value), &sec_pkcs12_crl_bag_chooser },
  255. { 0 }
  256. };
  257. const SEC_ASN1Template sec_PKCS12CertBagTemplate[] = {
  258. { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12CertBag) },
  259. { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12CertBag, bagID) },
  260. { SEC_ASN1_DYNAMIC | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED |
  261. SEC_ASN1_CONTEXT_SPECIFIC | 0,
  262. offsetof(sec_PKCS12CertBag, value), &sec_pkcs12_cert_bag_chooser },
  263. { 0 }
  264. };
  265. const SEC_ASN1Template sec_PKCS12SecretBagTemplate[] = {
  266. { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12SecretBag) },
  267. { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12SecretBag, secretType) },
  268. { SEC_ASN1_ANY, offsetof(sec_PKCS12SecretBag, secretContent) },
  269. { 0 }
  270. };
  271. const SEC_ASN1Template sec_PKCS12AttributeTemplate[] = {
  272. { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12Attribute) },
  273. { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12Attribute, attrType) },
  274. { SEC_ASN1_SET_OF | SEC_ASN1_DYNAMIC,
  275. offsetof(sec_PKCS12Attribute, attrValue),
  276. &sec_pkcs12_attr_chooser },
  277. { 0 }
  278. };