x509_oidtest.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <gmssl/oid.h>
  13. #include <gmssl/x509.h>
  14. #include <gmssl/x509_ext.h>
  15. #include <gmssl/rand.h>
  16. #include <gmssl/error.h>
  17. static int test_x509_name_type()
  18. {
  19. char *names[] = {
  20. "name",
  21. "surname",
  22. "givenName",
  23. "initials",
  24. "generationQualifier",
  25. "commonName",
  26. "localityName",
  27. "stateOrProvinceName",
  28. "organizationName",
  29. "organizationalUnitName",
  30. "title",
  31. "dnQualifier",
  32. "countryName",
  33. "serialNumber",
  34. "pseudonym",
  35. "domainComponent",
  36. };
  37. int oid;
  38. uint8_t buf[256];
  39. uint8_t *p = buf;
  40. const uint8_t *cp = buf;
  41. size_t len = 0;
  42. int i;
  43. format_print(stderr, 0, 0, "DER\n");
  44. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  45. oid = x509_name_type_from_name(names[i]);
  46. if (asn1_check(oid != OID_undef) != 1
  47. || x509_name_type_to_der(oid, &p, &len) != 1) {
  48. error_print();
  49. return -1;
  50. }
  51. format_bytes(stderr, 0, 4, "", buf, len);
  52. }
  53. format_print(stderr, 0, 0, "OID\n");
  54. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  55. if (x509_name_type_from_der(&oid, &cp, &len) != 1) {
  56. error_print();
  57. return -1;
  58. }
  59. if (oid != x509_name_type_from_name(names[i])) {
  60. error_print();
  61. return -1;
  62. }
  63. format_print(stderr, 0, 4, "%s\n", x509_name_type_name(oid));
  64. }
  65. if (len != 0) {
  66. error_print();
  67. return -1;
  68. }
  69. printf("%s() ok\n", __FUNCTION__);
  70. return 1;
  71. }
  72. static int test_x509_ext_id()
  73. {
  74. char *names[] = {
  75. "AuthorityKeyIdentifier",
  76. "SubjectKeyIdentifier",
  77. "KeyUsage",
  78. "CertificatePolicies",
  79. "PolicyMappings",
  80. "SubjectAltName",
  81. "IssuerAltName",
  82. "SubjectDirectoryAttributes",
  83. "BasicConstraints",
  84. "NameConstraints",
  85. "PolicyConstraints",
  86. "ExtKeyUsage",
  87. "CRLDistributionPoints",
  88. "InhibitAnyPolicy",
  89. "FreshestCRL",
  90. };
  91. int oid;
  92. uint32_t nodes[32];
  93. size_t nodes_cnt;
  94. uint8_t buf[256];
  95. uint8_t *p = buf;
  96. const uint8_t *cp = buf;
  97. size_t len = 0;
  98. int i;
  99. format_print(stderr, 0, 0, "DER\n");
  100. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  101. oid = x509_ext_id_from_name(names[i]);
  102. if (asn1_check(oid != OID_undef) != 1
  103. || x509_ext_id_to_der(oid, &p, &len) != 1) {
  104. error_print();
  105. return -1;
  106. }
  107. format_bytes(stderr, 0, 4, "", buf, len);
  108. }
  109. format_print(stderr, 0, 0, "ExtnID\n");
  110. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  111. if (x509_ext_id_from_der(&oid, nodes, &nodes_cnt, &cp, &len) != 1) {
  112. error_print();
  113. return -1;
  114. }
  115. if (oid != x509_ext_id_from_name(names[i])) {
  116. error_print();
  117. return -1;
  118. }
  119. format_print(stderr, 0, 4, "%s\n", x509_ext_id_name(oid));
  120. }
  121. if (len != 0) {
  122. error_print();
  123. return -1;
  124. }
  125. printf("%s() ok\n", __FUNCTION__);
  126. return 1;
  127. }
  128. static int test_x509_qualifier_id(void)
  129. {
  130. char *names[] = {
  131. "CPS",
  132. "userNotice",
  133. };
  134. int oid;
  135. uint8_t buf[256];
  136. uint8_t *p = buf;
  137. const uint8_t *cp = buf;
  138. size_t len = 0;
  139. int i;
  140. format_print(stderr, 0, 0, "DER\n");
  141. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  142. oid = x509_qualifier_id_from_name(names[i]);
  143. if (asn1_check(oid != OID_undef) != 1
  144. || x509_qualifier_id_to_der(oid, &p, &len) != 1) {
  145. error_print();
  146. return -1;
  147. }
  148. format_bytes(stderr, 0, 4, "", buf, len);
  149. }
  150. format_print(stderr, 0, 0, "OID\n");
  151. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  152. if (x509_qualifier_id_from_der(&oid, &cp, &len) != 1) {
  153. error_print();
  154. return -1;
  155. }
  156. if (asn1_check(oid == x509_qualifier_id_from_name(names[i])) != 1) {
  157. error_print();
  158. return -1;
  159. }
  160. format_print(stderr, 0, 4, "%s\n", x509_qualifier_id_name(oid));
  161. }
  162. if (len != 0) {
  163. error_print();
  164. return -1;
  165. }
  166. printf("%s() ok\n", __FUNCTION__);
  167. return 1;
  168. }
  169. static int test_x509_cert_policy_id(void)
  170. {
  171. char *names[] = {
  172. "anyPolicy",
  173. };
  174. int oid;
  175. uint32_t nodes[32];
  176. size_t nodes_cnt;
  177. uint8_t buf[256];
  178. uint8_t *p = buf;
  179. const uint8_t *cp = buf;
  180. size_t len = 0;
  181. int i;
  182. format_print(stderr, 0, 0, "DER\n");
  183. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  184. oid = x509_cert_policy_id_from_name(names[i]);
  185. if (asn1_check(oid != OID_undef) != 1
  186. || x509_cert_policy_id_to_der(oid, NULL, 0, &p, &len) != 1) {
  187. error_print();
  188. return -1;
  189. }
  190. format_bytes(stderr, 0, 4, "", buf, len);
  191. }
  192. format_print(stderr, 0, 0, "OID\n");
  193. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  194. if (x509_cert_policy_id_from_der(&oid, nodes, &nodes_cnt, &cp, &len) != 1) {
  195. error_print();
  196. return -1;
  197. }
  198. if (oid != x509_cert_policy_id_from_name(names[i])) {
  199. error_print();
  200. return -1;
  201. }
  202. format_print(stderr, 0, 4, "%s\n", x509_cert_policy_id_name(oid));
  203. }
  204. if (len != 0) {
  205. error_print();
  206. return -1;
  207. }
  208. printf("%s() ok\n", __FUNCTION__);
  209. return 1;
  210. }
  211. static int test_x509_key_purpose(void)
  212. {
  213. char *names[] = {
  214. "serverAuth",
  215. "clientAuth",
  216. "codeSigning",
  217. "emailProtection",
  218. "timeStamping",
  219. "OCSPSigning",
  220. };
  221. int oid;
  222. uint8_t buf[256];
  223. uint8_t *p = buf;
  224. const uint8_t *cp = buf;
  225. size_t len = 0;
  226. int i;
  227. format_print(stderr, 0, 0, "DER\n");
  228. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  229. oid = x509_key_purpose_from_name(names[i]);
  230. if (asn1_check(oid != OID_undef) != 1
  231. || x509_key_purpose_to_der(oid, &p, &len) != 1) {
  232. error_print();
  233. return -1;
  234. }
  235. format_bytes(stderr, 0, 4, "", buf, len);
  236. }
  237. format_print(stderr, 0, 0, "OID\n");
  238. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  239. if (x509_key_purpose_from_der(&oid, &cp, &len) != 1) {
  240. error_print();
  241. return -1;
  242. }
  243. if (oid != x509_key_purpose_from_name(names[i])) {
  244. error_print();
  245. return -1;
  246. }
  247. format_print(stderr, 0, 4, "%s\n", x509_key_purpose_name(oid));
  248. }
  249. if (len != 0) {
  250. error_print();
  251. return -1;
  252. }
  253. printf("%s() ok\n", __FUNCTION__);
  254. return 1;
  255. }
  256. int main(void)
  257. {
  258. if (test_x509_name_type() != 1) goto err;
  259. if (test_x509_ext_id() != 1) goto err;
  260. if (test_x509_qualifier_id() != 1) goto err;
  261. if (test_x509_cert_policy_id() != 1) goto err;
  262. if (test_x509_key_purpose() != 1) goto err;
  263. printf("%s all tests passed\n", __FILE__);
  264. return 0;
  265. err:
  266. error_print();
  267. return 1;
  268. }