sig.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. From ssl-lists-owner@mincom.com Mon Sep 30 02:37:40 1996
  2. Received: from cygnus.mincom.oz.au by orb.mincom.oz.au with SMTP id AA11782
  3. (5.65c/IDA-1.4.4 for eay); Mon, 30 Sep 1996 11:46:21 +1000
  4. Received: (from daemon@localhost) by cygnus.mincom.oz.au (8.7.5/8.7.3) id LAA18980 for ssl-users-outgoing; Mon, 30 Sep 1996 11:44:56 +1000 (EST)
  5. Received: from minbne.mincom.oz.au (minbne.mincom.oz.au [192.55.196.247]) by cygnus.mincom.oz.au (8.7.5/8.7.3) with SMTP id LAA18962 for <ssl-users@listserv.mincom.oz.au>; Mon, 30 Sep 1996 11:44:51 +1000 (EST)
  6. Received: by minbne.mincom.oz.au id AA22230
  7. (5.65c/IDA-1.4.4 for ssl-users@listserv.mincom.oz.au); Mon, 30 Sep 1996 11:38:41 +1000
  8. Received: from brutus.neuronio.pt (brutus.neuronio.pt [193.126.253.2]) by bunyip.cc.uq.oz.au (8.7.6/8.7.3) with SMTP id LAA15824 for <ssl-users@mincom.com>; Mon, 30 Sep 1996 11:40:07 +1000
  9. Received: (from sampo@localhost) by brutus.neuronio.pt (8.6.11/8.6.11) id BAA08729; Mon, 30 Sep 1996 01:37:40 +0100
  10. Date: Mon, 30 Sep 1996 01:37:40 +0100
  11. Message-Id: <199609300037.BAA08729@brutus.neuronio.pt>
  12. From: Sampo Kellomaki <sampo@neuronio.pt>
  13. To: ssl-users@mincom.com
  14. Cc: sampo@brutus.neuronio.pt
  15. Subject: Signing with envelope routines
  16. Sender: ssl-lists-owner@mincom.com
  17. Precedence: bulk
  18. Status: RO
  19. X-Status: D
  20. I have been trying to figure out how to produce signatures with EVP_
  21. routines. I seem to be able to read in private key and sign some
  22. data ok, but I can't figure out how I am supposed to read in
  23. public key so that I could verify my signature. I use self signed
  24. certificate.
  25. I figured I should use
  26. EVP_PKEY* pkey = PEM_ASN1_read(d2i_PrivateKey, PEM_STRING_EVP_PKEY,
  27. fp, NULL, NULL);
  28. to read in private key and this seems to work Ok.
  29. However when I try analogous
  30. EVP_PKEY* pkey = PEM_ASN1_read(d2i_PublicKey, PEM_STRING_X509,
  31. fp, NULL, NULL);
  32. the program fails with
  33. error:0D09508D:asn1 encoding routines:D2I_PUBLICKEY:unknown public key type:d2i_pu.c:93
  34. error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib:pem_lib.c:232
  35. I figured that the second argument to PEM_ASN1_read should match the
  36. name in my PEM encoded object, hence PEM_STRING_X509.
  37. PEM_STRING_EVP_PKEY seems to be somehow magical
  38. because it matches whatever private key there happens to be. I could
  39. not find a similar constant to use with getting the certificate, however.
  40. Is my approach of using PEM_ASN1_read correct? What should I pass in
  41. as name? Can I use normal (or even self signed) X509 certificate for
  42. verifying the signature?
  43. When will SSLeay documentation be written ;-)? If I would contribute
  44. comments to the code, would Eric take time to review them and include
  45. them in distribution?
  46. I'm using SSLeay-0.6.4. My program is included below along with the
  47. key and cert that I use.
  48. --Sampo
  49. -----------------------------------
  50. /* sign-it.cpp - Simple test app using SSLeay envelopes to sign data
  51. 29.9.1996, Sampo Kellomaki <sampo@iki.fi> */
  52. #include <stdio.h>
  53. #include "rsa.h"
  54. #include "evp.h"
  55. #include "objects.h"
  56. #include "x509.h"
  57. #include "err.h"
  58. #include "pem.h"
  59. #include "ssl.h"
  60. void main ()
  61. {
  62. int err;
  63. int sig_len;
  64. unsigned char sig_buf [4096];
  65. const char certfile[] = "plain-cert.pem";
  66. const char keyfile[] = "plain-key.pem";
  67. const char data[] = "I owe you...";
  68. EVP_MD_CTX md_ctx;
  69. EVP_PKEY* pkey;
  70. FILE* fp;
  71. SSL_load_error_strings();
  72. /* Read private key */
  73. fp = fopen (keyfile, "r"); if (fp == NULL) exit (1);
  74. pkey = (EVP_PKEY*)PEM_ASN1_read ((char *(*)())d2i_PrivateKey,
  75. PEM_STRING_EVP_PKEY,
  76. fp,
  77. NULL, NULL);
  78. if (pkey == NULL) { ERR_print_errors_fp (stderr); exit (1); }
  79. fclose (fp);
  80. /* Do the signature */
  81. EVP_SignInit (&md_ctx, EVP_md5());
  82. EVP_SignUpdate (&md_ctx, data, strlen(data));
  83. sig_len = sizeof(sig_buf);
  84. err = EVP_SignFinal (&md_ctx,
  85. sig_buf,
  86. &sig_len,
  87. pkey);
  88. if (err != 1) { ERR_print_errors_fp (stderr); exit (1); }
  89. EVP_PKEY_free (pkey);
  90. /* Read public key */
  91. fp = fopen (certfile, "r"); if (fp == NULL) exit (1);
  92. pkey = (EVP_PKEY*)PEM_ASN1_read ((char *(*)())d2i_PublicKey,
  93. PEM_STRING_X509,
  94. fp,
  95. NULL, NULL);
  96. if (pkey == NULL) { ERR_print_errors_fp (stderr); exit (1); }
  97. fclose (fp);
  98. /* Verify the signature */
  99. EVP_VerifyInit (&md_ctx, EVP_md5());
  100. EVP_VerifyUpdate (&md_ctx, data, strlen((char*)data));
  101. err = EVP_VerifyFinal (&md_ctx,
  102. sig_buf,
  103. sig_len,
  104. pkey);
  105. if (err != 1) { ERR_print_errors_fp (stderr); exit (1); }
  106. EVP_PKEY_free (pkey);
  107. printf ("Signature Verified Ok.\n");
  108. }
  109. /* EOF */
  110. --------------- plain-cert.pem -----------------
  111. -----BEGIN CERTIFICATE-----
  112. MIICLDCCAdYCAQAwDQYJKoZIhvcNAQEEBQAwgaAxCzAJBgNVBAYTAlBUMRMwEQYD
  113. VQQIEwpRdWVlbnNsYW5kMQ8wDQYDVQQHEwZMaXNib2ExFzAVBgNVBAoTDk5ldXJv
  114. bmlvLCBMZGEuMRgwFgYDVQQLEw9EZXNlbnZvbHZpbWVudG8xGzAZBgNVBAMTEmJy
  115. dXR1cy5uZXVyb25pby5wdDEbMBkGCSqGSIb3DQEJARYMc2FtcG9AaWtpLmZpMB4X
  116. DTk2MDkwNTAzNDI0M1oXDTk2MTAwNTAzNDI0M1owgaAxCzAJBgNVBAYTAlBUMRMw
  117. EQYDVQQIEwpRdWVlbnNsYW5kMQ8wDQYDVQQHEwZMaXNib2ExFzAVBgNVBAoTDk5l
  118. dXJvbmlvLCBMZGEuMRgwFgYDVQQLEw9EZXNlbnZvbHZpbWVudG8xGzAZBgNVBAMT
  119. EmJydXR1cy5uZXVyb25pby5wdDEbMBkGCSqGSIb3DQEJARYMc2FtcG9AaWtpLmZp
  120. MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAL7+aty3S1iBA/+yxjxv4q1MUTd1kjNw
  121. L4lYKbpzzlmC5beaQXeQ2RmGMTXU+mDvuqItjVHOK3DvPK7lTcSGftUCAwEAATAN
  122. BgkqhkiG9w0BAQQFAANBAFqPEKFjk6T6CKTHvaQeEAsX0/8YHPHqH/9AnhSjrwuX
  123. 9EBc0n6bVGhN7XaXd6sJ7dym9sbsWxb+pJdurnkxjx4=
  124. -----END CERTIFICATE-----
  125. ---------------- plain-key.pem -----------------
  126. -----BEGIN RSA PRIVATE KEY-----
  127. MIIBPAIBAAJBAL7+aty3S1iBA/+yxjxv4q1MUTd1kjNwL4lYKbpzzlmC5beaQXeQ
  128. 2RmGMTXU+mDvuqItjVHOK3DvPK7lTcSGftUCAwEAAQJBALjkK+jc2+iihI98riEF
  129. oudmkNziSRTYjnwjx8mCoAjPWviB3c742eO3FG4/soi1jD9A5alihEOXfUzloenr
  130. 8IECIQD3B5+0l+68BA/6d76iUNqAAV8djGTzvxnCxycnxPQydQIhAMXt4trUI3nc
  131. a+U8YL2HPFA3gmhBsSICbq2OptOCnM7hAiEA6Xi3JIQECob8YwkRj29DU3/4WYD7
  132. WLPgsQpwo1GuSpECICGsnWH5oaeD9t9jbFoSfhJvv0IZmxdcLpRcpslpeWBBAiEA
  133. 6/5B8J0GHdJq89FHwEG/H2eVVUYu5y/aD6sgcm+0Avg=
  134. -----END RSA PRIVATE KEY-----
  135. ------------------------------------------------