x509_algtest.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_alg.h>
  14. #include <gmssl/x509.h>
  15. #include <gmssl/rand.h>
  16. #include <gmssl/error.h>
  17. static int test_x509_digest_algor(void)
  18. {
  19. char *names[] = {
  20. "sm3",
  21. "md5",
  22. "sha1",
  23. "sha224",
  24. "sha256",
  25. "sha384",
  26. "sha512",
  27. };
  28. uint8_t buf[256];
  29. uint8_t *p = buf;
  30. const uint8_t *cp = buf;
  31. size_t len = 0;
  32. int oid;
  33. int i;
  34. format_print(stderr, 0, 0, "DER\n");
  35. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  36. oid = x509_digest_algor_from_name(names[i]);
  37. if (x509_digest_algor_to_der(oid, &p, &len) != 1) {
  38. error_print();
  39. return -1;
  40. }
  41. format_bytes(stderr, 0, 4, "", buf, len);
  42. }
  43. format_print(stderr, 0, 0, "OID\n");
  44. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  45. if (x509_digest_algor_from_der(&oid, &cp, &len) != 1) {
  46. error_print();
  47. return -1;
  48. }
  49. if (oid != x509_digest_algor_from_name(names[i])) {
  50. error_print();
  51. return 1;
  52. }
  53. format_print(stderr, 0, 4, "%s\n", x509_digest_algor_name(oid));
  54. }
  55. printf("%s() ok\n", __FUNCTION__);
  56. return 1;
  57. }
  58. static int test_x509_encryption_algor(void)
  59. {
  60. char *names[] = {
  61. "sm4-cbc",
  62. "aes128-cbc",
  63. "aes192-cbc",
  64. "aes256-cbc",
  65. };
  66. uint8_t iv[16] = {0};
  67. uint8_t buf[256];
  68. uint8_t *p = buf;
  69. const uint8_t *cp = buf;
  70. size_t len = 0;
  71. int oid;
  72. const uint8_t *params;
  73. size_t paramslen;
  74. int i;
  75. format_print(stderr, 0, 0, "DER\n");
  76. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  77. oid = x509_encryption_algor_from_name(names[i]);
  78. if (x509_encryption_algor_to_der(oid, iv, sizeof(iv), &p, &len) != 1) {
  79. error_print();
  80. return -1;
  81. }
  82. format_bytes(stderr, 0, 4, "", buf, len);
  83. }
  84. format_print(stderr, 0, 0, "OID\n");
  85. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  86. if (x509_encryption_algor_from_der(&oid, &params, &paramslen, &cp, &len) != 1
  87. || asn1_check(params != NULL) != 1
  88. || asn1_check(paramslen == sizeof(iv)) != 1) {
  89. error_print();
  90. return -1;
  91. }
  92. format_print(stderr, 0, 4, "%s\n", x509_encryption_algor_name(oid));
  93. }
  94. printf("%s() ok\n", __FUNCTION__);
  95. return 1;
  96. }
  97. static int test_x509_signature_algor(void)
  98. {
  99. char *names[] = {
  100. "sm2sign-with-sm3",
  101. "rsasign-with-sm3",
  102. "ecdsa-with-sha1",
  103. "ecdsa-with-sha224",
  104. "ecdsa-with-sha256",
  105. "ecdsa-with-sha384",
  106. "ecdsa-with-sha512",
  107. "sha1WithRSAEncryption",
  108. "sha224WithRSAEncryption",
  109. "sha256WithRSAEncryption",
  110. "sha384WithRSAEncryption",
  111. "sha512WithRSAEncryption",
  112. };
  113. uint8_t buf[256];
  114. uint8_t *p = buf;
  115. const uint8_t *cp = buf;
  116. size_t len = 0;
  117. int oid;
  118. int i;
  119. format_print(stderr, 0, 0, "DER\n");
  120. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  121. oid = x509_signature_algor_from_name(names[i]);
  122. if (x509_signature_algor_to_der(oid, &p, &len) != 1) {
  123. error_print();
  124. return -1;
  125. }
  126. format_bytes(stderr, 0, 4, "", buf, len);
  127. }
  128. format_print(stderr, 0, 0, "OID\n");
  129. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  130. if (x509_signature_algor_from_der(&oid, &cp, &len) != 1) {
  131. error_print();
  132. return -1;
  133. }
  134. format_print(stderr, 0, 4, "%s\n", x509_signature_algor_name(oid));
  135. }
  136. printf("%s() ok\n", __FUNCTION__);
  137. return 1;
  138. }
  139. static int test_x509_public_key_encryption_algor(void)
  140. {
  141. char *names[] = {
  142. "sm2encrypt",
  143. // "rsaesOAEP",
  144. // "rsaEncryption",
  145. };
  146. uint8_t buf[256];
  147. uint8_t *p = buf;
  148. const uint8_t *cp = buf;
  149. size_t len = 0;
  150. int oid;
  151. const uint8_t *params;
  152. size_t paramslen;
  153. int i;
  154. format_print(stderr, 0, 0, "DER\n");
  155. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  156. oid = x509_public_key_encryption_algor_from_name(names[i]);
  157. if (x509_public_key_encryption_algor_to_der(oid, &p, &len) != 1) {
  158. error_print();
  159. return -1;
  160. }
  161. format_bytes(stderr, 0, 4, "", buf, len);
  162. }
  163. format_print(stderr, 0, 0, "OID\n");
  164. for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
  165. if (x509_public_key_encryption_algor_from_der(&oid, &params, &paramslen, &cp, &len) != 1) {
  166. error_print();
  167. return -1;
  168. }
  169. format_print(stderr, 0, 4, "%s\n", x509_public_key_encryption_algor_name(oid));
  170. }
  171. printf("%s() ok\n", __FUNCTION__);
  172. return 1;
  173. }
  174. int main(void)
  175. {
  176. if (test_x509_digest_algor() != 1) goto err;
  177. if (test_x509_encryption_algor() != 1) goto err;
  178. if (test_x509_signature_algor() != 1) goto err;
  179. if (test_x509_public_key_encryption_algor() != 1) goto err;
  180. printf("%s all tests passed!\n", __FILE__);
  181. return 0;
  182. err:
  183. error_print();
  184. return 1;
  185. }