Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.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.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2006, 2007 Ola Bini <ola@ologix.com> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.ext.openssl;
 
 import java.util.List;
 
 import org.jruby.Ruby;

Author(s):
Ola Bini
 
 public class X509StoreCtx extends RubyObject {
     private static final long serialVersionUID = 2029690161026120504L;
 
     private static ObjectAllocator X509STORECTX_ALLOCATOR = new ObjectAllocator() {
         public IRubyObject allocate(Ruby runtimeRubyClass klass) {
             return new X509StoreCtx(runtimeklass);
         }
     };
 
     private static RubyObjectAdapter api = JavaEmbedUtils.newObjectAdapter();
     
     public static void createX509StoreCtx(Ruby runtimeRubyModule mX509) {
         RubyClass cX509StoreContext = mX509.defineClassUnder("StoreContext",runtime.getObject(),);
         cX509StoreContext.defineAnnotatedMethods(X509StoreCtx.class);
     }
 
     private StoreContext ctx;
     private RubyClass cX509Cert;
 
     public X509StoreCtx(Ruby runtimeRubyClass type) {
         super(runtimetype);
          = new StoreContext();
          = Utils.getClassFromPath(runtime"OpenSSL::X509::Certificate");
     }
 
     // constructor for creating callback parameter object of verify_cb
     X509StoreCtx(Ruby runtimeRubyClass typeStoreContext ctx) {
         super(runtimetype);
         this. = ctx;
          = Utils.getClassFromPath(runtime"OpenSSL::X509::Certificate");
     }
 
     @JRubyMethod(name="initialize", rest=true, frame=true)
     public IRubyObject _initialize(IRubyObject[] argsBlock block) {
         IRubyObject store;
         IRubyObject cert = getRuntime().getNil();
         IRubyObject chain = getRuntime().getNil();
         Store x509st;
         X509AuxCertificate x509 = null;
         List<X509AuxCertificatex509s = new ArrayList<X509AuxCertificate>();
 
         if(org.jruby.runtime.Arity.checkArgumentCount(getRuntime(),args,1,3) > 1) {
             cert = args[1];
         }
        if(args.length > 2) {
            chain = args[2];
        }
        store = args[0];
        x509st = ((X509Store)store).getStore();
        if(!cert.isNil()) {
            x509 = ((X509Cert)cert).getAuxCert();
        }
        if(!chain.isNil()) {
            x509s = new ArrayList<X509AuxCertificate>();
            for (IRubyObject obj : ((RubyArray)chain).toJavaArray()) {
                x509s.add(((X509Cert)obj).getAuxCert());
            }
        }
        if(.init(x509st,x509,x509s) != 1) {
            throw newStoreError(getRuntime(), null);
        }
        IRubyObject t = .getInstanceVariable(store,"@time");
        if(!t.isNil()) {
            set_time(t);
        }
        .setInstanceVariable(this"@verify_callback".getInstanceVariable(store"@verify_callback"));
        .setInstanceVariable(this"@cert"cert);
        return this;
    }
    public IRubyObject verify() {
        .setExtraData(1, getInstanceVariable("@verify_callback"));
        try {
            int result = .verifyCertificate();
            return result != 0 ? getRuntime().getTrue() : getRuntime().getFalse();
        } catch (Exception e) {
            // TODO: define suitable exception for jopenssl and catch it.
            throw newStoreError(getRuntime(), e.getMessage());
        }
    }
    public IRubyObject chain() {
        List<X509AuxCertificatechain = .getChain();
        if (chain == null) {
            return getRuntime().getNil();
        }
        List<IRubyObjectary = new ArrayList<IRubyObject>();
        try {
            for (X509AuxCertificate x509 : chain) {
                ary.add(.callMethod(getRuntime().getCurrentContext(), "new", RubyString.newString(getRuntime(), x509.getEncoded())));
            }
        } catch (CertificateEncodingException cee) {
            throw newStoreError(getRuntime(), cee.getMessage());
        }
        return getRuntime().newArray(ary);
    }
    public IRubyObject error() {
        return getRuntime().newFixnum(.getError());
    }
    @JRubyMethod(name="error=")
    public IRubyObject set_error(IRubyObject arg) {
        .setError(RubyNumeric.fix2int(arg));
        return arg;
    }
    public IRubyObject error_string() {
        int err = .getError();
        return getRuntime().newString(org.jruby.ext.openssl.x509store.X509Utils.verifyCertificateErrorString(err));
    }
    public IRubyObject error_depth() {
        ..println("WARNING: unimplemented method called: StoreContext#error_depth");
        return getRuntime().getNil();
    }
    public IRubyObject current_cert() {
        Ruby rt = getRuntime();
        X509AuxCertificate x509 = .getCurrentCertificate();
        try {
            return .callMethod(rt.getCurrentContext(), "new", RubyString.newString(rtx509.getEncoded()));
        } catch (CertificateEncodingException cee) {
            throw newStoreError(getRuntime(), cee.getMessage());
        }
    }
    public IRubyObject current_crl() {
        ..println("WARNING: unimplemented method called: StoreContext#current_crl");
        return getRuntime().getNil();
    }
    public IRubyObject cleanup() {
        ..println("WARNING: unimplemented method called: StoreContext#cleanup");
        return getRuntime().getNil();
    }
    @JRubyMethod(name="flags=")
    public IRubyObject set_flags(IRubyObject arg) {
        ..println("WARNING: unimplemented method called: StoreContext#set_flags");
        return getRuntime().getNil();
    }
    @JRubyMethod(name="purpose=")
    public IRubyObject set_purpose(IRubyObject arg) {
        ..println("WARNING: unimplemented method called: StoreContext#set_purpose");
        return getRuntime().getNil();
    }
    @JRubyMethod(name="trust=")
    public IRubyObject set_trust(IRubyObject arg) {
        ..println("WARNING: unimplemented method called: StoreContext#set_trust");
        return getRuntime().getNil();
    }
    @JRubyMethod(name="time=")
    public IRubyObject set_time(IRubyObject arg) {
        .setTime(0,((RubyTime)arg).getJavaDate());
        return arg;
    }
    private static RaiseException newStoreError(Ruby runtimeString message) {
        return Utils.newError(runtime"OpenSSL::X509::StoreError"message);
    }
}// X509StoreCtx
New to GrepCode? Check out our FAQ X