ckapi.perl 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #!perl
  2. #
  3. # This Source Code Form is subject to the terms of the Mozilla Public
  4. # License, v. 2.0. If a copy of the MPL was not distributed with this
  5. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. $copyright = '/* THIS IS A GENERATED FILE */
  7. /* This Source Code Form is subject to the terms of the Mozilla Public
  8. * License, v. 2.0. If a copy of the MPL was not distributed with this
  9. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  10. ';
  11. $count = -1;
  12. $i = 0;
  13. open(INPUT, "<$ARGV[0]") || die "Can't open $ARGV[0]: $!";
  14. while(<INPUT>) {
  15. s/^((?:[^"#]+|"[^"]*")*)(\s*#.*$)/$1/;
  16. next if (/^\s*$/);
  17. # print;
  18. /^([\S]+)\s+([^"][\S]*|"[^"]*")/;
  19. $name = $1;
  20. $value = $2;
  21. if( ($name =~ "FUNCTION") && !($name =~ "CK_FUNCTION") ) {
  22. $count++;
  23. $x[$count]{name} = $value;
  24. $i = 0;
  25. } else {
  26. if( $count < 0 ) {
  27. $value =~ s/"//g;
  28. $g{$name} = $value;
  29. } else {
  30. $x[$count]{args}[$i]{type} = $name;
  31. $x[$count]{args}[$i]{name} = $value;
  32. $i++;
  33. $x[$count]{nargs} = $i; # rewritten each time, oh well
  34. }
  35. }
  36. }
  37. close INPUT;
  38. # dodump();
  39. doprint();
  40. sub dodump {
  41. for( $j = 0; $j <= $count; $j++ ) {
  42. print "CK_RV CK_ENTRY $x[$j]{name}\n";
  43. for( $i = 0; $i < $x[$j]{nargs}; $i++ ) {
  44. print " $x[$j]{args}[$i]{type} $x[$j]{args}[$i]{name}";
  45. if( $i == ($x[$j]{nargs} - 1) ) {
  46. print "\n";
  47. } else {
  48. print ",\n";
  49. }
  50. }
  51. }
  52. }
  53. sub doprint {
  54. open(PROTOTYPE, ">nssckg.h") || die "Can't open nssckg.h: $!";
  55. open(TYPEDEF, ">nssckft.h") || die "Can't open nssckft.h: $!";
  56. open(EPV, ">nssckepv.h") || die "Can't open nssckepv.h: $!";
  57. open(API, ">nssck.api") || die "Can't open nssck.api: $!";
  58. select PROTOTYPE;
  59. print $copyright;
  60. print <<EOD
  61. #ifndef NSSCKG_H
  62. #define NSSCKG_H
  63. /*
  64. * nssckg.h
  65. *
  66. * This automatically-generated header file prototypes the Cryptoki
  67. * functions specified by PKCS#11.
  68. */
  69. #ifndef NSSCKT_H
  70. #include "nssckt.h"
  71. #endif /* NSSCKT_H */
  72. EOD
  73. ;
  74. for( $j = 0; $j <= $count; $j++ ) {
  75. print "CK_RV CK_ENTRY $x[$j]{name}\n";
  76. print "(\n";
  77. for( $i = 0; $i < $x[$j]{nargs}; $i++ ) {
  78. print " $x[$j]{args}[$i]{type} $x[$j]{args}[$i]{name}";
  79. if( $i == ($x[$j]{nargs} - 1) ) {
  80. print "\n";
  81. } else {
  82. print ",\n";
  83. }
  84. }
  85. print ");\n\n";
  86. }
  87. print <<EOD
  88. #endif /* NSSCKG_H */
  89. EOD
  90. ;
  91. select TYPEDEF;
  92. print $copyright;
  93. print <<EOD
  94. #ifndef NSSCKFT_H
  95. #define NSSCKFT_H
  96. /*
  97. * nssckft.h
  98. *
  99. * The automatically-generated header file declares a typedef
  100. * each of the Cryptoki functions specified by PKCS#11.
  101. */
  102. #ifndef NSSCKT_H
  103. #include "nssckt.h"
  104. #endif /* NSSCKT_H */
  105. EOD
  106. ;
  107. for( $j = 0; $j <= $count; $j++ ) {
  108. # print "typedef CK_RV (CK_ENTRY *CK_$x[$j]{name})(\n";
  109. print "typedef CK_CALLBACK_FUNCTION(CK_RV, CK_$x[$j]{name})(\n";
  110. for( $i = 0; $i < $x[$j]{nargs}; $i++ ) {
  111. print " $x[$j]{args}[$i]{type} $x[$j]{args}[$i]{name}";
  112. if( $i == ($x[$j]{nargs} - 1) ) {
  113. print "\n";
  114. } else {
  115. print ",\n";
  116. }
  117. }
  118. print ");\n\n";
  119. }
  120. print <<EOD
  121. #endif /* NSSCKFT_H */
  122. EOD
  123. ;
  124. select EPV;
  125. print $copyright;
  126. print <<EOD
  127. #ifndef NSSCKEPV_H
  128. #define NSSCKEPV_H
  129. /*
  130. * nssckepv.h
  131. *
  132. * This automatically-generated header file defines the type
  133. * CK_FUNCTION_LIST specified by PKCS#11.
  134. */
  135. #ifndef NSSCKT_H
  136. #include "nssckt.h"
  137. #endif /* NSSCKT_H */
  138. #ifndef NSSCKFT_H
  139. #include "nssckft.h"
  140. #endif /* NSSCKFT_H */
  141. #include "nssckp.h"
  142. struct CK_FUNCTION_LIST {
  143. CK_VERSION version;
  144. EOD
  145. ;
  146. for( $j = 0; $j <= $count; $j++ ) {
  147. print " CK_$x[$j]{name} $x[$j]{name};\n";
  148. }
  149. print <<EOD
  150. };
  151. #include "nsscku.h"
  152. #endif /* NSSCKEPV_H */
  153. EOD
  154. ;
  155. select API;
  156. print $copyright;
  157. print <<EOD
  158. /*
  159. * nssck.api
  160. *
  161. * This automatically-generated file is used to generate a set of
  162. * Cryptoki entry points within the object space of a Module using
  163. * the NSS Cryptoki Framework.
  164. *
  165. * The Module should have a .c file with the following:
  166. *
  167. * #define MODULE_NAME name
  168. * #define INSTANCE_NAME instance
  169. * #include "nssck.api"
  170. *
  171. * where "name" is some module-specific name that can be used to
  172. * disambiguate various modules. This included file will then
  173. * define the actual Cryptoki routines which pass through to the
  174. * Framework calls. All routines, except C_GetFunctionList, will
  175. * be prefixed with the name; C_GetFunctionList will be generated
  176. * to return an entry-point vector with these routines. The
  177. * instance specified should be the basic instance of NSSCKMDInstance.
  178. *
  179. * If, prior to including nssck.api, the .c file also specifies
  180. *
  181. * #define DECLARE_STRICT_CRYTPOKI_NAMES
  182. *
  183. * Then a set of "stub" routines not prefixed with the name will
  184. * be included. This would allow the combined module and framework
  185. * to be used in applications which are hard-coded to use the
  186. * PKCS#11 names (instead of going through the EPV). Please note
  187. * that such applications should be careful resolving symbols when
  188. * more than one PKCS#11 module is loaded.
  189. */
  190. #ifndef MODULE_NAME
  191. #error "Error: MODULE_NAME must be defined."
  192. #endif /* MODULE_NAME */
  193. #ifndef INSTANCE_NAME
  194. #error "Error: INSTANCE_NAME must be defined."
  195. #endif /* INSTANCE_NAME */
  196. #ifndef NSSCKT_H
  197. #include "nssckt.h"
  198. #endif /* NSSCKT_H */
  199. #ifndef NSSCKFWT_H
  200. #include "nssckfwt.h"
  201. #endif /* NSSCKFWT_H */
  202. #ifndef NSSCKFWC_H
  203. #include "nssckfwc.h"
  204. #endif /* NSSCKFWC_H */
  205. #ifndef NSSCKEPV_H
  206. #include "nssckepv.h"
  207. #endif /* NSSCKEPV_H */
  208. #define ADJOIN(x,y) x##y
  209. #define __ADJOIN(x,y) ADJOIN(x,y)
  210. /*
  211. * The anchor. This object is used to store an "anchor" pointer in
  212. * the Module's object space, so the wrapper functions can relate
  213. * back to this instance.
  214. */
  215. static NSSCKFWInstance *fwInstance = (NSSCKFWInstance *)0;
  216. static CK_RV CK_ENTRY
  217. __ADJOIN(MODULE_NAME,C_Initialize)
  218. (
  219. CK_VOID_PTR pInitArgs
  220. )
  221. {
  222. return NSSCKFWC_Initialize(&fwInstance, INSTANCE_NAME, pInitArgs);
  223. }
  224. #ifdef DECLARE_STRICT_CRYPTOKI_NAMES
  225. CK_RV CK_ENTRY
  226. C_Initialize
  227. (
  228. CK_VOID_PTR pInitArgs
  229. )
  230. {
  231. return __ADJOIN(MODULE_NAME,C_Initialize)(pInitArgs);
  232. }
  233. #endif /* DECLARE_STRICT_CRYPTOKI_NAMES */
  234. static CK_RV CK_ENTRY
  235. __ADJOIN(MODULE_NAME,C_Finalize)
  236. (
  237. CK_VOID_PTR pReserved
  238. )
  239. {
  240. return NSSCKFWC_Finalize(&fwInstance);
  241. }
  242. #ifdef DECLARE_STRICT_CRYPTOKI_NAMES
  243. CK_RV CK_ENTRY
  244. C_Finalize
  245. (
  246. CK_VOID_PTR pReserved
  247. )
  248. {
  249. return __ADJOIN(MODULE_NAME,C_Finalize)(pReserved);
  250. }
  251. #endif /* DECLARE_STRICT_CRYPTOKI_NAMES */
  252. static CK_RV CK_ENTRY
  253. __ADJOIN(MODULE_NAME,C_GetInfo)
  254. (
  255. CK_INFO_PTR pInfo
  256. )
  257. {
  258. return NSSCKFWC_GetInfo(fwInstance, pInfo);
  259. }
  260. #ifdef DECLARE_STRICT_CRYPTOKI_NAMES
  261. CK_RV CK_ENTRY
  262. C_GetInfo
  263. (
  264. CK_INFO_PTR pInfo
  265. )
  266. {
  267. return __ADJOIN(MODULE_NAME,C_GetInfo)(pInfo);
  268. }
  269. #endif /* DECLARE_STRICT_CRYPTOKI_NAMES */
  270. /*
  271. * C_GetFunctionList is defined at the end.
  272. */
  273. EOD
  274. ;
  275. for( $j = 4; $j <= $count; $j++ ) {
  276. print "static CK_RV CK_ENTRY\n";
  277. print "__ADJOIN(MODULE_NAME,$x[$j]{name})\n";
  278. print "(\n";
  279. for( $i = 0; $i < $x[$j]{nargs}; $i++ ) {
  280. print " $x[$j]{args}[$i]{type} $x[$j]{args}[$i]{name}";
  281. if( $i == ($x[$j]{nargs} - 1) ) {
  282. print "\n";
  283. } else {
  284. print ",\n";
  285. }
  286. }
  287. print ")\n";
  288. print "{\n";
  289. print " return NSSCKFW$x[$j]{name}(fwInstance, ";
  290. for( $i = 0; $i < $x[$j]{nargs}; $i++ ) {
  291. print "$x[$j]{args}[$i]{name}";
  292. if( $i == ($x[$j]{nargs} - 1) ) {
  293. print ");\n";
  294. } else {
  295. print ", ";
  296. }
  297. }
  298. print "}\n\n";
  299. print "#ifdef DECLARE_STRICT_CRYPTOKI_NAMES\n";
  300. print "CK_RV CK_ENTRY\n";
  301. print "$x[$j]{name}\n";
  302. print "(\n";
  303. for( $i = 0; $i < $x[$j]{nargs}; $i++ ) {
  304. print " $x[$j]{args}[$i]{type} $x[$j]{args}[$i]{name}";
  305. if( $i == ($x[$j]{nargs} - 1) ) {
  306. print "\n";
  307. } else {
  308. print ",\n";
  309. }
  310. }
  311. print ")\n";
  312. print "{\n";
  313. print " return __ADJOIN(MODULE_NAME,$x[$j]{name})(";
  314. for( $i = 0; $i < $x[$j]{nargs}; $i++ ) {
  315. print "$x[$j]{args}[$i]{name}";
  316. if( $i == ($x[$j]{nargs} - 1) ) {
  317. print ");\n";
  318. } else {
  319. print ", ";
  320. }
  321. }
  322. print "}\n";
  323. print "#endif /* DECLARE_STRICT_CRYPTOKI_NAMES */\n\n";
  324. }
  325. print <<EOD
  326. static CK_RV CK_ENTRY
  327. __ADJOIN(MODULE_NAME,C_GetFunctionList)
  328. (
  329. CK_FUNCTION_LIST_PTR_PTR ppFunctionList
  330. );
  331. static CK_FUNCTION_LIST FunctionList = {
  332. { 2, 1 },
  333. EOD
  334. ;
  335. for( $j = 0; $j <= $count; $j++ ) {
  336. print "__ADJOIN(MODULE_NAME,$x[$j]{name})";
  337. if( $j < $count ) {
  338. print ",\n";
  339. } else {
  340. print "\n};\n\n";
  341. }
  342. }
  343. print <<EOD
  344. static CK_RV CK_ENTRY
  345. __ADJOIN(MODULE_NAME,C_GetFunctionList)
  346. (
  347. CK_FUNCTION_LIST_PTR_PTR ppFunctionList
  348. )
  349. {
  350. *ppFunctionList = &FunctionList;
  351. return CKR_OK;
  352. }
  353. /* This one is always present */
  354. CK_RV CK_ENTRY
  355. C_GetFunctionList
  356. (
  357. CK_FUNCTION_LIST_PTR_PTR ppFunctionList
  358. )
  359. {
  360. return __ADJOIN(MODULE_NAME,C_GetFunctionList)(ppFunctionList);
  361. }
  362. #undef __ADJOIN
  363. EOD
  364. ;
  365. select STDOUT;
  366. }