Skip to content

Commit

Permalink
Merge pull request #6 from chef/btm/file_structure
Browse files Browse the repository at this point in the history
set initial file hierarchy
  • Loading branch information
btm authored Nov 15, 2017
2 parents 08c0cf4 + 2fdebfc commit f9f53f5
Show file tree
Hide file tree
Showing 17 changed files with 639 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ build-iPhoneSimulator/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
Gemfile.lock
# .ruby-version
# .ruby-gemset

Expand Down
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ rvm:
branches:
only:
- master
before_install: gem install bundler -v 1.12.5
before_install:
- gem install bundler
script: bundle exec rake spec
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in win32-certstore.gemspec
gemspec

gem 'rb-readline'
92 changes: 91 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,92 @@
# win32-certstore
Ruby library for accessing the certificate store on Windows
Ruby library for accessing the certificate store on Microsoft Windows:

## Subcommands

This library provides the following features.

### Open certificate store

Any valid certificate store can be opened in two ways:

**Notes: Valid certificate store names:
`CA -> Certification authority certificates.`
`MY -> A certificate store that holds certificates with associated private keys.`
`ROOT -> Root certificates.`
`SPC -> Software Publisher Certificate.`**

```
Win32::Certstore.open("Root") do |store|
//your code should be here!
end
```
or
```
store = Win32::Certstore.open("Root")
```

### List certificates

Lists certificates of a valid certificate store and returns output in JSON format:

```
Win32::Certstore.open("Root") do |store|
store.list
end
```
or
```
store = Win32::Certstore.open("Root")
store.list
```

### Add certificate

Add a valid certificate in a certificate store.

**Notes: The new certificate should be in the following formats `.cer|.crt|.pfx|.der`:**

```
Win32::Certstore.open("Root") do |store|
store.add(certificate_file_path)
end
```
or
```
store = Win32::Certstore.open("Root")
store.add(certificate_file_path)
```

## Requirements / setup

### Ruby

Ruby 1.9.3+ is required.

### Chef version

This library requires >= Chef 11.0.0.

## CONTRIBUTING:

Please file bugs against the WIN32-CERTSTORE project at https://github.com/chef/win32-certstore/issues.

More information on the contribution process for Chef projects can be found in the [Chef Contributions document](http://docs.chef.io/community_contributions.html).

# LICENSE:

Author:: Bryan McLellan (<[email protected]>)
Copyright:: Copyright (c) 2017 Chef Software, Inc.
License:: Apache License, Version 2.0

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2 changes: 1 addition & 1 deletion lib/win32-certstore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require 'win32/certstore/certstore'
require_relative "win32/certstore"
43 changes: 0 additions & 43 deletions lib/win32/api/reserved_names.rb

This file was deleted.

92 changes: 92 additions & 0 deletions lib/win32/certstore.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#
# Author:: Nimisha Sharad (<[email protected]>)
# Copyright:: Copyright (c) 2017 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'certstore/mixin/crypto'
require 'certstore/mixin/assertions'
require 'certstore/store_base'
require 'certstore/version'

module Win32
class Certstore
include Win32::Certstore::Mixin::Crypto
extend Win32::Certstore::Mixin::Assertions
include Chef::Mixin::WideString
include Win32::Certstore::StoreBase

attr_reader :store_name

def initialize(store_name)
@certstore_handler = open(store_name)
end

def self.open(store_name)
validate_store(store_name)
if block_given?
yield self.new(store_name)
else
self.new(store_name)
end
end

def list
list = cert_list(@certstore_handler)
close
return list
end

def add(cert_file_path)
add = cert_add(@certstore_handler, cert_file_path)
close
return add
end

private

attr_reader :certstore_handler

def open(store_name)
certstore_handler = CertOpenSystemStoreW(nil, wstring(store_name))
unless certstore_handler
last_error = FFI::LastError.error
raise Chef::Exceptions::Win32APIError, "Unable to open the Certificate Store `#{store_name}` with error: #{last_error}."
end
add_finalizer(certstore_handler)
certstore_handler
end

def add_finalizer(certstore_handler)
ObjectSpace.define_finalizer(self, self.class.finalize(certstore_handler))
end

def self.finalize(certstore_handler)
proc { puts "DESTROY OBJECT #{certstore_handler}" }
end

def close
closed = CertCloseStore(@certstore_handler, CERT_CLOSE_STORE_FORCE_FLAG)
unless closed
last_error = FFI::LastError.error
raise Chef::Exceptions::Win32APIError, "Unable to close the Certificate Store with error: #{last_error}."
end
remove_finalizer
end

def remove_finalizer
ObjectSpace.undefine_finalizer(self)
end
end
end
44 changes: 0 additions & 44 deletions lib/win32/certstore/certstore.rb

This file was deleted.

44 changes: 44 additions & 0 deletions lib/win32/certstore/mixin/assertions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Author:: Piyush Awasthi (<[email protected]>)
# Copyright:: Copyright (c) 2017 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Win32::Certstore::Mixin::Assertions

# Validate certificate store name
def validate_store(store_name)
unless valid_store_name.include?(store_name&.upcase)
raise ArgumentError, "Invalid Certificate Store."
end
end

# Validate certificate type
def validate_certificate(cert_file_path)
unless (!cert_file_path.nil? && File.extname(cert_file_path) =~ /.cer|.crt|.pfx|.der/ )
raise ArgumentError, "Invalid Certificate format."
end
end

private

# These Are Valid certificate store name
# CA -> Certification authority certificates.
# MY -> A certificate store that holds certificates with associated private keys.
# ROOT -> Root certificates.
# SPC -> Software Publisher Certificate.
def valid_store_name
["MY", "CA", "ROOT", "SPC"]
end
end
Loading

0 comments on commit f9f53f5

Please sign in to comment.