Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

460 lines
15 KiB

  1. /* Copyright (C) Microsoft Corporation, 1996-1999. All rights reserved. */
  2. /* ASN.1 definitions for SCHANNEL FORTEZZA and Private Key encoding */
  3. #include "spbase.h"
  4. #include "asn1enc.h"
  5. ASN1module_t ASN1ENC_Module = NULL;
  6. static int ASN1CALL ASN1Enc_FORTPublicKey(ASN1encoding_t enc, ASN1uint32_t tag, FORTPublicKey *val);
  7. static int ASN1CALL ASN1Enc_PrivateKeyInfo_attributes(ASN1encoding_t enc, ASN1uint32_t tag, PrivateKeyInfo_attributes *val);
  8. static int ASN1CALL ASN1Enc_AlgorithmIdentifier(ASN1encoding_t enc, ASN1uint32_t tag, AlgorithmIdentifier *val);
  9. static int ASN1CALL ASN1Enc_RSAPrivateKey(ASN1encoding_t enc, ASN1uint32_t tag, RSAPrivateKey *val);
  10. static int ASN1CALL ASN1Enc_PrivateKeyInfo(ASN1encoding_t enc, ASN1uint32_t tag, PrivateKeyInfo *val);
  11. static int ASN1CALL ASN1Enc_PrivateKeyData(ASN1encoding_t enc, ASN1uint32_t tag, PrivateKeyData *val);
  12. static int ASN1CALL ASN1Enc_PrivateKeyFile(ASN1encoding_t enc, ASN1uint32_t tag, PrivateKeyFile *val);
  13. static int ASN1CALL ASN1Enc_EnhancedKeyUsage(ASN1encoding_t enc, ASN1uint32_t tag, EnhancedKeyUsage *val);
  14. static int ASN1CALL ASN1Dec_FORTPublicKey(ASN1decoding_t dec, ASN1uint32_t tag, FORTPublicKey *val);
  15. static int ASN1CALL ASN1Dec_PrivateKeyInfo_attributes(ASN1decoding_t dec, ASN1uint32_t tag, PrivateKeyInfo_attributes *val);
  16. static int ASN1CALL ASN1Dec_AlgorithmIdentifier(ASN1decoding_t dec, ASN1uint32_t tag, AlgorithmIdentifier *val);
  17. static int ASN1CALL ASN1Dec_RSAPrivateKey(ASN1decoding_t dec, ASN1uint32_t tag, RSAPrivateKey *val);
  18. static int ASN1CALL ASN1Dec_PrivateKeyInfo(ASN1decoding_t dec, ASN1uint32_t tag, PrivateKeyInfo *val);
  19. static int ASN1CALL ASN1Dec_PrivateKeyData(ASN1decoding_t dec, ASN1uint32_t tag, PrivateKeyData *val);
  20. static int ASN1CALL ASN1Dec_PrivateKeyFile(ASN1decoding_t dec, ASN1uint32_t tag, PrivateKeyFile *val);
  21. static int ASN1CALL ASN1Dec_EnhancedKeyUsage(ASN1decoding_t dec, ASN1uint32_t tag, EnhancedKeyUsage *val);
  22. static void ASN1CALL ASN1Free_FORTPublicKey(FORTPublicKey *val);
  23. static void ASN1CALL ASN1Free_PrivateKeyInfo_attributes(PrivateKeyInfo_attributes *val);
  24. static void ASN1CALL ASN1Free_AlgorithmIdentifier(AlgorithmIdentifier *val);
  25. static void ASN1CALL ASN1Free_RSAPrivateKey(RSAPrivateKey *val);
  26. static void ASN1CALL ASN1Free_PrivateKeyInfo(PrivateKeyInfo *val);
  27. static void ASN1CALL ASN1Free_PrivateKeyData(PrivateKeyData *val);
  28. static void ASN1CALL ASN1Free_PrivateKeyFile(PrivateKeyFile *val);
  29. static void ASN1CALL ASN1Free_EnhancedKeyUsage(EnhancedKeyUsage *val);
  30. typedef ASN1BerEncFun_t ASN1EncFun_t;
  31. static const ASN1EncFun_t encfntab[5] = {
  32. (ASN1EncFun_t) ASN1Enc_FORTPublicKey,
  33. (ASN1EncFun_t) ASN1Enc_RSAPrivateKey,
  34. (ASN1EncFun_t) ASN1Enc_PrivateKeyInfo,
  35. (ASN1EncFun_t) ASN1Enc_PrivateKeyFile,
  36. (ASN1EncFun_t) ASN1Enc_EnhancedKeyUsage,
  37. };
  38. typedef ASN1BerDecFun_t ASN1DecFun_t;
  39. static const ASN1DecFun_t decfntab[5] = {
  40. (ASN1DecFun_t) ASN1Dec_FORTPublicKey,
  41. (ASN1DecFun_t) ASN1Dec_RSAPrivateKey,
  42. (ASN1DecFun_t) ASN1Dec_PrivateKeyInfo,
  43. (ASN1DecFun_t) ASN1Dec_PrivateKeyFile,
  44. (ASN1DecFun_t) ASN1Dec_EnhancedKeyUsage,
  45. };
  46. static const ASN1FreeFun_t freefntab[5] = {
  47. (ASN1FreeFun_t) ASN1Free_FORTPublicKey,
  48. (ASN1FreeFun_t) ASN1Free_RSAPrivateKey,
  49. (ASN1FreeFun_t) ASN1Free_PrivateKeyInfo,
  50. (ASN1FreeFun_t) ASN1Free_PrivateKeyFile,
  51. (ASN1FreeFun_t) ASN1Free_EnhancedKeyUsage,
  52. };
  53. static const ULONG sizetab[5] = {
  54. SIZE_ASN1ENC_Module_PDU_0,
  55. SIZE_ASN1ENC_Module_PDU_1,
  56. SIZE_ASN1ENC_Module_PDU_2,
  57. SIZE_ASN1ENC_Module_PDU_3,
  58. SIZE_ASN1ENC_Module_PDU_4,
  59. };
  60. /* forward declarations of values: */
  61. /* definitions of value components: */
  62. /* definitions of values: */
  63. void ASN1CALL ASN1ENC_Module_Startup(void)
  64. {
  65. ASN1ENC_Module = ASN1_CreateModule(0x10000, ASN1_BER_RULE_DER, ASN1FLAGS_NOASSERT, 5, (const ASN1GenericFun_t *) encfntab, (const ASN1GenericFun_t *) decfntab, freefntab, sizetab, 0x61686373);
  66. }
  67. void ASN1CALL ASN1ENC_Module_Cleanup(void)
  68. {
  69. ASN1_CloseModule(ASN1ENC_Module);
  70. ASN1ENC_Module = NULL;
  71. }
  72. static int ASN1CALL ASN1Enc_FORTPublicKey(ASN1encoding_t enc, ASN1uint32_t tag, FORTPublicKey *val)
  73. {
  74. if (!ASN1DEREncBitString(enc, tag ? tag : 0x3, (val)->length, (val)->value))
  75. return 0;
  76. return 1;
  77. }
  78. static int ASN1CALL ASN1Dec_FORTPublicKey(ASN1decoding_t dec, ASN1uint32_t tag, FORTPublicKey *val)
  79. {
  80. if (!ASN1BERDecBitString2(dec, tag ? tag : 0x3, val))
  81. return 0;
  82. return 1;
  83. }
  84. static void ASN1CALL ASN1Free_FORTPublicKey(FORTPublicKey *val)
  85. {
  86. if (val) {
  87. }
  88. }
  89. static int ASN1CALL ASN1Enc_PrivateKeyInfo_attributes(ASN1encoding_t enc, ASN1uint32_t tag, PrivateKeyInfo_attributes *val)
  90. {
  91. ASN1uint32_t nLenOff;
  92. void *pBlk;
  93. ASN1uint32_t i;
  94. ASN1encoding_t enc2;
  95. if (!ASN1BEREncExplicitTag(enc, tag ? tag : 0x80000000, &nLenOff))
  96. return 0;
  97. if (!ASN1DEREncBeginBlk(enc, ASN1_DER_SET_OF_BLOCK, &pBlk))
  98. return 0;
  99. for (i = 0; i < (val)->count; i++) {
  100. if (!ASN1DEREncNewBlkElement(pBlk, &enc2))
  101. return 0;
  102. if (!ASN1BEREncOpenType(enc2, &((val)->value)[i]))
  103. return 0;
  104. if (!ASN1DEREncFlushBlkElement(pBlk))
  105. return 0;
  106. }
  107. if (!ASN1DEREncEndBlk(pBlk))
  108. return 0;
  109. if (!ASN1BEREncEndOfContents(enc, nLenOff))
  110. return 0;
  111. return 1;
  112. }
  113. static int ASN1CALL ASN1Dec_PrivateKeyInfo_attributes(ASN1decoding_t dec, ASN1uint32_t tag, PrivateKeyInfo_attributes *val)
  114. {
  115. ASN1decoding_t dd;
  116. ASN1octet_t *di;
  117. ASN1uint32_t t;
  118. ASN1uint32_t n;
  119. if (!ASN1BERDecExplicitTag(dec, tag ? tag : 0x80000000, &dd, &di))
  120. return 0;
  121. (val)->count = n = 0;
  122. (val)->value = NULL;
  123. while (ASN1BERDecNotEndOfContents(dd, di)) {
  124. if (!ASN1BERDecPeekTag(dd, &t))
  125. return 0;
  126. if ((val)->count >= n) {
  127. n = n ? (n << 1) : 16;
  128. if (!((val)->value = (PrivateKeyInfo_attributes_Set *)ASN1DecRealloc(dd, (val)->value, n * sizeof(*(val)->value))))
  129. return 0;
  130. }
  131. if (!ASN1BERDecOpenType(dd, &((val)->value)[(val)->count]))
  132. return 0;
  133. ((val)->count)++;
  134. }
  135. if (!ASN1BERDecEndOfContents(dec, dd, di))
  136. return 0;
  137. return 1;
  138. }
  139. static void ASN1CALL ASN1Free_PrivateKeyInfo_attributes(PrivateKeyInfo_attributes *val)
  140. {
  141. ASN1uint32_t i;
  142. if (val) {
  143. ASN1open_free(&(val)->value[0]);
  144. for (i = 1; i < (val)->count; i++) {
  145. ASN1open_free(&(val)->value[i]);
  146. }
  147. ASN1Free((val)->value);
  148. }
  149. }
  150. static int ASN1CALL ASN1Enc_AlgorithmIdentifier(ASN1encoding_t enc, ASN1uint32_t tag, AlgorithmIdentifier *val)
  151. {
  152. ASN1uint32_t nLenOff;
  153. if (!ASN1BEREncExplicitTag(enc, tag ? tag : 0x10, &nLenOff))
  154. return 0;
  155. if (!ASN1BEREncObjectIdentifier2(enc, 0x6, &(val)->algorithm))
  156. return 0;
  157. if ((val)->o[0] & 0x80) {
  158. if (!ASN1BEREncOpenType(enc, &(val)->parameters))
  159. return 0;
  160. }
  161. if (!ASN1BEREncEndOfContents(enc, nLenOff))
  162. return 0;
  163. return 1;
  164. }
  165. static int ASN1CALL ASN1Dec_AlgorithmIdentifier(ASN1decoding_t dec, ASN1uint32_t tag, AlgorithmIdentifier *val)
  166. {
  167. ASN1decoding_t dd;
  168. ASN1octet_t *di;
  169. ASN1uint32_t t;
  170. if (!ASN1BERDecExplicitTag(dec, tag ? tag : 0x10, &dd, &di))
  171. return 0;
  172. ZeroMemory((val)->o, 1);
  173. if (!ASN1BERDecObjectIdentifier2(dd, 0x6, &(val)->algorithm))
  174. return 0;
  175. if (ASN1BERDecPeekTag(dd, &t)) {
  176. (val)->o[0] |= 0x80;
  177. if (!ASN1BERDecOpenType(dd, &(val)->parameters))
  178. return 0;
  179. }
  180. if (!ASN1BERDecEndOfContents(dec, dd, di))
  181. return 0;
  182. return 1;
  183. }
  184. static void ASN1CALL ASN1Free_AlgorithmIdentifier(AlgorithmIdentifier *val)
  185. {
  186. if (val) {
  187. if ((val)->o[0] & 0x80) {
  188. ASN1open_free(&(val)->parameters);
  189. }
  190. }
  191. }
  192. static int ASN1CALL ASN1Enc_RSAPrivateKey(ASN1encoding_t enc, ASN1uint32_t tag, RSAPrivateKey *val)
  193. {
  194. ASN1uint32_t nLenOff;
  195. if (!ASN1BEREncExplicitTag(enc, tag ? tag : 0x10, &nLenOff))
  196. return 0;
  197. if (!ASN1BEREncS32(enc, 0x2, (val)->version))
  198. return 0;
  199. if (!ASN1BEREncSX(enc, 0x2, &(val)->modulus))
  200. return 0;
  201. if (!ASN1BEREncS32(enc, 0x2, (val)->publicExponent))
  202. return 0;
  203. if (!ASN1BEREncSX(enc, 0x2, &(val)->privateExponent))
  204. return 0;
  205. if (!ASN1BEREncSX(enc, 0x2, &(val)->prime1))
  206. return 0;
  207. if (!ASN1BEREncSX(enc, 0x2, &(val)->prime2))
  208. return 0;
  209. if (!ASN1BEREncSX(enc, 0x2, &(val)->exponent1))
  210. return 0;
  211. if (!ASN1BEREncSX(enc, 0x2, &(val)->exponent2))
  212. return 0;
  213. if (!ASN1BEREncSX(enc, 0x2, &(val)->coefficient))
  214. return 0;
  215. if (!ASN1BEREncEndOfContents(enc, nLenOff))
  216. return 0;
  217. return 1;
  218. }
  219. static int ASN1CALL ASN1Dec_RSAPrivateKey(ASN1decoding_t dec, ASN1uint32_t tag, RSAPrivateKey *val)
  220. {
  221. ASN1decoding_t dd;
  222. ASN1octet_t *di;
  223. if (!ASN1BERDecExplicitTag(dec, tag ? tag : 0x10, &dd, &di))
  224. return 0;
  225. if (!ASN1BERDecS32Val(dd, 0x2, &(val)->version))
  226. return 0;
  227. if (!ASN1BERDecSXVal(dd, 0x2, &(val)->modulus))
  228. return 0;
  229. if (!ASN1BERDecS32Val(dd, 0x2, &(val)->publicExponent))
  230. return 0;
  231. if (!ASN1BERDecSXVal(dd, 0x2, &(val)->privateExponent))
  232. return 0;
  233. if (!ASN1BERDecSXVal(dd, 0x2, &(val)->prime1))
  234. return 0;
  235. if (!ASN1BERDecSXVal(dd, 0x2, &(val)->prime2))
  236. return 0;
  237. if (!ASN1BERDecSXVal(dd, 0x2, &(val)->exponent1))
  238. return 0;
  239. if (!ASN1BERDecSXVal(dd, 0x2, &(val)->exponent2))
  240. return 0;
  241. if (!ASN1BERDecSXVal(dd, 0x2, &(val)->coefficient))
  242. return 0;
  243. if (!ASN1BERDecEndOfContents(dec, dd, di))
  244. return 0;
  245. return 1;
  246. }
  247. static void ASN1CALL ASN1Free_RSAPrivateKey(RSAPrivateKey *val)
  248. {
  249. if (val) {
  250. ASN1intx_free(&(val)->modulus);
  251. ASN1intx_free(&(val)->privateExponent);
  252. ASN1intx_free(&(val)->prime1);
  253. ASN1intx_free(&(val)->prime2);
  254. ASN1intx_free(&(val)->exponent1);
  255. ASN1intx_free(&(val)->exponent2);
  256. ASN1intx_free(&(val)->coefficient);
  257. }
  258. }
  259. static int ASN1CALL ASN1Enc_PrivateKeyInfo(ASN1encoding_t enc, ASN1uint32_t tag, PrivateKeyInfo *val)
  260. {
  261. ASN1uint32_t nLenOff;
  262. if (!ASN1BEREncExplicitTag(enc, tag ? tag : 0x10, &nLenOff))
  263. return 0;
  264. if (!ASN1BEREncS32(enc, 0x2, (val)->version))
  265. return 0;
  266. if (!ASN1Enc_AlgorithmIdentifier(enc, 0, &(val)->privateKeyAlgorithm))
  267. return 0;
  268. if (!ASN1DEREncOctetString(enc, 0x4, ((val)->privateKey).length, ((val)->privateKey).value))
  269. return 0;
  270. if ((val)->o[0] & 0x80) {
  271. if (!ASN1Enc_PrivateKeyInfo_attributes(enc, 0, &(val)->attributes))
  272. return 0;
  273. }
  274. if (!ASN1BEREncEndOfContents(enc, nLenOff))
  275. return 0;
  276. return 1;
  277. }
  278. static int ASN1CALL ASN1Dec_PrivateKeyInfo(ASN1decoding_t dec, ASN1uint32_t tag, PrivateKeyInfo *val)
  279. {
  280. ASN1decoding_t dd;
  281. ASN1octet_t *di;
  282. ASN1uint32_t t;
  283. if (!ASN1BERDecExplicitTag(dec, tag ? tag : 0x10, &dd, &di))
  284. return 0;
  285. ZeroMemory((val)->o, 1);
  286. if (!ASN1BERDecS32Val(dd, 0x2, &(val)->version))
  287. return 0;
  288. if (!ASN1Dec_AlgorithmIdentifier(dd, 0, &(val)->privateKeyAlgorithm))
  289. return 0;
  290. if (!ASN1BERDecOctetString(dd, 0x4, &(val)->privateKey))
  291. return 0;
  292. ASN1BERDecPeekTag(dd, &t);
  293. if (t == 0x80000000) {
  294. (val)->o[0] |= 0x80;
  295. if (!ASN1Dec_PrivateKeyInfo_attributes(dd, 0, &(val)->attributes))
  296. return 0;
  297. }
  298. if (!ASN1BERDecEndOfContents(dec, dd, di))
  299. return 0;
  300. return 1;
  301. }
  302. static void ASN1CALL ASN1Free_PrivateKeyInfo(PrivateKeyInfo *val)
  303. {
  304. if (val) {
  305. ASN1Free_AlgorithmIdentifier(&(val)->privateKeyAlgorithm);
  306. ASN1octetstring_free(&(val)->privateKey);
  307. if ((val)->o[0] & 0x80) {
  308. ASN1Free_PrivateKeyInfo_attributes(&(val)->attributes);
  309. }
  310. }
  311. }
  312. static int ASN1CALL ASN1Enc_PrivateKeyData(ASN1encoding_t enc, ASN1uint32_t tag, PrivateKeyData *val)
  313. {
  314. ASN1uint32_t nLenOff;
  315. if (!ASN1BEREncExplicitTag(enc, tag ? tag : 0x10, &nLenOff))
  316. return 0;
  317. if (!ASN1Enc_AlgorithmIdentifier(enc, 0, &(val)->privateKeyAlgorithm))
  318. return 0;
  319. if (!ASN1DEREncOctetString(enc, 0x4, ((val)->privateKey).length, ((val)->privateKey).value))
  320. return 0;
  321. if (!ASN1BEREncEndOfContents(enc, nLenOff))
  322. return 0;
  323. return 1;
  324. }
  325. static int ASN1CALL ASN1Dec_PrivateKeyData(ASN1decoding_t dec, ASN1uint32_t tag, PrivateKeyData *val)
  326. {
  327. ASN1decoding_t dd;
  328. ASN1octet_t *di;
  329. if (!ASN1BERDecExplicitTag(dec, tag ? tag : 0x10, &dd, &di))
  330. return 0;
  331. if (!ASN1Dec_AlgorithmIdentifier(dd, 0, &(val)->privateKeyAlgorithm))
  332. return 0;
  333. if (!ASN1BERDecOctetString(dd, 0x4, &(val)->privateKey))
  334. return 0;
  335. if (!ASN1BERDecEndOfContents(dec, dd, di))
  336. return 0;
  337. return 1;
  338. }
  339. static void ASN1CALL ASN1Free_PrivateKeyData(PrivateKeyData *val)
  340. {
  341. if (val) {
  342. ASN1Free_AlgorithmIdentifier(&(val)->privateKeyAlgorithm);
  343. ASN1octetstring_free(&(val)->privateKey);
  344. }
  345. }
  346. static int ASN1CALL ASN1Enc_PrivateKeyFile(ASN1encoding_t enc, ASN1uint32_t tag, PrivateKeyFile *val)
  347. {
  348. ASN1uint32_t nLenOff;
  349. if (!ASN1BEREncExplicitTag(enc, tag ? tag : 0x10, &nLenOff))
  350. return 0;
  351. if (!ASN1DEREncOctetString(enc, 0x4, ((val)->name).length, ((val)->name).value))
  352. return 0;
  353. if (!ASN1Enc_PrivateKeyData(enc, 0, &(val)->privateKey))
  354. return 0;
  355. if (!ASN1BEREncEndOfContents(enc, nLenOff))
  356. return 0;
  357. return 1;
  358. }
  359. static int ASN1CALL ASN1Dec_PrivateKeyFile(ASN1decoding_t dec, ASN1uint32_t tag, PrivateKeyFile *val)
  360. {
  361. ASN1decoding_t dd;
  362. ASN1octet_t *di;
  363. if (!ASN1BERDecExplicitTag(dec, tag ? tag : 0x10, &dd, &di))
  364. return 0;
  365. if (!ASN1BERDecOctetString(dd, 0x4, &(val)->name))
  366. return 0;
  367. if (!ASN1Dec_PrivateKeyData(dd, 0, &(val)->privateKey))
  368. return 0;
  369. if (!ASN1BERDecEndOfContents(dec, dd, di))
  370. return 0;
  371. return 1;
  372. }
  373. static void ASN1CALL ASN1Free_PrivateKeyFile(PrivateKeyFile *val)
  374. {
  375. if (val) {
  376. ASN1octetstring_free(&(val)->name);
  377. ASN1Free_PrivateKeyData(&(val)->privateKey);
  378. }
  379. }
  380. static int ASN1CALL ASN1Enc_EnhancedKeyUsage(ASN1encoding_t enc, ASN1uint32_t tag, EnhancedKeyUsage *val)
  381. {
  382. ASN1uint32_t nLenOff;
  383. ASN1uint32_t i;
  384. if (!ASN1BEREncExplicitTag(enc, tag ? tag : 0x10, &nLenOff))
  385. return 0;
  386. for (i = 0; i < (val)->count; i++) {
  387. if (!ASN1BEREncObjectIdentifier2(enc, 0x6, &((val)->value)[i]))
  388. return 0;
  389. }
  390. if (!ASN1BEREncEndOfContents(enc, nLenOff))
  391. return 0;
  392. return 1;
  393. }
  394. static int ASN1CALL ASN1Dec_EnhancedKeyUsage(ASN1decoding_t dec, ASN1uint32_t tag, EnhancedKeyUsage *val)
  395. {
  396. ASN1decoding_t dd;
  397. ASN1octet_t *di;
  398. ASN1uint32_t t;
  399. ASN1uint32_t n;
  400. if (!ASN1BERDecExplicitTag(dec, tag ? tag : 0x10, &dd, &di))
  401. return 0;
  402. (val)->count = n = 0;
  403. (val)->value = NULL;
  404. while (ASN1BERDecNotEndOfContents(dd, di)) {
  405. if (!ASN1BERDecPeekTag(dd, &t))
  406. return 0;
  407. if ((val)->count >= n) {
  408. n = n ? (n << 1) : 16;
  409. if (!((val)->value = (UsageIdentifier *)ASN1DecRealloc(dd, (val)->value, n * sizeof(*(val)->value))))
  410. return 0;
  411. }
  412. if (!ASN1BERDecObjectIdentifier2(dd, 0x6, &((val)->value)[(val)->count]))
  413. return 0;
  414. ((val)->count)++;
  415. }
  416. if (!ASN1BERDecEndOfContents(dec, dd, di))
  417. return 0;
  418. return 1;
  419. }
  420. static void ASN1CALL ASN1Free_EnhancedKeyUsage(EnhancedKeyUsage *val)
  421. {
  422. ASN1uint32_t i;
  423. if (val) {
  424. for (i = 1; i < (val)->count; i++) {
  425. }
  426. ASN1Free((val)->value);
  427. }
  428. }