3 people like it.

Generate a X509 Certificate for testing purposes

Small script to generate a X509 certificate for testing purposes. In my case, for generating signed PDF documents. This will write the certificate and it's key out into a Pkcs 12 store. It relies on the BouncyCastle library (version 1.7 worked fine).

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
open System
open Org.BouncyCastle.Asn1.X509
open Org.BouncyCastle.Crypto
open Org.BouncyCastle.Security
open Org.BouncyCastle.Math
open Org.BouncyCastle.Crypto.Prng
open Org.BouncyCastle.Crypto.Generators
open Org.BouncyCastle.Pkcs
open Org.BouncyCastle.X509

let kpg = RsaKeyPairGenerator()
kpg.Init(KeyGenerationParameters(SecureRandom(CryptoApiRandomGenerator()), 1024))
let kp = kpg.GenerateKeyPair()

let gen = X509V3CertificateGenerator()
let certName = X509Name("CN=PickAName")
let serialNo = BigInteger.ProbablePrime(120, new Random())

gen.SetSerialNumber(serialNo)
gen.SetSubjectDN(certName)
gen.SetIssuerDN(certName)
gen.SetNotAfter(DateTime.Now.AddYears(100))
gen.SetNotBefore(DateTime.Now.Subtract(TimeSpan(7, 0, 0, 0)))
gen.SetSignatureAlgorithm("MD5WithRSA")
gen.SetPublicKey(kp.Public)

let cert = gen.Generate(kp.Private)

let store = Pkcs12Store()
let friendlyName = cert.IssuerDN.ToString()
let entry = X509CertificateEntry(cert)
store.SetCertificateEntry(friendlyName, entry)
store.SetKeyEntry(friendlyName, AsymmetricKeyEntry(kp.Private), [| entry |])
store.Save(IO.File.OpenWrite("X509.store"), Seq.toArray "A password here", SecureRandom(CryptoApiRandomGenerator()))
namespace System
namespace Org
namespace Org.BouncyCastle
namespace Org.BouncyCastle.Asn1
namespace Org.BouncyCastle.Asn1.X509
namespace Org.BouncyCastle.Crypto
namespace Org.BouncyCastle.Security
namespace Org.BouncyCastle.Math
namespace Org.BouncyCastle.Crypto.Prng
namespace Org.BouncyCastle.Crypto.Generators
namespace Org.BouncyCastle.Pkcs
namespace Org.BouncyCastle.X509
val kpg : RsaKeyPairGenerator

Full name: Script.kpg
Multiple items
type RsaKeyPairGenerator =
  new : unit -> RsaKeyPairGenerator
  member GenerateKeyPair : unit -> AsymmetricCipherKeyPair
  member Init : parameters:KeyGenerationParameters -> unit

Full name: Org.BouncyCastle.Crypto.Generators.RsaKeyPairGenerator

--------------------
RsaKeyPairGenerator() : unit
RsaKeyPairGenerator.Init(parameters: KeyGenerationParameters) : unit
Multiple items
type KeyGenerationParameters =
  new : random:SecureRandom * strength:int -> KeyGenerationParameters
  member Random : SecureRandom
  member Strength : int

Full name: Org.BouncyCastle.Crypto.KeyGenerationParameters

--------------------
KeyGenerationParameters(random: SecureRandom, strength: int) : unit
Multiple items
type SecureRandom =
  inherit Random
  new : unit -> SecureRandom + 2 overloads
  member GenerateSeed : length:int -> byte[]
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit + 1 overload
  member NextDouble : unit -> float
  member NextInt : unit -> int
  member NextLong : unit -> int64
  member SetSeed : inSeed:byte[] -> unit + 1 overload
  static member GetInstance : algorithm:string -> SecureRandom
  static member GetSeed : length:int -> byte[]

Full name: Org.BouncyCastle.Security.SecureRandom

--------------------
SecureRandom() : unit
SecureRandom(inSeed: byte []) : unit
SecureRandom(generator: IRandomGenerator) : unit
Multiple items
type CryptoApiRandomGenerator =
  new : unit -> CryptoApiRandomGenerator
  member AddSeedMaterial : seed:byte[] -> unit + 1 overload
  member NextBytes : bytes:byte[] -> unit + 1 overload

Full name: Org.BouncyCastle.Crypto.Prng.CryptoApiRandomGenerator

--------------------
CryptoApiRandomGenerator() : unit
val kp : AsymmetricCipherKeyPair

Full name: Script.kp
RsaKeyPairGenerator.GenerateKeyPair() : AsymmetricCipherKeyPair
val gen : X509V3CertificateGenerator

Full name: Script.gen
Multiple items
type X509V3CertificateGenerator =
  new : unit -> X509V3CertificateGenerator
  member AddExtension : oid:string * critical:bool * extensionValue:Asn1Encodable -> unit + 3 overloads
  member CopyAndAddExtension : oid:string * critical:bool * cert:X509Certificate -> unit + 1 overload
  member Generate : privateKey:AsymmetricKeyParameter -> X509Certificate + 1 overload
  member Reset : unit -> unit
  member SetIssuerDN : issuer:X509Name -> unit
  member SetIssuerUniqueID : uniqueID:bool[] -> unit
  member SetNotAfter : date:DateTime -> unit
  member SetNotBefore : date:DateTime -> unit
  member SetPublicKey : publicKey:AsymmetricKeyParameter -> unit
  ...

Full name: Org.BouncyCastle.X509.X509V3CertificateGenerator

--------------------
X509V3CertificateGenerator() : unit
val certName : X509Name

Full name: Script.certName
Multiple items
type X509Name =
  inherit Asn1Encodable
  new : dirName:string -> X509Name + 14 overloads
  member Equivalent : other:X509Name -> bool + 1 overload
  member GetOidList : unit -> IList
  member GetOids : unit -> ArrayList
  member GetValueList : unit -> IList + 1 overload
  member GetValues : unit -> ArrayList + 1 overload
  member ToAsn1Object : unit -> Asn1Object
  member ToString : unit -> string + 2 overloads
  static val C : DerObjectIdentifier
  static val O : DerObjectIdentifier
  ...

Full name: Org.BouncyCastle.Asn1.X509.X509Name

--------------------
X509Name(dirName: string) : unit
X509Name(ordering: Collections.IList, attributes: Collections.IDictionary) : unit
X509Name(oids: Collections.IList, values: Collections.IList) : unit
X509Name(dirName: string, converter: X509NameEntryConverter) : unit
X509Name(reverse: bool, dirName: string) : unit
X509Name(ordering: Collections.IList, attributes: Collections.IDictionary, converter: X509NameEntryConverter) : unit
X509Name(oids: Collections.IList, values: Collections.IList, converter: X509NameEntryConverter) : unit
X509Name(reverse: bool, dirName: string, converter: X509NameEntryConverter) : unit
X509Name(reverse: bool, lookUp: Collections.IDictionary, dirName: string) : unit
X509Name(reverse: bool, lookUp: Collections.IDictionary, dirName: string, converter: X509NameEntryConverter) : unit
val serialNo : BigInteger

Full name: Script.serialNo
Multiple items
type BigInteger =
  new : value:string -> BigInteger + 7 overloads
  member Abs : unit -> BigInteger
  member Add : value:BigInteger -> BigInteger
  member And : value:BigInteger -> BigInteger
  member AndNot : val:BigInteger -> BigInteger
  member BitCount : int
  member BitLength : int
  member ClearBit : n:int -> BigInteger
  member CompareTo : obj:obj -> int + 1 overload
  member Divide : val:BigInteger -> BigInteger
  ...

Full name: Org.BouncyCastle.Math.BigInteger

--------------------
BigInteger(value: string) : unit
BigInteger(bytes: byte []) : unit
BigInteger(str: string, radix: int) : unit
BigInteger(sign: int, bytes: byte []) : unit
BigInteger(sizeInBits: int, random: Random) : unit
BigInteger(bytes: byte [], offset: int, length: int) : unit
BigInteger(bitLength: int, certainty: int, random: Random) : unit
BigInteger(sign: int, bytes: byte [], offset: int, length: int) : unit
BigInteger.ProbablePrime(bitLength: int, random: Random) : BigInteger
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit
  member NextDouble : unit -> float

Full name: System.Random

--------------------
Random() : unit
Random(Seed: int) : unit
X509V3CertificateGenerator.SetSerialNumber(serialNumber: BigInteger) : unit
X509V3CertificateGenerator.SetSubjectDN(subject: X509Name) : unit
X509V3CertificateGenerator.SetIssuerDN(issuer: X509Name) : unit
X509V3CertificateGenerator.SetNotAfter(date: DateTime) : unit
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
DateTime()
   (+0 other overloads)
DateTime(ticks: int64) : unit
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
   (+0 other overloads)
property DateTime.Now: DateTime
DateTime.AddYears(value: int) : DateTime
X509V3CertificateGenerator.SetNotBefore(date: DateTime) : unit
DateTime.Subtract(value: TimeSpan) : DateTime
DateTime.Subtract(value: DateTime) : TimeSpan
Multiple items
type TimeSpan =
  struct
    new : ticks:int64 -> TimeSpan + 3 overloads
    member Add : ts:TimeSpan -> TimeSpan
    member CompareTo : value:obj -> int + 1 overload
    member Days : int
    member Duration : unit -> TimeSpan
    member Equals : value:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member Hours : int
    member Milliseconds : int
    member Minutes : int
    ...
  end

Full name: System.TimeSpan

--------------------
TimeSpan()
TimeSpan(ticks: int64) : unit
TimeSpan(hours: int, minutes: int, seconds: int) : unit
TimeSpan(days: int, hours: int, minutes: int, seconds: int) : unit
TimeSpan(days: int, hours: int, minutes: int, seconds: int, milliseconds: int) : unit
X509V3CertificateGenerator.SetSignatureAlgorithm(signatureAlgorithm: string) : unit
X509V3CertificateGenerator.SetPublicKey(publicKey: AsymmetricKeyParameter) : unit
property AsymmetricCipherKeyPair.Public: AsymmetricKeyParameter
val cert : X509Certificate

Full name: Script.cert
X509V3CertificateGenerator.Generate(privateKey: AsymmetricKeyParameter) : X509Certificate
X509V3CertificateGenerator.Generate(privateKey: AsymmetricKeyParameter, random: SecureRandom) : X509Certificate
property AsymmetricCipherKeyPair.Private: AsymmetricKeyParameter
val store : Pkcs12Store

Full name: Script.store
Multiple items
type Pkcs12Store =
  new : unit -> Pkcs12Store + 1 overload
  member Aliases : IEnumerable
  member ContainsAlias : alias:string -> bool
  member Count : int
  member DeleteEntry : alias:string -> unit
  member GetCertificate : alias:string -> X509CertificateEntry
  member GetCertificateAlias : cert:X509Certificate -> string
  member GetCertificateChain : alias:string -> X509CertificateEntry[]
  member GetKey : alias:string -> AsymmetricKeyEntry
  member IsCertificateEntry : alias:string -> bool
  ...

Full name: Org.BouncyCastle.Pkcs.Pkcs12Store

--------------------
Pkcs12Store() : unit
Pkcs12Store(input: IO.Stream, password: char []) : unit
val friendlyName : string

Full name: Script.friendlyName
property X509Certificate.IssuerDN: X509Name
X509Name.ToString() : string
X509Name.ToString(reverse: bool, oidSymbols: Collections.IDictionary) : string
val entry : X509CertificateEntry

Full name: Script.entry
Multiple items
type X509CertificateEntry =
  inherit Pkcs12Entry
  new : cert:X509Certificate -> X509CertificateEntry + 2 overloads
  member Certificate : X509Certificate
  member Equals : obj:obj -> bool
  member GetHashCode : unit -> int

Full name: Org.BouncyCastle.Pkcs.X509CertificateEntry

--------------------
X509CertificateEntry(cert: X509Certificate) : unit
X509CertificateEntry(cert: X509Certificate, attributes: Collections.IDictionary) : unit
Pkcs12Store.SetCertificateEntry(alias: string, certEntry: X509CertificateEntry) : unit
Pkcs12Store.SetKeyEntry(alias: string, keyEntry: AsymmetricKeyEntry, chain: X509CertificateEntry []) : unit
Multiple items
type AsymmetricKeyEntry =
  inherit Pkcs12Entry
  new : key:AsymmetricKeyParameter -> AsymmetricKeyEntry + 2 overloads
  member Equals : obj:obj -> bool
  member GetHashCode : unit -> int
  member Key : AsymmetricKeyParameter

Full name: Org.BouncyCastle.Pkcs.AsymmetricKeyEntry

--------------------
AsymmetricKeyEntry(key: AsymmetricKeyParameter) : unit
AsymmetricKeyEntry(key: AsymmetricKeyParameter, attributes: Collections.IDictionary) : unit
Pkcs12Store.Save(stream: IO.Stream, password: char [], random: SecureRandom) : unit
Multiple items
namespace Org.BouncyCastle.Crypto.IO

--------------------
namespace System.IO
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
IO.File.OpenWrite(path: string) : IO.FileStream
module Seq

from Microsoft.FSharp.Collections
val toArray : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Seq.toArray
Raw view Test code New version

More information

Link:http://fssnip.net/kk
Posted:11 years ago
Author:mavnn
Tags: x509 , crypto