Class: TrubyLicense
- Inherits:
-
Object
- Object
- TrubyLicense
- Defined in:
- lib/truby_license.rb
Defined Under Namespace
Classes: InvalidLicense, InvalidPassword, LicenseData, PrivateKeyNeeded, TrubyException, X500Principal
Instance Method Summary collapse
- #deserialize_license(license_blob) ⇒ Object
-
#initialize(password, key) ⇒ TrubyLicense
constructor
Creates an instance able to read and write licenses using the supplied password and key.
- #serialize_license(license_data) ⇒ Object
Constructor Details
#initialize(password, key) ⇒ TrubyLicense
Creates an instance able to read and write licenses using the supplied password and key. For writing licenses the key needs to be a private key, otherwise a public key will do.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/truby_license.rb', line 27 def initialize password, key raise ArgumentError.new("password must be a String") unless password.is_a? String raise ArgumentError.new("key must be an OpenSSL DSA key") unless key.is_a? OpenSSL::PKey::DSA @password = password @key = key # to hold the ciphers used in the crypt method @cipher ||= {} # some extra parameters TrueLicense uses @iterations = 2005 @salt = "\xCE\xFB\xDE\xAC\x05\x02\x19q" end |
Instance Method Details
#deserialize_license(license_blob) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/truby_license.rb', line 41 def deserialize_license license_blob begin license_data = gunzip(decrypt(license_blob)) rescue OpenSSL::Cipher::CipherError raise InvalidPassword.new "Could not decrypt license blob" rescue Zlib::GzipFile::Error raise InvalidLicense.new "Invalid format of license blob: not gzipped corrrectly" end JavabeanXml.from_xml license_data, :string => lambda { |value, properties| value }, "de.schlichtherle.xml.GenericCertificate" => lambda { |v, properties| sig = b64d(properties[:signature]) license =properties[:encoded] algorithm = properties[:signatureAlgorithm] encoding = properties[:signatureEncoding] unless algorithm == "SHA1withDSA" raise NotImplementedError.new( "signature algorithm %s has not been implemented". % algorithm ) end unless encoding == "US-ASCII/Base64" raise NotImplementedError.new( "signature encoding %s has not been implemented". % encoding ) end unless verify_signature(license, sig) raise InvalidLicense.new("License signature mismatch") end JavabeanXml.from_xml(license, :long => lambda { |value, p| value.to_i }, :string => lambda { |value, p| value }, "java.util.Date" => lambda { |value, p| Time.at(value / 1000) }, "javax.security.auth.x500.X500Principal" => lambda { |value, p| X500Principal.new value }, "de.schlichtherle.license.LicenseContent" => lambda {|value, properties| ld = LicenseData.new ld.members.each do |prop| ld[prop] = properties[prop.to_sym] end ld } ) } end |
#serialize_license(license_data) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/truby_license.rb', line 91 def serialize_license license_data lic_data = license_data.clone # allows us to make changes unless @key.private? raise PrivateKeyNeeded.new("Cannot use a public key to encrypt the license") end # make sure issuer and holder are properly wrapped in X500Principal objects [:issuer, :holder].each do |prop| if lic_data[prop].is_a? String lic_data[prop] = X500Principal.new lic_data[prop] end end lic_data.consumerType = lic_data.consumerType.to_s inner = JavabeanXml.to_xml lic_data, LicenseData => lambda { |value| { :class => "de.schlichtherle.license.LicenseContent", :properties => lic_data.members.inject({}) {|props, prop| props[prop.to_sym] = lic_data[prop] props } } }, X500Principal => lambda { |value| { :class => "javax.security.auth.x500.X500Principal", :value => { :class => :string, :value => value.name } } }, Time => lambda { |value| { :class => "java.util.Date", :value => { :class => :long, :value => value.to_i * 1000 } } }, String => lambda { |value| { :class => :string, :value => value } } outer = JavabeanXml.to_xml( { :class => "de.schlichtherle.xml.GenericCertificate", :properties => { :encoded => inner, :signature => b64e(sign(inner)), :signatureAlgorithm => "SHA1withDSA", :signatureEncoding => "US-ASCII/Base64" } }, String => lambda { |value| { :class => :string, :value => value } } ) encrypt(gzip(outer)) end |