Sha256: 7c0bc90f8600dde4e4bb21eda94fc1ec466a4cf7f6135fcd6d8c1fcdd9e945b4

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require 'openssl'

module Sec
  attach_function 'SecIdentityCopyPrivateKey', [:pointer, :pointer], :osstatus
  attach_function 'SecIdentityCopyCertificate', [:pointer, :pointer], :osstatus

  attach_variable 'kSecAttrKeyClass', :pointer
  attach_variable 'kSecAttrLabel', :pointer
end

module Keychain
  class Identity < Sec::Base
    register_type 'SecIdentity'

    ATTR_MAP = Certificate::ATTR_MAP.merge(Key::ATTR_MAP)

    INVERSE_ATTR_MAP = ATTR_MAP.invert
    define_attributes(ATTR_MAP)

    def klass
      Sec::Classes::IDENTITY.to_ruby
    end

    def certificate
      certificate_ref = FFI::MemoryPointer.new(:pointer)
      status = Sec.SecIdentityCopyCertificate(self, certificate_ref)
      Sec.check_osstatus(status)

      Certificate.new(certificate_ref.read_pointer).release_on_gc
    end

    def private_key
      key_ref = FFI::MemoryPointer.new(:pointer)
      status = Sec.SecIdentityCopyPrivateKey(self, key_ref)
      Sec.check_osstatus(status)

      Key.new(key_ref.read_pointer).release_on_gc
    end

    def pkcs12(passphrase = '')
      flags = Sec::SecItemImportExportKeyParameters.new
      flags[:version] = Sec::SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION
      flags[:passphrase] = CF::String.from_string(passphrase).to_ptr

      data_ptr = FFI::MemoryPointer.new(:pointer)
      status = Sec.SecItemExport(self, :kSecFormatPKCS12, 0, flags, data_ptr)
      Sec.check_osstatus(status)

      data = CF::Data.new(data_ptr.read_pointer)
      result = OpenSSL::PKCS12.new(data.to_s, passphrase)
      data.release
      result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-keychain-0.4.0 lib/keychain/identity.rb