Sha256: 3c08378525c4291ed85d26c42f31178dd0d0f0745f08a34b5ff6e18fe9dae021

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 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

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

  ATTR_MAP = Keychain::Certificate::ATTR_MAP.merge(Keychain::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)

    Keychain::Certificate.new(certificate_ref.read_pointer)
  end

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

    Keychain::Key.new(key_ref.read_pointer)
  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)
    data.release
    result
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-keychain-0.2.1 lib/keychain/identity.rb
ruby-keychain-0.2.0 lib/keychain/identity.rb