Skip to content

Commit

Permalink
Updated with code and readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
jprichardson committed Oct 7, 2011
1 parent 01e41f9 commit 14d736a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.DS_Store
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,2 +1,2 @@
Copyright: JP Richardson 2011
Copyright (c) 2011, JP Richardson
Tri-licensed: GPL v2, LGPL v2, MIT
15 changes: 15 additions & 0 deletions README.asciidoc
Expand Up @@ -14,9 +14,24 @@ As of now, this gem only contains code relevant to the generation of keys and ce
iOS development. Of course, this could easily be expanded to include other areas.



Testing
-------

Verify that the tests pass on your machine. This was tested on *Mac OS X 10.7 (Lion)*.

----
ruby test/keychain_test.rb
----


Usage
-----

.Install
----
gem install keychain_manager
----


License
Expand Down
78 changes: 78 additions & 0 deletions lib/keychain_manager.rb
@@ -0,0 +1,78 @@
#Mac OS X Keychain Manager Gem.
#Copyright (c) 2011, JP Richardson
#email: jprichardson@gmail.com

class KeychainManager
attr_reader :name

CMD_KC = 'security'
CMD_SSL = 'openssl'
@file = nil

def initialize(name)
@name = name
end

def create
`#{CMD_KC} create-keychain -p "" #{@name}`
end

def delete
`#{CMD_KC} delete-keychain #{self.file}`
end

def exist?
exists?
end

def exists?
`#{CMD_KC} list-keychains`.include?(@name)
end

def export_identities(p12_file)
`#{CMD_KC} export -k #{self.file} -t identities -f pkcs12 -P '' -o #{p12_file}`
end

def file
return @file unless @file.nil?
KeychainManager.keychain_files.each do |f|
if f.include?(@name)
@file = f
break
end
end
@file
end

def import_apple_cert(apple_cert_file)
`#{CMD_KC} import #{apple_cert_file} -k #{self.file}`
end

def import_rsa_key(rsa_file)
`#{CMD_KC} import #{rsa_file} -P "" -k #{self.file}`
end

########### CLASS Methods

def self.convert_p12_to_pem(p12_file, pem_file)
#`expect -c "spawn #{CMD_SSL} pkcs12 -nodes -in #{p12_file} -out #{pem_file}; expect -re \\\"Enter Import Password:\\\"; send \\\"\\n\\\"; expect eof"`
`#{CMD_SSL} pkcs12 -passin pass: -nodes -in #{p12_file} -out #{pem_file}`
end

def self.generate_cert_request(email, country, rsa_file, cert_file)
`#{CMD_SSL} req -new -key #{rsa_file} -out #{cert_file} -subj "/#{email}, CN=CERT_NAME, C=#{country}"`
end

def self.generate_rsa_key(rsa_file, keysize=2048)
`#{CMD_SSL} genrsa -out #{rsa_file} #{keysize}`
end

def self.keychain_files
files = []
`#{CMD_KC} list-keychains`.split("\n").each do |file|
files << file.strip.gsub('"', '')
end
files
end

end
64 changes: 64 additions & 0 deletions test/keychain_test.rb
@@ -0,0 +1,64 @@
require "test/unit"

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))

require 'keychain_manager'

class KeychainManagerTest < Test::Unit::TestCase

def setup
# Do nothing
end

def teardown

end

def test_create_delete_exists
kcm = KeychainManager.new("some_keychain")
assert !kcm.exists?
kcm.create
assert kcm.exists?
kcm.delete
assert !kcm.exists?
end

def test_file
kcm = KeychainManager.new("some_keychain")
assert !kcm.exists?
assert_nil kcm.file
kcm.create
assert_not_nil kcm.file
kcm.delete
assert !kcm.exists?
end

def test_generate_rsa_key
rsa_tmp = '/tmp/test.rsa'
File.delete(rsa_tmp) if File.exists?(rsa_tmp)
KeychainManager.generate_rsa_key(rsa_tmp, 2048)
assert File.exists?(rsa_tmp)
end

def test_generate_cert_request
rsa_tmp = '/tmp/test.rsa'
File.delete(rsa_tmp) if File.exists?(rsa_tmp)
KeychainManager.generate_rsa_key(rsa_tmp, 2048)

cert_tmp = '/tmp/test.cert'
File.delete(cert_tmp) if File.exists?(cert_tmp)
KeychainManager.generate_cert_request('partners@reflect7.com', 'US', rsa_tmp, cert_tmp)
assert File.exists?(cert_tmp)
end

def test_import_rsa_key
rsa_tmp = '/tmp/test.rsa'
KeychainManager.generate_rsa_key(rsa_tmp, 2048)

kcm = KeychainManager.new("some_keychain")
kcm.create
assert kcm.import_rsa_key(rsa_tmp).include?('1 key imported')
kcm.delete
end
end

0 comments on commit 14d736a

Please sign in to comment.