selfsign.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* NOCW */
  2. /* cc -o ssdemo -I../include selfsign.c ../libcrypto.a */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <openssl/pem.h>
  6. #include <openssl/conf.h>
  7. #include <openssl/x509v3.h>
  8. int mkit(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days);
  9. int main()
  10. {
  11. BIO *bio_err;
  12. X509 *x509=NULL;
  13. EVP_PKEY *pkey=NULL;
  14. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  15. bio_err=BIO_new_fp(stderr, BIO_NOCLOSE);
  16. mkit(&x509,&pkey,512,0,365);
  17. RSA_print_fp(stdout,pkey->pkey.rsa,0);
  18. X509_print_fp(stdout,x509);
  19. PEM_write_PrivateKey(stdout,pkey,NULL,NULL,0,NULL, NULL);
  20. PEM_write_X509(stdout,x509);
  21. X509_free(x509);
  22. EVP_PKEY_free(pkey);
  23. #ifdef CUSTOM_EXT
  24. /* Only needed if we add objects or custom extensions */
  25. X509V3_EXT_cleanup();
  26. OBJ_cleanup();
  27. #endif
  28. CRYPTO_mem_leaks(bio_err);
  29. BIO_free(bio_err);
  30. return(0);
  31. }
  32. #ifdef WIN16
  33. # define MS_CALLBACK _far _loadds
  34. # define MS_FAR _far
  35. #else
  36. # define MS_CALLBACK
  37. # define MS_FAR
  38. #endif
  39. static void MS_CALLBACK callback(p, n, arg)
  40. int p;
  41. int n;
  42. void *arg;
  43. {
  44. char c='B';
  45. if (p == 0) c='.';
  46. if (p == 1) c='+';
  47. if (p == 2) c='*';
  48. if (p == 3) c='\n';
  49. fputc(c,stderr);
  50. }
  51. int mkit(x509p,pkeyp,bits,serial,days)
  52. X509 **x509p;
  53. EVP_PKEY **pkeyp;
  54. int bits;
  55. int serial;
  56. int days;
  57. {
  58. X509 *x;
  59. EVP_PKEY *pk;
  60. RSA *rsa;
  61. X509_NAME *name=NULL;
  62. X509_NAME_ENTRY *ne=NULL;
  63. X509_EXTENSION *ex=NULL;
  64. if ((pkeyp == NULL) || (*pkeyp == NULL))
  65. {
  66. if ((pk=EVP_PKEY_new()) == NULL)
  67. {
  68. abort();
  69. return(0);
  70. }
  71. }
  72. else
  73. pk= *pkeyp;
  74. if ((x509p == NULL) || (*x509p == NULL))
  75. {
  76. if ((x=X509_new()) == NULL)
  77. goto err;
  78. }
  79. else
  80. x= *x509p;
  81. rsa=RSA_generate_key(bits,RSA_F4,callback,NULL);
  82. if (!EVP_PKEY_assign_RSA(pk,rsa))
  83. {
  84. abort();
  85. goto err;
  86. }
  87. rsa=NULL;
  88. X509_set_version(x,3);
  89. ASN1_INTEGER_set(X509_get_serialNumber(x),serial);
  90. X509_gmtime_adj(X509_get_notBefore(x),0);
  91. X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days);
  92. X509_set_pubkey(x,pk);
  93. name=X509_get_subject_name(x);
  94. /* This function creates and adds the entry, working out the
  95. * correct string type and performing checks on its length.
  96. * Normally we'd check the return value for errors...
  97. */
  98. X509_NAME_add_entry_by_txt(name,"C",
  99. MBSTRING_ASC, "UK", -1, -1, 0);
  100. X509_NAME_add_entry_by_txt(name,"CN",
  101. MBSTRING_ASC, "OpenSSL Group", -1, -1, 0);
  102. X509_set_issuer_name(x,name);
  103. /* Add extension using V3 code: we can set the config file as NULL
  104. * because we wont reference any other sections. We can also set
  105. * the context to NULL because none of these extensions below will need
  106. * to access it.
  107. */
  108. ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_cert_type, "server");
  109. X509_add_ext(x,ex,-1);
  110. X509_EXTENSION_free(ex);
  111. ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_comment,
  112. "example comment extension");
  113. X509_add_ext(x,ex,-1);
  114. X509_EXTENSION_free(ex);
  115. ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_ssl_server_name,
  116. "www.openssl.org");
  117. X509_add_ext(x,ex,-1);
  118. X509_EXTENSION_free(ex);
  119. #if 0
  120. /* might want something like this too.... */
  121. ex = X509V3_EXT_conf_nid(NULL, NULL, NID_basic_constraints,
  122. "critical,CA:TRUE");
  123. X509_add_ext(x,ex,-1);
  124. X509_EXTENSION_free(ex);
  125. #endif
  126. #ifdef CUSTOM_EXT
  127. /* Maybe even add our own extension based on existing */
  128. {
  129. int nid;
  130. nid = OBJ_create("1.2.3.4", "MyAlias", "My Test Alias Extension");
  131. X509V3_EXT_add_alias(nid, NID_netscape_comment);
  132. ex = X509V3_EXT_conf_nid(NULL, NULL, nid,
  133. "example comment alias");
  134. X509_add_ext(x,ex,-1);
  135. X509_EXTENSION_free(ex);
  136. }
  137. #endif
  138. if (!X509_sign(x,pk,EVP_md5()))
  139. goto err;
  140. *x509p=x;
  141. *pkeyp=pk;
  142. return(1);
  143. err:
  144. return(0);
  145. }