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.

176 lines
5.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: msgasn1.cpp
  8. //
  9. // Contents: Conversion APIs to/from ASN.1 data structures
  10. //
  11. // Functions: ICM_Asn1ToAttribute
  12. // ICM_Asn1ToAlgorithmIdentifier
  13. // ICM_Asn1FromAlgorithmIdentifier
  14. //
  15. // History: 16-Apr-96 kevinr created
  16. //
  17. //--------------------------------------------------------------------------
  18. #include "global.hxx"
  19. #include <dbgdef.h>
  20. void *ICM_Alloc( IN size_t cbBytes);
  21. void ICM_Free( IN void *pv);
  22. //+-------------------------------------------------------------------------
  23. // Convert an CRYPT_ATTRIBUTE to an ASN1 Attribute
  24. //
  25. // Returns FALSE iff conversion failed.
  26. //--------------------------------------------------------------------------
  27. BOOL
  28. WINAPI
  29. ICM_Asn1ToAttribute(
  30. IN PCRYPT_ATTRIBUTE patr,
  31. OUT Attribute *poatr)
  32. {
  33. BOOL fRet;
  34. DWORD i;
  35. Any *pAny;
  36. PCRYPT_ATTR_BLOB pblob;
  37. DWORD cbValue;
  38. PBYTE pbValue;
  39. for (i=patr->cValue, pblob=patr->rgValue, cbValue = 0;
  40. i>0;
  41. i--, pblob++) {
  42. cbValue += pblob->cbData;
  43. }
  44. poatr->attributeType.count = 16;
  45. if (!PkiAsn1ToObjectIdentifier(
  46. patr->pszObjId,
  47. &poatr->attributeType.count,
  48. poatr->attributeType.value))
  49. goto PkiAsn1ToObjectIdentifierError;
  50. poatr->attributeValue.value = (Any *)ICM_Alloc(
  51. patr->cValue * sizeof(Any) + cbValue);
  52. if (NULL == poatr->attributeValue.value)
  53. goto AttributeValueMallocError;
  54. poatr->attributeValue.count = patr->cValue;
  55. pbValue = (PBYTE) (poatr->attributeValue.value + patr->cValue);
  56. for (i=patr->cValue, pAny=poatr->attributeValue.value, pblob=patr->rgValue;
  57. i>0;
  58. i--, pAny++, pblob++) {
  59. DWORD cbData = pblob->cbData;
  60. if (cbData)
  61. memcpy(pbValue, pblob->pbData, cbData);
  62. pAny->length = cbData;
  63. pAny->value = pbValue;
  64. pbValue += cbData;
  65. }
  66. assert(pbValue == ((PBYTE) (poatr->attributeValue.value + patr->cValue)) +
  67. cbValue);
  68. fRet = TRUE;
  69. CommonReturn:
  70. return fRet;
  71. ErrorReturn:
  72. fRet = FALSE;
  73. goto CommonReturn;
  74. SET_ERROR(PkiAsn1ToObjectIdentifierError,CRYPT_E_OID_FORMAT)
  75. TRACE_ERROR(AttributeValueMallocError) // error already set
  76. }
  77. //+-------------------------------------------------------------------------
  78. // Convert an CRYPT_ALGORITHM_IDENTIFIER to an ASN1 AlgorithmIdentifier
  79. //
  80. // Returns FALSE iff conversion failed.
  81. //--------------------------------------------------------------------------
  82. BOOL
  83. WINAPI
  84. ICM_Asn1ToAlgorithmIdentifier(
  85. IN PCRYPT_ALGORITHM_IDENTIFIER pai,
  86. OUT AlgorithmIdentifier *pAsn1AlgId)
  87. {
  88. BOOL fRet;
  89. static BYTE abDerNULL[] = {5, 0};
  90. pAsn1AlgId->algorithm.count = 16;
  91. if (!PkiAsn1ToObjectIdentifier(
  92. pai->pszObjId,
  93. &pAsn1AlgId->algorithm.count,
  94. pAsn1AlgId->algorithm.value))
  95. goto PkiAsn1ToObjectIdentifierError;
  96. pAsn1AlgId->bit_mask = parameters_present;
  97. if (0 == pai->Parameters.cbData) {
  98. pAsn1AlgId->parameters.length = sizeof( abDerNULL);
  99. pAsn1AlgId->parameters.value = abDerNULL;
  100. } else {
  101. pAsn1AlgId->parameters.length = pai->Parameters.cbData;
  102. pAsn1AlgId->parameters.value = pai->Parameters.pbData;
  103. }
  104. fRet = TRUE;
  105. CommonReturn:
  106. return fRet;
  107. ErrorReturn:
  108. fRet = FALSE;
  109. goto CommonReturn;
  110. SET_ERROR(PkiAsn1ToObjectIdentifierError,CRYPT_E_OID_FORMAT)
  111. }
  112. //+-------------------------------------------------------------------------
  113. // Convert an ASN1 AlgorithmIdentifier to an CRYPT_ALGORITHM_IDENTIFIER
  114. //
  115. // Returns FALSE iff conversion failed.
  116. //--------------------------------------------------------------------------
  117. BOOL
  118. WINAPI
  119. ICM_Asn1FromAlgorithmIdentifier(
  120. IN AlgorithmIdentifier *pAsn1AlgId,
  121. OUT PCRYPT_ALGORITHM_IDENTIFIER pai)
  122. {
  123. DWORD cbObjId;
  124. BOOL fRet;
  125. cbObjId = 0;
  126. if (!PkiAsn1FromObjectIdentifier(
  127. pAsn1AlgId->algorithm.count,
  128. pAsn1AlgId->algorithm.value,
  129. NULL,
  130. &cbObjId))
  131. goto PkiAsn1FromObjectIdentifierSizeError;
  132. pai->pszObjId = (LPSTR)ICM_Alloc( cbObjId);
  133. if (!PkiAsn1FromObjectIdentifier(
  134. pAsn1AlgId->algorithm.count,
  135. pAsn1AlgId->algorithm.value,
  136. pai->pszObjId,
  137. &cbObjId))
  138. goto PkiAsn1FromObjectIdentifierError;
  139. if (pAsn1AlgId->bit_mask & parameters_present) {
  140. pai->Parameters.cbData = pAsn1AlgId->parameters.length;
  141. pai->Parameters.pbData = (PBYTE)pAsn1AlgId->parameters.value;
  142. } else {
  143. pai->Parameters.cbData = 0;
  144. pai->Parameters.pbData = NULL;
  145. }
  146. fRet = TRUE;
  147. CommonReturn:
  148. return fRet;
  149. ErrorReturn:
  150. fRet = FALSE;
  151. goto CommonReturn;
  152. TRACE_ERROR(PkiAsn1FromObjectIdentifierSizeError)
  153. TRACE_ERROR(PkiAsn1FromObjectIdentifierError)
  154. }