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.

172 lines
4.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1996
  6. //
  7. // File: tdotoid.cpp
  8. //
  9. // Contents: Convert Dot OID ("1.2.3") to ASN.1 encoded content octets.
  10. //
  11. // See Usage() for list of test options.
  12. //
  13. //
  14. // Functions: main
  15. //
  16. // History: 04-Jan-01 philh created
  17. //--------------------------------------------------------------------------
  18. #include <windows.h>
  19. #include <assert.h>
  20. #include "wincrypt.h"
  21. #include "certtest.h"
  22. #include "unicode.h"
  23. #include "asn1util.h"
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <memory.h>
  28. #include <time.h>
  29. const BYTE rgbSeqTag[] = {ASN1UTIL_TAG_SEQ, 0};
  30. const BYTE rgbOIDTag[] = {ASN1UTIL_TAG_OID, 0};
  31. const ASN1UTIL_EXTRACT_VALUE_PARA rgExtractAttrPara[] = {
  32. // 0 - Attribute ::= SEQUENCE {
  33. ASN1UTIL_STEP_INTO_VALUE_OP, rgbSeqTag,
  34. // 1 - type EncodedObjectID,
  35. ASN1UTIL_RETURN_CONTENT_BLOB_FLAG |
  36. ASN1UTIL_STEP_OVER_VALUE_OP, rgbOIDTag,
  37. };
  38. #define ATTR_OID_VALUE_INDEX 1
  39. #define ATTR_VALUE_COUNT \
  40. (sizeof(rgExtractAttrPara) / sizeof(rgExtractAttrPara[0]))
  41. BOOL fDecode = FALSE;
  42. void DotValToEncodedOid(
  43. LPCSTR pszDotVal
  44. )
  45. {
  46. CRYPT_ATTRIBUTE Attr;
  47. BYTE rgbEncoded[512];
  48. DWORD cbEncoded;
  49. CRYPT_DER_BLOB rgValueBlob[ATTR_VALUE_COUNT];
  50. DWORD cValue;
  51. DWORD i;
  52. BYTE *pb;
  53. DWORD cb;
  54. // Encode an Attribute that only has the OID.
  55. Attr.pszObjId = (LPSTR) pszDotVal;
  56. Attr.cValue = 0;
  57. Attr.rgValue = NULL;
  58. cbEncoded = sizeof(rgbEncoded);
  59. if (!CryptEncodeObject(
  60. X509_ASN_ENCODING,
  61. PKCS_ATTRIBUTE,
  62. &Attr,
  63. rgbEncoded,
  64. &cbEncoded
  65. )) {
  66. printf("\n");
  67. printf("Asn1Encode(%s)", pszDotVal);
  68. PrintLastError("");
  69. return;
  70. }
  71. cValue = ATTR_VALUE_COUNT;
  72. if (0 >= Asn1UtilExtractValues(
  73. rgbEncoded,
  74. cbEncoded,
  75. 0, // dwFlags
  76. &cValue,
  77. rgExtractAttrPara,
  78. rgValueBlob
  79. ) || ATTR_OID_VALUE_INDEX >= cValue) {
  80. printf("\n");
  81. printf("ExtractValues(%s)", pszDotVal);
  82. PrintLastError("");
  83. return;
  84. }
  85. pb = rgValueBlob[ATTR_OID_VALUE_INDEX].pbData;
  86. cb = rgValueBlob[ATTR_OID_VALUE_INDEX].cbData;
  87. printf("\n// \"%s\"\n{", pszDotVal);
  88. for (i = 0; i < cb; i++) {
  89. printf("0x%02X", pb[i]);
  90. if ((i+1) < cb)
  91. printf(", ");
  92. }
  93. printf("};\n\n");
  94. if (fDecode) {
  95. PCRYPT_ATTRIBUTE pDecodedAttr = NULL;
  96. DWORD cbDecodedAttr;
  97. if (!CryptDecodeObject(
  98. X509_ASN_ENCODING,
  99. PKCS_ATTRIBUTE,
  100. rgbEncoded,
  101. cbEncoded,
  102. CRYPT_DECODE_ALLOC_FLAG,
  103. (void *) &pDecodedAttr,
  104. &cbDecodedAttr
  105. ))
  106. PrintLastError("CryptDecodeObject");
  107. else {
  108. printf("Decoded OID:: %s\n", pDecodedAttr->pszObjId);
  109. LocalFree(pDecodedAttr);
  110. }
  111. }
  112. }
  113. void Usage(void)
  114. {
  115. int i;
  116. printf("Usage: tdotoid <OID String 1> <OID String 2> ...\n");
  117. printf("Options are:\n");
  118. printf(" -d - Decode after encoding\n");
  119. printf(" -h - This message\n");
  120. printf("\n");
  121. }
  122. int _cdecl main(int argc, char * argv[])
  123. {
  124. int ReturnStatus;
  125. while (--argc>0)
  126. {
  127. if (**++argv == '-')
  128. {
  129. switch(argv[0][1])
  130. {
  131. case 'd':
  132. fDecode = TRUE;
  133. break;
  134. case 'h':
  135. default:
  136. goto BadUsage;
  137. }
  138. } else
  139. DotValToEncodedOid(argv[0]);
  140. }
  141. ReturnStatus = 0;
  142. CommonReturn:
  143. return ReturnStatus;
  144. BadUsage:
  145. Usage();
  146. ReturnStatus = -1;
  147. goto CommonReturn;
  148. }