This is a mirror of official site: http://jasper-net.blogspot.com/

Creating a self-signed certificate in C#

| Sunday, May 30, 2010
For a personal project involving SSL, I wanted to create some certificates that could be used to authenticate the client and server to each other. Nothing fancy - self-signed is perfectly fine in this case since the client would have an actual copy of the server cert to use when validating the server, and having the cert on the filesystem is secure enough for the task. In any case, I was disappointed to find out that even with all of the other crypto and certificate support, .NET lacks support for this. I was also disappointed by how difficult it was to figure out how to do this.
CertCreateSelfSignCertificate sounds promising, but it ends up not being quite enough. It turns out that you have to do the following (as simple as I know how to make it, anyway):

  1. CryptAcquireContext(out providerContext, randomContainerName, null, PROV_RSA_FULL, CRYPT_NEWKEYSET)
  2. CryptGenKey(providerContext, AT_KEYEXCHANGE, CRYPT_EXPORTABLE, out cryptKey)
  3. CertStrToName(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, name, CERT_X500_NAME_STR, 0, dataBuffer, ref dataLength, 0)
  4. cert = CreateSelfSignCertificate(providerContext, blob(dataBuffer, dataLength), 0, KeyProviderInfo(randomContainerName, PROV_RSA_FULL, AT_KEYEXCHANGE), 0, startTime, endTime, 0)
  5. certificateStore = CertOpenStore("Memory", 0, 0, CERT_STORE_CREATE_NEW_FLAG, 0)
  6. CertAddCertificateContextToStore(certificateStore, cert, CERT_STORE_ADD_NEW, out storeCert)
  7. CertSetCertificateContextProperty(storeCert, CERT_KEY_PROV_INFO_PROP_ID, 0, KeyProviderInfo(randomContainerName, PROV_RSA_FULL, AT_KEYEXCHANGE))
  8. PFXExportCertStoreEx(certificateStore, pfxBlob, password, 0, EXPORT_PRIVATE_KEYS)
  9. Free everything.

In case anybody is interested, source code is attached and is free for use by anybody as long as you don't hold me or Microsoft liable for it -- I have no idea whether this is actually the right or best way to do this. Give it the X500 distinguished name, validity start and end dates, and an optional password for encrypting the key data, and it will give you the PFX file data. Let me know if you find any bugs or have any suggestions.

Read more: Where are we going, and what's with the handbasket?

Posted via email from jasper22's posterous

60 comments:

Anonymous said...

aXbt michael kors sale
vRra plaque ghd pas cher
rEfs north face sale
5dJtt cheap ugg boots uk
0nHah michael kors handbags

Anonymous said...

iHmw ghd hair straightener australia
qPxt cheap uggs
cSgm michael kors bags
4xAxw cheap ugg boots uk
5iPyh chi hair products
5yFjz ghd straightener
6dJiy nfl football jerseys
2jZpi ghd nz sale
8kNsm cheap north face
4sKzu ugg australia
0bCpi ghd iv styler
5uTck michael kors sale
9jAvr nfl shop canada
3oPhi ghd españa
9oFsn ugg boots sale

Anonymous said...

http://www.tomnjudy.com/WordPress/wp-content/plugins/bbpress/topic.php?id=9611&replies=1#post-11585

http://thesweepspot.com/forum/topic/wwwlouisvuittonwalletukusacom-jw65252#post-667298

http://www.roomthebook.com/walls/viewtopic.php?f=14&t=563664&p=621962#p621962

http://www.amusic.de/node/93488

http://social.makanchi.kz/members/usazwyhit/activity/10466

http://www.mymonda.de/bbpress/topic.php?id=19597&replies=1#post-28132

http://ekstasisdance.com/community/topic.php?id=198250&replies=1#post-205395

http://pro.cleverinvestor.com/knowledge/topic.php?id=18419&replies=1#post-20087

http://schoolofeducators.com/members/usahumblepg/activity/180784

http://stuff.avenathus.com/forum/topic/wwwlouisvuittonwalletukusacom-ens8550098?replies=1#post-174167

http://pulejawan.pk/members/usatqueqa/activity/12438

http://studioeightphotographycourses.co.uk/student-forum/topic.php?id=76740&replies=1#post-87537

http://scuola007.altervista.org/social/members/usakaybqv/activity/2813

http://www.followmyego.com/view/post:156394

http://www.businessdeaf.ru/bbpress/topic.php?id=1481&replies=1#post-1628

http://womanlifeweb.net/ku/topic.php?id=12345&replies=1#post-13901

http://okutamaclimbing.com/bbpress/topic.php?id=12842&replies=1#post-19060

http://wearedocs.com/members/usanjsayh/activity/109777

http://egliseenmission-evangelisation.org/forum1/topic.php?id=386729&replies=1#post-408782

http://www.lcplunderground.com/view/post:281082

Anonymous said...

http://www.mydestinlife.com/forum/viewtopic.php?f=19&t=174209

http://bmm.org.za/node/2#comment-226229

http://petrabook.com/profile.php?user=38197&v=comments

http://bbs.bjadks.cn/showtopic-30633.aspx

http://www.pibla.de/pix/displayimage.php?pos=-2563

http://www.yazj.cn/yazj/read.php?tid=100741&ds=1

http://kpgp.kr/index.php/User:Uqmlaywawbg#FT1096--spun50

http://www.1pjd.de/index.php?site=forum_topic&topic=1

http://art.miratopia.com/?q=node/4#comment-58973

http://www.hkhobby.com/forum/viewthread.php?tid=248029&extra=page%3D1

http://mxyxy.com.h001.360iis.net/apps.php?q=diary&a=detail&did=253378&uid=29143

http://www.corridosalterados.com/showthread.php?9226-www-cheaplouisvuittonwalletsny-com-Bbap10&p=33434#post33434

http://www.bplanproject.eu/forum_en/index.php?topic=327924.msg421511#msg421511

http://stroitforum.com/showthread.php?p=5855#post5855

http://euros-embedded.com/wiki/index.php/User:Marfnbgrdhr#FT1016--spun76

http://www.hlzyz.org/bbs/boke.asp?cthweklcfpd.showtopic.64177.html

http://bbs.fjzippo.com/forum.php?mod=viewthread&tid=254772

http://capne.net/bbPress/topic.php?id=793321&replies=1#post-832533

http://realdeeprecordings.com/forum/topic/315450?replies=1#post-317223

http://yournetbank.com/showthread.php?246-www.cheaplouisvuittonbagsfr.com-dTic31&p=450#post450

Anonymous said...

buy tramadol online tramadol hcl para que sirve - tramadol 50mg street price

Anonymous said...

xanax no prescription online where to order xanax - xanax overdose side effects

Anonymous said...

order xanax xanax g3720 - xanax drug price

Anonymous said...

buy tramadol online tramadol ultram fibromyalgia - buy tramadol saturday delivery

Anonymous said...

tramadol without prescription buy tramadol 6914 - buy tramadol hcl 50mg

Anonymous said...

xanax online can you order xanax online - xanax 2mg purepac

Anonymous said...

buy tramadol online tramadol hcl 627 - buy tramadol online cheap

Anonymous said...

generic xanax xanax effects on dogs - xanax bars g3722 effects

Anonymous said...

coach outlet kphsmptx coach usa hvhfzkex coach factory outlet kfaltdsi coach factory cqzxrsvl

Anonymous said...

generic xanax xanax bars injecting - xanax dosage .75

Anonymous said...

buy tramadol online tramadol hydrochloride 50 mg effects - tramadol overdose how much

Anonymous said...

xanax 1mg xanax drug of abuse - pictures generic xanax 1 mg

Anonymous said...

buy carisoprodol carisoprodol 350 mg generic - carisoprodol max dosage

Anonymous said...

buy tramadol no prescription tramadol hcl 50 mg high - tramadol 100

Anonymous said...

buy tramadol online buy tramadol online without rx - buy tramadol online usa pharmacy

Anonymous said...

buy tramadol online where should i buy tramadol from online - tramadol hcl 50 mg recreational use

Anonymous said...

buy tramadol online tramadol pain killer - tramadol 627 get high

Anonymous said...

buy tramadol online tramadol withdrawal vertigo - tramadol 50 mg price per pill

Anonymous said...

buy tramadol online canadian tramadol no prescription - buy tramadol online prescription

Anonymous said...

buy tramadol online tramadol hcl and zoloft - buying tramadol with mastercard

Anonymous said...

buy tramadol tramadol 50 mg with tylenol - where to buy tramadol online usa

Anonymous said...

buy cialis online buy cialis professional cheap - cialis online+toronto

Anonymous said...

buy tramadol online order tramadol in florida - tramadol hcl er 100mg

Anonymous said...

buy tramadol online tramadol equivalent - tramadol for dogs addiction

Anonymous said...

xanax 1mg generic xanax effectiveness - what does 1mg xanax look like

Anonymous said...

buy cialis generic order cialis from us - where to buy cheap cialis online

Anonymous said...

cialis online usa buy cheap cialis no prescription - buy cialis soft online

Anonymous said...

buy cialis online cialis online ricetta - generic cialis online overnight

Anonymous said...

xanax online xanax mexico - xanax effects long term use

Anonymous said...

learn how to buy tramdadol tramadol hydrochloride 50mg tablets - tramadol effects

Anonymous said...

order tramadol online cod tramadol no prescription free shipping - tramadol no prescription

Anonymous said...

learn how to buy tramdadol tramadol like ultram - tramadol used get high

Anonymous said...

buy tramadol buy tramadol health solutions network - can you buy tramadol

Anonymous said...

buy tramadol cheap tramadol on line - tramadol 50mg like

Anonymous said...

buy tramadol tramadol and breastfeeding - tramadol withdrawal day 3

Anonymous said...

buy tramadol cheap tramadol no prescription - order tramadol online no prescription overnight

Anonymous said...

buy tramadol cod next day delivery tramadol 50mg dosage - buy tramadol online without script

Anonymous said...

buy tramadol tramadol sale no prescription - tramadol 50mg dosage for dogs

Anonymous said...

http://landvoicelearning.com/#21906 tramadol hcl 50 mg addiction - where to buy tramadol cod

Anonymous said...

learn how to buy tramdadol tramadol for dogs addictive - tramadol hydrochloride 50mg overdose

Anonymous said...

order tramadol online tramadol 50mg tablets narcotic - buy viagra tramadol

Anonymous said...

tramadol online tramadol contraindications - get tramadol online no prescription

Anonymous said...

buy tramadol online tramadol ultram side effects dogs - tramadol ultram withdrawal

Anonymous said...

buy tramadol 30 tramadol 50mg - buy tramadol overnight cod

Anonymous said...

http://reidmoody.com/#45570 70 mg ativan - dosage of ativan

Anonymous said...

cheap ativan online ativan vs xanax flying anxiety - ativan overdose side effects

Anonymous said...

tramadol online troy hill pharmacy online tramadol - tramadol 50 mg vs oxycodone 30 mg

Anonymous said...

ways to buy ativan online ativan withdrawal pain - can overdose ativan fatal

Anonymous said...

http://reidmoody.com/#51208 are ativan and xanax the same - ativan withdrawal blood pressure

Anonymous said...

http://ranchodelastortugas.com/#37284 xanax high blood pressure medication - oxycontin xanax drug interactions

Anonymous said...

buy tramadol online mastercard overnight tramadol 50 mg vs vicodin - tramadol (ultram) 50 mg tablet

Anonymous said...

http://staam.org/#90873 tramadol dosage adults - tramadol hcl 50 mg canine

Anonymous said...

http://ranchodelastortugas.com/#71453 2mg xanax urine - round xanax 1mg

Anonymous said...

buy tramadol online tramadol withdrawal leg cramps - ultram er tramadol hcl

Anonymous said...

buy tramadol online buy tramadol for dogs online - buy cheap tramadol online cod

Anonymous said...

http://ranchodelastortugas.com/#58720 overdose with xanax and alcohol - xanax no prescription united states