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.

177 lines
4.6 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 1995 - 1999
  5. //
  6. // File: pkioss.cpp
  7. //
  8. // Contents: PKI OSS support functions.
  9. //
  10. // Functions: PkiOssEncode
  11. // PkiOssEncode2
  12. // PkiAsn1Decode
  13. // PkiAsn1Decode2
  14. //
  15. // History: 23-Oct-98 philh created
  16. //--------------------------------------------------------------------------
  17. #include "global.hxx"
  18. #include <msasn1.h>
  19. #include <dbgdef.h>
  20. //+-------------------------------------------------------------------------
  21. // OSS Encode function. The encoded output is allocated and must be freed
  22. // by calling ossFreeBuf
  23. //--------------------------------------------------------------------------
  24. int
  25. WINAPI
  26. PkiOssEncode(
  27. IN OssGlobal *Pog,
  28. IN void *pvOssInfo,
  29. IN int id,
  30. OUT BYTE **ppbEncoded,
  31. OUT DWORD *pcbEncoded
  32. )
  33. {
  34. int iStatus;
  35. OssBuf ossBuf;
  36. ossBuf.length = 0;
  37. ossBuf.value = NULL;
  38. iStatus = ossEncode(Pog, id, pvOssInfo, &ossBuf);
  39. if (0 == iStatus) {
  40. *ppbEncoded = ossBuf.value;
  41. *pcbEncoded = ossBuf.length;
  42. } else {
  43. *ppbEncoded = NULL;
  44. *pcbEncoded = 0;
  45. }
  46. return iStatus;
  47. }
  48. //+-------------------------------------------------------------------------
  49. // OSS Encode function. The encoded output isn't allocated.
  50. //
  51. // If pbEncoded is NULL, does a length only calculation.
  52. //--------------------------------------------------------------------------
  53. int
  54. WINAPI
  55. PkiOssEncode2(
  56. IN OssGlobal *Pog,
  57. IN void *pvOssInfo,
  58. IN int id,
  59. OUT OPTIONAL BYTE *pbEncoded,
  60. IN OUT DWORD *pcbEncoded
  61. )
  62. {
  63. int iStatus;
  64. OssBuf ossBuf;
  65. DWORD cbEncoded;
  66. if (NULL == pbEncoded)
  67. cbEncoded = 0;
  68. else
  69. cbEncoded = *pcbEncoded;
  70. if (0 == cbEncoded) {
  71. // Length only calculation
  72. ossBuf.length = 0;
  73. ossBuf.value = NULL;
  74. iStatus = ossEncode(Pog, id, pvOssInfo, &ossBuf);
  75. if (0 == iStatus) {
  76. if (pbEncoded)
  77. iStatus = (int) ASN1_ERR_OVERFLOW;
  78. cbEncoded = ossBuf.length;
  79. if (ossBuf.value)
  80. ossFreeBuf(Pog, ossBuf.value);
  81. }
  82. } else {
  83. ossBuf.length = cbEncoded;
  84. ossBuf.value = pbEncoded;
  85. iStatus = ossEncode(Pog, id, pvOssInfo, &ossBuf);
  86. if (0 == iStatus)
  87. cbEncoded = ossBuf.length;
  88. else if (MORE_BUF == iStatus) {
  89. // Re-do as length only calculation
  90. iStatus = PkiOssEncode2(
  91. Pog,
  92. pvOssInfo,
  93. id,
  94. NULL, // pbEncoded
  95. &cbEncoded
  96. );
  97. if (0 == iStatus)
  98. iStatus = (int) ASN1_ERR_OVERFLOW;
  99. } else
  100. cbEncoded = 0;
  101. }
  102. *pcbEncoded = cbEncoded;
  103. return iStatus;
  104. }
  105. //+-------------------------------------------------------------------------
  106. // OSS Decode function. The allocated, decoded structure, **pvOssInfo, must
  107. // be freed by calling ossFreePDU().
  108. //--------------------------------------------------------------------------
  109. int
  110. WINAPI
  111. PkiOssDecode(
  112. IN OssGlobal *Pog,
  113. OUT void **ppvOssInfo,
  114. IN int id,
  115. IN const BYTE *pbEncoded,
  116. IN DWORD cbEncoded
  117. )
  118. {
  119. int iStatus;
  120. OssBuf ossBuf;
  121. int pdunum = id;
  122. ossBuf.length = cbEncoded;
  123. ossBuf.value = (BYTE *) pbEncoded;
  124. *ppvOssInfo = NULL;
  125. iStatus = ossDecode(Pog, &pdunum, &ossBuf, ppvOssInfo);
  126. return iStatus;
  127. }
  128. //+-------------------------------------------------------------------------
  129. // OSS Decode function. The allocated, decoded structure, **pvOssInfo, must
  130. // be freed by calling ossFreePDU().
  131. //
  132. // For a successful decode, *ppbEncoded is advanced
  133. // past the decoded bytes and *pcbDecoded is decremented by the number
  134. // of decoded bytes.
  135. //--------------------------------------------------------------------------
  136. int
  137. WINAPI
  138. PkiOssDecode2(
  139. IN OssGlobal *Pog,
  140. OUT void **ppvOssInfo,
  141. IN int id,
  142. IN OUT BYTE **ppbEncoded,
  143. IN OUT DWORD *pcbEncoded
  144. )
  145. {
  146. int iStatus;
  147. OssBuf ossBuf;
  148. int pdunum = id;
  149. ossBuf.length = *pcbEncoded;
  150. ossBuf.value = *ppbEncoded;
  151. *ppvOssInfo = NULL;
  152. iStatus = ossDecode(Pog, &pdunum, &ossBuf, ppvOssInfo);
  153. if (0 == iStatus) {
  154. *ppbEncoded = ossBuf.value;
  155. *pcbEncoded = ossBuf.length;
  156. } else if (MORE_INPUT == iStatus)
  157. iStatus = (int) ASN1_ERR_EOD;
  158. return iStatus;
  159. }